diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-23 17:05:43 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-23 18:24:48 +0200 |
commit | a570e494bca43c30d7ec91d293051c0c818509b5 (patch) | |
tree | 4eea8c16d1434893ca51b721c5498ece65c836aa /src/game | |
parent | ec5c7cbccc533bacab661e991e7011584d3bb47f (diff) |
feat: add statusline mode styling
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/components/statusline.cpp | 77 | ||||
-rw-r--r-- | src/game/components/statusline.hpp | 7 | ||||
-rw-r--r-- | src/game/game.cpp | 9 | ||||
-rw-r--r-- | src/game/status_manager.cpp | 10 | ||||
-rw-r--r-- | src/game/status_manager.hpp | 4 |
5 files changed, 90 insertions, 17 deletions
diff --git a/src/game/components/statusline.cpp b/src/game/components/statusline.cpp index 5b59a53..1892f86 100644 --- a/src/game/components/statusline.cpp +++ b/src/game/components/statusline.cpp @@ -3,15 +3,17 @@ #include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" #include "util/color.hpp" +#include "util/ranges.hpp" #include <fmt/color.h> +#include <optional> #include <utility> StatusLine::StatusLine(std::shared_ptr<ComponentMatrix> component_matrix) noexcept : _component_matrix(std::move(component_matrix)), _need_render(false) { - _component_matrix->fill(' '); + _component_matrix->fill(ComponentElement({.value = ' '})); } auto StatusLine::get() const noexcept -> const std::shared_ptr<ComponentMatrix> & @@ -39,11 +41,16 @@ auto StatusLine::get_background_color() const noexcept -> uint32_t return STATUSLINE_COLOR; } -void StatusLine::set_status( +void StatusLine::set_section_status( StatusLineSection section, const std::string_view &status, int32_t start) noexcept { + if (status.length() == 0U) + { + return; + } + _clear_section(section, start); auto section_start = _get_section_start_x(section); @@ -56,11 +63,60 @@ void StatusLine::set_status( const auto status_len = static_cast<int32_t>(status.length()); + const auto section_style = + _section_styles.contains(section) ? _section_styles.at(section) : Style(); + + auto is_style_applied = false; + + if (section_style.padding_left > 0U) + { + auto padding_remaining = section_style.padding_left; + + _component_matrix->set(pos, {.value = ' ', .style = section_style}); + padding_remaining--; + + for (auto index : IotaView(0U, padding_remaining)) + { + pos += Vector2::right(); + + _component_matrix->set(pos, {.value = ' '}); + } + + is_style_applied = true; + } + + pos += Vector2::right(); + + auto status_offset_start = 0U; + + // Apply the section style to the first status character if it hasn't + // already been applied + if (!is_style_applied) + { + _component_matrix->set(pos, {.value = status[0], .style = section_style}); + status_offset_start++; + pos += Vector2::right(); + + is_style_applied = true; + } + + const auto final_status_length = + status_len <= section_length ? status_len : section_length; + _matrix_write_string( pos, - status_len <= section_length - ? status - : status.substr(0U, static_cast<uint32_t>(section_length))); + status.substr(status_offset_start, static_cast<uint32_t>(final_status_length))); + + pos = pos.to_direction(Vector2::right(), final_status_length); + + for (auto index : IotaView(0U, section_style.padding_right)) + { + _component_matrix->set(pos, {.value = ' '}); + + pos += Vector2::right(); + } + + _component_matrix->set(pos, {.value = ' ', .style = {.reset_before = true}}); set_need_render(true); } @@ -70,6 +126,11 @@ void StatusLine::set_section_length(StatusLineSection section, int32_t length) n _section_lengths[section] = length; } +void StatusLine::set_section_style(StatusLineSection section, const Style &style) noexcept +{ + _section_styles[section] = style; +} + void StatusLine::_matrix_write_string( const Vector2 &position, const std::string_view &str) noexcept @@ -78,7 +139,7 @@ void StatusLine::_matrix_write_string( for (const auto &character : str) { - _component_matrix->set(working_pos, character); + _component_matrix->set(working_pos, {.value = character}); working_pos += Vector2::right(); } @@ -110,9 +171,9 @@ void StatusLine::_clear_section(StatusLineSection section, int32_t start) noexce auto section_length = _section_lengths[section]; - for (auto index = 0; index < section_length - start; index++) + for (auto index : IotaView(0, section_length - start)) { - _component_matrix->set(pos, ' '); + _component_matrix->set(pos, ComponentElement({.value = ' '})); pos += Vector2::right(); } diff --git a/src/game/components/statusline.hpp b/src/game/components/statusline.hpp index 8c1969b..e009174 100644 --- a/src/game/components/statusline.hpp +++ b/src/game/components/statusline.hpp @@ -3,6 +3,7 @@ #include "interfaces/matrix.hpp" #include "interfaces/statusline.hpp" +#include "engine/data/style.hpp" #include "engine/data/vector2.hpp" #include <memory> @@ -26,16 +27,20 @@ public: auto get_background_color() const noexcept -> uint32_t override; - void set_status( + void set_section_status( StatusLineSection section, const std::string_view &status, int32_t start) noexcept override; void set_section_length(StatusLineSection section, int32_t length) noexcept override; + void + set_section_style(StatusLineSection section, const Style &style) noexcept override; + private: std::shared_ptr<ComponentMatrix> _component_matrix; std::unordered_map<StatusLineSection, int32_t> _section_lengths; + std::unordered_map<StatusLineSection, Style> _section_styles; bool _need_render; void diff --git a/src/game/game.cpp b/src/game/game.cpp index e969fe1..fd53841 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -80,13 +80,20 @@ void Game::on_start() statusline->set_section_length(StatusLineSection::G, statusline_section_g_length); statusline->set_section_length(StatusLineSection::H, statusline_section_h_length); + statusline->set_section_style( + StatusLineSection::A, + {.bg_color = static_cast<uint32_t>(fmt::color::green), + .bold = true, + .padding_left = 1U, + .padding_right = 1U}); + _scene->register_component(statusline, Vector2({.x = 0, .y = 1})); _status_manager->bind(statusline); _minimum_cursor_pos_y = 2; - _status_manager->set_section_title(StatusLineSection::A, " "); + _status_manager->set_section_title(StatusLineSection::A, ""); _status_manager->set_section_title(StatusLineSection::B, "X: "); _status_manager->set_section_title(StatusLineSection::C, "Y: "); _status_manager->set_section_title(StatusLineSection::D, "Paused: "); diff --git a/src/game/status_manager.cpp b/src/game/status_manager.cpp index 36add4c..e4838cc 100644 --- a/src/game/status_manager.cpp +++ b/src/game/status_manager.cpp @@ -13,23 +13,23 @@ void StatusManager::set_section_title( const StatusLineSection §ion, const std::string_view &title) noexcept { - if (_title_lengths.contains(section)) + if (_section_title_lengths.contains(section)) { fmt::print(stderr, "Error: can't set statusbar section title more than once"); return; } - _statusline->set_status(section, title); + _statusline->set_section_status(section, title); - _title_lengths[section] = static_cast<int>(title.length()); + _section_title_lengths[section] = static_cast<int>(title.length()); } void StatusManager::set_section_body( const StatusLineSection §ion, const std::string_view &body) noexcept { - auto section_title_length = _title_lengths[section]; + auto section_title_length = _section_title_lengths[section]; - _statusline->set_status(section, body, section_title_length + 1); + _statusline->set_section_status(section, body, section_title_length + 1); } diff --git a/src/game/status_manager.hpp b/src/game/status_manager.hpp index 85faf3c..8224003 100644 --- a/src/game/status_manager.hpp +++ b/src/game/status_manager.hpp @@ -6,8 +6,8 @@ #include <yacppdic/auto_wirable.hpp> #include <memory> +#include <optional> #include <string_view> -#include <unordered_map> class StatusManager : public IStatusManager, public yacppdic::AutoWirable<IStatusManager, StatusManager> @@ -26,5 +26,5 @@ public: private: std::shared_ptr<IStatusLine> _statusline; - std::unordered_map<StatusLineSection, int32_t> _title_lengths; + std::unordered_map<StatusLineSection, int32_t> _section_title_lengths; }; |