| 1 | /* |
| 2 | =========================================================================== |
| 3 | blockattack - Block Attack - Rise of the Blocks |
| 4 | Copyright (C) 2005-2023 Poul Sander |
| 5 | |
| 6 | This program is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see http://www.gnu.org/licenses/ |
| 18 | |
| 19 | Source information and contacts persons can be found at |
| 20 | https://www.blockattack.net |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include "themes.hpp" |
| 26 | #include "sago/SagoMisc.hpp" |
| 27 | #include <vector> |
| 28 | #include <sstream> |
| 29 | #include <map> |
| 30 | #include "nlohmann/json.hpp" |
| 31 | #include <iostream> |
| 32 | #include "global.hpp" |
| 33 | #include <boost/algorithm/string/predicate.hpp> |
| 34 | #include <fmt/core.h> |
| 35 | |
| 36 | using json = nlohmann::json; |
| 37 | |
| 38 | |
| 39 | const int NUMBER_OF_CUSTOM_SLOTS = 2; |
| 40 | |
| 41 | |
| 42 | void to_json(json& j, const ImgScale& p) { |
| 43 | switch (p) { |
| 44 | case ImgScale::Stretch: |
| 45 | j = "stretch"; |
| 46 | break; |
| 47 | case ImgScale::Tile: |
| 48 | j = "tile"; |
| 49 | break; |
| 50 | case ImgScale::Resize: |
| 51 | j = "resize"; |
| 52 | break; |
| 53 | case ImgScale::Cut: |
| 54 | j = "cut"; |
| 55 | break; |
| 56 | default: |
| 57 | j = "stretch"; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void from_json(const json& j, ImgScale& p) { |
| 63 | std::string s = j; |
| 64 | if (s == "stretch") { |
| 65 | p = ImgScale::Stretch; |
| 66 | } |
| 67 | else if (s == "tile") { |
| 68 | p = ImgScale::Tile; |
| 69 | } |
| 70 | else if (s == "resize") { |
| 71 | p = ImgScale::Resize; |
| 72 | } |
| 73 | else if (s == "cut") { |
| 74 | p = ImgScale::Cut; |
| 75 | } |
| 76 | else { |
| 77 | p = ImgScale::Stretch; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void to_json(json& j, const BackGroundData& p) { |
| 82 | j = json{ {"background_name", p.name}, {"background_sprite", p.background_sprite}, {"background_sprite_16x9", p.background_sprite_16x9}, {"background_scale", p.background_scale}, {"tileMoveSpeedX", p.tileMoveSpeedX}, {"tileMoveSpeedY", p.tileMoveSpeedY} }; |
| 83 | } |
| 84 | |
| 85 | void to_json(json& j, const Theme& p) { |
| 86 | j = json{ {"theme_name", p.theme_name}, {"back_board", p.back_board}, {"background_name", p.background.name}, {"decoration_name", p.decoration.name} }; |
| 87 | } |
| 88 | |
| 89 | void to_json(json& j, const DecorationData& p) { |
| 90 | j = json{ {"name", p.name}, {"decoration_sprites", p.decoration_sprites} }; |
| 91 | } |
| 92 | |
| 93 | void to_json(json& j, const ThemeFileData& p) { |
| 94 | j = json{ {"background_data", p.background_data}, {"decoration_data", p.decoration_data}, {"themes", p.themes} }; |
| 95 | } |
| 96 | |
| 97 | void from_json(const json& j, BackGroundData& p) { |
| 98 | j.at("background_name").get_to(p.name); |
| 99 | j.at("background_sprite").get_to(p.background_sprite); |
| 100 | j.at("background_sprite_16x9").get_to(p.background_sprite_16x9); |
| 101 | j.at("background_scale").get_to(p.background_scale); |
| 102 | j.at("tileMoveSpeedX").get_to(p.tileMoveSpeedX); |
| 103 | j.at("tileMoveSpeedY").get_to(p.tileMoveSpeedY); |
| 104 | } |
| 105 | |
| 106 | void from_json(const json& j, Theme& p) { |
| 107 | j.at("theme_name").get_to(p.theme_name); |
| 108 | j.at("back_board").get_to(p.back_board); |
| 109 | j.at("background_name").get_to(p.background.name); |
| 110 | j.at("decoration_name").get_to(p.decoration.name); |
| 111 | } |
| 112 | |
| 113 | void from_json(const json& j, DecorationData& p) { |
| 114 | j.at("name").get_to(p.name); |
| 115 | j.at("decoration_sprites").get_to(p.decoration_sprites); |
| 116 | } |
| 117 | |
| 118 | void from_json(const json& j, ThemeFileData& p) { |
| 119 | if (j.contains("background_data")) { |
| 120 | j.at("background_data").get_to(p.background_data); |
| 121 | } |
| 122 | if (j.contains("decoration_data")) { |
| 123 | j.at("decoration_data").get_to(p.decoration_data); |
| 124 | } |
| 125 | if (j.contains("themes")) { |
| 126 | j.at("themes").get_to(p.themes); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | static std::vector<Theme> themes(1); |
| 133 | static std::map<std::string, BackGroundData> background_data; |
| 134 | static std::map<std::string, DecorationData> decoration_data; |
| 135 | static std::map<std::string, ThemeBorderData> border_data; |
| 136 | static bool initialized = false; |
| 137 | static size_t current_theme = 0; |
| 138 | |
| 139 | |
| 140 | void ThemesFillMissingFields(Theme& theme) { |
| 141 | if (theme.background.name.empty() || !ThemesBackgroundExists(theme.background.name) ){ |
| 142 | //If the theme does not define a background then use the standard. |
| 143 | theme.background.name = "standard"; |
| 144 | } |
| 145 | theme.background = background_data[theme.background.name]; |
| 146 | } |
| 147 | |
| 148 | static void ThemesReadDataFromFile(const std::string& filename) { |
| 149 | if (globalData.verboseLevel) { |
| 150 | std::cout << "Reading theme data from " << filename << "\n"; |
| 151 | } |
| 152 | std::string s = sago::GetFileContent(filename); |
| 153 | if (s.empty()) { |
| 154 | std::cout << "File \"" << filename << "\" does not exit!\n"; |
| 155 | return; |
| 156 | } |
| 157 | json j = json::parse(s); |
| 158 | ThemeFileData tfd = j; |
| 159 | for (const auto& bg : tfd.background_data) { |
| 160 | background_data[bg.name] = bg; |
| 161 | } |
| 162 | for (const auto& dec : tfd.decoration_data) { |
| 163 | decoration_data[dec.name] = dec; |
| 164 | } |
| 165 | for (const Theme& theme : tfd.themes) { |
| 166 | if (globalData.verboseLevel) { |
| 167 | std::cout << "Adding theme " << theme.theme_name << "\n"; |
| 168 | } |
| 169 | themes.push_back(theme); |
| 170 | ThemesFillMissingFields(themes.back()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | static void ThemesInitBackGroundData() { |
| 176 | BackGroundData standard; |
| 177 | standard.name = "standard"; |
| 178 | standard.background_sprite = "background"; |
| 179 | standard.background_sprite_16x9 = "background_sixteen_nine"; |
| 180 | standard.background_scale = ImgScale::Stretch; |
| 181 | background_data[standard.name] = standard; |
| 182 | DecorationData smileys; |
| 183 | decoration_data[smileys.name] = smileys; |
| 184 | } |
| 185 | |
| 186 | static void ThemesInitBorderData() { |
| 187 | ThemeBorderData standard; |
| 188 | standard.name = "standard"; |
| 189 | standard.border_sprite = "board_back_back"; |
| 190 | standard.border_sprite_offset = {-60, -68}; |
| 191 | border_data[standard.name] = standard; |
| 192 | ThemeBorderData mirror = standard; |
| 193 | mirror.name = "mirror"; |
| 194 | mirror.border_sprite = "board_back_back_mirror"; |
| 195 | mirror.border_sprite_offset = {-116, -68}; |
| 196 | mirror.score_label_offset = {-100, 80}; |
| 197 | mirror.time_label_offset = {-100,129}; |
| 198 | mirror.chain_label_offset = {-100, 178}; |
| 199 | mirror.speed_label_offset = {-100, 227}; |
| 200 | border_data[mirror.name] = mirror; |
| 201 | } |
| 202 | |
| 203 | static bool str_startswith(const std::string& s, const char* prefix) { |
| 204 | return (s.rfind(prefix, 0) == 0); |
| 205 | } |
| 206 | |
| 207 | static void ThemesDumpData() { |
| 208 | ThemeFileData tfd; |
| 209 | tfd.themes = themes; |
| 210 | for (auto& pair : background_data) { |
| 211 | tfd.background_data.push_back(pair.second); |
| 212 | } |
| 213 | for (auto& pair : decoration_data) { |
| 214 | tfd.decoration_data.push_back(pair.second); |
| 215 | } |
| 216 | json j = tfd; |
| 217 | std::string s = j.dump(4); |
| 218 | sago::WriteFileContent("themes_dump.json", s); |
| 219 | } |
| 220 | |
| 221 | void ThemesSaveCustomSlots() { |
| 222 | ThemeFileData tfd; |
| 223 | for (const Theme& theme : themes) { |
| 224 | if ( str_startswith(theme.theme_name,"custom_slot_")) { |
| 225 | tfd.themes.push_back(theme); |
| 226 | } |
| 227 | } |
| 228 | json j = tfd; |
| 229 | std::string s = j.dump(4); |
| 230 | sago::WriteFileContent("custom_themes.json", s); |
| 231 | } |
| 232 | |
| 233 | static void ThemesAddCustomSlot(std::vector<Theme>& themes, int slot_number) { |
| 234 | std::string slot_name = fmt::format("custom_slot_{}", slot_number); |
| 235 | for (const Theme& theme : themes) { |
| 236 | if (theme.theme_name == slot_name) { |
| 237 | //Custom slot already exists |
| 238 | return; |
| 239 | } |
| 240 | } |
| 241 | Theme new_theme; |
| 242 | new_theme.theme_name = slot_name; |
| 243 | themes.push_back(new_theme); |
| 244 | ThemesFillMissingFields(themes.back()); |
| 245 | } |
| 246 | |
| 247 | void ThemesInit() { |
| 248 | if (initialized) { |
| 249 | return; |
| 250 | } |
| 251 | initialized = true; |
| 252 | ThemesInitBackGroundData(); |
| 253 | ThemesInitCustomBackgrounds(); |
| 254 | ThemesInitBorderData(); |
| 255 | themes.resize(1); //Add the default theme |
| 256 | ThemesFillMissingFields(themes[0]); |
| 257 | const std::vector<std::string>& theme_files = sago::GetFileList("themes"); |
| 258 | for (const std::string& filename : theme_files) { |
| 259 | if (boost::algorithm::ends_with(filename,".json")) { |
| 260 | ThemesReadDataFromFile("themes/"+filename); |
| 261 | } |
| 262 | } |
| 263 | ThemesReadDataFromFile("custom_themes.json"); |
| 264 | for (int i=1; i <= NUMBER_OF_CUSTOM_SLOTS; ++i) { |
| 265 | ThemesAddCustomSlot(themes, i); |
| 266 | } |
| 267 | ThemesDumpData(); |
| 268 | } |
| 269 | |
| 270 | Theme ThemesGetNext() { |
| 271 | ThemesInit(); |
| 272 | current_theme++; |
| 273 | current_theme = current_theme % themes.size(); |
| 274 | return themes.at(current_theme); |
| 275 | } |
| 276 | |
| 277 | Theme& ThemesGet(size_t theme_number) { |
| 278 | ThemesInit(); |
| 279 | return themes.at(theme_number % themes.size()); |
| 280 | } |
| 281 | |
| 282 | size_t ThemesGetNumber(const std::string& name) { |
| 283 | size_t ret = 0; // Default to 0 |
| 284 | ThemesInit(); |
| 285 | for (size_t i = 0; i < themes.size(); ++i) { |
| 286 | if (themes[i].theme_name == name) { |
| 287 | ret = i; |
| 288 | } |
| 289 | } |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | void ThemesAddOrReplace(const Theme& theme) { |
| 294 | ThemesInit(); |
| 295 | for (size_t i = 0; i < themes.size(); ++i) { |
| 296 | if (themes[i].theme_name == theme.theme_name) { |
| 297 | themes[i] = theme; |
| 298 | return; |
| 299 | } |
| 300 | } |
| 301 | themes.push_back(theme); |
| 302 | } |
| 303 | |
| 304 | bool ThemesBackgroundExists(const std::string& name) { |
| 305 | ThemesInit(); |
| 306 | return background_data.find(name) != background_data.end(); |
| 307 | } |
| 308 | |
| 309 | BackGroundData ThemesGetNextBackground(const std::string& current) { |
| 310 | ThemesInit(); |
| 311 | BackGroundData ret = background_data["standard"]; |
| 312 | for (auto& pair : background_data) { |
| 313 | if (pair.first == current) { |
| 314 | auto it = background_data.find(current); |
| 315 | ++it; |
| 316 | if (it == background_data.end()) { |
| 317 | it = background_data.begin(); |
| 318 | } |
| 319 | ret = it->second; |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | std::string ThemesGetNextBoardBackground(const std::string& current) { |
| 327 | ThemesInit(); |
| 328 | std::vector<std::string> back_boards = {"back_board", "back_board_sample_snow", "trans_cover"}; |
| 329 | std::string ret = "back_board"; |
| 330 | for (size_t i = 0; i < back_boards.size(); ++i) { |
| 331 | if (back_boards[i] == current) { |
| 332 | ret = back_boards[(i + 1) % back_boards.size()]; |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | ThemeBorderData ThemesGetBorder(const std::string& name) { |
| 340 | ThemesInit(); |
| 341 | auto it = border_data.find(name); |
| 342 | if (it == border_data.end()) { |
| 343 | return border_data["standard"]; |
| 344 | } |
| 345 | return it->second; |
| 346 | } |
| 347 | |
| 348 | ThemeBorderData ThemesGetNextBorder(const std::string& current) { |
| 349 | ThemesInit(); |
| 350 | ThemeBorderData ret = border_data["standard"]; |
| 351 | for (auto& pair : border_data) { |
| 352 | if (pair.first == current) { |
| 353 | auto it = border_data.find(current); |
| 354 | ++it; |
| 355 | if (it == border_data.end()) { |
| 356 | it = border_data.begin(); |
| 357 | } |
| 358 | ret = it->second; |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | void ThemesInitCustomBackgrounds() { |
| 366 | std::vector<std::string> custom_backgrounds = sago::GetFileList("textures/backgrounds"); |
| 367 | std::stringstream sprite_stream; |
| 368 | sprite_stream << "{\n"; |
| 369 | bool first = true; |
| 370 | for (const std::string& filename : custom_backgrounds) { |
| 371 | std::cout << "Found custom background " << filename << "\n"; |
| 372 | if (boost::algorithm::ends_with(filename, ".png") || boost::algorithm::ends_with(filename, ".jpg")) { |
| 373 | BackGroundData bg; |
| 374 | bg.name = filename; |
| 375 | std::string texture_name = fmt::format("custom_background_{}",filename);; |
| 376 | bg.background_sprite = texture_name; |
| 377 | bg.background_sprite_16x9 = texture_name; |
| 378 | bg.background_scale = ImgScale::Resize; |
| 379 | background_data[bg.name] = bg; |
| 380 | if (!first) { |
| 381 | sprite_stream << ",\n"; |
| 382 | } |
| 383 | sprite_stream << "\"" << texture_name << "\": {\n"; |
| 384 | sprite_stream << " \"texture\": \"" << "backgrounds/" << filename.substr(0, filename.size()-4) << "\",\n"; |
| 385 | sprite_stream << " \"topx\": 0,\n"; |
| 386 | sprite_stream << " \"topy\": 0,\n"; |
| 387 | sprite_stream << " \"height\": 7680,\n"; |
| 388 | sprite_stream << " \"width\": 10240,\n"; |
| 389 | sprite_stream << " \"number_of_frames\": 1,\n"; |
| 390 | sprite_stream << " \"frame_time\": 1\n"; |
| 391 | sprite_stream << "}\n"; |
| 392 | first = false; |
| 393 | |
| 394 | } |
| 395 | } |
| 396 | sprite_stream << "}\n"; |
| 397 | sago::WriteFileContent("sprites/custom_backgrounds.sprite", sprite_stream.str()); |
| 398 | } |