limboai/editor/mode_switch_button.h

72 lines
1.8 KiB
C++

/**
* mode_switch_button.h
* =============================================================================
* Copyright 2021-2023 Serhii Snitsaruk
*
* 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.
* =============================================================================
*/
#ifndef MODE_SWITCH_BUTTON
#define MODE_SWITCH_BUTTON
#ifdef LIMBOAI_MODULE
#include "scene/gui/button.h"
#include "core/typedefs.h"
#include "scene/resources/texture.h"
#define SET_ICON(m_tex) set_icon(m_tex)
#endif // LIMBOAI_MODULE
#ifdef LIMBOAI_GDEXTENSION
#include <godot_cpp/classes/button.hpp>
#include <godot_cpp/classes/texture2d.hpp>
#include <godot_cpp/templates/vector.hpp>
using namespace godot;
#define SET_BUTTON_ICON(m_tex) set_button_icon(m_tex)
#endif // LIMBOAI_GDEXTENSION
class ModeSwitchButton : public Button {
GDCLASS(ModeSwitchButton, Button);
private:
struct Mode {
int id = 0;
Ref<Texture2D> icon = nullptr;
String tooltip = "";
};
int current_mode_index = -1;
Vector<Mode> modes;
_FORCE_INLINE_ void _set_mode_by_index(int p_index) {
current_mode_index = p_index;
SET_BUTTON_ICON(modes[current_mode_index].icon);
if (!modes[current_mode_index].tooltip.is_empty()) {
set_tooltip_text(modes[current_mode_index].tooltip);
}
}
protected:
static void _bind_methods();
void _notification(int p_what);
public:
void add_mode(int p_id, const Ref<Texture2D> &p_icon, const String &p_tooltip = "");
int get_mode() const { return modes.size() > 0 ? modes[current_mode_index].id : -1; }
void set_mode(int p_id, bool p_no_signal = false);
_FORCE_INLINE_ void next_mode() { set_mode((current_mode_index + 1) % modes.size()); };
void clear();
ModeSwitchButton();
};
#endif // MODE_SWITCH_BUTTON