ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
Toggle main menu visibility
载入中...
搜索中...
未找到
UIButton.h
浏览该文件的文档.
1
#pragma once
2
#include "
../Core/Core.h
"
3
#include "
UIRendererComponent.h
"
4
#include <functional>
5
6
namespace
Shit
{
15
class
SHIT_API
SHIT_REFLECT
(BlackList)
UIButton
:
public
UIRendererComponent
{
16
SHIT_REFLECT_BODY
(
UIButton
)
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 调用) ---
32
void
onPointerEnter
();
33
void
onPointerExit
();
34
void
onPointerDown
();
35
void
onPointerUp
();
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
50
void
resetPressed
();
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
}
Core.h
SHIT_API
#define SHIT_API
定义
Core.h:14
SHIT_REFLECT_BODY
#define SHIT_REFLECT_BODY(Type)
定义
Macros.h:62
SHIT_REFLECT
#define SHIT_REFLECT(Mode)
定义
Macros.h:59
SHIT_META
#define SHIT_META(...)
定义
Macros.h:68
UIRendererComponent.h
Shit::UIButton::onPointerUp
void onPointerUp()
Shit::UIButton::onPointerExit
void onPointerExit()
Shit::UIButton::onPointerEnter
void onPointerEnter()
Shit::UIButton::resetPressed
void resetPressed()
复位残留的按下状态(按钮在按下后离开 visible 未收到 onPointerUp 时使用),不触发 onClick
Shit::UIButton::UIButton
UIButton()=default
Shit::UIButton::wasPointerDown
bool wasPointerDown() const
定义
UIButton.h:42
Shit::UIButton::onRender
void onRender(const SDL_FRect &) override
子类实现:在给定屏幕矩形内绘制自身(通过 Renderer 静态 API 绘制)
Shit::UIButton::State
State
定义
UIButton.h:18
Shit::UIButton::State::Normal
@ Normal
定义
UIButton.h:18
Shit::UIButton::setInteractable
void setInteractable(bool interactable)
Shit::UIButton::~UIButton
~UIButton() override=default
Shit::UIButton::getState
State getState() const
定义
UIButton.h:41
Shit::UIButton::isInteractable
bool isInteractable() const
定义
UIButton.h:38
Shit::UIButton::setColors
void setColors(const ColorBlock &colors)
定义
UIButton.h:45
Shit::UIButton::setOnClick
void setOnClick(std::function< void()> callback)
定义
UIButton.h:47
Shit::UIButton::getColors
const ColorBlock & getColors() const
定义
UIButton.h:44
Shit::UIButton::onPointerDown
void onPointerDown()
Shit::UIRendererComponent::UIRendererComponent
UIRendererComponent()
Shit
定义
AudioPlayer.h:10
Shit::Color
RGBA 颜色(每通道 8 位)
定义
Core.h:26
Shit::UIButton::ColorBlock
ColorTint 过渡的四种状态颜色(乘到 UIImage 像素上)
定义
UIButton.h:21
Shit::UIButton::ColorBlock::highlightedColor
Color highlightedColor
定义
UIButton.h:23
Shit::UIButton::ColorBlock::disabledColor
Color disabledColor
定义
UIButton.h:25
Shit::UIButton::ColorBlock::pressedColor
Color pressedColor
定义
UIButton.h:24
Shit::UIButton::ColorBlock::normalColor
Color normalColor
定义
UIButton.h:22
include
ShitEngine
UI
UIButton.h
制作者
1.17.0