ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
UIButton.h
浏览该文件的文档.
1#pragma once
2#include "../Core/Core.h"
4#include <functional>
5
6namespace Shit {
17 public:
18 enum class State { Normal, Highlighted, Pressed, Disabled };
19
21 struct ColorBlock {
22 Color normalColor = Color{ 255, 255, 255, 255 };
23 Color highlightedColor = Color{ 230, 230, 230, 255 };
24 Color pressedColor = Color{ 180, 180, 180, 255 };
25 Color disabledColor = Color{ 128, 128, 128, 128 };
26 };
27
28 UIButton() = default;
29 ~UIButton() override = default;
30
31 // --- pointer 事件(由 UIRenderSystem raycasting 调用) ---
36
37 // --- getter & setter ---
38 bool isInteractable() const { return m_interactable; }
39 void setInteractable(bool interactable);
40
41 State getState() const { return m_state; }
42 bool wasPointerDown() const { return m_isPressed; }
43
44 const ColorBlock& getColors() const { return m_colors; }
45 void setColors(const ColorBlock& colors) { m_colors = colors; applyCurrentColor(); }
46
47 void setOnClick(std::function<void()> callback) { m_onClick = std::move(callback); }
48
51
52 protected:
53 void onRender(const SDL_FRect& /*screenRect*/) override;
54
55 private:
56 void setState(State newState);
57 void applyCurrentColor();
58
59 SHIT_META(Disable)
60 State m_state = State::Normal;
61 SHIT_META(({.displayName = "Interactable", .tooltip = "是否可交互"}))
62 bool m_interactable = true;
63 SHIT_META(Disable)
64 bool m_isPointerInside = false;
65 SHIT_META(Disable)
66 bool m_isPressed = false;
67 SHIT_META(Disable)
68 ColorBlock m_colors;
69 SHIT_META(Disable)
70 std::function<void()> m_onClick;
71 };
72}
#define SHIT_API
定义 Core.h:14
#define SHIT_REFLECT_BODY(Type)
定义 Macros.h:62
#define SHIT_REFLECT(Mode)
定义 Macros.h:59
#define SHIT_META(...)
定义 Macros.h:68
void onPointerUp()
void onPointerExit()
void onPointerEnter()
void resetPressed()
复位残留的按下状态(按钮在按下后离开 visible 未收到 onPointerUp 时使用),不触发 onClick
UIButton()=default
bool wasPointerDown() const
定义 UIButton.h:42
void onRender(const SDL_FRect &) override
子类实现:在给定屏幕矩形内绘制自身(通过 Renderer 静态 API 绘制)
State
定义 UIButton.h:18
@ Normal
定义 UIButton.h:18
void setInteractable(bool interactable)
~UIButton() override=default
State getState() const
定义 UIButton.h:41
bool isInteractable() const
定义 UIButton.h:38
void setColors(const ColorBlock &colors)
定义 UIButton.h:45
void setOnClick(std::function< void()> callback)
定义 UIButton.h:47
const ColorBlock & getColors() const
定义 UIButton.h:44
void onPointerDown()
定义 AudioPlayer.h:10
RGBA 颜色(每通道 8 位)
定义 Core.h:26
ColorTint 过渡的四种状态颜色(乘到 UIImage 像素上)
定义 UIButton.h:21
Color highlightedColor
定义 UIButton.h:23
Color disabledColor
定义 UIButton.h:25
Color pressedColor
定义 UIButton.h:24
Color normalColor
定义 UIButton.h:22