| 1 | /* |
| 2 | =========================================================================== |
| 3 | * Sago Multi Scrambler Puzzle |
| 4 | Copyright (C) 2022-2024 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://github.com/sago007/saland |
| 21 | =========================================================================== |
| 22 | */ |
| 23 | |
| 24 | #include "editor_loader.hpp" |
| 25 | #include "../sago/SagoMisc.hpp" |
| 26 | #include <boost/algorithm/string/predicate.hpp> |
| 27 | #include <iostream> |
| 28 | |
| 29 | |
| 30 | void to_json(nlohmann::json& j, const SagoSprite& p) { |
| 31 | j = nlohmann::json{{"texture", p.texture}, {"topx", p.topx}, {"topy", p.topy}, {"height", p.height}, {"width", p.width}, {"number_of_frames", p.number_of_frames}, {"frame_time", p.frame_time}, {"originx", p.originx}, {"originy", p.originy}}; |
| 32 | } |
| 33 | |
| 34 | void from_json(const nlohmann::json& j, SagoSprite& p) { |
| 35 | j.at("texture").get_to(p.texture); |
| 36 | j.at("topx").get_to(p.topx); |
| 37 | j.at("topy").get_to(p.topy); |
| 38 | j.at("height").get_to(p.height); |
| 39 | j.at("width").get_to(p.width); |
| 40 | if (j.contains("number_of_frames")) { |
| 41 | j.at("number_of_frames").get_to(p.number_of_frames); |
| 42 | } |
| 43 | if (j.contains("frame_time")) { |
| 44 | j.at("frame_time").get_to(p.frame_time); |
| 45 | } |
| 46 | if(j.contains("originx")) { |
| 47 | j.at("originx").get_to(p.originx); |
| 48 | } |
| 49 | if(j.contains("originy")) { |
| 50 | j.at("originy").get_to(p.originy); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void from_json(const nlohmann::json& j, SagoSpriteFile& p) { |
| 55 | std::cout << "from_json\n"; |
| 56 | for (auto& [key, value] : j.items()) { |
| 57 | SagoSprite s; |
| 58 | if (!value.is_object()) { |
| 59 | continue; |
| 60 | } |
| 61 | from_json(value, s); |
| 62 | p.sprites[key] = s; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void AppendFromFile(const std::string& json_content, const std::string& filename, std::map<std::string, SagoSprite>& sprites) { |
| 67 | nlohmann::json j = nlohmann::json::parse(json_content); |
| 68 | //std::map<std::string, SagoSprite> file; |
| 69 | for (auto& sprite : j.items()) { |
| 70 | std::cout << sprite.key() << "\n"; |
| 71 | if (!sprite.value().is_object()) { |
| 72 | std::cout << "Not an object\n"; |
| 73 | continue; |
| 74 | } |
| 75 | std::string name = sprite.key(); |
| 76 | SagoSprite s = sprite.value(); |
| 77 | s.meta_from_file = filename; |
| 78 | s.meta_name = name; |
| 79 | sprites[name] = s; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | std::map<std::string, SagoSprite> LoadSprites() { |
| 84 | std::map<std::string, SagoSprite> sprites; |
| 85 | std::vector<std::string> spritefiles = sago::GetFileList("sprites"); |
| 86 | for (std::string& item : spritefiles ) { |
| 87 | if (boost::algorithm::ends_with(item,".sprite")) { |
| 88 | std::string filename = "sprites/"+item; |
| 89 | std::string content = sago::GetFileContent(filename.c_str()); |
| 90 | if (content.empty()) { |
| 91 | std::cout << "File \"" << filename << "\" does not exit!\n"; |
| 92 | return sprites; |
| 93 | } |
| 94 | std::cout << "Reading " << filename << "\n"; |
| 95 | AppendFromFile(content, item, sprites); |
| 96 | } |
| 97 | } |
| 98 | return sprites; |
| 99 | } |