git repos / sago_multi_scrambler_puzzle2

commit 66f9f660

Poul Sander · 2024-09-12 20:57
66f9f66052792fa0294439fd926c0d83cc5268c0 patch · browse files
parent 99317f86a6246dd9a25d0c40aca918c954dd5b9a

Improve the texture selector a bit

Changed files

A src/editor/SagoTextureSelector.cpp
A src/editor/SagoTextureSelector.hpp
M src/sago_multi_scrambler_puzzle2.cpp before
diff --git a/src/editor/SagoTextureSelector.cpp b/src/editor/SagoTextureSelector.cpp new file mode 100644 index 0000000..1cb33a8 --- /dev/null +++ b/src/editor/SagoTextureSelector.cpp
@@ -0,0 +1,102 @@
+#include "SagoTextureSelector.hpp"
+#include <iostream>
+#include "imgui.h"
+#include "backends/imgui_impl_sdl2.h"
+#include "backends/imgui_impl_sdlrenderer2.h"
+#include <SDL_image.h>
+#include "../../sago/SagoMisc.hpp"
+#include "../globals.hpp"
+
+
+static void addLinesToCanvas(SDL_Renderer* renderer, SDL_Texture* texture, int xstep = 32, int ystep = 32, int xoffset = 0, int yoffset = 0) {
+ int width, height;
+ SDL_QueryTexture(texture, nullptr, nullptr, &width, &height);
+
+ SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
+ if (xstep > 0) {
+ for (int i = xoffset; i < width; i += xstep) {
+ SDL_RenderDrawLine(renderer, i, 0, i, height);
+ }
+ }
+ if (ystep > 0) {
+ for (int i = yoffset; i < height; i += ystep) {
+ SDL_RenderDrawLine(renderer, 0, i, width, i);
+ }
+ }
+}
+
+static void addFolderToList(const std::string& folder, std::vector<std::string>& list, const std::string& filter = "") {
+ std::vector<std::string> textures = sago::GetFileList(folder.c_str());
+ for (const auto& texture : textures) {
+ std::cout << "Texture: " << texture << "\n";
+ if (filter.empty() || texture.find(filter) != std::string::npos) {
+ list.push_back(texture);
+ }
+ }
+}
+
+static std::vector<std::string> populateTree(const std::string& filter = "") {
+ std::vector<std::string> textures;
+ addFolderToList("textures", textures, filter);
+ return textures;
+}
+
+std::string remove_file_extension(const std::string& filename) {
+ size_t lastindex = filename.find_last_of(".");
+ return filename.substr(0, lastindex);
+}
+
+void SagoTextureSelector::runTextureSelectorFrame(SDL_Renderer* target) {
+ ImGui::Begin("TextureList", nullptr, ImGuiWindowFlags_NoCollapse);
+ static char filter[256] = "";
+ ImGui::InputText("Filter", filter, IM_ARRAYSIZE(filter));
+ ImGui::Separator();
+ for (const auto& texture : textures) {
+ if (filter[0] == '\0' || texture.find(filter) != std::string::npos) {
+ if (ImGui::Selectable(texture.c_str(), selected_texture == texture)) {
+ selected_texture = texture;
+ }
+ }
+ }
+
+ ImGui::End();
+
+ ImGui::Begin("Canvas");
+ if (selected_texture.length()) {
+ int tex_w, tex_h;
+ SDL_Texture* current_texture = globalData.dataHolder->getTexturePtr(remove_file_extension(selected_texture));
+ SDL_QueryTexture(current_texture, nullptr, nullptr, &tex_w, &tex_h);
+ ImGui::Text("Size: %d x %d", tex_w, tex_h);
+ ImGui::Image(current_texture, ImVec2((float)tex_w, (float)tex_h));
+ addLinesToCanvas(target, current_texture);
+ }
+ ImGui::End();
+
+}
+
+
+
+SagoTextureSelector::SagoTextureSelector() {
+}
+
+SagoTextureSelector::~SagoTextureSelector() {
+}
+
+bool SagoTextureSelector::IsActive() {
+ return isActive;
+}
+
+void SagoTextureSelector::ProcessInput(const SDL_Event& event, bool &processed) {
+ ImGui_ImplSDL2_ProcessEvent(&event);
+}
+
+void SagoTextureSelector::Draw(SDL_Renderer* target) {
+ runTextureSelectorFrame(target);
+}
+
+void SagoTextureSelector::Update() {
+}
+
+void SagoTextureSelector::Init() {
+ textures = populateTree();
+}
\ No newline at end of file
diff --git a/src/editor/SagoTextureSelector.hpp b/src/editor/SagoTextureSelector.hpp new file mode 100644 index 0000000..2d14f0e --- /dev/null +++ b/src/editor/SagoTextureSelector.hpp
@@ -0,0 +1,51 @@
+/*
+===========================================================================
+ * Sago Multi Scrambler Puzzle
+Copyright (C) 2022 Poul Sander
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see http://www.gnu.org/licenses/
+
+Source information and contacts persons can be found at
+https://github.com/sago007/saland
+===========================================================================
+*/
+
+#include "../../sago/GameStateInterface.hpp"
+#include <vector>
+#include <string>
+
+
+
+
+#pragma once
+
+class SagoTextureSelector : public sago::GameStateInterface {
+public:
+ SagoTextureSelector();
+ SagoTextureSelector(const SagoTextureSelector& orig) = delete;
+ virtual ~SagoTextureSelector();
+
+ bool IsActive() override;
+ void ProcessInput(const SDL_Event& event, bool &processed) override;
+ void Draw(SDL_Renderer* target) override;
+ void Update() override;
+ void Init();
+
+ void runTextureSelectorFrame(SDL_Renderer* target);
+
+private:
+ bool isActive = true;
+ std::vector<std::string> textures;
+ std::string selected_texture;
+};
\ No newline at end of file
diff --git a/src/sago_multi_scrambler_puzzle2.cpp b/src/sago_multi_scrambler_puzzle2.cpp index 6f224e6..da5f4bd 100644 --- a/src/sago_multi_scrambler_puzzle2.cpp +++ b/src/sago_multi_scrambler_puzzle2.cpp
@@ -32,7 +32,7 @@ https://github.com/sago007/saland
#include "ImageSelectState.hpp"
#include "sago_common.hpp"
#include "MainGameState.hpp"
-#include "SagoImGui.hpp"
+#include "editor/SagoTextureSelector.hpp"
#ifndef VERSIONNUMBER
#define VERSIONNUMBER "0.1.0"
@@ -74,6 +74,14 @@ void runFolder(const std::string& folder_name) {
UninitGame();
}
+void runEditor() {
+ InitGame();
+ SagoTextureSelector sts;
+ sts.Init();
+ RunGameState(sts);
+ UninitGame();
+}
+
int main(int argc, const char* argv[]) {
boost::program_options::options_description desc("Options");
@@ -85,6 +93,7 @@ int main(int argc, const char* argv[]) {
("input-file", boost::program_options::value< std::vector<std::string> >(), "Image to open directly")
("collection", boost::program_options::value< std::string >(), "Jump straigt to a named collection. Like \"fairy_tales\"")
("folder", boost::program_options::value< std::string >(), "Open a specific folder.")
+ ("editor", "Opens a build in editor. Not implemented yet.")
;
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
@@ -113,6 +122,10 @@ int main(int argc, const char* argv[]) {
runFolder(folder);
return 0;
}
+ if (vm.count("editor")) {
+ runEditor();
+ return 0;
+ }
runGame();
return 0;
}