From a7991a8f064a5e17a96cdb2f2f7a3c0f465083a9 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 15 Feb 2025 22:14:19 -0500 Subject: [PATCH] Fixing more bugs related to percentages and then prototype a more complex UI. --- Makefile | 2 +- combat_ui.cpp | 30 +++++++++++++++++------------- combat_ui.hpp | 8 ++++++++ gui.cpp | 7 +++++++ lel.cpp | 18 ++++++++++++------ 5 files changed, 45 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 097aa95..a1533ce 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ tracy_build: meson compile -j 10 -C builddir test: build - ./builddir/runtests "[lel]" + ./builddir/runtests run: build test powershell "cp ./builddir/zedcaster.exe ." diff --git a/combat_ui.cpp b/combat_ui.cpp index 0a6ab5f..895f651 100644 --- a/combat_ui.cpp +++ b/combat_ui.cpp @@ -6,14 +6,17 @@ namespace gui { CombatUI::CombatUI(GameLevel level) : $layout(RAY_VIEW_X, RAY_VIEW_HEIGHT, RAY_VIEW_WIDTH, SCREEN_HEIGHT - RAY_VIEW_HEIGHT), - $level(level) + $level(level), + $font{FONT_FILE_NAME} { - bool good = $layout.parse( - "[attack1 | attack2 | attack3 | heal]" - ); - + bool good = $layout.parse($grid); dbc::check(good, "failed to parse combat layout"); + render(); + } + + void CombatUI::render() { + for(auto& [name, cell] : $layout.cells) { sf::RectangleShape button; button.setPosition({float(cell.x + 10), float(cell.y + 10)}); @@ -23,14 +26,11 @@ namespace gui { button.setOutlineThickness(5); $shapes.insert_or_assign(name, button); - sf::RectangleShape inner; - auto inner_cell = lel::center(30, 40, cell); - inner.setPosition({float(inner_cell.x), float(inner_cell.y)}); - inner.setSize({float(inner_cell.w), float(inner_cell.h)}); - inner.setOutlineColor({100, 0, 0}); - inner.setOutlineThickness(5); - inner.setFillColor({50, 50, 50}); - $shapes.insert_or_assign(name + "_text", inner); + sf::Text label($font, name); + auto bounds = label.getLocalBounds(); + auto inner_cell = lel::center(bounds.size.x, bounds.size.y, cell); + label.setPosition({float(inner_cell.x), float(inner_cell.y)}); + $labels.push_back(label); } } @@ -38,6 +38,10 @@ namespace gui { for(auto& [name, shape] : $shapes) { window.draw(shape); } + + for(auto& label : $labels) { + window.draw(label); + } } void CombatUI::click(int x, int y) { diff --git a/combat_ui.hpp b/combat_ui.hpp index 1522450..a0bfc67 100644 --- a/combat_ui.hpp +++ b/combat_ui.hpp @@ -3,16 +3,24 @@ #include "levelmanager.hpp" #include #include +#include +#include #include "lel.hpp" namespace gui { class CombatUI { public: + std::string $grid = + "[*%(200)hp | _ | *%(200)ap | _ ]" + "[attack1 | attack2 | attack3 | heal]"; lel::Parser $layout; GameLevel $level; + sf::Font $font; std::unordered_map $shapes; + std::vector $labels; CombatUI(GameLevel level); + void render(); void draw(sf::RenderWindow& window); void update_level(GameLevel &level) { $level = level; } void click(int x, int y); diff --git a/gui.cpp b/gui.cpp index 0ccd273..248add2 100644 --- a/gui.cpp +++ b/gui.cpp @@ -161,6 +161,9 @@ namespace gui { case CLOSE: dbc::log("Nothing to close."); break; + case TICK: + // do nothing + break; default: dbc::sentinel("unhandled event in IDLE"); } @@ -243,6 +246,10 @@ namespace gui { case KEY::R: $stats.reset(); break; + case KEY::G: + $combat_view.render(); + event(Event::TICK); + break; case KEY::M: event(Event::MAP_OPEN); break; diff --git a/lel.cpp b/lel.cpp index 6eeb192..11974c5 100644 --- a/lel.cpp +++ b/lel.cpp @@ -35,21 +35,27 @@ namespace lel { dbc::check(cell_width > 0, "invalid cell width calc"); dbc::check(cell_height > 0, "invalid cell height calc"); + for(auto& [name, cell] : cells) { cell.x = grid_x + (cell.col * cell_width); cell.y = grid_y + (cell.row * cell_height); - cell.max_w = cell.max_w == 0 ? cell_width : cell.max_w; - cell.max_h = cell.max_h == 0 ? cell_height : cell.max_h; + // ZED: getting a bit hairy but this should work if(cell.percent) { - cell.max_w = cell.max_w * 0.01 * cell_width; - cell.max_h = cell.max_h * 0.01 * cell_height; + // when percent mode we have to take unset to 100% + if(cell.max_w == 0) cell.max_w = 100; + if(cell.max_h == 0) cell.max_h = 100; + cell.max_w *= cell_width * 0.01; + cell.max_h *= cell_height * 0.01; + } else { + if(cell.max_w == 0) cell.max_w = cell_width; + if(cell.max_h == 0) cell.max_h = cell_height; } cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w); cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h); - cell.mid_x = std::midpoint(cell.x, cell.x + cell_width); - cell.mid_y = std::midpoint(cell.y, cell.y + cell_height); + cell.mid_x = std::midpoint(cell.x, cell.x + cell.w); + cell.mid_y = std::midpoint(cell.y, cell.y + cell.h); if(cell.right) cell.x += cell_width - cell.w; if(cell.bottom) cell.y += cell_height - cell.h;