Max OS 0.3
Loading...
Searching...
No Matches
desktop.cpp
Go to the documentation of this file.
1
13#include <gui/desktop.h>
14
15using namespace MaxOS;
16using namespace MaxOS::common;
17using namespace MaxOS::gui;
18using namespace MaxOS::drivers::peripherals;
19
26 : CompositeWidget(0, 0, gc->width(), gc->height()),
28 m_graphics_context(gc),
29 colour(Colour(0xA8, 0xA8, 0xA8)) {
30
31 // Set the mouse m_position to the center of the screen
32 m_mouse_x = gc->width() / 2;
33 m_mouse_y = gc->height() / 2;
34
35 // Draw the initial mouse cursor
37
38 // Draw the desktop
40}
41
42Desktop::~Desktop() = default;
43
50
51 // If there is a widget in focus then send a focus lost event to it
52 if(this->m_focussed_widget != nullptr)
54
55 // Focus the new widget and send a focus event to it
56 this->m_focussed_widget = widget;
58}
59
66
67 // Remove the widget from where ever it already is
69
70 // Add it back in the front
71 m_children.push_front(front_widget);
72}
73
78
79 // Draw the horizontal line
80 for(uint32_t x = m_mouse_x - 3; x <= m_mouse_x + 3; ++x) {
82 }
83
84 // Draw the vertical line
85 for(uint32_t y = m_mouse_y - 3; y <= m_mouse_y + 3; ++y) {
87 }
88}
89
98
99 // Loop through the invalid rectangles
101
102 // Check if the area intersects with the invalid rectangle
104 continue;
105
106 // Get the parts of the area that are covered by the invalid rectangle
108
109 // Invalidate the covered areas
110 for(auto& covered_area : covered_areas)
112
113 // The entire area will be invalidated by now
114 return;
115
116 }
117
118 // Add the area to the invalid areas, store where it was added
120
121 // If the m_position is the last item then the invalidation buffer is full
122 if(vector_position == m_invalid_areas.end()) {
123
124 // Invalidate the entire desktop
125 m_invalid_areas.clear();
127
128 }
129}
130
138
139 // Calculate the rectangle
144
145 // Draw the background, a rectangle the size of the desktop of the given colour
147}
148
155
156 // Check if the new widget is under the mouse
157 bool under_mouse = child_widget->contains_coordinate(m_mouse_x, m_mouse_y);
158
159 // If the mouse is over the widget then send a mouse leave event to the child widget as it is no longer under the mouse
160 if(under_mouse)
162
163 // Add the widget to the desktop
165
166 // If the mouse is over the new widget then send a mouse enter event to the child widget
167 if(under_mouse)
169}
170
177
178 // Check if anything is invalid and needs to be redrawn
179 if(m_invalid_areas.empty())
180 return;
181
182 // Erase the mouse cursor
184
185 // Loop through the invalid areas
186 while(!m_invalid_areas.empty()) {
187
188 // Redraw the m_first_memory_chunk area
190 m_invalid_areas.pop_front();
192
193 }
194
195 // Can now draw the mouse cursor
197
198}
199
206
207 // Invalidate the area
209
210}
211
212
220
221 // Calculate the m_position of the mouse on the desktop
225
226 // Restrain the mouse to the desktop
229
230 // Remove the old cursor from the screen as it will be redrawn in the new m_position
232
233 // If a widget is being dragged then pass the event to it
234 if(m_dragged_widget != nullptr)
236
237 // Handle the mouse moving event (pass it to the widget that the mouse is over)
240
241 // Update the mouse m_position
244
245 // Draw the new cursor
247}
248
255
256 // The widget that handled the event becomes the widget being dragged
258}
259
266
267 // Pass the event to the widget
269
270 // Dragging has stopped
271 m_dragged_widget = nullptr;
272}
273
280
281 // Pass the event to the widget that is in focus
282 if(m_focussed_widget != nullptr)
284}
285
292
293 // Pass the event to the widget that is in focus
294 if(m_focussed_widget != nullptr)
296}
Stores the red, green, blue and alpha values of a colour. Can be parsed from a hex string,...
Definition colour.h:91
Draws pixels to the screen, and handles drawing lines, rectangles and circles.
void invert_pixel(uint32_t x, uint32_t y)
Inverts a pixel.
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
Vector< Rectangle< Type > > subtract(const Rectangle< Type > &)
Definition rectangle.h:142
Type width
The width of the rectangle.
Definition rectangle.h:26
Dynamically stores an array of elements.
Definition vector.h:39
virtual void on_key_up(KeyCode, const KeyboardState &)
Handle the key up event.
Definition keyboard.cpp:36
virtual void on_key_down(KeyCode, const KeyboardState &)
Handle the key down event.
Definition keyboard.cpp:27
Holds the state of the keyboard.
Definition keyboard.h:216
Handles events that are triggered by the mouse driver.
Definition mouse.h:72
virtual void on_mouse_move_event(int8_t x, int8_t y)
Called when the mouse is moved.
Definition mouse.cpp:70
A widget that can contain other widgets.
Definition widget.h:88
void on_mouse_move_widget(uint32_t from_x, uint32_t from_y, uint32_t to_x, uint32_t to_y) override
Passes the event to the child that the mouse is over, also generates a leave/enter event for children...
Definition widget.cpp:472
void on_mouse_button_released(uint32_t x, uint32_t y, uint8_t button) override
Passes the event to the child that the mouse is over. (Event handling should be done by the derived c...
Definition widget.cpp:544
common::Vector< Widget * > m_children
Widgets contained within this composite widget.
Definition widget.h:92
void on_mouse_leave_widget(uint32_t from_x, uint32_t from_y) override
Passes the event to the child that the mouse is over. (Event handling should be done by the derived c...
Definition widget.cpp:443
void add_child(Widget *child) override
Adds a child to the widget.
Definition widget.cpp:403
drivers::peripherals::MouseEventHandler * on_mouse_button_pressed(uint32_t x, uint32_t y, uint8_t button) override
Passes the event to the child that the mouse is over.
Definition widget.cpp:520
void draw(common::GraphicsContext *gc, common::Rectangle< int32_t > &area, common::Vector< Widget * >::iterator start)
Draws a section of the widget and the m_children after a specific child.
Definition widget.cpp:348
void on_mouse_enter_widget(uint32_t to_x, uint32_t to_y) override
Passes the event to the child that the mouse is over. (Event handling should be done by the derived c...
Definition widget.cpp:416
void add_child(Widget *) final
Adds a child widget to the desktop.
Definition desktop.cpp:154
void internal_invalidate(common::Rectangle< int32_t > &area, common::Vector< common::Rectangle< int32_t > >::iterator start, common::Vector< common::Rectangle< int32_t > >::iterator stop)
Goes through the passed areas and invalidates the areas that are covered by the given area.
Definition desktop.cpp:97
void on_mouse_up_event(uint8_t button) final
When the mouse button is released pass the event to the widget that the mouse is over.
Definition desktop.cpp:265
void draw_self(common::GraphicsContext *gc, common::Rectangle< int32_t > &area) final
Draws a certain area of the desktop.
Definition desktop.cpp:137
void on_time(const common::Time &time)
Redraws the desktop when a time event occurs.
Definition desktop.cpp:176
void on_mouse_move_event(int8_t x, int8_t y) final
When the mouse moves on the desktop update the m_position of the mouse and redraw the cursor....
Definition desktop.cpp:219
void on_key_down(drivers::peripherals::KeyCode key_down_code, const drivers::peripherals::KeyboardState &key_down_state) final
When a key is pressed pass the event to the widget that is currently focussed.
Definition desktop.cpp:279
common::Colour colour
The background colour of the desktop (.
Definition desktop.h:47
void on_key_up(drivers::peripherals::KeyCode key_up_code, const drivers::peripherals::KeyboardState &key_up_state) final
When a key is pressed pass the event to the widget that is currently focussed.
Definition desktop.cpp:291
Desktop(common::GraphicsContext *gc)
Creates a new desktop with the given graphics context.
Definition desktop.cpp:25
void invert_mouse_cursor()
Draws the mouse cursor at the current mouse m_position by inverting the pixels (mouse is a plus sign)
Definition desktop.cpp:77
drivers::peripherals::MouseEventHandler * m_dragged_widget
The widget that the mouse is currently dragging.
Definition desktop.h:36
uint32_t m_mouse_y
The vertical position of the mouse cursor.
Definition desktop.h:31
uint32_t m_mouse_x
The horizontal position of the mouse cursor.
Definition desktop.h:30
Widget * m_focussed_widget
The widget that currently is receiving keyboard input and is at the front.
Definition desktop.h:35
void set_focus(Widget *) final
Updates the currently focussed widget to be the given widget.
Definition desktop.cpp:49
void on_mouse_down_event(uint8_t button) final
When the mouse button is pressed pass the event to the widget that the mouse is over.
Definition desktop.cpp:254
common::GraphicsContext * m_graphics_context
Where to draw the desktop to.
Definition desktop.h:33
common::Vector< common::Rectangle< int32_t > > m_invalid_areas
The areas of the desktop that need to be redrawn.
Definition desktop.h:42
A graphical object that can be drawn on the screen.
Definition widget.h:29
virtual void on_focus()
Handles the event when the widget is focussed.
Definition widget.cpp:216
virtual void on_focus_lost()
Handles the event when the widget is unfocused.
Definition widget.cpp:223
void invalidate()
Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.
Definition widget.cpp:55
common::Rectangle< int32_t > position()
Get the position of the widget.
Definition widget.cpp:127
void bring_to_front()
Brings this widget to the front of the screen.
Definition widget.cpp:230
Defines a Desktop class for managing the GUI desktop, handling mouse and keyboard events,...
KeyCode
A mapping of key codes to their values.
Definition keyboard.h:40
Stores the year, month, day, hour, minute and second of a time.
Definition time.h:24