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/components/statusline.cpp | |
parent | ec5c7cbccc533bacab661e991e7011584d3bb47f (diff) |
feat: add statusline mode styling
Diffstat (limited to 'src/game/components/statusline.cpp')
-rw-r--r-- | src/game/components/statusline.cpp | 77 |
1 files changed, 69 insertions, 8 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(); } |