This commit is contained in:
2024-08-29 15:00:27 +02:00
parent 6c6a60e980
commit 21a2c16607
31 changed files with 457 additions and 409 deletions
+4 -5
View File
@@ -1,19 +1,18 @@
#include "UI_button.hpp"
UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color, std::function<void(std::string)> on_click)
UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color,
std::function<void(std::string)> on_click)
: UI_item(id, UI_BUTTON_TYPE, text, 12, fg_color, bg_color)
{
this->m_on_click = on_click;
}
UI_button::UI_button(const json& j)
: UI_item(UI_BUTTON_TYPE),
m_on_click(nullptr)
UI_button::UI_button(const json &j) : UI_item(UI_BUTTON_TYPE), m_on_click(nullptr)
{
from_json(j);
}
void UI_button::from_json(const json& j)
void UI_button::from_json(const json &j)
{
UI_item::from_json(j);
}
+11 -9
View File
@@ -6,14 +6,16 @@
class UI_button : public UI_item
{
public:
UI_button(std::string id, std::string text, Color fg_color, Color bg_color, std::function<void(std::string)> on_click = nullptr);
UI_button(const json& j);
void from_json(const json& j) override;
json to_json() const override;
public:
UI_button(std::string id, std::string text, Color fg_color, Color bg_color,
std::function<void(std::string)> on_click = nullptr);
UI_button(const json &j);
void click() const override;
private:
std::function<void(std::string)> m_on_click = nullptr;
void from_json(const json &j) override;
json to_json() const override;
void click() const override;
private:
std::function<void(std::string)> m_on_click = nullptr;
};
+7 -7
View File
@@ -1,11 +1,12 @@
#include "UI_checkbox.hpp"
UI_checkbox::UI_checkbox()
: UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr)
UI_checkbox::UI_checkbox() : UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr)
{
}
UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, std::function<void(std::string, std::vector<std::string>)> on_change)
UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options,
std::function<void(std::string, std::vector<std::string>)> on_change)
: UI_x_boxes(id, UI_CHECKBOX_TYPE, text, fg_color, bg_color, options), m_selected(), m_on_change(on_change)
{
for (size_t i = 0; i < m_options.size(); i++)
@@ -14,8 +15,7 @@ UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color
}
}
UI_checkbox::UI_checkbox(const json& j) :
UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr)
UI_checkbox::UI_checkbox(const json &j) : UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr)
{
from_json(j);
}
@@ -61,10 +61,10 @@ std::vector<std::string> UI_checkbox::selected_text() const
return selected_text;
}
void UI_checkbox::from_json(const json& j)
void UI_checkbox::from_json(const json &j)
{
UI_x_boxes::from_json(j);
if (j.contains("selected"))
{
m_selected.clear();
+17 -15
View File
@@ -1,26 +1,28 @@
#pragma once
#include <cstdint>
#include "UI_item.hpp"
#include "UI_x_boxes.hpp"
#include <cstdint>
#define UI_CHECKBOX_TYPE std::string("UI_checkbox")
class UI_checkbox : public UI_x_boxes
{
public:
UI_checkbox();
UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, std::function<void(std::string, std::vector<std::string>)> on_change = nullptr);
UI_checkbox(const json& j);
public:
UI_checkbox();
UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
std::function<void(std::string, std::vector<std::string>)> on_change = nullptr);
UI_checkbox(const json &j);
void toggle_selected(size_t selected);
void toggle_selected(std::string selected);
std::vector<bool> selected() const;
std::vector<std::string> selected_text() const;
void from_json(const json& j) override;
json to_json() const override;
private:
std::vector<bool> m_selected;
void toggle_selected(size_t selected);
void toggle_selected(std::string selected);
std::vector<bool> selected() const;
std::vector<std::string> selected_text() const;
std::function<void(std::string, std::vector<std::string>)> m_on_change;
void from_json(const json &j) override;
json to_json() const override;
private:
std::vector<bool> m_selected;
std::function<void(std::string, std::vector<std::string>)> m_on_change;
};
+6 -6
View File
@@ -1,17 +1,17 @@
#include "UI_combobox.hpp"
UI_combobox::UI_combobox()
: UI_x_boxes(UI_COMBOBOX_TYPE)
UI_combobox::UI_combobox() : UI_x_boxes(UI_COMBOBOX_TYPE)
{
}
UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected, std::function<void(std::string, std::string)> on_change)
UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options, size_t selected,
std::function<void(std::string, std::string)> on_change)
: UI_x_boxes(id, UI_COMBOBOX_TYPE, text, fg_color, bg_color, options), m_selected(selected), m_on_change(on_change)
{
}
UI_combobox::UI_combobox(const json& j)
: UI_x_boxes(UI_COMBOBOX_TYPE), m_on_change(nullptr)
UI_combobox::UI_combobox(const json &j) : UI_x_boxes(UI_COMBOBOX_TYPE), m_on_change(nullptr)
{
from_json(j);
}
@@ -49,7 +49,7 @@ std::string UI_combobox::selected_text() const
return m_options[m_selected];
}
void UI_combobox::from_json(const json& j)
void UI_combobox::from_json(const json &j)
{
UI_x_boxes::from_json(j);
std::string selected_str = j.at("selected");
+17 -15
View File
@@ -1,27 +1,29 @@
#pragma once
#include <functional>
#include "UI_item.hpp"
#include "UI_x_boxes.hpp"
#include <functional>
#define UI_COMBOBOX_TYPE std::string("UI_combobox")
class UI_combobox : public UI_x_boxes
{
public:
UI_combobox();
UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected = 0, std::function<void(std::string, std::string)> on_change = nullptr);
UI_combobox(const json& j);
public:
UI_combobox();
UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
size_t selected = 0, std::function<void(std::string, std::string)> on_change = nullptr);
UI_combobox(const json &j);
void set_selected(size_t selected);
void set_selected(std::string selected) override;
size_t selected() const;
std::string selected_text() const;
void from_json(const json& j) override;
json to_json() const override;
private:
size_t m_selected;
void set_selected(size_t selected);
void set_selected(std::string selected) override;
size_t selected() const;
std::string selected_text() const;
std::function<void(std::string, std::string)> m_on_change;
void from_json(const json &j) override;
json to_json() const override;
private:
size_t m_selected;
std::function<void(std::string, std::string)> m_on_change;
};
+2 -3
View File
@@ -5,8 +5,7 @@ UI_dial::UI_dial(std::string id, std::string text, Color fg_color, Color bg_colo
{
}
UI_dial::UI_dial(const json& j)
: UI_item(UI_DIAL_TYPE)
UI_dial::UI_dial(const json &j) : UI_item(UI_DIAL_TYPE)
{
from_json(j);
}
@@ -36,7 +35,7 @@ double UI_dial::max() const
return m_max;
}
void UI_dial::from_json(const json& j)
void UI_dial::from_json(const json &j)
{
UI_item::from_json(j);
+14 -13
View File
@@ -5,20 +5,21 @@
class UI_dial : public UI_item
{
public:
UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value);
UI_dial(const json& j);
public:
UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value);
UI_dial(const json &j);
void set_value(double value) override;
double value() const;
void set_value(double value) override;
double value() const;
double min() const;
double max() const;
double min() const;
double max() const;
void from_json(const json& j) override;
json to_json() const override;
private:
double m_min;
double m_max;
double m_value;
void from_json(const json &j) override;
json to_json() const override;
private:
double m_min;
double m_max;
double m_value;
};
+7 -5
View File
@@ -1,16 +1,18 @@
#include "UI_item.hpp"
UI_item::UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color)
: m_id(id), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), m_bg_color(bg_color), enabled(true), visible(true)
UI_item::UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color)
: m_id(id), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), m_bg_color(bg_color),
enabled(true), visible(true)
{
}
UI_item::UI_item(std::string type)
: m_id(""), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black), m_bg_color(Color::White), enabled(true), visible(true)
: m_id(""), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black), m_bg_color(Color::White),
enabled(true), visible(true)
{
}
UI_item::UI_item(const json& j)
UI_item::UI_item(const json &j)
{
from_json(j);
}
@@ -106,7 +108,7 @@ bool UI_item::is_visible() const
return visible;
}
void UI_item::from_json(const json& j)
void UI_item::from_json(const json &j)
{
if (!is_type(j))
return;
+15 -8
View File
@@ -1,19 +1,20 @@
#pragma once
#include <functional>
#include <thread>
#include "color.hpp"
#include <cstdint>
#include <functional>
#include <nlohmann/json.hpp>
#include <string>
#include "color.hpp"
#include <thread>
using json = nlohmann::json;
#define UNUSED(x) (void)(x)
class UI_item {
class UI_item
{
public:
UI_item(std::string type);
UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color);
UI_item(const json& j);
UI_item(const json &j);
std::string id() const;
std::string type() const;
@@ -37,13 +38,19 @@ class UI_item {
void set_visible(bool visible);
bool is_visible() const;
virtual void from_json(const json& j);
virtual void from_json(const json &j);
virtual json to_json() const;
virtual void click() const {};
virtual void set_selected(std::string selected) {UNUSED(selected);};
virtual void set_value(double value) {UNUSED(value);};
virtual void set_selected(std::string selected)
{
UNUSED(selected);
};
virtual void set_value(double value)
{
UNUSED(value);
};
protected:
std::string m_id;
+3 -4
View File
@@ -5,18 +5,17 @@ UI_label::UI_label(std::string id, std::string text, Color fg_color, Color bg_co
{
}
UI_label::UI_label(const json& j)
: UI_item(UI_LABEL_TYPE)
UI_label::UI_label(const json &j) : UI_item(UI_LABEL_TYPE)
{
from_json(j);
}
void UI_label::from_json(const json& j)
void UI_label::from_json(const json &j)
{
UI_item::from_json(j);
}
json UI_label::to_json() const
{
return UI_item::to_json();
return UI_item::to_json();
}
+4 -4
View File
@@ -1,15 +1,15 @@
#pragma once
#include <string>
#include "UI_item.hpp"
#include <string>
#define UI_LABEL_TYPE std::string("UI_label")
class UI_label : public UI_item
{
public:
public:
UI_label(std::string id, std::string text, Color fg_color, Color bg_color);
UI_label(const json& j);
UI_label(const json &j);
void from_json(const json& j) override;
void from_json(const json &j) override;
json to_json() const override;
};
+5 -8
View File
@@ -1,14 +1,11 @@
#include "UI_led.hpp"
UI_led::UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color) :
UI_item(id, UI_LED_TYPE, text, 12, fg_color, bg_color),
m_led_color(led_color)
UI_led::UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color)
: UI_item(id, UI_LED_TYPE, text, 12, fg_color, bg_color), m_led_color(led_color)
{
}
UI_led::UI_led(const json& j) :
UI_item(UI_LED_TYPE)
UI_led::UI_led(const json &j) : UI_item(UI_LED_TYPE)
{
from_json(j);
}
@@ -23,7 +20,7 @@ void UI_led::set_led_color(Color led_color)
m_led_color = led_color;
}
void UI_led::from_json(const json& j)
void UI_led::from_json(const json &j)
{
UI_item::from_json(j);
std::string color;
@@ -35,5 +32,5 @@ json UI_led::to_json() const
{
json j = UI_item::to_json();
j["led_color"] = m_led_color.to_hex();
return j;
return j;
}
+6 -5
View File
@@ -1,20 +1,21 @@
#pragma once
#include <string>
#include "UI_item.hpp"
#include <string>
#define UI_LED_TYPE std::string("UI_led")
class UI_led : public UI_item
{
public:
public:
UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color);
UI_led(const json& j);
UI_led(const json &j);
Color led_color() const;
void set_led_color(Color led_color);
void from_json(const json& j) override;
void from_json(const json &j) override;
json to_json() const override;
private:
private:
Color m_led_color;
};
+4 -5
View File
@@ -1,14 +1,13 @@
#include "UI_slider.hpp"
UI_slider::UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, std::function<void(std::string, double)> on_change)
UI_slider::UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max,
double value, std::function<void(std::string, double)> on_change)
: UI_item(id, UI_SLIDER_TYPE, text, 12, fg_color, bg_color), m_min(min), m_max(max), m_value(value)
{
this->m_on_change = on_change;
}
UI_slider::UI_slider(const json& j)
: UI_item(UI_SLIDER_TYPE),
m_on_change(nullptr)
UI_slider::UI_slider(const json &j) : UI_item(UI_SLIDER_TYPE), m_on_change(nullptr)
{
from_json(j);
}
@@ -42,7 +41,7 @@ double UI_slider::max() const
return m_max;
}
void UI_slider::from_json(const json& j)
void UI_slider::from_json(const json &j)
{
UI_item::from_json(j);
+16 -14
View File
@@ -5,22 +5,24 @@
class UI_slider : public UI_item
{
public:
UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, std::function<void(std::string, double)> on_change = nullptr);
UI_slider(const json& j);
public:
UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value,
std::function<void(std::string, double)> on_change = nullptr);
UI_slider(const json &j);
void set_value(double value) override;
double value() const;
void set_value(double value) override;
double value() const;
double min() const;
double max() const;
double min() const;
double max() const;
void from_json(const json& j) override;
json to_json() const override;
private:
double m_min;
double m_max;
double m_value;
void from_json(const json &j) override;
json to_json() const override;
std::function<void(std::string, double)> m_on_change;
private:
double m_min;
double m_max;
double m_value;
std::function<void(std::string, double)> m_on_change;
};
+4 -5
View File
@@ -1,13 +1,12 @@
#include "UI_textbox.hpp"
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_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), m_on_change(on_change)
{
}
UI_textbox::UI_textbox(const json& j)
: UI_item(UI_TEXTBOX_TYPE),
m_on_change(nullptr)
UI_textbox::UI_textbox(const json &j) : UI_item(UI_TEXTBOX_TYPE), m_on_change(nullptr)
{
from_json(j);
}
@@ -33,7 +32,7 @@ std::string UI_textbox::hint() const
return m_hint;
}
void UI_textbox::from_json(const json& j)
void UI_textbox::from_json(const json &j)
{
UI_item::from_json(j);
m_hint = j.at("hint");
+8 -6
View File
@@ -1,23 +1,25 @@
#pragma once
#include <string>
#include "UI_item.hpp"
#include <string>
#define UI_TEXTBOX_TYPE std::string("UI_textbox")
class UI_textbox : public UI_item
{
public:
UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, std::function<void(std::string, std::string)> on_change = nullptr);
UI_textbox(const json& j);
public:
UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color,
std::function<void(std::string, std::string)> on_change = nullptr);
UI_textbox(const json &j);
void set_text(std::string text) override;
void set_hint(std::string hint);
std::string hint() const;
void from_json(const json& j) override;
void from_json(const json &j) override;
json to_json() const override;
private:
private:
std::string m_hint;
std::function<void(std::string, std::string)> m_on_change;
};
+7 -7
View File
@@ -1,12 +1,12 @@
#include "UI_thermo.hpp"
UI_thermo::UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value)
UI_thermo::UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max,
double value)
: UI_item(id, UI_THERMO_TYPE, text, 12, fg_color, bg_color), m_min(min), m_max(max), m_value(value)
{
}
UI_thermo::UI_thermo(const json& j)
: UI_item(UI_THERMO_TYPE)
UI_thermo::UI_thermo(const json &j) : UI_item(UI_THERMO_TYPE)
{
from_json(j);
}
@@ -50,7 +50,7 @@ void UI_thermo::add_color_stop(double value, Color color)
{
if (value <= 0 || value >= 1)
return;
this->m_color_map[value] = color;
}
@@ -80,7 +80,7 @@ void UI_thermo::remove_color_stop_normalized(double value)
this->m_color_map.erase(normalized);
}
void UI_thermo::from_json(const json& j)
void UI_thermo::from_json(const json &j)
{
UI_item::from_json(j);
@@ -88,7 +88,7 @@ void UI_thermo::from_json(const json& j)
this->m_max = j.at("max");
this->m_value = j.at("value");
this->m_color_map.clear();
for (auto& i : j["color_map"])
for (auto &i : j["color_map"])
{
double k = i.at("key");
std::string c = i.at("value");
@@ -103,7 +103,7 @@ json UI_thermo::to_json() const
j["max"] = this->m_max;
j["value"] = this->m_value;
j["color_map"] = json::array();
for (auto& [key, value] : this->m_color_map)
for (auto &[key, value] : this->m_color_map)
{
json item = json::object();
item["key"] = key;
+21 -20
View File
@@ -6,28 +6,29 @@
// TODO: Add color scale
class UI_thermo : public UI_item
{
public:
UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value);
UI_thermo(const json& j);
public:
UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value);
UI_thermo(const json &j);
void set_value(double value) override;
double value() const;
void set_value(double value) override;
double value() const;
double min() const;
double max() const;
double min() const;
double max() const;
void set_start_color(Color color);
void set_end_color(Color color);
void add_color_stop(double value, Color color);
void add_color_stop_normalized(double value, Color color);
void remove_color_stop(double value);
void remove_color_stop_normalized(double value);
void set_start_color(Color color);
void set_end_color(Color color);
void add_color_stop(double value, Color color);
void add_color_stop_normalized(double value, Color color);
void remove_color_stop(double value);
void remove_color_stop_normalized(double value);
void from_json(const json& j) override;
json to_json() const override;
private:
double m_min;
double m_max;
double m_value;
std::unordered_map<double, Color> m_color_map;
void from_json(const json &j) override;
json to_json() const override;
private:
double m_min;
double m_max;
double m_value;
std::unordered_map<double, Color> m_color_map;
};
+4 -4
View File
@@ -1,16 +1,16 @@
#include "UI_x_boxes.hpp"
UI_x_boxes::UI_x_boxes(std::string type)
: UI_item(type), m_options()
UI_x_boxes::UI_x_boxes(std::string type) : UI_item(type), m_options()
{
}
UI_x_boxes::UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options)
UI_x_boxes::UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options)
: UI_item(id, type, text, 12, fg_color, bg_color), m_options(options)
{
}
void UI_x_boxes::from_json(const json& j)
void UI_x_boxes::from_json(const json &j)
{
UI_item::from_json(j);
if (j.contains("options"))
+9 -7
View File
@@ -4,12 +4,14 @@
class UI_x_boxes : public UI_item
{
public:
UI_x_boxes(std::string type);
UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options);
public:
UI_x_boxes(std::string type);
UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options);
void from_json(const json& j) override;
json to_json() const override;
protected:
std::vector<std::string> m_options;
void from_json(const json &j) override;
json to_json() const override;
protected:
std::vector<std::string> m_options;
};