add menu
This commit is contained in:
parent
068c319890
commit
bee2fff538
28
Game.cpp
28
Game.cpp
@ -165,6 +165,34 @@ namespace ZL
|
|||||||
bool cfgLoaded = sparkEmitter.loadFromJsonFile("../config/spark_config.json", renderer, CONST_ZIP_FILE);
|
bool cfgLoaded = sparkEmitter.loadFromJsonFile("../config/spark_config.json", renderer, CONST_ZIP_FILE);
|
||||||
uiManager.loadFromFile("../config/ui.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<Texture>(
|
cubemapTexture = std::make_shared<Texture>(
|
||||||
std::array<TextureDataStruct, 6>{
|
std::array<TextureDataStruct, 6>{
|
||||||
CreateTextureDataFromBmp24("./resources/sky/space_rt.bmp", CONST_ZIP_FILE),
|
CreateTextureDataFromBmp24("./resources/sky/space_rt.bmp", CONST_ZIP_FILE),
|
||||||
|
|||||||
@ -188,6 +188,57 @@ namespace ZL {
|
|||||||
for (auto& c : node->children) collectButtons(c);
|
for (auto& c : node->children) collectButtons(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool UiManager::setButtonCallback(const std::string& name, std::function<void(const std::string&)> 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) {
|
void UiManager::draw(Renderer& renderer) {
|
||||||
if (!root) return;
|
if (!root) return;
|
||||||
|
|
||||||
|
|||||||
15
UiManager.h
15
UiManager.h
@ -69,6 +69,12 @@ namespace ZL {
|
|||||||
|
|
||||||
std::shared_ptr<UiButton> findButton(const std::string& name);
|
std::shared_ptr<UiButton> findButton(const std::string& name);
|
||||||
|
|
||||||
|
bool setButtonCallback(const std::string& name, std::function<void(const std::string&)> cb);
|
||||||
|
|
||||||
|
bool pushMenuFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile = "");
|
||||||
|
bool popMenu();
|
||||||
|
void clearMenuStack();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<UiNode> parseNode(const json& j, Renderer& renderer, const std::string& zipFile);
|
std::shared_ptr<UiNode> parseNode(const json& j, Renderer& renderer, const std::string& zipFile);
|
||||||
void layoutNode(const std::shared_ptr<UiNode>& node);
|
void layoutNode(const std::shared_ptr<UiNode>& node);
|
||||||
@ -78,6 +84,15 @@ namespace ZL {
|
|||||||
std::vector<std::shared_ptr<UiButton>> buttons;
|
std::vector<std::shared_ptr<UiButton>> buttons;
|
||||||
|
|
||||||
std::shared_ptr<UiButton> pressedButton;
|
std::shared_ptr<UiButton> pressedButton;
|
||||||
|
|
||||||
|
struct MenuState {
|
||||||
|
std::shared_ptr<UiNode> root;
|
||||||
|
std::vector<std::shared_ptr<UiButton>> buttons;
|
||||||
|
std::shared_ptr<UiButton> pressedButton;
|
||||||
|
std::string path;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<MenuState> menuStack;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ZL
|
} // namespace ZL
|
||||||
72
config/settings.json
Normal file
72
config/settings.json
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -28,13 +28,13 @@
|
|||||||
"type": "Button",
|
"type": "Button",
|
||||||
"name": "playButton",
|
"name": "playButton",
|
||||||
"x": 100,
|
"x": 100,
|
||||||
"y": 100,
|
"y": 300,
|
||||||
"width": 200,
|
"width": 200,
|
||||||
"height": 50,
|
"height": 50,
|
||||||
"textures": {
|
"textures": {
|
||||||
"normal": "./resources/button.png",
|
"normal": "./resources/button.png",
|
||||||
"hover": "./resources/rock.png",
|
"hover": "./resources/button.png",
|
||||||
"pressed": "./resources/sand.png"
|
"pressed": "./resources/button.png"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -45,22 +45,22 @@
|
|||||||
"width": 200,
|
"width": 200,
|
||||||
"height": 50,
|
"height": 50,
|
||||||
"textures": {
|
"textures": {
|
||||||
"normal": "./resources/button.png",
|
"normal": "./resources/sand.png",
|
||||||
"hover": "./resources/rock.png",
|
"hover": "./resources/sand.png",
|
||||||
"pressed": "./resources/sand.png"
|
"pressed": "./resources/sand.png"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "Button",
|
"type": "Button",
|
||||||
"name": "exitButton",
|
"name": "exitButton",
|
||||||
"x": 150,
|
"x": 100,
|
||||||
"y": 300,
|
"y": 100,
|
||||||
"width": 200,
|
"width": 200,
|
||||||
"height": 50,
|
"height": 50,
|
||||||
"textures": {
|
"textures": {
|
||||||
"normal": "./resources/button.png",
|
"normal": "./resources/rock.png",
|
||||||
"hover": "./resources/rock.png",
|
"hover": "./resources/rock.png",
|
||||||
"pressed": "./resources/sand.png"
|
"pressed": "./resources/rock.png"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user