Max OS 0.3
Loading...
Searching...
No Matches
button.cpp
Go to the documentation of this file.
1
10#include <gui/font/amiga_font.h>
11
12using namespace MaxOS::common;
13using namespace MaxOS::gui;
14using namespace MaxOS::gui::widgets;
15using namespace MaxOS::drivers;
16using namespace MaxOS::drivers::peripherals;
17
18ButtonEventHandler::ButtonEventHandler() = default;
19
20ButtonEventHandler::~ButtonEventHandler() = default;
21
29
30 switch (event->type) {
31
32 case ButtonEvents::PRESSED:
34 break;
35
36 case ButtonEvents::RELEASED:
38 break;
39
40 }
41 return event;
42}
43
52
61
71Button::Button(int32_t left, int32_t top, uint32_t width, uint32_t height, const string& text)
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}
81
82Button::~Button() = default;
83
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
146 text_area);
147
148}
149
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}
170
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}
190
197: Event(ButtonEvents::RELEASED),
198 source(source)
199{
200
201}
202
203ButtonReleasedEvent::~ButtonReleasedEvent() = default;
204
210: Event(ButtonEvents::PRESSED),
211 source(source)
212{
213}
214
215ButtonPressedEvent::~ButtonPressedEvent() = default;
Defines the Amiga system font used in the GUI.
Defines a Button widget for the GUI, including event handling for button presses and releases.
ButtonEvents
The events that a button can trigger.
Definition button.h:27
Stores the red, green, blue and alpha values of a colour. Can be parsed from a hex string,...
Definition colour.h:91
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 ...
Used to store information about an event, has a type and a return value.
Draws pixels to the screen, and handles drawing lines, rectangles and circles.
A pair of two objects.
Definition pair.h:22
Stores the left, top, width and height of a rectangle.
Definition rectangle.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
Handles events that are triggered by the mouse driver.
Definition mouse.h:72
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
A graphical object that can be drawn on the screen.
Definition widget.h:29
void invalidate()
Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.
Definition widget.cpp:55
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 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
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
virtual common::Coordinates absolute_coordinates(common::Coordinates coordinates)
Get the absolute coordinates of the widget (relative to the screen)
Definition widget.cpp:98
common::Event< ButtonEvents > * on_event(common::Event< ButtonEvents > *) override
Handles the button events.
Definition button.cpp:28
virtual void on_button_pressed(Button *source)
Handles the button pressed event.
Definition button.cpp:49
virtual void on_button_released(Button *source)
Handles the button released event.
Definition button.cpp:58
Event that is triggered when a button is pressed.
Definition button.h:36
ButtonPressedEvent(Button *)
Construct a new Button Pressed Event object.
Definition button.cpp:209
Event that is triggered when a button is released.
Definition button.h:48
ButtonReleasedEvent(Button *)
Construct a new Button Released Event object.
Definition button.cpp:196
A button widget, can be clicked.
Definition button.h:76
void draw(common::GraphicsContext *gc, common::Rectangle< int32_t > &area) override
Draws the button.
Definition button.cpp:90
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
drivers::peripherals::MouseEventHandler * on_mouse_button_pressed(uint32_t x, uint32_t y, uint8_t button) override
Handles the mouse button pressed event.
Definition button.cpp:158
gui::Font font
The font to use for the button text.
Definition button.h:91
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.
Definition button.cpp:71
void on_mouse_button_released(uint32_t x, uint32_t y, uint8_t button) override
Handles the mouse button released event.
Definition button.cpp:178
common::Colour foreground_colour
The colour of the button text.
Definition button.h:89