mirror of
https://github.com/usatiuk/cardboy.git
synced 2025-10-28 23:27:49 +01:00
118 lines
3.9 KiB
C++
118 lines
3.9 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<unsigned nWidth, unsigned nHeight>
|
|
class GridWindow : public Window {
|
|
public:
|
|
explicit GridWindow(SubSurface* owner) : Window(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) {
|
|
_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);
|
|
}
|
|
}
|
|
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& 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, Pixel(true));
|
|
} else {
|
|
this->_owner->draw_rect(i * _cell_width, j * _cell_height, _cell_width, _cell_height, Pixel(false));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::array<std::array<std::optional<SubSurface>, 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
|