From 1cb93af0bcf997306a3f05513df34b854029e10c Mon Sep 17 00:00:00 2001 From: rasmus Date: Mon, 12 Jan 2026 20:57:37 +0100 Subject: [PATCH] You can now have in-window buttons and they work. --- Desktop_Test/example_programs.h | 48 ++++++++++++++++++++++++++++++--- Desktop_Test/program.cpp | 4 +++ Desktop_Test/program.h | 2 +- Desktop_Test/tft_handler.cpp | 8 +++--- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/Desktop_Test/example_programs.h b/Desktop_Test/example_programs.h index d54f015..975b521 100644 --- a/Desktop_Test/example_programs.h +++ b/Desktop_Test/example_programs.h @@ -98,11 +98,42 @@ private: bool showing_buttons = false; public: - void on_click_event(CLICK_EVENT event) override { + void on_click_event(CLICK_EVENT event) { if (_window->minimized) return; + + for (short i = 0; i < _window->content_buttons.size(); ++i) { + auto c_button = _window->content_buttons[i]; + if (event.x >= c_button.x && event.x <= (c_button.x + c_button.width) && event.y >= c_button.y && event.y <= (c_button.y + c_button.height)) { + if (c_button.action == "show resources") { + for (short j = 0; j < _window->content_buttons.size(); ++j) { + _window->content_buttons[j].pressed = false; + } + + Serial.println("I'm tired, boss..."); + + _window->content_buttons[i].pressed = true; + _display_state->update_display.store(true); + break; + } + + if (c_button.action == "show tasks") { + for (short j = 0; j < _window->content_buttons.size(); ++j) { + _window->content_buttons[j].pressed = false; + } + + Serial.println("ok..."); + + _window->content_buttons[i].pressed = true; + _display_state->update_display.store(true); + break; + } + } + } } void run() override { + EventManager::getInstance().subscribe(this); + _window->x += 150; _window->width -= 50; @@ -154,11 +185,20 @@ public: show_resources.action = "show resources"; show_resources.x = _window->x + 7; show_resources.y = _window->y + 40; - show_resources.width = 70; + show_resources.width = 80; show_resources.height = 10; show_resources.pressed = true; + ContentButton show_tasks; + show_tasks.action = "show tasks"; + show_tasks.x = _window->x + show_resources.width + 13; + show_tasks.y = _window->y + 40; + show_tasks.width = 70; + show_tasks.height = 10; + show_tasks.pressed = false; + _window->content_buttons.push_back(show_resources); + _window->content_buttons.push_back(show_tasks); // Variables for metrics size_t total_ram; @@ -223,7 +263,7 @@ public: vTaskDelay(pdMS_TO_TICKS(1)); if (counter == 1000) { - Serial.printf("internal_total %i\n", internal_total); + /*Serial.printf("internal_total %i\n", internal_total); Serial.printf("internal_free %i\n", internal_free); Serial.printf("psram_total %i\n", psram_total); Serial.printf("psram_free %i\n", psram_free); @@ -232,7 +272,7 @@ public: Serial.printf("used_ram %i\n", used_ram); Serial.printf("ram_percent %f.2\n", ram_percent); Serial.printf("cpu_percent %f.2\n", cpu_percent); - Serial.println("==============="); + Serial.println("===============");*/ _display_state->update_display.store(true); counter = 0; } diff --git a/Desktop_Test/program.cpp b/Desktop_Test/program.cpp index 83641c9..a1c341e 100644 --- a/Desktop_Test/program.cpp +++ b/Desktop_Test/program.cpp @@ -37,6 +37,10 @@ void Program::task_wrapper(void* pvParameters) { vTaskDelete(NULL); } +void Program::on_click_event(CLICK_EVENT event) { + // Default implementation, do nothing +} + void Program::close() { _running = false; if (_task_handle) { diff --git a/Desktop_Test/program.h b/Desktop_Test/program.h index c0bc691..883e15a 100644 --- a/Desktop_Test/program.h +++ b/Desktop_Test/program.h @@ -107,7 +107,7 @@ public: virtual void run() = 0; // listen to click events - void on_click_event(CLICK_EVENT event) override {} + void on_click_event(CLICK_EVENT event) override; // Called when the program should stop virtual void close(); diff --git a/Desktop_Test/tft_handler.cpp b/Desktop_Test/tft_handler.cpp index b2458b4..35dd55a 100644 --- a/Desktop_Test/tft_handler.cpp +++ b/Desktop_Test/tft_handler.cpp @@ -37,15 +37,17 @@ void TFT_Handler::draw_button(short x, short y, short size_x, short size_y, int if (pressed) { sprite.fillRect(x - 2, y - 2, size_x + 2, size_y + 2, COL_BLACK); sprite.fillRect(x - 1, y - 1, size_x + 1, size_y + 1, COL_DARK_GREY); - sprite.fillRect(x + 2, y + 2, size_x + 2, size_y + 2, COL_WHITE); sprite.fillRect(x, y, size_x, size_y, color); sprite.setTextColor(COL_BLACK); sprite.setTextSize(1); sprite.drawString(str.c_str(), x, y); } else { + sprite.fillRect(x, y, size_x + 2, size_y + 2, COL_BLACK); + sprite.fillRect(x, y, size_x + 1, size_y + 1, COL_DARK_GREY); sprite.fillRect(x, y, size_x, size_y, color); - sprite.fillRect(x, y, size_x, size_y, color); - sprite.fillRect(x, y, size_x, size_y, color); + sprite.setTextColor(COL_BLACK); + sprite.setTextSize(1); + sprite.drawString(str.c_str(), x, y); } }