Move from function pointers to std::function

This commit is contained in:
2024-08-19 01:07:52 +02:00
parent 45aa67473f
commit d257dc154f
9 changed files with 15 additions and 23 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
#include "UI_button.hpp"
UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color, UI_button_on_click 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->p_on_click = on_click;
+2 -3
View File
@@ -3,12 +3,11 @@
#include "UI_item.hpp"
#define UI_BUTTON_TYPE std::string("UIButton")
typedef void(*UI_button_on_click)(std::string id);
class UI_button : public UI_item
{
public:
UI_button(std::string id, std::string text, Color fg_color, Color bg_color, UI_button_on_click on_click);
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;
@@ -16,5 +15,5 @@ class UI_button : public UI_item
void click() const;
private:
UI_button_on_click p_on_click = nullptr;
std::function<void(std::string)> p_on_click = nullptr;
};
+1 -1
View File
@@ -5,7 +5,7 @@ UI_checkbox::UI_checkbox()
{
}
UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, UI_checkbox_on_change 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), p_selected(), p_on_change(on_change)
{
}
+2 -3
View File
@@ -4,13 +4,12 @@
#include "UI_x_boxes.hpp"
#define UI_CHECKBOX_TYPE std::string("UIcheckbox")
typedef void (*UI_checkbox_on_change) (std::string id, std::vector<std::string> selected);
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, UI_checkbox_on_change on_change = nullptr);
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);
@@ -23,5 +22,5 @@ class UI_checkbox : public UI_x_boxes
private:
std::vector<bool> p_selected;
UI_checkbox_on_change p_on_change;
std::function<void(std::string, std::vector<std::string>)> p_on_change;
};
+2 -7
View File
@@ -5,16 +5,11 @@ UI_combobox::UI_combobox()
{
}
UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected, UI_combobox_on_change 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), p_selected(selected), p_on_change(on_change)
{
}
UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, UI_combobox_on_change on_change)
: UI_x_boxes(id, UI_COMBOBOX_TYPE, text, fg_color, bg_color, options), p_selected(0), p_on_change(on_change)
{
}
UI_combobox::UI_combobox(const json& j)
: UI_x_boxes(UI_COMBOBOX_TYPE), p_on_change(nullptr)
{
@@ -28,7 +23,7 @@ void UI_combobox::set_selected(size_t selected)
p_selected = selected;
if (p_on_change != nullptr)
{
p_on_change(p_id, selected);
p_on_change(p_id, selected_text());
}
}
}
+3 -4
View File
@@ -1,17 +1,16 @@
#pragma once
#include <functional>
#include "UI_item.hpp"
#include "UI_x_boxes.hpp"
#define UI_COMBOBOX_TYPE std::string("UIcombobox")
typedef void (*UI_combobox_on_change) (std::string id, size_t selected);
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, UI_combobox_on_change on_change = nullptr);
UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options, UI_combobox_on_change on_change = nullptr);
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);
@@ -24,5 +23,5 @@ class UI_combobox : public UI_x_boxes
private:
size_t p_selected;
UI_combobox_on_change p_on_change;
std::function<void(std::string, std::string)> p_on_change;
};
+1
View File
@@ -1,4 +1,5 @@
#pragma once
#include <functional>
#include <cstdint>
#include <nlohmann/json.hpp>
#include <string>
+1 -1
View File
@@ -1,6 +1,6 @@
#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, UI_slider_on_change 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), p_min(min), p_max(max), p_value(value)
{
this->p_on_change = on_change;
+2 -3
View File
@@ -2,12 +2,11 @@
#include "UI_item.hpp"
#define UI_SLIDER_TYPE std::string("UISlider")
typedef void(*UI_slider_on_change)(std::string, double value);
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, UI_slider_on_change on_change);
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);
@@ -20,5 +19,5 @@ class UI_slider : public UI_item
double p_max;
double p_value;
UI_slider_on_change p_on_change;
std::function<void(std::string, double)> p_on_change;
};