diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-07 19:45:17 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:57:01 +0200 |
commit | 8805b1fe27344e8086cebabf869b7a02d2376f05 (patch) | |
tree | a90a2e2dda1bcb98fb4de5cd983138e5441c2222 /src/engine/components | |
parent | f778317bae709f397345a2d5e04e23864c6391b3 (diff) |
refactor: decouple statusline from scene & cursor controller
Might be slightly slower than previously though...
Diffstat (limited to 'src/engine/components')
-rw-r--r-- | src/engine/components/statusline.cpp | 99 | ||||
-rw-r--r-- | src/engine/components/statusline.hpp | 45 |
2 files changed, 144 insertions, 0 deletions
diff --git a/src/engine/components/statusline.cpp b/src/engine/components/statusline.cpp new file mode 100644 index 0000000..ae0f3fa --- /dev/null +++ b/src/engine/components/statusline.cpp @@ -0,0 +1,99 @@ +#include "statusline.hpp" + +#include "engine/data/vector2.hpp" +#include "util/color.hpp" + +#include <fmt/color.h> + +#include <utility> + +StatusLine::StatusLine(std::shared_ptr<ComponentMatrix> component_matrix) noexcept + : _component_matrix(std::move(component_matrix)), _need_render(false) +{ + _component_matrix->fill(' '); +} + +auto StatusLine::get() const noexcept -> const std::shared_ptr<ComponentMatrix> & +{ + return _component_matrix; +} + +auto StatusLine::get_need_render() const noexcept -> bool +{ + return _need_render; +} + +void StatusLine::set_need_render(bool need_render) noexcept +{ + _need_render = need_render; +} + +auto StatusLine::get_foreground_color() const noexcept -> uint32_t +{ + return static_cast<uint32_t>(fmt::color::white); +} + +auto StatusLine::get_background_color() const noexcept -> uint32_t +{ + return STATUSLINE_COLOR; +} + +void StatusLine::set_status( + StatusLineSection section, + const std::string_view &status, + int32_t start) noexcept +{ + _clear_section(section, start); + + auto section_start = _get_section_start_x(section); + + auto pos = Vector2({.x = section_start + start, .y = 0}); + + for (const auto &character : status) + { + _component_matrix->set(pos, character); + + pos += Vector2::right(); + } + + set_need_render(true); +} + +void StatusLine::set_section_length(StatusLineSection section, int32_t length) noexcept +{ + _section_lengths[section] = length; +} + +auto StatusLine::_get_section_start_x(StatusLineSection section) const noexcept -> int32_t +{ + int32_t section_start = 0; + + auto section_index = static_cast<int32_t>(section); + + while (section_index > 0) + { + const auto prev_section = static_cast<StatusLineSection>(section_index - 1); + + section_start += static_cast<int32_t>(_section_lengths.at(prev_section)); + + section_index--; + } + + return section_start; +} + +void StatusLine::_clear_section(StatusLineSection section, int32_t start) noexcept +{ + auto section_start = _get_section_start_x(section); + + auto pos = Vector2({.x = section_start + start, .y = 0}); + + auto section_length = _section_lengths[section]; + + for (auto index = 0; index < section_length - start; index++) + { + _component_matrix->set(pos, ' '); + + pos += Vector2::right(); + } +} diff --git a/src/engine/components/statusline.hpp b/src/engine/components/statusline.hpp new file mode 100644 index 0000000..3fdb8d3 --- /dev/null +++ b/src/engine/components/statusline.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include "interfaces/matrix.hpp" +#include "interfaces/statusline.hpp" + +#include "engine/data/vector2.hpp" + +#include <memory> +#include <string_view> +#include <unordered_map> + +constexpr uint32_t STATUSLINE_COLOR = 0x1A1A1AU; + +class StatusLine : public IStatusLine +{ +public: + explicit StatusLine(std::shared_ptr<ComponentMatrix> component_matrix) noexcept; + + auto get() const noexcept -> const std::shared_ptr<ComponentMatrix> & override; + + [[nodiscard]] auto get_need_render() const noexcept -> bool override; + + void set_need_render(bool need_render) noexcept override; + + auto get_foreground_color() const noexcept -> uint32_t override; + + auto get_background_color() const noexcept -> uint32_t override; + + void set_status( + StatusLineSection section, + const std::string_view &status, + int32_t start) noexcept override; + + void set_section_length(StatusLineSection section, int32_t length) noexcept override; + +private: + std::shared_ptr<ComponentMatrix> _component_matrix; + std::unordered_map<StatusLineSection, int32_t> _section_lengths; + bool _need_render; + + [[nodiscard]] auto _get_section_start_x(StatusLineSection section) const noexcept + -> int32_t; + + void _clear_section(StatusLineSection section, int32_t start) noexcept; +}; |