diff --git a/Game.cpp b/Game.cpp index cb53b47..aaf08a7 100755 --- a/Game.cpp +++ b/Game.cpp @@ -165,6 +165,34 @@ namespace ZL bool cfgLoaded = sparkEmitter.loadFromJsonFile("../config/spark_config.json", renderer, CONST_ZIP_FILE); uiManager.loadFromFile("../config/ui.json", renderer, CONST_ZIP_FILE); + uiManager.setButtonCallback("playButton", [this](const std::string& name) { + std::cerr << "Play button pressed: " << name << std::endl; + }); + + uiManager.setButtonCallback("exitButton", [](const std::string& name) { + Environment::exitGameLoop = true; + }); + + uiManager.setButtonCallback("settingsButton", [this](const std::string& name) { + if (uiManager.pushMenuFromFile("../config/settings.json", this->renderer, CONST_ZIP_FILE)) { + + uiManager.setButtonCallback("Opt1", [this](const std::string& n) { + std::cerr << "Opt1 pressed: " << n << std::endl; + }); + + uiManager.setButtonCallback("Opt2", [this](const std::string& n) { + std::cerr << "Opt2 pressed: " << n << std::endl; + }); + + uiManager.setButtonCallback("backButton", [this](const std::string& n) { + uiManager.popMenu(); + }); + } + else { + std::cerr << "Failed to open settings menu" << std::endl; + } + }); + cubemapTexture = std::make_shared( std::array{ CreateTextureDataFromBmp24("./resources/sky/space_rt.bmp", CONST_ZIP_FILE), diff --git a/UiManager.cpp b/UiManager.cpp index 2e09b75..670429c 100644 --- a/UiManager.cpp +++ b/UiManager.cpp @@ -188,6 +188,57 @@ namespace ZL { for (auto& c : node->children) collectButtons(c); } + bool UiManager::setButtonCallback(const std::string& name, std::function cb) { + auto b = findButton(name); + if (!b) { + std::cerr << "UiManager: setButtonCallback failed, button not found: " << name << std::endl; + return false; + } + b->onClick = std::move(cb); + return true; + } + + bool UiManager::pushMenuFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile) { + MenuState prev; + prev.root = root; + prev.buttons = buttons; + prev.pressedButton = pressedButton; + prev.path = ""; + + try { + loadFromFile(path, renderer, zipFile); + menuStack.push_back(std::move(prev)); + return true; + } + catch (const std::exception& e) { + std::cerr << "UiManager: pushMenuFromFile failed to load " << path << " : " << e.what() << std::endl; + return false; + } + } + + bool UiManager::popMenu() { + if (menuStack.empty()) { + std::cerr << "UiManager: popMenu called but menu stack is empty" << std::endl; + return false; + } + auto s = menuStack.back(); + menuStack.pop_back(); + + root = s.root; + buttons = s.buttons; + pressedButton = s.pressedButton; + + for (auto& b : buttons) { + if (b) b->buildMesh(); + } + + return true; + } + + void UiManager::clearMenuStack() { + menuStack.clear(); + } + void UiManager::draw(Renderer& renderer) { if (!root) return; diff --git a/UiManager.h b/UiManager.h index 87bbbe1..2ea6723 100644 --- a/UiManager.h +++ b/UiManager.h @@ -69,6 +69,12 @@ namespace ZL { std::shared_ptr findButton(const std::string& name); + bool setButtonCallback(const std::string& name, std::function cb); + + bool pushMenuFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile = ""); + bool popMenu(); + void clearMenuStack(); + private: std::shared_ptr parseNode(const json& j, Renderer& renderer, const std::string& zipFile); void layoutNode(const std::shared_ptr& node); @@ -78,6 +84,15 @@ namespace ZL { std::vector> buttons; std::shared_ptr pressedButton; + + struct MenuState { + std::shared_ptr root; + std::vector> buttons; + std::shared_ptr pressedButton; + std::string path; + }; + + std::vector menuStack; }; } // namespace ZL \ No newline at end of file diff --git a/config/settings.json b/config/settings.json new file mode 100644 index 0000000..1a9b026 --- /dev/null +++ b/config/settings.json @@ -0,0 +1,72 @@ +{ + "root": { + "type": "FrameLayout", + "x": 0, + "y": 0, + "width": 1280, + "height": 720, + "children": [ + { + "type": "FrameLayout", + "name": "centerPanel", + "x": 480, + "y": 160, + "width": 320, + "height": 400, + "children": [ + { + "type": "LinearLayout", + "name": "settingsButtons", + "orientation": "vertical", + "spacing": 10, + "x": 0, + "y": 0, + "width": 300, + "height": 300, + "children": [ + { + "type": "Button", + "name": "Opt1", + "x": 100, + "y": 300, + "width": 200, + "height": 50, + "textures": { + "normal": "./resources/button.png", + "hover": "./resources/button.png", + "pressed": "./resources/button.png" + } + }, + { + "type": "Button", + "name": "Opt2", + "x": 100, + "y": 200, + "width": 200, + "height": 50, + "textures": { + "normal": "./resources/button.png", + "hover": "./resources/button.png", + "pressed": "./resources/button.png" + } + }, + { + "type": "Button", + "name": "backButton", + "x": 100, + "y": 100, + "width": 200, + "height": 50, + "textures": { + "normal": "./resources/button.png", + "hover": "./resources/button.png", + "pressed": "./resources/button.png" + } + } + ] + } + ] + } + ] + } + } \ No newline at end of file diff --git a/config/ui.json b/config/ui.json index e21e113..8127c68 100644 --- a/config/ui.json +++ b/config/ui.json @@ -28,13 +28,13 @@ "type": "Button", "name": "playButton", "x": 100, - "y": 100, + "y": 300, "width": 200, "height": 50, "textures": { "normal": "./resources/button.png", - "hover": "./resources/rock.png", - "pressed": "./resources/sand.png" + "hover": "./resources/button.png", + "pressed": "./resources/button.png" } }, { @@ -45,22 +45,22 @@ "width": 200, "height": 50, "textures": { - "normal": "./resources/button.png", - "hover": "./resources/rock.png", + "normal": "./resources/sand.png", + "hover": "./resources/sand.png", "pressed": "./resources/sand.png" } }, { "type": "Button", "name": "exitButton", - "x": 150, - "y": 300, + "x": 100, + "y": 100, "width": 200, "height": 50, "textures": { - "normal": "./resources/button.png", + "normal": "./resources/rock.png", "hover": "./resources/rock.png", - "pressed": "./resources/sand.png" + "pressed": "./resources/rock.png" } } ]