mirror of
https://github.com/usatiuk/cardboy.git
synced 2025-10-28 23:27:49 +01:00
121 lines
4.1 KiB
C++
121 lines
4.1 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, typename PixelType, unsigned nWidth, unsigned nHeight>
|
|
class GridWindow : public Window<SurfaceType, PixelType> {
|
|
public:
|
|
explicit GridWindow(SurfaceType* owner) : Window<SurfaceType, PixelType>(owner) {
|
|
for (int i = 0; i < nWidth; ++i) {
|
|
for (int j = 0; j < nHeight; ++j) {
|
|
_grid[i][j].emplace(owner);
|
|
}
|
|
}
|
|
}
|
|
|
|
EventHandlingResult handle(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(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) {
|
|
_grid[i][j]->set_pos(i * _cell_width + 1, j * _cell_height + 1, _cell_width - 1, _cell_height - 1);
|
|
}
|
|
}
|
|
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, PixelType>& get_subsurface(unsigned x, unsigned y) {
|
|
// assert(x >= nWidth && y >= nHeight);
|
|
return *_grid[x][y];
|
|
}
|
|
|
|
void refresh() override {
|
|
for (int i = 0; i < nWidth; ++i) {
|
|
for (int j = 0; j < nHeight; ++j) {
|
|
if (_grid[i][j]->has_window()) {
|
|
_grid[i][j]->get_window()->refresh();
|
|
}
|
|
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:
|
|
std::array<std::array<std::optional<SubSurface<SurfaceType, PixelType>>, 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
|