跳到主要内容
版本:最新版 🚧

类:UIButton

UIButton 类表示 UI 系统中的按钮组件。它是 UIComponent 的一个专用子类,支持按下和保持事件的回调、动态颜色和可自定义的大小。

使用这个类在你的 UI 中创建交互式按钮。

该类的源文件位于 os/ui/UIComponents/UIButton.h


Constructor

UIButton

 UIButton() {}

Creates a new button component. Use the setname, setsize, setcolor/setcolorfunc, onpress, and onhold methods to customize the button.

Methods

GetName

virtual string GetName();

Retrieves the name of the button.

Returns:

  • string: The name of the button.

SetName

void SetName(string name);

Sets the name of the button.

Parameters:

  • name (string): The name to assign to the button.

GetColor

virtual Color GetColor();

Gets the current color of the button. If a color function (color_func) is defined, it overrides the static color property.

Returns:

  • Color: The button's color.

SetColor

void SetColor(Color color);

Sets a static color for the button.

Parameters:

  • color (Color): The color to assign to the button.

SetColorFunc

void SetColorFunc(std::function<Color()> color_func);

Assigns a dynamic color function for the button. The function determines the button's color dynamically at runtime.

Parameters:

  • color_func (std::function<Color()>): A function that returns the button's color.

GetSize

virtual Dimension GetSize();

Gets the size of the button.

Returns:

  • Dimension: The dimensions (width and height) of the button.

SetSize

void SetSize(Dimension dimension);

Sets the size of the button.

Parameters:

  • dimension (Dimension): The new size of the button.

PressCallback

virtual bool PressCallback();

Triggers the button's press callback if defined. Override this method to customize the button's press behavior.

Returns:

  • bool: true if a press callback is defined and executed, false otherwise.

OnPress

void OnPress(std::function<void()> press_callback);

Sets a callback function to be executed when the button is pressed.

Parameters:

  • press_callback (std::function<void()>): The function to call on button press.

HoldCallback

virtual bool HoldCallback();

Triggers the button's hold callback if defined.

Returns:

  • bool: true if a hold callback is defined and executed, false otherwise.

OnHold

void OnHold(std::function<void()> hold_callback);

Sets a callback function to be executed when the button is held.

Parameters:

  • hold_callback (std::function<void()>): The function to call on button hold.

Render

virtual bool Render(Point origin);

Renders the button on the screen by filling its area with the assigned color.

Parameters:

  • origin (Point): The top-left corner of the button's rendering area.

Returns:

  • bool: true if rendering was successful.

KeyEvent

virtual bool KeyEvent(Point xy, KeyInfo* keyInfo);

Handles key events on the button. Triggers the press or hold callback based on the event type.

Parameters:

  • xy (Point): The position of the event relative to the button.
  • keyInfo (KeyInfo*): Information about the key event.

Returns:

  • bool: true if the event was handled, false otherwise.

Comments