ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
UIButton类 参考

UI 按钮控件,基于鼠标 Raycasting 触发交互 更多...

#include <UIButton.h>

类 UIButton 继承关系图:
UIRendererComponent Component

struct  ColorBlock
 ColorTint 过渡的四种状态颜色(乘到 UIImage 像素上) 更多...

Public 类型

enum class  State {
  Normal , Highlighted ,
  Pressed , Disabled
}

Public 成员函数

 UIButton ()=default
 ~UIButton () override=default
void onPointerEnter ()
void onPointerExit ()
void onPointerDown ()
void onPointerUp ()
bool isInteractable () const
void setInteractable (bool interactable)
State getState () const
bool wasPointerDown () const
const ColorBlockgetColors () const
void setColors (const ColorBlock &colors)
void setOnClick (std::function< void()> callback)
void resetPressed ()
 复位残留的按下状态(按钮在按下后离开 visible 未收到 onPointerUp 时使用),不触发 onClick
Public 成员函数 继承自 UIRendererComponent
 UIRendererComponent ()
 ~UIRendererComponent () override=default
void onAttach () override
void onDetach () override
void onDestroy () override
virtual bool containsPoint (const SDL_FRect &screenRect, const Vector2 &point) const
 点击命中检测:默认矩形内即命中,子类可覆写为更精确的形状
int getZIndex () const
void setZIndex (int zIndex)
bool isVisible () const
void setVisible (bool visible)
Public 成员函数 继承自 Component
 Component ()
virtual ~Component ()=default
virtual void onCreate ()
virtual void onAfterDeserialize ()
 反序列化完成后回调(Prefab::apply 逐组件调用一次,字段已写入)。 用于重建"直写字段绕过了 setter、无法自动触发的引擎状态"(如精灵纹理加载)。
 Component (const Component &)=delete
Componentoperator= (const Component &)=delete
 Component (Component &&)=delete
Componentoperator= (Component &&)=delete
GameObjectgetOwner () const
bool isRegistered () const

Protected 成员函数

void onRender (const SDL_FRect &) override
 子类实现:在给定屏幕矩形内绘制自身(通过 Renderer 静态 API 绘制)
Protected 成员函数 继承自 UIRendererComponent
void requestSort ()
 components变更后通知系统重排(实例:zIndex 变化)
Protected 成员函数 继承自 Component
void setRegistered (bool registered)

额外继承的成员函数

静态 Protected 成员函数 继承自 UIRendererComponent
static SDL_Color toSDLColor (const Color &color)
 Color → SDL_Color 边界转换(供 UIText / UITextInput 系列传给 SDL_ttf 用)
Protected 属性 继承自 UIRendererComponent
int m_zIndex = 0
 渲染层级(值越大越靠上)
bool m_isVisible = true
 是否参与渲染与命中
Protected 属性 继承自 Component
GameObjectm_owner = nullptr
bool m_isRegistered = false

详细描述

UI 按钮控件,基于鼠标 Raycasting 触发交互

UIRenderSystem 的命中检测驱动状态机:Normal → Highlighted (悬停) → Pressed (按下)。 释放且仍在按钮内时触发 onClick。不支持交互时进入 Disabled 状态。 采用 ColorTint 过渡:状态变化时改写同 GameObjectUIImage 的 color(若存在)。 UI 渲染交给同 GameObjectUIImage / 自定义控件,本控件不直接绘制。

成员枚举类型说明

◆ State

enum class State
strong
枚举值
Normal 
Highlighted 
Pressed 
Disabled 

构造及析构函数说明

◆ UIButton()

UIButton ( )
default

◆ ~UIButton()

~UIButton ( )
overridedefault

成员函数说明

◆ getColors()

const ColorBlock & getColors ( ) const
inline

◆ getState()

State getState ( ) const
inline

◆ isInteractable()

bool isInteractable ( ) const
inline

◆ onPointerDown()

void onPointerDown ( )

◆ onPointerEnter()

void onPointerEnter ( )

◆ onPointerExit()

void onPointerExit ( )

◆ onPointerUp()

void onPointerUp ( )

◆ onRender()

void onRender ( const SDL_FRect & screenRect)
overrideprotectedvirtual

子类实现:在给定屏幕矩形内绘制自身(通过 Renderer 静态 API 绘制)

实现了 UIRendererComponent.

◆ resetPressed()

void resetPressed ( )

复位残留的按下状态(按钮在按下后离开 visible 未收到 onPointerUp 时使用),不触发 onClick

◆ setColors()

void setColors ( const ColorBlock & colors)
inline

◆ setInteractable()

void setInteractable ( bool interactable)

◆ setOnClick()

void setOnClick ( std::function< void()> callback)
inline

◆ wasPointerDown()

bool wasPointerDown ( ) const
inline

该类的文档由以下文件生成: