UI message parser

move message parser threads to UI set functions
This commit is contained in:
2024-08-28 19:59:23 +02:00
parent 70b953dc16
commit 8cabf7a6e9
8 changed files with 28 additions and 13 deletions
+1 -1
View File
@@ -27,6 +27,6 @@ void UI_button::click() const
{ {
if (m_on_click != nullptr) if (m_on_click != nullptr)
{ {
m_on_click(m_id); std::thread([this] { m_on_click(m_id); }).detach();
} }
} }
+1 -1
View File
@@ -27,7 +27,7 @@ void UI_checkbox::toggle_selected(size_t selected)
m_selected[selected] = !m_selected[selected]; m_selected[selected] = !m_selected[selected];
if (m_on_change != nullptr) if (m_on_change != nullptr)
{ {
m_on_change(m_id, selected_text()); std::thread([this] { m_on_change(m_id, selected_text()); }).detach();
} }
} }
+1 -1
View File
@@ -23,7 +23,7 @@ void UI_combobox::set_selected(size_t selected)
m_selected = selected; m_selected = selected;
if (m_on_change != nullptr) if (m_on_change != nullptr)
{ {
m_on_change(m_id, selected_text()); std::thread([this] { m_on_change(m_id, selected_text()); }).detach();
} }
} }
+2 -1
View File
@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include <thread>
#include <cstdint> #include <cstdint>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <string> #include <string>
@@ -18,7 +19,7 @@ class UI_item {
std::string type() const; std::string type() const;
bool is_type(json j); bool is_type(json j);
void set_text(std::string text); virtual void set_text(std::string text);
std::string text() const; std::string text() const;
void set_text_size(uint8_t text_size); void set_text_size(uint8_t text_size);
+2 -2
View File
@@ -19,10 +19,10 @@ void UI_slider::set_value(double value)
if (value == m_value) if (value == m_value)
return; return;
this->m_value = value;
if (m_on_change != nullptr) if (m_on_change != nullptr)
{ {
this->m_value = value; std::thread([this] { m_on_change(m_id, m_value); }).detach();
m_on_change(m_id, value);
} }
} }
+13 -2
View File
@@ -1,7 +1,7 @@
#include "UI_textbox.hpp" #include "UI_textbox.hpp"
UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color) UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, std::function<void(std::string, std::string)> on_change)
: UI_item(id, UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color), m_hint(hint) : 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); 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) void UI_textbox::set_hint(std::string hint)
{ {
m_hint = hint; m_hint = hint;
+4 -1
View File
@@ -7,9 +7,11 @@
class UI_textbox : public UI_item class UI_textbox : public UI_item
{ {
public: 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<void(std::string, std::string)> on_change);
UI_textbox(const json& j); UI_textbox(const json& j);
void set_text(std::string text) override;
void set_hint(std::string hint); void set_hint(std::string hint);
std::string hint() const; std::string hint() const;
@@ -17,4 +19,5 @@ public:
json to_json() const override; json to_json() const override;
private: private:
std::string m_hint; std::string m_hint;
std::function<void(std::string, std::string)> m_on_change;
}; };
+4 -4
View File
@@ -85,25 +85,25 @@ void event_handler(json event)
case Event::clicked: case Event::clicked:
{ {
spdlog::debug("Event {}: {}", type, std::string(event.at("id"))); 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; break;
} }
case Event::value_changed: case Event::value_changed:
{ {
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"])); 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; break;
} }
case Event::text_changed: case Event::text_changed:
{ {
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text"))); 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; break;
} }
case Event::selected: case Event::selected:
{ {
spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("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; break;
} }
default: default: