Files
cardboy/Firmware/sdk/library/include_public/GridWindow.hpp

129 lines
4.5 KiB
C++

//
// Created by Stepan Usatiuk on 26.07.2025.
//
#ifndef GRIDWINDOW_HPP
#define GRIDWINDOW_HPP
#include <string>
#include "Fonts.hpp"
#include "SubSurface.hpp"
#include "Window.hpp"
#include "utils.hpp"
template<typename SurfaceType, unsigned nWidth, unsigned nHeight>
class GridWindow : public Window<SurfaceType> {
public:
using PixelType = typename SurfaceType::PixelType;
explicit GridWindow(SurfaceType* owner) : Window<SurfaceType>(owner) {
for (int i = 0; i < nWidth; ++i) {
for (int j = 0; j < nHeight; ++j) {
_grid[i][j].emplace(owner);
}
}
}
EventHandlingResult handle_v(KeyboardEvent keyboardEvent) override {
if (keyboardEvent.key_code == Key::Escape) {
if (!_has_focus) {
return EventHandlingResult::CONTINUE;
} else {
auto res = _grid[_current_focus_x][_current_focus_y]->get_window()->handle(keyboardEvent);
if (res == EventHandlingResult::DONE) {
return EventHandlingResult::DONE;
} else {
_has_focus = false;
}
}
} else if (keyboardEvent.key_code == Key::Enter) {
if (!_has_focus) {
_has_focus = true;
} else {
return _grid[_current_focus_x][_current_focus_y]->get_window()->handle(keyboardEvent);
}
} else {
if (_has_focus) {
return _grid[_current_focus_x][_current_focus_y]->get_window()->handle(keyboardEvent);
}
if (keyboardEvent.key_code == Key::Left) {
if (_current_focus_x > 0) {
_current_focus_x--;
}
} else if (keyboardEvent.key_code == Key::Right) {
if (_current_focus_x < nWidth - 1) {
_current_focus_x++;
}
} else if (keyboardEvent.key_code == Key::Up) {
if (_current_focus_y > 0) {
_current_focus_y--;
}
} else if (keyboardEvent.key_code == Key::Down) {
if (_current_focus_y < nHeight - 1) {
_current_focus_y++;
}
}
}
refresh();
return EventHandlingResult::DONE;
}
EventHandlingResult handle_v(SurfaceResizeEvent resize) override {
_cell_width = this->_owner->get_width() / nWidth;
_cell_height = this->_owner->get_height() / nHeight;
for (int i = 0; i < nWidth; ++i) {
for (int j = 0; j < nHeight; ++j) {
if constexpr (is_specialization_of<SubSurface, SurfaceType>::value) {
_grid[i][j]->set_pos(this->_owner->get_x_offset() + i * _cell_width + 1,
this->_owner->get_y_offset() + j * _cell_height + 1, _cell_width - 2,
_cell_height - 2);
} else {
_grid[i][j]->set_pos(i * _cell_width + 1, j * _cell_height + 1, _cell_width - 2, _cell_height - 2);
}
}
}
refresh();
return EventHandlingResult::DONE;
}
template<typename WindowType, typename... Args>
void set_window(unsigned x, unsigned y, Args&&... args) {
_grid[x][y]->template set_window<WindowType>(std::forward<Args>(args)...);
}
SubSurface<SurfaceType>& get_subsurface(unsigned x, unsigned y) {
// assert(x >= nWidth && y >= nHeight);
return *_grid[x][y];
}
void refresh() {
for (int i = 0; i < nWidth; ++i) {
for (int j = 0; j < nHeight; ++j) {
if (i == _current_focus_x && j == _current_focus_y) {
this->_owner->draw_rect(i * _cell_width, j * _cell_height, _cell_width, _cell_height,
PixelType(true));
} else {
this->_owner->draw_rect(i * _cell_width, j * _cell_height, _cell_width, _cell_height,
PixelType(false));
}
}
}
}
private:
using SubType = std::conditional_t<is_specialization_of<SubSurface, SurfaceType>::value, SurfaceType,
SubSurface<SurfaceType>>;
std::array<std::array<std::optional<SubType>, nWidth>, nHeight> _grid;
unsigned _cell_width = 0;
unsigned _cell_height = 0;
unsigned _current_focus_x = 0;
unsigned _current_focus_y = 0;
bool _has_focus = false;
};
#endif // TEXTWINDOW_HPP