| 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 "SagoTextureSelector.hpp" |
| 25 | #include <iostream> |
| 26 | #include "imgui.h" |
| 27 | #include "backends/imgui_impl_sdl2.h" |
| 28 | #include "backends/imgui_impl_sdlrenderer2.h" |
| 29 | #include <SDL_image.h> |
| 30 | #include "../../sago/SagoMisc.hpp" |
| 31 | #include "../global.hpp" |
| 32 | #include "../common.h" |
| 33 | |
| 34 | const char* const editor_texture_grid_size = "editor_texture_grid_size"; |
| 35 | |
| 36 | |
| 37 | class ChangeSDLColor |
| 38 | { |
| 39 | public: |
| 40 | ChangeSDLColor(SDL_Renderer* renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a) : renderer(renderer) { |
| 41 | SDL_GetRenderDrawColor(renderer, &oldr, &oldg, &oldb, &olda); |
| 42 | SDL_SetRenderDrawColor(renderer, r, g, b, a); |
| 43 | } |
| 44 | |
| 45 | ChangeSDLColor(SDL_Renderer* renderer) : renderer(renderer) { |
| 46 | SDL_GetRenderDrawColor(renderer, &oldr, &oldg, &oldb, &olda); |
| 47 | } |
| 48 | |
| 49 | void setRed() { |
| 50 | SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); |
| 51 | } |
| 52 | |
| 53 | void setYellow() { |
| 54 | SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); |
| 55 | } |
| 56 | |
| 57 | void reset() { |
| 58 | SDL_SetRenderDrawColor(renderer, oldr, oldg, oldb, olda); |
| 59 | } |
| 60 | |
| 61 | ~ChangeSDLColor() { |
| 62 | SDL_SetRenderDrawColor(renderer, oldr, oldg, oldb, olda); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | SDL_Renderer* renderer; |
| 67 | Uint8 oldr, oldg, oldb, olda; |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | static void addLinesToCanvas(SDL_Renderer* renderer, SDL_Texture* texture, int xstep = 32, int ystep = 32, int xoffset = 0, int yoffset = 0) { |
| 72 | int width, height; |
| 73 | SDL_QueryTexture(texture, nullptr, nullptr, &width, &height); |
| 74 | Uint8 r, g, b, a; |
| 75 | SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a); |
| 76 | |
| 77 | if (xstep > 0) { |
| 78 | for (int i = 0; i < width+1; i += xstep) { |
| 79 | ImGui::GetWindowDrawList()->AddLine(ImVec2(i+xoffset, yoffset), ImVec2(i+xoffset, height+yoffset), IM_COL32(r, g, b, a)); |
| 80 | } |
| 81 | } |
| 82 | if (ystep > 0) { |
| 83 | for (int i = 0; i < height+1; i += ystep) { |
| 84 | ImGui::GetWindowDrawList()->AddLine(ImVec2(xoffset, i+yoffset), ImVec2(width+xoffset, i+yoffset), IM_COL32(r, g, b, a)); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static void addRectableToCanvas(SDL_Renderer* renderer, int topx = 0, int topy = 0, int height = 100, int width = 100, int xoffset = 0, int yoffset = 0) { |
| 90 | Uint8 r, g, b, a; |
| 91 | SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a); |
| 92 | topx += xoffset; |
| 93 | topy += yoffset; |
| 94 | ImGui::GetWindowDrawList()->AddLine(ImVec2(topx, topy), ImVec2(topx+width, topy), IM_COL32(r, g, b, a)); |
| 95 | ImGui::GetWindowDrawList()->AddLine(ImVec2(topx+width, topy), ImVec2(topx+width, topy+height), IM_COL32(r, g, b, a)); |
| 96 | ImGui::GetWindowDrawList()->AddLine(ImVec2(topx+width, topy+height), ImVec2(topx, topy+height), IM_COL32(r, g, b, a)); |
| 97 | ImGui::GetWindowDrawList()->AddLine(ImVec2(topx, topy+height), ImVec2(topx, topy), IM_COL32(r, g, b, a)); |
| 98 | } |
| 99 | |
| 100 | static void addFolderToList(const std::string& folder, std::vector<std::string>& list, const std::string& filter = "") { |
| 101 | std::vector<std::string> textures = sago::GetFileList(folder.c_str()); |
| 102 | for (const auto& texture : textures) { |
| 103 | std::cout << "Texture: " << texture << "\n"; |
| 104 | if (texture.find(".png") == std::string::npos) { |
| 105 | continue; |
| 106 | } |
| 107 | if (filter.empty() || texture.find(filter) != std::string::npos) { |
| 108 | list.push_back(texture); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static std::vector<std::string> populateTree(const std::string& filter = "") { |
| 114 | std::vector<std::string> textures; |
| 115 | addFolderToList("textures", textures, filter); |
| 116 | return textures; |
| 117 | } |
| 118 | |
| 119 | std::string remove_file_extension(const std::string& filename) { |
| 120 | size_t lastindex = filename.find_last_of("."); |
| 121 | return filename.substr(0, lastindex); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | static void ImGuiWritePartOfImage(SDL_Texture* texture, int topx, int topy, int w, int h) { |
| 126 | int tex_w, tex_h; |
| 127 | SDL_QueryTexture(texture, nullptr, nullptr, &tex_w, &tex_h); |
| 128 | float sprite_w = w; |
| 129 | float sprite_h = h; |
| 130 | float topxf = topx; |
| 131 | float topyf = topy; |
| 132 | ImVec2 uv0 = ImVec2(topxf / tex_w, topyf / tex_h); |
| 133 | ImVec2 uv1 = ImVec2((topxf + sprite_w) / tex_w, (topyf + sprite_h) / tex_h); |
| 134 | ImGui::Image((ImTextureID)(intptr_t)texture, ImVec2((float)w, (float)h), uv0, uv1); |
| 135 | } |
| 136 | |
| 137 | void SagoTextureSelector::runSpriteSelectorFrame(SDL_Renderer* target) { |
| 138 | ImGui::Begin("SpriteList", nullptr, ImGuiWindowFlags_NoCollapse); |
| 139 | static char filter[256] = ""; |
| 140 | ImGui::InputText("Filter", filter, IM_ARRAYSIZE(filter)); |
| 141 | ImGui::Separator(); |
| 142 | for (const auto& sprite : sprites) { |
| 143 | std::string sprite_name = sprite.first; |
| 144 | if (filter[0] == '\0' || sprite_name.find(filter) != std::string::npos) { |
| 145 | if (ImGui::Selectable(sprite_name.c_str(), selected_sprite == sprite_name)) { |
| 146 | selected_sprite = sprite_name; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | ImGui::End(); |
| 152 | |
| 153 | ImGui::Begin("SpriteViewer"); |
| 154 | if (selected_sprite.length()) { |
| 155 | const SagoSprite& current_sprite = sprites[selected_sprite]; |
| 156 | SDL_Texture* current_texture = globalData.dataHolder->getTexturePtr(current_sprite.texture); |
| 157 | ImGui::Text("Size: %d x %d", current_sprite.width, current_sprite.height); |
| 158 | ImGui::BeginChild("Test"); |
| 159 | int offset = current_sprite.width * (SDL_GetTicks()/current_sprite.frame_time % current_sprite.number_of_frames); |
| 160 | ImGuiWritePartOfImage(current_texture, current_sprite.topx+offset, current_sprite.topy, current_sprite.width, current_sprite.height); |
| 161 | ImGui::EndChild(); |
| 162 | } |
| 163 | ImGui::End(); |
| 164 | |
| 165 | ImGui::Begin("SpriteTexture"); |
| 166 | if (selected_sprite.length() && sprites[selected_sprite].texture.length()) { |
| 167 | int tex_w, tex_h; |
| 168 | const SagoSprite& current_sprite = sprites[selected_sprite]; |
| 169 | SDL_Texture* current_texture = globalData.dataHolder->getTexturePtr(current_sprite.texture); |
| 170 | SDL_QueryTexture(current_texture, nullptr, nullptr, &tex_w, &tex_h); |
| 171 | ImGui::Text("Size: %d x %d", tex_w, tex_h); |
| 172 | ImGui::BeginChild("Test"); |
| 173 | ImVec2 p = ImGui::GetCursorScreenPos(); |
| 174 | ImGui::Image((ImTextureID)(intptr_t)current_texture, ImVec2((float)tex_w, (float)tex_h)); |
| 175 | ChangeSDLColor color(target); |
| 176 | color.setYellow(); |
| 177 | for (int i = 1; i < current_sprite.number_of_frames; i++) { |
| 178 | addRectableToCanvas(target, current_sprite.topx+i*current_sprite.width, current_sprite.topy, current_sprite.height, current_sprite.width, p.x, p.y); |
| 179 | } |
| 180 | color.setRed(); |
| 181 | addRectableToCanvas(target, current_sprite.topx, current_sprite.topy, current_sprite.height, current_sprite.width, p.x, p.y); |
| 182 | ImGui::EndChild(); |
| 183 | } |
| 184 | ImGui::End(); |
| 185 | } |
| 186 | |
| 187 | void SagoTextureSelector::runTextureSelectorFrame(SDL_Renderer* target) { |
| 188 | ImGui::Begin("TextureList", nullptr, ImGuiWindowFlags_NoCollapse); |
| 189 | if (ImGui::SliderInt("grid size", &grid_size, 0, 100)) |
| 190 | { |
| 191 | Config::getInstance()->setInt(editor_texture_grid_size, grid_size); |
| 192 | } |
| 193 | static char filter[256] = ""; |
| 194 | ImGui::InputText("Filter", filter, IM_ARRAYSIZE(filter)); |
| 195 | ImGui::Separator(); |
| 196 | for (const auto& texture : textures) { |
| 197 | if (filter[0] == '\0' || texture.find(filter) != std::string::npos) { |
| 198 | if (ImGui::Selectable(texture.c_str(), selected_texture == texture)) { |
| 199 | selected_texture = texture; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | ImGui::End(); |
| 204 | |
| 205 | ImGui::Begin("TextureViewer"); |
| 206 | if (selected_texture.length()) { |
| 207 | int tex_w, tex_h; |
| 208 | SDL_Texture* current_texture = globalData.dataHolder->getTexturePtr(remove_file_extension(selected_texture)); |
| 209 | SDL_QueryTexture(current_texture, nullptr, nullptr, &tex_w, &tex_h); |
| 210 | ImGui::Text("Size: %d x %d", tex_w, tex_h); |
| 211 | ImGui::BeginChild("Test"); |
| 212 | ImVec2 p = ImGui::GetCursorScreenPos(); |
| 213 | ImGui::Image((ImTextureID)(intptr_t)current_texture, ImVec2((float)tex_w, (float)tex_h)); |
| 214 | ChangeSDLColor color(target); |
| 215 | color.setRed(); |
| 216 | if (grid_size) |
| 217 | { |
| 218 | addLinesToCanvas(target, current_texture, grid_size, grid_size, p.x, p.y); |
| 219 | } |
| 220 | ImGui::EndChild(); |
| 221 | } |
| 222 | ImGui::End(); |
| 223 | |
| 224 | } |
| 225 | |
| 226 | |
| 227 | |
| 228 | SagoTextureSelector::SagoTextureSelector() { |
| 229 | grid_size = Config::getInstance()->getInt(editor_texture_grid_size, 32); |
| 230 | } |
| 231 | |
| 232 | SagoTextureSelector::~SagoTextureSelector() { |
| 233 | } |
| 234 | |
| 235 | bool SagoTextureSelector::IsActive() { |
| 236 | return isActive; |
| 237 | } |
| 238 | |
| 239 | void SagoTextureSelector::ProcessInput(const SDL_Event& event, bool &processed) { |
| 240 | } |
| 241 | |
| 242 | void SagoTextureSelector::Draw(SDL_Renderer* target) { |
| 243 | runTextureSelectorFrame(target); |
| 244 | runSpriteSelectorFrame(target); |
| 245 | } |
| 246 | |
| 247 | void SagoTextureSelector::Update() { |
| 248 | } |
| 249 | |
| 250 | void SagoTextureSelector::Init() { |
| 251 | textures = populateTree(); |
| 252 | sprites = LoadSprites(); |
| 253 | } |