Radio option.

This commit is contained in:
Dario 2025-01-19 15:50:06 -03:00 committed by Mr-Wiseguy
parent 4967d31125
commit b9eeb57d99
9 changed files with 168 additions and 38 deletions

View File

@ -4,9 +4,6 @@
namespace recompui {
static const std::string_view hover_state = "hover";
static const std::string_view disabled_state = "disabled";
Button::Button(const std::string &text, ButtonStyle style, Element *parent) : Element(parent, Events(EventType::Click, EventType::Hover, EventType::Enable), "button") {
this->style = style;

View File

@ -2,9 +2,6 @@
namespace recompui {
static const std::string_view hover_state = "hover";
static const std::string_view disabled_state = "disabled";
Clickable::Clickable(Element *parent, bool draggable) : Element(parent, Events(EventType::Click, EventType::Hover, EventType::Enable, draggable ? EventType::Drag : EventType::None)) {
if (draggable) {
set_drag(Drag::Drag);

View File

@ -2,4 +2,101 @@
namespace recompui {
// RadioOption
RadioOption::RadioOption(std::string_view name, uint32_t index, Element *parent) : Element(parent, Events(EventType::Click, EventType::Focus, EventType::Hover, EventType::Enable), "label") {
this->index = index;
set_text(name);
set_cursor(Cursor::Pointer);
set_font_size(28.0f);
set_letter_spacing(3.08f);
set_line_height(28.0f);
set_font_weight(700);
set_border_color(Color{ 242, 242, 242, 255 });
set_border_bottom_width(0.0f);
set_color(Color{ 255, 255, 255, 153 });
hover_style.set_color(Color{ 255, 255, 255, 204 });
checked_style.set_color(Color{ 255, 255, 255, 255 });
checked_style.set_border_bottom_width(1.0f);
add_style(&hover_style, { hover_state });
add_style(&checked_style, { checked_state });
}
void RadioOption::set_pressed_callback(std::function<void(uint32_t)> callback) {
pressed_callback = callback;
}
void RadioOption::set_selected_state(bool enable) {
set_style_enabled(checked_state, enable);
}
void RadioOption::process_event(const Event &e) {
switch (e.type) {
case EventType::Click:
pressed_callback(index);
break;
case EventType::Hover:
set_style_enabled(hover_state, std::get<EventHover>(e.variant).active);
break;
case EventType::Enable:
set_style_enabled(disabled_state, !std::get<EventEnable>(e.variant).active);
break;
default:
break;
}
}
// Radio
void Radio::set_index_internal(uint32_t index, bool setup, bool trigger_callbacks) {
if (this->index != index || setup) {
options[this->index]->set_selected_state(false);
this->index = index;
options[index]->set_selected_state(true);
if (trigger_callbacks) {
for (const auto &function : index_changed_callbacks) {
function(index);
}
}
}
}
void Radio::option_selected(uint32_t index) {
set_index_internal(index, false, true);
}
Radio::Radio(Element *parent) : Container(FlexDirection::Row, JustifyContent::FlexStart, parent) {
set_gap(12.0f);
}
Radio::~Radio() {
}
void Radio::add_option(std::string_view name) {
RadioOption *option = get_current_context().create_element<RadioOption>(name, uint32_t(options.size()), this);
option->set_pressed_callback(std::bind(&Radio::option_selected, this, std::placeholders::_1));
options.emplace_back(option);
// The first option was added, select it.
if (options.size() == 1) {
set_index_internal(0, true, false);
}
}
void Radio::set_index(uint32_t index) {
set_index_internal(index, false, false);
}
uint32_t Radio::get_index() const {
return index;
}
void Radio::add_index_changed_callback(std::function<void(uint32_t)> callback) {
index_changed_callbacks.emplace_back(callback);
}
};

View File

@ -1,7 +1,38 @@
#pragma once
#include "ui_element.h"
#include "ui_container.h"
#include "ui_label.h"
namespace recompui {
class RadioOption : public Element {
private:
Style hover_style;
Style checked_style;
std::function<void(uint32_t)> pressed_callback = nullptr;
uint32_t index = 0;
protected:
virtual void process_event(const Event &e) override;
public:
RadioOption(std::string_view name, uint32_t index, Element *parent);
void set_pressed_callback(std::function<void(uint32_t)> callback);
void set_selected_state(bool enable);
};
class Radio : public Container {
private:
std::vector<RadioOption *> options;
uint32_t index = 0;
std::vector<std::function<void(uint32_t)>> index_changed_callbacks;
void set_index_internal(uint32_t index, bool setup, bool trigger_callbacks);
void option_selected(uint32_t index);
public:
Radio(Element *parent);
virtual ~Radio();
void add_option(std::string_view name);
void set_index(uint32_t index);
uint32_t get_index() const;
void add_index_changed_callback(std::function<void(uint32_t)> callback);
};
} // namespace recompui

View File

@ -4,10 +4,6 @@
namespace recompui {
static const std::string_view checked_state = "checked";
static const std::string_view hover_state = "hover";
static const std::string_view disabled_state = "disabled";
Toggle::Toggle(Element *parent) : Element(parent, Events(EventType::Click, EventType::Hover, EventType::Enable), "button") {
set_width(162.0f);
set_height(72.0f);

View File

@ -5,6 +5,10 @@
namespace recompui {
constexpr std::string_view checked_state = "checked";
constexpr std::string_view hover_state = "hover";
constexpr std::string_view disabled_state = "disabled";
struct Color {
uint8_t r = 255;
uint8_t g = 255;

View File

@ -19,6 +19,7 @@ void ConfigOptionElement::process_event(const Event &e) {
}
ConfigOptionElement::ConfigOptionElement(Element *parent) : Element(parent, Events(EventType::Hover)) {
set_gap(8.0f);
set_min_height(100.0f);
name_label = get_current_context().create_element<Label>(LabelStyle::Normal, this);
@ -52,27 +53,14 @@ void ConfigOptionSlider::slider_value_changed(double v) {
printf("%s changed to %f.\n", name.c_str(), v);
}
ConfigOptionSlider::ConfigOptionSlider(Element *parent) : ConfigOptionElement(parent) {
ConfigOptionSlider::ConfigOptionSlider(double value, double min_value, double max_value, Element *parent) : ConfigOptionElement(parent) {
slider = get_current_context().create_element<Slider>(SliderType::Percent, this);
slider->set_value(value);
slider->set_min_value(min_value);
slider->set_max_value(max_value);
slider->add_value_changed_callback(std::bind(&ConfigOptionSlider::slider_value_changed, this, std::placeholders::_1));
}
ConfigOptionSlider::~ConfigOptionSlider() {
}
void ConfigOptionSlider::set_value(double v) {
slider->set_value(v);
}
void ConfigOptionSlider::set_min_value(double v) {
slider->set_min_value(v);
}
void ConfigOptionSlider::set_max_value(double v) {
slider->set_max_value(v);
}
// ConfigOptionTextInput
void ConfigOptionTextInput::text_changed(const std::string &text) {
@ -85,8 +73,18 @@ ConfigOptionTextInput::ConfigOptionTextInput(Element *parent) : ConfigOptionElem
text_input->add_text_changed_callback(std::bind(&ConfigOptionTextInput::text_changed, this, std::placeholders::_1));
}
ConfigOptionTextInput::~ConfigOptionTextInput() {
// ConfigOptionRadio
void ConfigOptionRadio::index_changed(uint32_t index) {
printf("%s changed to %d.\n", name.c_str(), index);
}
ConfigOptionRadio::ConfigOptionRadio(const std::initializer_list<std::string_view> &options, Element *parent) : ConfigOptionElement(parent) {
radio = get_current_context().create_element<Radio>(this);
radio->add_index_changed_callback(std::bind(&ConfigOptionRadio::index_changed, this, std::placeholders::_1));
for (std::string_view option : options) {
radio->add_option(option);
}
}
// ConfigSubMenu
@ -169,9 +167,7 @@ void ConfigSubMenu::add_option(ConfigOptionElement *option, std::string_view nam
}
void ConfigSubMenu::add_slider_option(std::string_view name, std::string_view description, double min, double max) {
ConfigOptionSlider *option_slider = get_current_context().create_element<ConfigOptionSlider>(config_scroll_container);
option_slider->set_min_value(min);
option_slider->set_max_value(max);
ConfigOptionSlider *option_slider = get_current_context().create_element<ConfigOptionSlider>((min + max) / 2.0, min, max, config_scroll_container);
add_option(option_slider, name, description);
}
@ -180,6 +176,11 @@ void ConfigSubMenu::add_text_option(std::string_view name, std::string_view desc
add_option(option_text_input, name, description);
}
void ConfigSubMenu::add_radio_option(std::string_view name, std::string_view description, const std::initializer_list<std::string_view> &options) {
ConfigOptionRadio *option_radio = get_current_context().create_element<ConfigOptionRadio>(options, config_scroll_container);
add_option(option_radio, name, description);
}
void ConfigSubMenu::set_enter_sub_menu_callback(std::function<void()> callback) {
enter_sub_menu_callback = callback;
}

View File

@ -6,6 +6,7 @@
#include "elements/ui_button.h"
#include "elements/ui_container.h"
#include "elements/ui_label.h"
#include "elements/ui_radio.h"
#include "elements/ui_scroll_container.h"
#include "elements/ui_slider.h"
#include "elements/ui_text_input.h"
@ -35,11 +36,7 @@ protected:
void slider_value_changed(double v);
public:
ConfigOptionSlider(Element *parent);
virtual ~ConfigOptionSlider();
void set_value(double v);
void set_min_value(double v);
void set_max_value(double v);
ConfigOptionSlider(double value, double min_value, double max_value, Element *parent);
};
class ConfigOptionTextInput : public ConfigOptionElement {
@ -49,7 +46,15 @@ protected:
void text_changed(const std::string &text);
public:
ConfigOptionTextInput(Element *parent);
virtual ~ConfigOptionTextInput();
};
class ConfigOptionRadio : public ConfigOptionElement {
protected:
Radio *radio = nullptr;
void index_changed(uint32_t index);
public:
ConfigOptionRadio(const std::initializer_list<std::string_view> &options, Element *parent);
};
class ConfigSubMenu : public Element {
@ -77,6 +82,7 @@ public:
void clear_options();
void add_slider_option(std::string_view name, std::string_view description, double min, double max);
void add_text_option(std::string_view name, std::string_view description);
void add_radio_option(std::string_view name, std::string_view description, const std::initializer_list<std::string_view> &options);
void set_enter_sub_menu_callback(std::function<void()> callback);
void set_quit_sub_menu_callback(std::function<void()> callback);
};

View File

@ -102,6 +102,7 @@ void ModMenu::mod_configure_requested() {
ext_config_sub_menu->clear_options();
ext_config_sub_menu->add_slider_option("Slider Option", "Description for slider option.", 0.0, 100.0);
ext_config_sub_menu->add_text_option("Text Option", "Description for simple option.");
ext_config_sub_menu->add_radio_option("Radio Option", "Description for radio option.", { "First", "Second", "Third" });
ext_config_sub_menu->enter(mod_details[active_mod_index].mod_id);
}
}