diff --git a/assets/scenes.json b/assets/scenes.json index 2f052c8..03b95fd 100644 --- a/assets/scenes.json +++ b/assets/scenes.json @@ -1,27 +1,53 @@ { "DEATH": { "layout": [ - "[=text]" + "[_|_|_|_|_|_]", + "[_|_|_|*%(200,100)buttons|_|_]" ], "background": "death_scene", "actors": [ ], "fixtures": [ - ] + ], + "buttons": { + "layout": [ + "[_]", + "[_|=*%(300, 100)Restart|_|_|_]", + "[_|=*%(300, 100)Quit|_|_|_]", + "[_]" + ], + "actions": { + "Restart": 43, + "Quit": 38 + } + } }, "NEXT_LEVEL": { "layout": [ - "[=text]" + "[_|_|_|_|_|_]", + "[_|_|_|*%(200,100)buttons|_|_]" ], "background": "next_level_scene", "actors": [ ], "fixtures": [ - ] + ], + "buttons": { + "layout": [ + "[_]", + "[_|=*%(300, 100)Continue|_|_|_]", + "[_|=*%(300, 100)Quit|_|_|_]", + "[_]" + ], + "actions": { + "Continue": 43, + "Quit": 38 + } + } }, "STARTING": { "layout": [ - "[_|*%(200, 200)ag_bot|_|*%(200,100)speech|_|_]", + "[_|*%(200, 200)ag_bot|_|=*%(200,100)speech|_|_]", "[_|_|_ |*%(200,100)buttons|_|_]" ], "background": "starting_scene", @@ -51,16 +77,43 @@ "at_mid": false, "flipped": false } - ] + ], + "buttons": { + "layout": [ + "[_]", + "[_|=*%(300, 100)Start|_|_|_]", + "[_|=*%(300, 100)About|_|_|_]", + "[_|=*%(300, 100)Quit|_|_|_]", + "[_]" + ], + "actions": { + "Start": 43, + "About": 43, + "Quit": 38 + } + } }, "WIN": { "layout": [ - "[=text]" + "[_|_|_|_|_|_]", + "[_|_|_|*%(200,100)buttons|_|_]" ], "background": "win_scene", "actors": [ ], "fixtures": [ - ] + ], + "buttons": { + "layout": [ + "[_]", + "[_|=*%(300, 100)Restart|_|_|_]", + "[_|=*%(300, 100)Quit|_|_|_]", + "[_]" + ], + "actions": { + "Restart": 43, + "Quit": 38 + } + } } } diff --git a/assets/scenes/death_scene.png b/assets/scenes/death_scene.png index 428ad1c..5654129 100644 Binary files a/assets/scenes/death_scene.png and b/assets/scenes/death_scene.png differ diff --git a/assets/scenes/next_level_scene.png b/assets/scenes/next_level_scene.png index 8cbdbfc..ff81251 100644 Binary files a/assets/scenes/next_level_scene.png and b/assets/scenes/next_level_scene.png differ diff --git a/assets/scenes/win_scene.png b/assets/scenes/win_scene.png index f4dafe4..a724368 100644 Binary files a/assets/scenes/win_scene.png and b/assets/scenes/win_scene.png differ diff --git a/src/game/components.hpp b/src/game/components.hpp index bfc5ade..84d4e64 100644 --- a/src/game/components.hpp +++ b/src/game/components.hpp @@ -94,6 +94,7 @@ namespace components { std::vector layout; json actors; json fixtures; + json buttons; }; struct Storyboard { @@ -168,7 +169,7 @@ namespace components { using ComponentMap = std::unordered_map; ENROLL_COMPONENT(Tile, display, foreground, background); - ENROLL_COMPONENT(AnimatedScene, background, layout, actors, fixtures); + ENROLL_COMPONENT(AnimatedScene, background, layout, actors, fixtures, buttons); ENROLL_COMPONENT(Sprite, name, scale); ENROLL_COMPONENT(Curative, hp); ENROLL_COMPONENT(LightSource, strength, radius); diff --git a/src/graphics/scene.hpp b/src/graphics/scene.hpp index e0b0b9c..d6ad2bd 100644 --- a/src/graphics/scene.hpp +++ b/src/graphics/scene.hpp @@ -44,6 +44,10 @@ namespace scene { void update(); bool mouse(float x, float y, guecs::Modifiers mods); void attach_text(const std::string& actor, const std::string& text); + + nlohmann::json& buttons() { return $scene.buttons; }; + lel::Cell& button_cell() { return $ui.cell_for("buttons"); } + Element config_scene_element(nlohmann::json& config, bool duped); sf::Vector2f position_sprite(textures::SpriteTexture& st, const std::string& cell_name, sf::Vector2f scale, bool at_mid, float x_diff=0.0f, float y_diff=0.0f); diff --git a/src/gui/fsm.cpp b/src/gui/fsm.cpp index 640630b..9a53796 100644 --- a/src/gui/fsm.cpp +++ b/src/gui/fsm.cpp @@ -68,11 +68,18 @@ namespace gui { switch(ev) { case MOUSE_CLICK: + mouse_action(guecs::NO_MODS); + break; + case MOUSE_MOVE: { + mouse_action({1 << guecs::ModBit::hover}); + } break; + case START: close_scene(); state(State::IDLE); break; - case MOUSE_MOVE: { - } break; + case QUIT: + FSM::IDLE(ev, {}); + break; case TICK: break; default: @@ -86,8 +93,17 @@ namespace gui { switch(ev) { case MOUSE_CLICK: - show_scene("STARTING"); - state(State::START_SCENE); + mouse_action(guecs::NO_MODS); + break; + case MOUSE_MOVE: { + mouse_action({1 << guecs::ModBit::hover}); + } break; + case START: + close_scene(); + state(State::IDLE); + break; + case QUIT: + FSM::IDLE(ev, {}); break; default: break; @@ -99,8 +115,17 @@ namespace gui { switch(ev) { case MOUSE_CLICK: - show_scene("STARTING"); - state(State::START_SCENE); + mouse_action(guecs::NO_MODS); + break; + case MOUSE_MOVE: { + mouse_action({1 << guecs::ModBit::hover}); + } break; + case START: + close_scene(); + state(State::IDLE); + break; + case QUIT: + FSM::IDLE(ev, {}); break; default: break; @@ -112,10 +137,18 @@ namespace gui { switch(ev) { case MOUSE_CLICK: - next_level(); + mouse_action(guecs::NO_MODS); + break; + case MOUSE_MOVE: { + mouse_action({1 << guecs::ModBit::hover}); + } break; + case START: close_scene(); state(State::IDLE); break; + case QUIT: + FSM::IDLE(ev, {}); + break; default: break; } @@ -246,7 +279,7 @@ namespace gui { fmt::println("!!!!!!!!!!!!!! COMBAT START in IDLE"); break; case COMBAT_STOP: - fmt::println("!!!!!!!!!!!!!!!! COMBAT STOP in IDLE"); + fmt::println("!!!!!!!!!!!!!! COMBAT STOP in IDLE"); break; default: break; // ignore everything else @@ -278,6 +311,12 @@ namespace gui { void FSM::mouse_action(guecs::Modifiers mods) { sf::Vector2f pos = mouse_position(); + + if($cur_scene != nullptr) { + $cur_scene->mouse(pos.x, pos.y, mods); + return; + } + if($debug_ui.active) $debug_ui.mouse(pos.x, pos.y, mods); $status_ui.mouse(pos.x, pos.y, mods); @@ -460,6 +499,9 @@ namespace gui { show_scene("NEXT_LEVEL"); state(State::NEXT_LEVEL_SCENE); break; + case eGUI::START: + event(Event::START, data); + break; case eGUI::DEATH: { $status_ui.update(); diff --git a/src/gui/scene_ui.cpp b/src/gui/scene_ui.cpp index 0cc9362..baed9af 100644 --- a/src/gui/scene_ui.cpp +++ b/src/gui/scene_ui.cpp @@ -1,16 +1,60 @@ #include "gui/scene_ui.hpp" #include "game/config.hpp" +#include "events.hpp" +#include "gui/guecstra.hpp" +#include +#define DEBUG 0 namespace gui { void SceneUI::init() { $view_sprite.setPosition({0,0}); $scene.init(); + create_buttons($scene.buttons()); + } + + void SceneUI::create_buttons(nlohmann::json& buttons) { + // buttons are optional + if(buttons.size() == 0) return; + + // alright have buttons + has_buttons = true; + + std::string layout{}; + auto& actions = buttons["actions"]; + + for(auto& line : buttons["layout"]) { + layout.append(line); + } + + auto& button_cell = $scene.button_cell(); + + $ui.position(button_cell.x, button_cell.y, button_cell.w, button_cell.h); + $ui.layout(layout); + + for(auto& [name, cell] : $ui.cells()) { + auto ui_id = $ui.entity(name); + $ui.set(ui_id, {guecs::to_wstring(name)}); + $ui.set(ui_id, {}); + $ui.set(ui_id, {}); + + if(actions.contains(name)) { + auto event_id = actions[name]; + $ui.set(ui_id, guecs::make_action(ui_id, event_id)); + } + } + + $ui.init(); } void SceneUI::render(sf::RenderTarget &target) { $scene.render($view_texture); target.draw($view_sprite); + + if(has_buttons) { + $ui.render(target); + if(DEBUG) $ui.debug_layout(target); + } } void SceneUI::update() { @@ -19,6 +63,7 @@ namespace gui { bool SceneUI::mouse(float x, float y, guecs::Modifiers mods) { $scene.mouse(x, y, mods); + $ui.mouse(x, y, mods); return false; } diff --git a/src/gui/scene_ui.hpp b/src/gui/scene_ui.hpp index 49be65d..32bdbb2 100644 --- a/src/gui/scene_ui.hpp +++ b/src/gui/scene_ui.hpp @@ -12,8 +12,11 @@ namespace gui { sf::Sprite $view_sprite{$view_texture.getTexture()}; AnimatedScene $scene_data{load_scene(name)}; scene::Engine $scene{$scene_data}; + bool has_buttons = false; + guecs::UI $ui; AnimatedScene load_scene(const std::string& name); + void create_buttons(nlohmann::json& buttons); void init(); void render(sf::RenderTarget &target);