PocketPutr/Desktop_Test/window_manager.cpp
2025-12-14 13:00:01 +01:00

99 lines
2.5 KiB
C++

#include "HardwareSerial.h"
#include "GLOBALS.h"
#include "window_manager.h"
#include <algorithm>
void WindowManager::init(TFT_Handler* th) {
tft = th;
}
void WindowManager::create_window(Window window) {
windows.push_back(window);
draw_window(window);
}
void WindowManager::close_window(int window_id) {
for (auto it = windows.begin(); it != windows.end(); ++it) {
if (it->id == window_id) {
windows.erase(it);
break;
}
}
draw_all();
}
void WindowManager::draw_all() {
for (const auto& win : windows) {
draw_window(win);
}
}
void WindowManager::draw_window(Window window) {
// Outer shadow
//tft->draw_box(window.x, window.y, window.width + 2, window.height + 2, 0x0000);
tft->draw_rect(window.x, window.y, window.width + 2, window.height + 2, 1, 0x0000);
// Inner shadow
tft->draw_rect(window.x, window.y, window.width + 1, window.height + 1, 1, 0x8410);
// Highlight
tft->draw_rect(window.x - 1, window.y - 1, window.width + 1, window.height + 1, 1, 0xffff);
// Window
tft->draw_box(window.x, window.y, window.width, window.height, window.background_color);
// Decorations
tft->draw_box(window.x + 3, window.y + 3, window.width - 6, 30, window.foreground_color);
// Close button
tft->draw_box(window.x + 6, window.y + 5, 25, 25, COL_RED);
// Minimize button
tft->draw_box(window.x + 6 + 30, window.y + 5, 25, 25, COL_LIGHT_GREY);
// Window title
tft->draw_text(window.x + 6 + 65, window.y + 10, window.title.c_str());
// Window content
tft->draw_box(window.x + 3, window.y + 30 + 5, window.width - 6, window.height - 7 - 30, COL_WHITE);
}
int WindowManager::click_event(CLICK_EVENT event) {
int id = -1;
if (event.event == CLICK_EVENTS::NONE) {
return -1;
}
// Iterate BACKWARDS - last window is on top
for (int i = windows.size() - 1; i >= 0; i--) {
Window& window = windows[i];
if (event.event != CLICK_EVENTS::LEFT_CLICK)
continue;
if (event.x >= window.x && event.x <= (window.x + window.width)) {
if (event.y >= window.y && event.y <= (window.y + window.height)) {
if (window.focused)
break;
// Unfocus all windows
for (size_t j = 0; j < windows.size(); j++) {
windows[j].focused = false;
}
// Focus this window
window.focused = true;
id = window.id;
// Move window to end (top of z-order)
auto it = windows.begin() + i;
std::rotate(it, it + 1, windows.end());
}
}
}
return id;
}