Skip to main content

ToggleButton

A ToggleButton is a Button specialization that swaps the two bitmaps when clicked, such that the previous "pressed" bitmap, now becomes the one displayed when button is not pressed. This can by used to give the effect of a button that can be pressed in and when it is subsequently pressed, it will pop back out.

Inherits from: Button, AbstractButton, Widget, Drawable

Public Functions

voidforceState(bool activeState)
Allows the ToggleButton to be forced into either the pressed state, or the normal state.
boolgetState() const
Gets the state of the ToggleButton as set with forceState.
virtual voidhandleClickEvent(const ClickEvent & event)
Updates the current state of the button.
virtual voidsetBitmaps(const Bitmap & bitmapReleased, const Bitmap & bitmapPressed)
Sets the two bitmaps used by this button.

Protected Attributes

BitmaporiginalPressed
Contains the bitmap that was originally being displayed when button is pressed.

Additional inherited members

Public Functions inherited from Button

Button()
virtual voiddraw(const Rect & invalidatedArea) const
Draw this drawable.
uint8_tgetAlpha() const
Gets the current alpha value of the widget.
BitmapgetCurrentlyDisplayedBitmap() const
Gets currently displayed bitmap.
virtual RectgetSolidRect() const
Get (the largest possible) rectangle that is guaranteed to be solid (opaque).
virtual voidinvalidateContent() const
Tell the framework that the contents of the Drawable needs to be redrawn.
voidsetAlpha(uint8_t newAlpha)
Sets the opacity (alpha value).

Protected Attributes inherited from Button

uint8_talpha
The current alpha value. 255=solid, 0=invisible.
Bitmapdown
The image to display when button is pressed.
Bitmapup
The image to display when button is released (normal state).

Public Functions inherited from AbstractButton

AbstractButton()
Sets this Widget touchable so the user can interact with buttons.
virtual voidexecuteAction()
Executes the previously set action.
virtual boolgetPressedState() const
Function to determine if the AbstractButton is currently pressed.
voidsetAction(GenericCallback< const AbstractButton & > & callback)
Associates an action with the button.

Protected Attributes inherited from AbstractButton

GenericCallback< const AbstractButton & > *action
The callback to be executed when this AbstractButton is clicked.
boolpressed
Is the button pressed or released? True if pressed.

Public Functions inherited from Widget

virtual voidgetLastChild(int16_t x, int16_t y, Drawable ** last)
Since a Widget is only one Drawable, Widget::getLastChild simply yields itself as result, but only if the Widget isVisible and isTouchable.

Public Functions inherited from Drawable

voidcenter()
Centers the Drawable inside its parent.
voidcenterX()
Center the Drawable horizontally inside its parent.
voidcenterY()
Center the Drawable vertically inside its parent.
virtual voidchildGeometryChanged()
This function can be called on parent nodes to signal that the size or position of one or more of its children has changed.
virtual voiddraw(const Rect & invalidatedArea) const =0
Draw this drawable.
Drawable()
Initializes a new instance of the Drawable class.
voiddrawToDynamicBitmap(BitmapId id)
Render the Drawable object into a dynamic bitmap.
voidexpand(int margin =0)
Expands the Drawable to have the same size as its parent with a given margin around the edge.
RectgetAbsoluteRect() const
Helper function for obtaining the rectangle this Drawable covers, expressed in absolute coordinates.
virtual Drawable *getFirstChild()
Function for obtaining the first child of this drawable if any.
int16_tgetHeight() const
Gets the height of this Drawable.
virtual voidgetLastChild(int16_t x, int16_t y, Drawable ** last) =0
Function for obtaining the the last child of this drawable that intersects with the specified point.
Drawable *getNextSibling()
Gets the next sibling node.
Drawable *getParent() const
Returns the parent node.
const Rect &getRect() const
Gets the rectangle this Drawable covers, in coordinates relative to its parent.
virtual RectgetSolidRect() const =0
Get (the largest possible) rectangle that is guaranteed to be solid (opaque).
virtual RectgetSolidRectAbsolute()
Helper function for obtaining the largest solid rect (as implemented by getSolidRect()) expressed in absolute coordinates.
virtual voidgetVisibleRect(Rect & rect) const
Function for finding the visible part of this drawable.
int16_tgetWidth() const
Gets the width of this Drawable.
int16_tgetX() const
Gets the x coordinate of this Drawable, relative to its parent.
int16_tgetY() const
Gets the y coordinate of this Drawable, relative to its parent.
virtual voidhandleDragEvent(const DragEvent & event)
Defines the event handler interface for DragEvents.
virtual voidhandleGestureEvent(const GestureEvent & event)
Defines the event handler interface for GestureEvents.
virtual voidhandleTickEvent()
Called periodically by the framework if the Drawable instance has subscribed to timer ticks.
virtual voidinvalidate() const
Tell the framework that this entire Drawable needs to be redrawn.
virtual voidinvalidateContent() const
Tell the framework that the contents of the Drawable needs to be redrawn.
virtual voidinvalidateRect(Rect & invalidatedArea) const
Request that a region of this drawable is redrawn.
boolisTouchable() const
Gets whether this Drawable receives touch events or not.
boolisVisible() const
Gets whether this Drawable is visible.
virtual voidmoveRelative(int16_t x, int16_t y)
Moves the drawable.
virtual voidmoveTo(int16_t x, int16_t y)
Moves the drawable.
virtual voidsetHeight(int16_t height)
Sets the height of this drawable.
voidsetPosition(const Drawable & drawable)
Sets the position of the Drawable to the same as the given Drawable.
voidsetPosition(int16_t x, int16_t y, int16_t width, int16_t height)
Sets the size and position of this Drawable, relative to its parent.
voidsetTouchable(bool touch)
Controls whether this Drawable receives touch events or not.
voidsetVisible(bool vis)
Controls whether this Drawable should be visible.
virtual voidsetWidth(int16_t width)
Sets the width of this drawable.
voidsetWidthHeight(const Bitmap & bitmap)
Sets the dimensions (width and height) of the Drawable without changing the x and y coordinates).
voidsetWidthHeight(const Drawable & drawable)
Sets the dimensions (width and height) of the Drawable without changing the x and y coordinates).
voidsetWidthHeight(const Rect & rect)
Sets the dimensions (width and height) of the Drawable without changing the x and y coordinates).
voidsetWidthHeight(int16_t width, int16_t height)
Sets the dimensions (width and height) of the Drawable without changing the x and y coordinates).
virtual voidsetX(int16_t x)
Sets the x coordinate of this Drawable, relative to its parent.
voidsetXY(const Drawable & drawable)
Sets the x and y coordinates of this Drawable.
voidsetXY(int16_t x, int16_t y)
Sets the x and y coordinates of this Drawable, relative to its parent.
virtual voidsetY(int16_t y)
Sets the y coordinate of this Drawable, relative to its parent.
virtual voidtranslateRectToAbsolute(Rect & r) const
Helper function for converting a region of this Drawable to absolute coordinates.
virtual ~Drawable()
Finalizes an instance of the Drawable class.

Protected Attributes inherited from Drawable

Drawable *nextSibling
Pointer to the next Drawable.
Drawable *parent
Pointer to this drawable's parent.
Rectrect
The coordinates of this Drawable, relative to its parent.
booltouchable
True if this drawable should receive touch events.
boolvisible
True if this drawable should be drawn.

Public Functions Documentation

forceState

void forceState(boolactiveState)

Allows the ToggleButton to be forced into either the pressed state, or the normal state.

In the pressed state, the Button will always be shown as pressed down (and shown as released when the user presses it). In the normal state, the Button will be show as released or pressed depending on its actual state.

Parameters:
activeStateIf true, swap the images for released and pressed. If false display the Button normally.

getState

bool getState()const

Gets the state of the ToggleButton as set with forceState.

Returns:

True if the button has been toggled, i.e. the pressed state is shown when the button is not pressed.

handleClickEvent

virtual void handleClickEvent(const ClickEvent &event)

Updates the current state of the button.

The state can be either pressed or released, and if the new state is different from the current state, the button is also invalidated to force a redraw.

If the button state is changed from ClickEvent::PRESSED to ClickEvent::RELEASED, the associated action (if any) is also executed.

Parameters:
eventInformation about the click.

Reimplements: touchgfx::AbstractButton::handleClickEvent

setBitmaps

virtual void setBitmaps(const Bitmap &bitmapReleased ,
const Bitmap &bitmapPressed
)

Sets the two bitmaps used by this button.

One bitmap for the released (normal) state and one bitmap for the pressed state. The images are expected to be of the same dimensions, and the Button is resized to the dimensions of the pressed Bitmap.

Parameters:
bitmapReleasedBitmap to use when button is released.
bitmapPressedBitmap to use when button is pressed.
Note

It is assumed that the dimensions of the bitmaps are the same. Unexpected (visual) behavior may be observed if the bitmaps are of different sizes. The user code must call invalidate() in order to update the button on the display.

Reimplements: touchgfx::Button::setBitmaps

Protected Attributes Documentation

originalPressed

Bitmap originalPressed

Contains the bitmap that was originally being displayed when button is pressed.