Skylicht Engine
Loading...
Searching...
No Matches
Skylicht::UI::CUISwitch Class Reference

Toggle switch control. More...

#include <UserInterface/CUISwitch.h>

Inheritance diagram for Skylicht::UI::CUISwitch:
Skylicht::UI::CUIBase

Public Member Functions

 CUISwitch (CUIContainer *container, CGUIElement *element)
 Construct a switch from a root CGUIElement.
 CUISwitch (CUIContainer *container, CGUIElement *element, CGUIElement *bg, CGUIElement *handle, CGUIElement *on)
 Construct a switch using explicit sub-elements.
virtual void onPressed ()
 Called when the element is considered "pressed" (high-level event). Subclasses may trigger OnPressed callback from here.
void setToggle (bool b, bool invokeEvent=true, bool doAnimation=false)
 Set the toggle state programmatically.
bool isToggle ()
 Returns current toggle state.
void setHandleColor (const SColor &on, const SColor &off)
 Set colors used for the handle in on/off states.
CGUIElementgetBackground ()
 Get the background GUI element.
CGUIElementgetHandle ()
 Get the handle GUI element.
Public Member Functions inherited from Skylicht::UI::CUIBase
 CUIBase (CUIContainer *container, CGUIElement *element)
 Constructor.
virtual ~CUIBase ()
 Virtual destructor.
void remove ()
 Remove this UI object from its container and perform cleanup.
virtual void update ()
virtual void setEnable (bool b)
 Enable or disable this UI object.
virtual void setVisible (bool b)
 Set visibility of this UI object.
bool isEnable ()
 Returns true if the object is enabled.
bool isVisible ()
 Returns true if the object is visible.
int getPointerId ()
 Get the pointer ID associated with this UI object.
void setPointerId (int id)
 Set the pointer ID for this UI object.
void resetTouch ()
 Reset pointer internal state (hover/down).
void setContinueGameEvent (bool b)
 Control whether input events continue to the game layer.
bool isContinueGameEvent ()
 Returns true when events are allowed to continue to the game layer.
bool isContinueKeyEvent ()
void setSkipPointerEventWhenDrag (bool b)
 When true pointer events will be skipped while dragging.
bool isPointerHover ()
 Returns true if pointer is hovering this object.
bool isPointerDown ()
 Returns true if pointer is down on this object.
CGUIElementgetElement ()
 Get the underlying CGUIElement pointer.
CCanvasgetCanvas ()
 Retrieve the canvas that the underlying element belongs to.
CUIContainergetContainer ()
 Get parent container.
core::vector3dfgetRectTransform ()
 Returns a pointer to the internal rect transform (4 corners).
void getRectTransform (CGUIElement *element, core::vector3df *t)
virtual void onPointerHover (int pointerId, float pointerX, float pointerY)
 Called when pointer enters or hovers over the element.
virtual void onPointerOut (int pointerId, float pointerX, float pointerY)
 Called when pointer leaves the element.
virtual void onPointerDown (int pointerId, float pointerX, float pointerY)
 Called when pointer is pressed down on the element.
virtual void onPointerUp (int pointerId, float pointerX, float pointerY)
 Called when pointer is released over the element.
virtual void onPointerMove (int pointerId, float pointerX, float pointerY)
 Called when pointer moves while over or dragging the element.
virtual void onFocus ()
 Called when element receives focus.
virtual void onLostFocus ()
 Called when element loses focus.
virtual void onKeyEvent (const SEvent &event)
 Called when a keyboard event is delivered to this element.
void startMotion (EMotionEvent event)
 Start motions associated with a specific motion event.
void stopMotion (EMotionEvent event)
 Stop motions associated with a specific motion event.
bool isMotionPlaying (EMotionEvent event)
 Query whether any motion is currently playing for the given event.
virtual CMotionaddMotion (EMotionEvent event, CMotion *motion)
 Add a CMotion instance to the list for the given event. The motion will be initialized and managed by this object.
virtual CMotionaddMotion (EMotionEvent event, CGUIElement *element, CMotion *motion)
 Add a CMotion instance bound to a specific CGUIElement for the given event.
bool removeMotion (EMotionEvent event, CMotion *motion)
 Remove a motion instance from the given event list.
void removeMotions (EMotionEvent event)
 Remove all motions attached to the specified event.
std::vector< CMotion * > & getMotions (EMotionEvent event)
 Access the motion vector for a specific event.
void convertToUICoordinate (float &pointerX, float &pointerY)
 Convert screen pointer coordinates to this UI object's local UI coordinates.
void convertWorldToLocal (CGUIElement *element, float &x, float &y)
 Convert world coordinates to the local coordinate space of a CGUIElement.
void convertLocalToWorld (CGUIElement *element, float &x, float &y)
 Convert local coordinates of a CGUIElement to world coordinates.
virtual bool acceptDragFocus ()
 Whether this UI element should accept drag-focus when the pointer is already down and the user drags over it.
void setSoundId (int slot, int soundId)
 Set the sound id associated with this UI object.
int getSoundId (int slot)
 Get the sound id associated with this UI object.
void setUserData (void *data)
 Attach an application-defined pointer to this UI object.
void * getUserData ()
 Retrieve the pointer previously set via setUserData.

Public Attributes

std::function< void(CUISwitch *, bool)> OnChanged
 Callback invoked when the toggle state changes.
Public Attributes inherited from Skylicht::UI::CUIBase
std::function< void(int, float, float)> OnPointerHover
std::function< void(int, float, float)> OnPointerOut
std::function< void(int, float, float)> OnPointerDown
std::function< void(int, float, float)> OnPointerUp
std::function< void(int, float, float, bool)> OnPointerMove
std::function< void(CUIBase *)> OnPressed
std::function< void(CUIBase *)> OnFocus
std::function< void(CUIBase *)> OnLostFocus
std::function< void(CUIBase *, const SEvent &)> OnKeyEvent
std::function< void(CUIBase *, EMotionEvent)> OnMotionFinish [(int) EMotionEvent::NumEvent]

Protected Member Functions

void stopTween ()
void playTween ()

Protected Attributes

CGUIElementm_background
CGUIElementm_on
CGUIElementm_handle
bool m_toggleStatus
core::vector3df m_onPosition
core::vector3df m_offPosition
CTweenm_tween
SColor m_onColor
SColor m_offColor
Protected Attributes inherited from Skylicht::UI::CUIBase
CUIContainerm_container
CGUIElementm_element
core::vector3df m_rectTransform [4]
std::vector< CMotion * > m_motions [(int) EMotionEvent::NumEvent]
bool m_enable
bool m_visible
bool m_isPointerHover
bool m_isPointerDown
bool m_skipPointerEventWhenDrag
bool m_continueGameEvent
bool m_continueKeyEvent
int m_pointerId
float m_pointerDownX
float m_pointerDownY
int m_soundId [4]
void * m_userData

Detailed Description

Toggle switch control.

CUISwitch is a UI control that represents a two-state toggle (on/off). It composes three GUI elements:

  • m_background : optional background track
  • m_handle : the movable handle that represents current state
  • m_on : an alternate handle (or visual) used for the "on" state

Visual transitions between states can be animated using an internal tween (m_tween). Colors for the handle in each state are configurable via setHandleColor().

The control exposes the OnChanged callback which is invoked whenever the toggle value changes (either from setToggle() when invokeEvent is true, or from user interaction via onPressed()).

See also
CUIBase

Constructor & Destructor Documentation

◆ CUISwitch() [1/2]

Skylicht::UI::CUISwitch::CUISwitch ( CUIContainer * container,
CGUIElement * element )

Construct a switch from a root CGUIElement.

Parameters
containerParent UI container that owns this control.
elementRoot CGUIElement which may contain children: "Background", "HandleOff", "HandleOn".

The constructor finds sub-elements by path and initializes m_offPosition / m_onPosition. m_on is hidden by default.

◆ CUISwitch() [2/2]

Skylicht::UI::CUISwitch::CUISwitch ( CUIContainer * container,
CGUIElement * element,
CGUIElement * bg,
CGUIElement * handle,
CGUIElement * on )

Construct a switch using explicit sub-elements.

Parameters
containerParent UI container that owns this control.
elementRoot CGUIElement for the control.
bgBackground element to use for the track.
handleHandle element representing the movable knob.
onOptional alternate handle/visual for "on" state.

Member Function Documentation

◆ onPressed()

virtual void Skylicht::UI::CUISwitch::onPressed ( )
virtual

Called when the element is considered "pressed" (high-level event). Subclasses may trigger OnPressed callback from here.

Reimplemented from Skylicht::UI::CUIBase.

◆ playTween()

void Skylicht::UI::CUISwitch::playTween ( )
protected

Create and start a tween to animate handle between positions.

◆ setHandleColor()

void Skylicht::UI::CUISwitch::setHandleColor ( const SColor & on,
const SColor & off )
inline

Set colors used for the handle in on/off states.

Parameters
onColor to use when the switch is on.
offColor to use when the switch is off.

◆ setToggle()

void Skylicht::UI::CUISwitch::setToggle ( bool b,
bool invokeEvent = true,
bool doAnimation = false )

Set the toggle state programmatically.

Parameters
bNew state: true = on, false = off.
invokeEventIf true, OnChanged will be invoked after state change.
doAnimationIf true, state change will be animated using tweening; otherwise the handle position and color are set immediately.

The function updates m_toggleStatus, moves the handle to the correct position and applies the corresponding handle color. If animation is requested, the internal tween will animate the handle between positions.

◆ stopTween()

void Skylicht::UI::CUISwitch::stopTween ( )
protected

Stop and release the current tween if any.

Member Data Documentation

◆ OnChanged

std::function<void(CUISwitch*, bool)> Skylicht::UI::CUISwitch::OnChanged

Callback invoked when the toggle state changes.

Parameters
senderPointer to the sender CUISwitch.
stateNew toggle state: true = on, false = off.

This callback is invoked from setToggle() when invokeEvent is true and from onPressed() after the user toggles the switch.


The documentation for this class was generated from the following file: