aboutsummaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-05-02 22:57:51 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:59 +0200
commit741a6008e806abb8e5aaa8f8fb91827fa97ece9d (patch)
tree05edce915b6cd56abefeedfa723d83cc1b936566 /src/engine
parent73290d21d996c7e596ef3c71d55a506f1aa38f2c (diff)
refactor: move statusline to engine graphics folder
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/graphics/statusline.cpp121
-rw-r--r--src/engine/graphics/statusline.hpp50
2 files changed, 171 insertions, 0 deletions
diff --git a/src/engine/graphics/statusline.cpp b/src/engine/graphics/statusline.cpp
new file mode 100644
index 0000000..1abb87b
--- /dev/null
+++ b/src/engine/graphics/statusline.cpp
@@ -0,0 +1,121 @@
+#include "statusline.hpp"
+
+#include "engine/escape.hpp"
+#include "util/color.hpp"
+
+#include <string>
+#include <utility>
+
+StatusLine::StatusLine(
+ std::shared_ptr<ICursorController> cursor_controller,
+ std::shared_ptr<IWindow> window
+) noexcept
+ : _cursor_controller(std::move(cursor_controller)), _window(std::move(window))
+{
+}
+
+void StatusLine::initialize_background() noexcept
+{
+ const auto previous_position = _move_to_statusline(0);
+
+ auto background_color = get_background_esc_seq(STATUSBAR_COLOR);
+
+ fmt::print("{}{}", background_color, std::string(_window->size().get_width(), ' '));
+ fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC));
+
+ _move_back(previous_position);
+}
+
+void StatusLine::set_status(
+ const StatusLineSection &section,
+ const std::string_view &status,
+ std::size_t start
+) noexcept
+{
+ _clear_section(section, start);
+
+ auto section_start = _get_section_start_x(section);
+
+ const auto previous_position =
+ _move_to_statusline(section_start + static_cast<int32_t>(start));
+
+ auto background_color = get_background_esc_seq(STATUSBAR_COLOR);
+
+ auto section_length = _sections_lengths[section];
+
+ fmt::print("{}{}", background_color, status);
+ fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC));
+
+ _move_back(previous_position);
+}
+
+void StatusLine::set_section_length(
+ const StatusLineSection &section,
+ uint32_t length
+) noexcept
+{
+ _sections_lengths[section] = length;
+}
+
+auto StatusLine::_move_to_statusline(int32_t x) noexcept -> Vector2
+{
+ const auto previous_position = _cursor_controller->where();
+
+ const auto window_size = _window->size();
+
+ _cursor_controller->hide();
+
+ auto window_height = static_cast<Vector2::Value>(window_size.get_height());
+
+ _cursor_controller->move_to(Vector2({ .x = x, .y = window_height }), true);
+
+ return previous_position;
+}
+
+void StatusLine::_move_back(Vector2 previous_position) noexcept
+{
+ _cursor_controller->move_to(previous_position, true);
+ _cursor_controller->show();
+}
+
+auto StatusLine::_get_section_start_x(const StatusLineSection &section) const noexcept
+ -> int32_t
+{
+ int32_t section_start = 0;
+
+ auto section_index = static_cast<int32_t>(section);
+
+ while (section_index > 0)
+ {
+ section_start +=
+ static_cast<int32_t>(_sections_lengths.at(StatusLineSection(section_index - 1)
+ ));
+
+ section_index--;
+ }
+
+ return section_start;
+}
+
+void StatusLine::_clear_section(
+ const StatusLineSection &section,
+ std::size_t start
+) noexcept
+{
+ auto section_start = _get_section_start_x(section);
+
+ auto start_int32 = static_cast<int32_t>(start);
+
+ const auto previous_position = _move_to_statusline(section_start + start_int32);
+
+ auto background_color = get_background_esc_seq(STATUSBAR_COLOR);
+
+ auto section_length = _sections_lengths[section];
+
+ auto start_uint32 = static_cast<uint32_t>(start);
+
+ fmt::print("{}{}", background_color, std::string(section_length - start_uint32, ' '));
+ fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC));
+
+ _move_back(previous_position);
+}
diff --git a/src/engine/graphics/statusline.hpp b/src/engine/graphics/statusline.hpp
new file mode 100644
index 0000000..181090d
--- /dev/null
+++ b/src/engine/graphics/statusline.hpp
@@ -0,0 +1,50 @@
+#pragma once
+
+#include "interfaces/cursor.hpp"
+#include "interfaces/statusline.hpp"
+#include "interfaces/window.hpp"
+
+#include "engine/data/vector2.hpp"
+
+#include <fmt/core.h>
+
+#include <memory>
+#include <string_view>
+#include <unordered_map>
+
+constexpr uint32_t STATUSBAR_COLOR = 0x1A1A1AU;
+
+class StatusLine : public IStatusLine
+{
+public:
+ StatusLine(
+ std::shared_ptr<ICursorController> cursor_controller,
+ std::shared_ptr<IWindow> window
+ ) noexcept;
+
+ void initialize_background() noexcept override;
+
+ void set_status(
+ const StatusLineSection &section,
+ const std::string_view &status,
+ std::size_t start
+ ) noexcept override;
+
+ void set_section_length(const StatusLineSection &section, uint32_t length) noexcept
+ override;
+
+private:
+ std::unordered_map<StatusLineSection, uint32_t> _sections_lengths;
+
+ std::shared_ptr<ICursorController> _cursor_controller;
+ std::shared_ptr<IWindow> _window;
+
+ auto _move_to_statusline(int32_t x) noexcept -> Vector2;
+
+ void _move_back(Vector2 previous_position) noexcept;
+
+ [[nodiscard]] auto _get_section_start_x(const StatusLineSection &section
+ ) const noexcept -> int32_t;
+
+ void _clear_section(const StatusLineSection &section, std::size_t start) noexcept;
+};