aboutsummaryrefslogtreecommitdiff
path: root/src/game/components/statusline.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-08 16:29:16 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:57:01 +0200
commit45e3ae285c63d91dcac426cffdd9d9648e48399b (patch)
treea3d483027356fec5a953545e691ea07ef417bf63 /src/game/components/statusline.cpp
parentdcc6d3d5cafe47d53d1b321476bf73bb2d65ae9b (diff)
refactor: move components to game folder
Diffstat (limited to 'src/game/components/statusline.cpp')
-rw-r--r--src/game/components/statusline.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/game/components/statusline.cpp b/src/game/components/statusline.cpp
new file mode 100644
index 0000000..5b59a53
--- /dev/null
+++ b/src/game/components/statusline.cpp
@@ -0,0 +1,119 @@
+#include "statusline.hpp"
+
+#include "engine/data/bounds.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});
+
+ const auto column_cnt = static_cast<int32_t>(_component_matrix->get_column_cnt());
+
+ const auto section_length = _section_lengths[section];
+
+ const auto status_len = static_cast<int32_t>(status.length());
+
+ _matrix_write_string(
+ pos,
+ status_len <= section_length
+ ? status
+ : status.substr(0U, static_cast<uint32_t>(section_length)));
+
+ set_need_render(true);
+}
+
+void StatusLine::set_section_length(StatusLineSection section, int32_t length) noexcept
+{
+ _section_lengths[section] = length;
+}
+
+void StatusLine::_matrix_write_string(
+ const Vector2 &position,
+ const std::string_view &str) noexcept
+{
+ auto working_pos = position;
+
+ for (const auto &character : str)
+ {
+ _component_matrix->set(working_pos, character);
+
+ working_pos += Vector2::right();
+ }
+}
+
+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();
+ }
+}