Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::gui::widgets::Button Class Reference

A button widget, can be clicked. More...

#include <button.h>

Inheritance diagram for MaxOS::gui::widgets::Button:
MaxOS::gui::Widget MaxOS::common::EventManager< ButtonEvents > MaxOS::drivers::peripherals::KeyboardEventHandler MaxOS::common::EventHandler< KeyboardEvents >

Public Member Functions

 Button (int32_t left, int32_t top, uint32_t width, uint32_t height, const string &text)
 Construct a new Button object at a specific position and size with text.
 
void draw (common::GraphicsContext *gc, common::Rectangle< int32_t > &area) override
 Draws the button.
 
drivers::peripherals::MouseEventHandleron_mouse_button_pressed (uint32_t x, uint32_t y, uint8_t button) override
 Handles the mouse button pressed event.
 
void on_mouse_button_released (uint32_t x, uint32_t y, uint8_t button) override
 Handles the mouse button released event.
 
- Public Member Functions inherited from MaxOS::gui::Widget
 Widget ()
 Construct a new Widget object.
 
 Widget (int32_t left, int32_t top, uint32_t width, uint32_t height)
 Construct a new Widget object at a specific position and size.
 
void invalidate ()
 Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.
 
virtual void invalidate (common::Rectangle< int32_t > &area)
 Invalidates a specific area of the widget. This forces that part of the screen to be redrawn on the next screen update.
 
virtual void add_child (Widget *child)
 Set the parent of a widget to this widget, making it into a child.
 
virtual common::Coordinates absolute_coordinates (common::Coordinates coordinates)
 Get the absolute coordinates of the widget (relative to the screen)
 
virtual bool contains_coordinate (uint32_t x, uint32_t y)
 Check if the widget contains a specific coordinate.
 
common::Rectangle< int32_tposition ()
 Get the position of the widget.
 
void move (int32_t left, int32_t top)
 Set the position of the widget, and invalidate the old and new positions so they are redrawn.
 
void resize (int32_t width, int32_t height)
 Set the size of the widget, and invalidate the old and new positions so they are redrawn.
 
void focus ()
 Set the current focused widget to be this widget.
 
virtual void on_focus ()
 Handles the event when the widget is focussed.
 
virtual void on_focus_lost ()
 Handles the event when the widget is unfocused.
 
void bring_to_front ()
 Brings this widget to the front of the screen.
 
virtual void on_mouse_enter_widget (uint32_t to_x, uint32_t to_y)
 Handles the event when the mouse is moved on to the widget.
 
virtual void on_mouse_leave_widget (uint32_t from_x, uint32_t from_y)
 Handles the event when the mouse is moved out of the widget.
 
virtual void on_mouse_move_widget (uint32_t from_x, uint32_t from_y, uint32_t to_x, uint32_t to_y)
 Handles the event when the mouse is moved over the widget.
 
- Public Member Functions inherited from MaxOS::drivers::peripherals::KeyboardEventHandler
common::Event< KeyboardEvents > * on_event (common::Event< KeyboardEvents > *) override
 Handle the trigger of an event.
 
virtual void on_key_down (KeyCode, const KeyboardState &)
 Handle the key down event.
 
virtual void on_key_up (KeyCode, const KeyboardState &)
 Handle the key up event.
 
- Public Member Functions inherited from MaxOS::common::EventHandler< KeyboardEvents >
virtual Event< KeyboardEvents > * on_event (Event< KeyboardEvents > *event)
 This function is called when an event is raised.
 
- Public Member Functions inherited from MaxOS::common::EventManager< ButtonEvents >
void connect_event_handler (EventHandler< ButtonEvents > *handler)
 connect an event handler to the event manager if it is not already connected
 
void disconnect_event_handler (EventHandler< ButtonEvents > *handler)
 disconnect an event handler from the event manager if it is connected
 
Vector< Event< ButtonEvents > * > raise_event (Event< ButtonEvents > *event)
 Calls the on_event function of all the event m_handlers connected to the event manager and returns a list of the results.
 

Public Attributes

common::Colour background_colour
 The colour of the button background.
 
common::Colour foreground_colour
 The colour of the button text.
 
common::Colour border_colour
 The colour of the bar around the button.
 
gui::Font font
 The font to use for the button text.
 
string text
 The text to display on the button.
 

Additional Inherited Members

- Protected Member Functions inherited from MaxOS::gui::Widget
virtual void set_focus (Widget *)
 Sets the widget that is currently focussed.
 
virtual void bring_to_front (Widget *)
 Brings a specific widget to the front of the screen.
 
- Protected Attributes inherited from MaxOS::gui::Widget
common::Rectangle< int32_t > m_position
 The position and size of the widget (relative to its parent)
 
Widgetm_parent { nullptr }
 The widget that owns this widget.
 
bool m_valid { false }
 Is the widget drawn to the screen and up to date.
 
uint32_t m_min_width { 5 }
 The smallest m_width the widget can be resized to.
 
uint32_t m_min_height { 5 }
 The smallest m_height the widget can be resized to.
 
uint32_t m_max_width { 0x8FFFFFFF }
 The largest m_width the widget can be resized to.
 
uint32_t m_max_height { 0x8FFFFFFF }
 The largest m_height the widget can be resized to.
 
- Protected Attributes inherited from MaxOS::common::EventManager< ButtonEvents >
Vector< EventHandler< ButtonEvents > * > m_handlers
 

Detailed Description

A button widget, can be clicked.

Definition at line 76 of file button.h.

Constructor & Destructor Documentation

◆ Button()

Button::Button ( int32_t  left,
int32_t  top,
uint32_t  width,
uint32_t  height,
const string text 
)

Construct a new Button object at a specific position and size with text.

Parameters
leftHow many pixels from the left of the parent widget the button is
topHow many pixels from the top of the parent widget the button is
widthHow many pixels wide the button is
heightHow many pixels tall the button is
textThe text to display on the button

Definition at line 71 of file button.cpp.

72: Widget(left, top, width, height),
73 background_colour(Colour(0xFF, 0xFF, 0xFF)),
74 foreground_colour(Colour(0x00, 0x00, 0x00)),
75 border_colour(Colour(0x57, 0x57, 0x57)),
76 font((uint8_t*) AMIGA_FONT),
77 text(text)
78{
79
80}
Stores the red, green, blue and alpha values of a colour. Can be parsed from a hex string,...
Definition colour.h:91
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Widget()
Construct a new Widget object.
Definition widget.cpp:19
common::Colour border_colour
The colour of the bar around the button.
Definition button.h:90
common::Colour background_colour
The colour of the button background.
Definition button.h:88
string text
The text to display on the button.
Definition button.h:92
gui::Font font
The font to use for the button text.
Definition button.h:91
common::Colour foreground_colour
The colour of the button text.
Definition button.h:89

Member Function Documentation

◆ draw()

void Button::draw ( common::GraphicsContext gc,
common::Rectangle< int32_t > &  area 
)
overridevirtual

Draws the button.

Parameters
gcThe graphics context to draw to
areaThe area to draw to

Reimplemented from MaxOS::gui::Widget.

Definition at line 90 of file button.cpp.

90 {
91
92 // Default Draw Operation
94
95 // Get the absolute m_position of the button
98
99 // Get the x and y m_position of the button
100 int32_t x = button_coordinates.first;
101 int32_t y = button_coordinates.second;
102
103 // Draw the background for the button
104 gc->fill_rectangle(x + area.left, y + area.top, x + area.left + area.width,
106
107 // Draw the border
108
109 // Top Border
111
112 // Start in the top left corner of the button and end in the top right corner
113 gc->draw_line(x + area.left, y, x + area.left + area.width - 1, y,
115 }
116
117 // Left Border
119
120 // Start in the top left corner and end in the bottom left corner
121 gc->draw_line(x, y + area.top, x, y + area.top + area.height - 1,
123 }
124
125 // Right Border
127
128 // Start in the top right corner and end in the bottom right corner
129 gc->draw_line(x + area.left, y + button_position.height - 1,
130 x + area.left + area.width - 1,
132 }
133
134 // Bottom Border
136
137 // Start in the bottom left corner and end in the bottom right corner
138 gc->draw_line(x + button_position.width - 1, y + area.top,
139 x + button_position.width - 1,
140 y + area.top + area.height - 1, border_colour);
141 }
142
143 // Draw the text
144 common::Rectangle<int32_t> text_area(area.left - 1, area.top - 1, area.width, area.height);
146 text_area);
147
148}
A pair of two objects.
Definition pair.h:22
Type height
The height of the rectangle.
Definition rectangle.h:27
Type top
The top coordinate of the rectangle.
Definition rectangle.h:25
bool intersects(const Rectangle< Type > &)
Definition rectangle.h:88
Type left
The left coordinate of the rectangle.
Definition rectangle.h:24
Type width
The width of the rectangle.
Definition rectangle.h:26
virtual void draw_text(int32_t x, int32_t y, common::Colour foreground_colour, common::Colour background_colour, common::GraphicsContext *context, string text)
write the entire text to the screen
Definition font.cpp:41
virtual void draw(common::GraphicsContext *gc, common::Rectangle< int32_t > &area)
Draw the widget on the screen.
Definition widget.cpp:48
common::Rectangle< int32_t > position()
Get the position of the widget.
Definition widget.cpp:127
virtual common::Coordinates absolute_coordinates(common::Coordinates coordinates)
Get the absolute coordinates of the widget (relative to the screen)
Definition widget.cpp:98

References MaxOS::gui::Widget::absolute_coordinates(), background_colour, border_colour, MaxOS::gui::Widget::draw(), MaxOS::gui::Font::draw_text(), font, foreground_colour, MaxOS::common::Rectangle< Type >::height, MaxOS::common::Rectangle< Type >::intersects(), MaxOS::common::Rectangle< Type >::left, MaxOS::gui::Widget::position(), text, MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

◆ on_mouse_button_pressed()

MouseEventHandler * Button::on_mouse_button_pressed ( uint32_t  x,
uint32_t  y,
uint8_t  button 
)
overridevirtual

Handles the mouse button pressed event.

Parameters
xThe x m_position of the mouse
yThe y m_position of the mouse
buttonThe button that was pressed
Returns
The mouse event handler

Reimplemented from MaxOS::gui::Widget.

Definition at line 158 of file button.cpp.

158 {
159
160 // Raise the event
162
163 // Change the button colour
164 background_colour = Colour(0x57, 0x57, 0x57);
166
167 // Pass the event on (that it was handled)
168 return Widget::on_mouse_button_pressed(x, y, button);
169}
Vector< Event< ButtonEvents > * > raise_event(Event< ButtonEvents > *event)
Calls the on_event function of all the event m_handlers connected to the event manager and returns a ...
void invalidate()
Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.
Definition widget.cpp:55
virtual drivers::peripherals::MouseEventHandler * on_mouse_button_pressed(uint32_t x, uint32_t y, uint8_t button)
Handles the event when the mouse is pressed on the widget.
Definition widget.cpp:289
Event that is triggered when a button is pressed.
Definition button.h:36

References background_colour, MaxOS::gui::Widget::invalidate(), MaxOS::gui::Widget::on_mouse_button_pressed(), and MaxOS::common::EventManager< ButtonEvents >::raise_event().

◆ on_mouse_button_released()

void Button::on_mouse_button_released ( uint32_t  x,
uint32_t  y,
uint8_t  button 
)
overridevirtual

Handles the mouse button released event.

Parameters
xThe x m_position of the mouse
yThe y m_position of the mouse
buttonThe button that was released

Reimplemented from MaxOS::gui::Widget.

Definition at line 178 of file button.cpp.

178 {
179
180 // Raise the button released event
182
183 // Change the button colour
184 background_colour = Colour(0xFF, 0xFF, 0xFF);
186
187 // Pass the event on (that it was handled)
189}
virtual void on_mouse_button_released(uint32_t x, uint32_t y, uint8_t button)
Handles the event when the mouse is released on the widget.
Definition widget.cpp:308
Event that is triggered when a button is released.
Definition button.h:48

References background_colour, MaxOS::gui::Widget::invalidate(), MaxOS::gui::Widget::on_mouse_button_released(), and MaxOS::common::EventManager< ButtonEvents >::raise_event().

Member Data Documentation

◆ background_colour

common::Colour MaxOS::gui::widgets::Button::background_colour

The colour of the button background.

Definition at line 88 of file button.h.

Referenced by draw(), on_mouse_button_pressed(), and on_mouse_button_released().

◆ border_colour

common::Colour MaxOS::gui::widgets::Button::border_colour

The colour of the bar around the button.

Definition at line 90 of file button.h.

Referenced by draw().

◆ font

gui::Font MaxOS::gui::widgets::Button::font

The font to use for the button text.

Definition at line 91 of file button.h.

Referenced by draw().

◆ foreground_colour

common::Colour MaxOS::gui::widgets::Button::foreground_colour

The colour of the button text.

Definition at line 89 of file button.h.

Referenced by draw().

◆ text

string MaxOS::gui::widgets::Button::text

The text to display on the button.

Definition at line 92 of file button.h.

Referenced by draw().


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