From a570e494bca43c30d7ec91d293051c0c818509b5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 23 Jun 2022 17:05:43 +0200 Subject: feat: add statusline mode styling --- src/game/components/statusline.cpp | 77 ++++++++++++++++++++++++++++++++++---- src/game/components/statusline.hpp | 7 +++- 2 files changed, 75 insertions(+), 9 deletions(-) (limited to 'src/game/components') 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 +#include #include StatusLine::StatusLine(std::shared_ptr 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 & @@ -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(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(section_length))); + status.substr(status_offset_start, static_cast(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 @@ -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 _component_matrix; std::unordered_map _section_lengths; + std::unordered_map _section_styles; bool _need_render; void -- cgit v1.2.3-18-g5258