aboutsummaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/data/style.hpp20
-rw-r--r--src/engine/graphics/component_renderer.cpp50
-rw-r--r--src/engine/graphics/component_renderer.hpp5
3 files changed, 65 insertions, 10 deletions
diff --git a/src/engine/data/style.hpp b/src/engine/data/style.hpp
new file mode 100644
index 0000000..50c1faa
--- /dev/null
+++ b/src/engine/data/style.hpp
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <cstdint>
+#include <optional>
+
+class Style
+{
+public:
+ // Colors
+ std::optional<uint32_t> fg_color{};
+ std::optional<uint32_t> bg_color{};
+
+ // Toggles
+ bool bold = false;
+ bool reset_before = false;
+
+ // Spacing
+ uint32_t padding_left = 0U;
+ uint32_t padding_right = 0U;
+};
diff --git a/src/engine/graphics/component_renderer.cpp b/src/engine/graphics/component_renderer.cpp
index 1ab4c08..eb024bc 100644
--- a/src/engine/graphics/component_renderer.cpp
+++ b/src/engine/graphics/component_renderer.cpp
@@ -23,21 +23,39 @@ void ComponentRenderer::render(
_cursor_controller->hide();
_cursor_controller->move_to(position);
- const auto component_matrix = component->get();
-
- const auto foreground_color = component->get_foreground_color();
- const auto background_color = component->get_background_color();
+ _use_component_colors(component);
- fmt::print(
- "{}{}",
- get_background_esc_seq(background_color),
- get_foreground_esc_seq(foreground_color));
+ const auto component_matrix = component->get();
for (const auto &row : *component_matrix)
{
- for (const auto &col : row)
+ for (const auto &element : row)
{
- std::cout.put(col);
+ if (element.style.reset_before)
+ {
+ fmt::print(RESET_ALL_MODES, fmt::arg("esc", ESC));
+ _use_component_colors(component);
+ }
+
+ const auto opt_element_fg_color = element.style.fg_color;
+ const auto opt_element_bg_color = element.style.bg_color;
+
+ if (opt_element_fg_color.has_value())
+ {
+ fmt::print("{}", get_foreground_esc_seq(opt_element_fg_color.value()));
+ }
+
+ if (opt_element_bg_color.has_value())
+ {
+ fmt::print("{}", get_background_esc_seq(opt_element_bg_color.value()));
+ }
+
+ if (element.style.bold)
+ {
+ fmt::print(SET_BOLD_MODE, fmt::arg("esc", ESC));
+ }
+
+ std::cout.put(element.value);
}
const auto current_pos = _cursor_controller->where();
@@ -53,3 +71,15 @@ void ComponentRenderer::render(
_cursor_controller->move_to(previous_pos);
_cursor_controller->show();
}
+
+void ComponentRenderer::_use_component_colors(
+ const std::shared_ptr<IComponent> &component) noexcept
+{
+ const auto foreground_color = component->get_foreground_color();
+ const auto background_color = component->get_background_color();
+
+ fmt::print(
+ "{}{}",
+ get_background_esc_seq(background_color),
+ get_foreground_esc_seq(foreground_color));
+}
diff --git a/src/engine/graphics/component_renderer.hpp b/src/engine/graphics/component_renderer.hpp
index 4f53e07..ff1bc47 100644
--- a/src/engine/graphics/component_renderer.hpp
+++ b/src/engine/graphics/component_renderer.hpp
@@ -8,6 +8,8 @@
#include <memory>
+constexpr auto SET_BOLD_MODE = "{esc}[1m";
+
class ComponentRenderer : public IComponentRenderer
{
public:
@@ -20,4 +22,7 @@ public:
private:
std::shared_ptr<ICursorController> _cursor_controller;
+
+ static void
+ _use_component_colors(const std::shared_ptr<IComponent> &component) noexcept;
};