From 8cabf7a6e90035e0975e8faafceffbb80811e2b3 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Wed, 28 Aug 2024 19:59:23 +0200 Subject: [PATCH] UI message parser move message parser threads to UI set functions --- src/UI/UI_button.cpp | 2 +- src/UI/UI_checkbox.cpp | 2 +- src/UI/UI_combobox.cpp | 2 +- src/UI/UI_item.hpp | 3 ++- src/UI/UI_slider.cpp | 4 ++-- src/UI/UI_textbox.cpp | 15 +++++++++++++-- src/UI/UI_textbox.hpp | 5 ++++- tools/message_parser.cpp | 8 ++++---- 8 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/UI/UI_button.cpp b/src/UI/UI_button.cpp index d612d5a..70a8d81 100644 --- a/src/UI/UI_button.cpp +++ b/src/UI/UI_button.cpp @@ -27,6 +27,6 @@ void UI_button::click() const { if (m_on_click != nullptr) { - m_on_click(m_id); + std::thread([this] { m_on_click(m_id); }).detach(); } } diff --git a/src/UI/UI_checkbox.cpp b/src/UI/UI_checkbox.cpp index d3426fc..3556ea2 100644 --- a/src/UI/UI_checkbox.cpp +++ b/src/UI/UI_checkbox.cpp @@ -27,7 +27,7 @@ void UI_checkbox::toggle_selected(size_t selected) m_selected[selected] = !m_selected[selected]; if (m_on_change != nullptr) { - m_on_change(m_id, selected_text()); + std::thread([this] { m_on_change(m_id, selected_text()); }).detach(); } } diff --git a/src/UI/UI_combobox.cpp b/src/UI/UI_combobox.cpp index 8b33835..f51122f 100644 --- a/src/UI/UI_combobox.cpp +++ b/src/UI/UI_combobox.cpp @@ -23,7 +23,7 @@ void UI_combobox::set_selected(size_t selected) m_selected = selected; if (m_on_change != nullptr) { - m_on_change(m_id, selected_text()); + std::thread([this] { m_on_change(m_id, selected_text()); }).detach(); } } diff --git a/src/UI/UI_item.hpp b/src/UI/UI_item.hpp index 0332e78..8c3894e 100644 --- a/src/UI/UI_item.hpp +++ b/src/UI/UI_item.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include #include @@ -18,7 +19,7 @@ class UI_item { std::string type() const; bool is_type(json j); - void set_text(std::string text); + virtual void set_text(std::string text); std::string text() const; void set_text_size(uint8_t text_size); diff --git a/src/UI/UI_slider.cpp b/src/UI/UI_slider.cpp index 405fd53..5178301 100644 --- a/src/UI/UI_slider.cpp +++ b/src/UI/UI_slider.cpp @@ -19,10 +19,10 @@ void UI_slider::set_value(double value) if (value == m_value) return; + this->m_value = value; if (m_on_change != nullptr) { - this->m_value = value; - m_on_change(m_id, value); + std::thread([this] { m_on_change(m_id, m_value); }).detach(); } } diff --git a/src/UI/UI_textbox.cpp b/src/UI/UI_textbox.cpp index b6376b3..2285117 100644 --- a/src/UI/UI_textbox.cpp +++ b/src/UI/UI_textbox.cpp @@ -1,7 +1,7 @@ #include "UI_textbox.hpp" -UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color) - : UI_item(id, UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color), m_hint(hint) +UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, std::function on_change) + : UI_item(id, UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color), m_hint(hint), m_on_change(on_change) { } @@ -11,6 +11,17 @@ UI_textbox::UI_textbox(const json& j) from_json(j); } +void UI_textbox::set_text(std::string text) +{ + if (text == m_text) + return; + m_text = text; + if (m_on_change != nullptr) + { + std::thread([this] { m_on_change(m_id, m_text); }).detach(); + } +} + void UI_textbox::set_hint(std::string hint) { m_hint = hint; diff --git a/src/UI/UI_textbox.hpp b/src/UI/UI_textbox.hpp index 180dda4..e813d48 100644 --- a/src/UI/UI_textbox.hpp +++ b/src/UI/UI_textbox.hpp @@ -7,9 +7,11 @@ class UI_textbox : public UI_item { public: - UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color); + UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, std::function on_change); UI_textbox(const json& j); + void set_text(std::string text) override; + void set_hint(std::string hint); std::string hint() const; @@ -17,4 +19,5 @@ public: json to_json() const override; private: std::string m_hint; + std::function m_on_change; }; \ No newline at end of file diff --git a/tools/message_parser.cpp b/tools/message_parser.cpp index 87f30eb..8cfbc38 100644 --- a/tools/message_parser.cpp +++ b/tools/message_parser.cpp @@ -85,25 +85,25 @@ void event_handler(json event) case Event::clicked: { spdlog::debug("Event {}: {}", type, std::string(event.at("id"))); - std::thread([id = std::string(event.at("id"))] { g_simulators->invoke_active_simulator()->get_UI_item(id)->click(); }).detach(); + g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click(); break; } case Event::value_changed: { spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"])); - std::thread([id = std::string(event.at("id")), value = (double)(event["value"])] { g_simulators->invoke_active_simulator()->get_UI_item(id)->set_value(value); }).detach(); + g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_value((double)event.at("value")); break; } case Event::text_changed: { spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text"))); - std::thread([id = std::string(event.at("id")), text = std::string(event.at("text"))] { g_simulators->invoke_active_simulator()->get_UI_item(id)->set_text(text); }).detach(); + g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_text(std::string(event.at("text"))); break; } case Event::selected: { spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("selected"))); - std::thread([id = std::string(event.at("id")), selected = std::string(event.at("selected"))]{ g_simulators->invoke_active_simulator()->get_UI_item(id)->set_selected(selected); }).detach(); + g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_selected(std::string(event.at("selected"))); break; } default: