2023-12-09 18:31:51 +00:00
|
|
|
/**
|
|
|
|
* mode_switch_button.cpp
|
|
|
|
* =============================================================================
|
2024-03-21 20:38:57 +00:00
|
|
|
* Copyright 2021-2024 Serhii Snitsaruk
|
2023-12-09 18:31:51 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
|
|
|
|
2024-01-11 10:22:02 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2023-12-09 18:31:51 +00:00
|
|
|
#include "mode_switch_button.h"
|
|
|
|
|
2024-01-07 12:45:27 +00:00
|
|
|
#include "../util/limbo_string_names.h"
|
|
|
|
|
|
|
|
#ifdef LIMBOAI_MODULE
|
2023-12-09 18:31:51 +00:00
|
|
|
#include "core/error/error_macros.h"
|
|
|
|
#include "core/object/object.h"
|
|
|
|
#include "core/variant/variant.h"
|
2024-01-07 12:45:27 +00:00
|
|
|
#endif // LIMBOAI_MODULE
|
2023-12-09 18:31:51 +00:00
|
|
|
|
|
|
|
void ModeSwitchButton::add_mode(int p_id, const Ref<Texture2D> &p_icon, const String &p_tooltip) {
|
|
|
|
bool unique_id = true;
|
|
|
|
for (int i = 0; i < modes.size(); i++) {
|
|
|
|
if (modes[i].id == p_id) {
|
|
|
|
unique_id = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERR_FAIL_COND_MSG(unique_id == false, "ID is already in use by another button state: " + itos(p_id));
|
|
|
|
|
|
|
|
Mode mode;
|
|
|
|
mode.id = p_id;
|
|
|
|
mode.icon = p_icon;
|
|
|
|
mode.tooltip = p_tooltip;
|
|
|
|
modes.append(mode);
|
|
|
|
|
|
|
|
if (current_mode_index == -1) {
|
2023-12-10 13:35:28 +00:00
|
|
|
_set_mode_by_index(0);
|
2023-12-09 18:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModeSwitchButton::set_mode(int p_id, bool p_no_signal) {
|
|
|
|
ERR_FAIL_COND_MSG(modes.is_empty(), "Cannot set button state with zero states.");
|
|
|
|
|
|
|
|
int idx = -1;
|
|
|
|
for (int i = 0; i < modes.size(); i++) {
|
|
|
|
if (modes[i].id == p_id) {
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ERR_FAIL_COND_MSG(idx == -1, "Button state not found with such id: " + itos(p_id));
|
|
|
|
|
2023-12-10 13:35:28 +00:00
|
|
|
_set_mode_by_index(idx);
|
2023-12-09 18:31:51 +00:00
|
|
|
if (!p_no_signal) {
|
2024-01-09 12:42:54 +00:00
|
|
|
emit_signal(LW_NAME(mode_changed));
|
2023-12-09 18:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModeSwitchButton::clear() {
|
|
|
|
current_mode_index = -1;
|
|
|
|
modes.clear();
|
|
|
|
}
|
|
|
|
|
2024-01-09 12:34:24 +00:00
|
|
|
void ModeSwitchButton::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_READY: {
|
2024-01-09 12:42:54 +00:00
|
|
|
connect(LW_NAME(pressed), callable_mp(this, &ModeSwitchButton::next_mode));
|
2024-01-09 12:34:24 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-09 18:31:51 +00:00
|
|
|
void ModeSwitchButton::_bind_methods() {
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("add_mode", "id", "icon", "tooltip"), &ModeSwitchButton::add_mode);
|
2023-12-09 18:31:51 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_mode"), &ModeSwitchButton::get_mode);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_mode", "id"), &ModeSwitchButton::set_mode);
|
2023-12-09 18:31:51 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("next_mode"), &ModeSwitchButton::next_mode);
|
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("mode_changed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
ModeSwitchButton::ModeSwitchButton() {
|
|
|
|
}
|
2024-01-11 10:22:02 +00:00
|
|
|
|
|
|
|
#endif // ! TOOLS_ENABLED
|