From f0829bb9ea8fad4646b0f73e1edfd4589e5ca991 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 30 Dec 2024 16:33:56 -0500 Subject: [PATCH] Starting a bit of refactoring to sort out how to handle the various UIs. --- gui.cpp | 113 +++++++++++++++++++++++++++++------------------------- gui.hpp | 36 ++++++++++++++--- panel.hpp | 3 +- 3 files changed, 94 insertions(+), 58 deletions(-) diff --git a/gui.cpp b/gui.cpp index 70570ba..3485a7e 100644 --- a/gui.cpp +++ b/gui.cpp @@ -29,49 +29,7 @@ using namespace std::chrono_literals; using namespace ftxui; using namespace components; - -GUI::GUI(DinkyECS::World &world, Map& game_map) : - $game_map(game_map), - $log({{"Welcome to the game!"}}), - $status_ui(0, 0, STATUS_UI_WIDTH, STATUS_UI_HEIGHT), - $map_view(GAME_MAP_PIXEL_POS, 0, 0, 0, true), - $inventory_ui(INVENTORY_PIXEL_X, INVENTORY_PIXEL_Y, INVENTORY_WIDTH, INVENTORY_HEIGHT), - $lights(game_map.width(), game_map.height()), - $world(world), - $sounds("./assets"), - $renderer() -{ - // this needs a config file soon - // $sounds.load("ambient", "ambient_sound.mp3"); - $sounds.load("loot_gold", "loot_gold.mp3"); - $sounds.load("combat_player_hit", "combat_player_hit.mp3"); - $sounds.load("combat_enemy_hit", "combat_enemy_hit.mp3"); - $sounds.load("combat_miss", "combat_miss.mp3"); - resize_map(MAX_FONT_SIZE); - init_shaders(); -} - -void GUI::resize_map(int new_size) { - $renderer.resize_grid(new_size, $map_view); - // set canvas to best size - $canvas = Canvas($map_view.width * 2, $map_view.height * 4); -} - -void GUI::save_world() { - $log.log("Game saved!"); - save::to_file("./savefile.world", $world, $game_map); -} - -void GUI::create_renderer() { - $renderer.init_terminal(); - auto player = $world.get_the(); - - $map_view.set_renderer(Renderer([&] { - System::draw_map($world, $game_map, $lights.lighting(), $canvas, $map_view.width, $map_view.height); - return canvas($canvas); - })); - - +void InventoryUI::create_render() { auto cell = [](const char* t) { return text(t) | border; }; auto inventory_test = Renderer([cell] { return hflow({ @@ -85,6 +43,12 @@ void GUI::create_renderer() { }) | border | flex; }); + set_renderer(inventory_test); +} + +void StatusUI::create_render() { + auto player = $world.get_the(); + auto status_rend = Renderer([&, player]{ const auto& player_combat = $world.get(player.entity); const auto& inventory = $world.get(player.entity); @@ -112,8 +76,53 @@ void GUI::create_renderer() { }); }); - $status_ui.set_renderer(status_rend); - $inventory_ui.set_renderer(inventory_test); + set_renderer(status_rend); +} + + + +GUI::GUI(DinkyECS::World &world, Map& game_map) : + $game_map(game_map), + $status_ui(world), + $map_view(), + $inventory_ui(), + $lights(game_map.width(), game_map.height()), + $world(world), + $sounds("./assets"), + $renderer() +{ + // this needs a config file soon + // $sounds.load("ambient", "ambient_sound.mp3"); + $sounds.load("loot_gold", "loot_gold.mp3"); + $sounds.load("combat_player_hit", "combat_player_hit.mp3"); + $sounds.load("combat_enemy_hit", "combat_enemy_hit.mp3"); + $sounds.load("combat_miss", "combat_miss.mp3"); + resize_map(MAX_FONT_SIZE); + init_shaders(); +} + +void GUI::resize_map(int new_size) { + $renderer.resize_grid(new_size, $map_view); + // set canvas to best size + $canvas = Canvas($map_view.width * 2, $map_view.height * 4); + dbc::log("WHY IS RESIZING LIGHTS STILL WORKING?!!!!!!!!!!!!!!!"); +} + +void GUI::save_world() { + $status_ui.log("Game saved!"); + save::to_file("./savefile.world", $world, $game_map); +} + +void GUI::create_renderer() { + $renderer.init_terminal(); + + $map_view.set_renderer(Renderer([&] { + System::draw_map($world, $game_map, $lights.lighting(), $canvas, $map_view.width, $map_view.height); + return canvas($canvas); + })); + + $status_ui.create_render(); + $inventory_ui.create_render(); } void GUI::handle_world_events() { @@ -129,32 +138,32 @@ void GUI::handle_world_events() { auto enemy_combat = $world.get(entity); if(damage.enemy_did > 0) { - $log.log(format("Enemy HIT YOU for {} damage!", damage.enemy_did)); - $log.log(format("-- Enemy has {} HP left.", enemy_combat.hp)); + $status_ui.log(format("Enemy HIT YOU for {} damage!", damage.enemy_did)); + $status_ui.log(format("-- Enemy has {} HP left.", enemy_combat.hp)); $sounds.play("combat_enemy_hit"); shake(); } else { - $log.log("Enemy MISSED YOU."); + $status_ui.log("Enemy MISSED YOU."); } if(damage.player_did > 0) { - $log.log(format("You HIT enemy for {} damage!", damage.player_did)); + $status_ui.log(format("You HIT enemy for {} damage!", damage.player_did)); $sounds.play("combat_player_hit"); } else { $sounds.play("combat_miss"); - $log.log("You MISSED the enemy."); + $status_ui.log("You MISSED the enemy."); } } break; case eGUI::LOOT: { auto &loot = std::any_cast(data); auto inventory = $world.get(player.entity); $sounds.play("loot_gold"); - $log.log(format("You found {} gold. You have {} now.", + $status_ui.log(format("You found {} gold. You have {} now.", loot.amount, inventory.gold)); } break; default: - $log.log(format("INVALID EVENT! {},{}", evt, entity)); + $status_ui.log(format("INVALID EVENT! {},{}", evt, entity)); } } } diff --git a/gui.hpp b/gui.hpp index c9dba65..1d97ab3 100644 --- a/gui.hpp +++ b/gui.hpp @@ -48,14 +48,40 @@ struct UnDumbTSS { } }; -class GUI { + +class InventoryUI : public Panel { + public: + InventoryUI() : Panel(INVENTORY_PIXEL_X, INVENTORY_PIXEL_Y, INVENTORY_WIDTH, INVENTORY_HEIGHT) {} + void create_render(); +}; + +class StatusUI : public Panel { + public: + ActionLog $log; string $status_text = "NOT DEAD"; + DinkyECS::World& $world; + + StatusUI(DinkyECS::World& world) : + Panel(0, 0, STATUS_UI_WIDTH, STATUS_UI_HEIGHT), + $log({{"Welcome to the game!"}}), + $world(world) {} + void create_render(); + void log(string msg) { + $log.log(msg); + } +}; + +class MapViewUI : public Panel { + public: + MapViewUI() : Panel(GAME_MAP_PIXEL_POS, 0, 0, 0, true) {} +}; + +class GUI { Canvas $canvas; Map& $game_map; - ActionLog $log; - Panel $status_ui; - Panel $map_view; - Panel $inventory_ui; + StatusUI $status_ui; + MapViewUI $map_view; + InventoryUI $inventory_ui; LightRender $lights; bool $show_modal = false; Component $test_button; diff --git a/panel.hpp b/panel.hpp index e61a1aa..e764708 100644 --- a/panel.hpp +++ b/panel.hpp @@ -17,7 +17,8 @@ const int UI_PANEL_BORDER_PX=5; using ftxui::Renderer, ftxui::Component, ftxui::Element, ftxui::Screen; -struct Panel { +class Panel { +public: int x; int y; int width;