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

The desktop that contains all the windows, handles the drawing of the screen and the mouse on every tick. More...

#include <desktop.h>

Inheritance diagram for MaxOS::gui::Desktop:
MaxOS::gui::CompositeWidget MaxOS::drivers::peripherals::MouseEventHandler MaxOS::gui::Widget MaxOS::common::EventHandler< MouseEvents > MaxOS::drivers::peripherals::KeyboardEventHandler MaxOS::common::EventHandler< KeyboardEvents >

Public Member Functions

 Desktop (common::GraphicsContext *gc)
 Creates a new desktop with the given graphics context.
 
void add_child (Widget *) final
 Adds a child widget to the desktop.
 
void on_time (const common::Time &time)
 Redraws the desktop when a time event occurs.
 
void invalidate (common::Rectangle< int32_t > &area) final
 Invalidate the given area of the desktop.
 
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. Pass the event to the widget that the mouse is over.
 
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.
 
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.
 
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.
 
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.
 
- Public Member Functions inherited from MaxOS::gui::CompositeWidget
 CompositeWidget (int32_t left, int32_t top, uint32_t width, uint32_t height)
 Construct a new Composite Widget object at a specific position and size.
 
void draw (common::GraphicsContext *gc, common::Rectangle< int32_t > &area) override
 Draws a section of the widget and its m_children.
 
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 class)
 
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 class)
 
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.
 
drivers::peripherals::MouseEventHandleron_mouse_button_pressed (uint32_t x, uint32_t y, uint8_t button) override
 Passes the event to the child that the mouse is over.
 
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 class)
 
- 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 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.
 
- Public Member Functions inherited from MaxOS::drivers::peripherals::KeyboardEventHandler
common::Event< KeyboardEvents > * on_event (common::Event< KeyboardEvents > *) override
 Handle the trigger of an 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::drivers::peripherals::MouseEventHandler
common::Event< MouseEvents > * on_event (common::Event< MouseEvents > *) override
 Handles the triggered event and calls the appropriate function.
 
- Public Member Functions inherited from MaxOS::common::EventHandler< MouseEvents >
virtual Event< MouseEvents > * on_event (Event< MouseEvents > *event)
 This function is called when an event is raised.
 

Public Attributes

common::Colour colour
 The background colour of the desktop (.
 

Protected Member Functions

void set_focus (Widget *) final
 Updates the currently focussed widget to be the given widget.
 
void bring_to_front (Widget *) final
 Brings the given widget to the front of the desktop.
 
void invert_mouse_cursor ()
 Draws the mouse cursor at the current mouse m_position by inverting the pixels (mouse is a plus sign)
 
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.
 
void draw_self (common::GraphicsContext *gc, common::Rectangle< int32_t > &area) final
 Draws a certain area of the desktop.
 
- Protected Member Functions inherited from MaxOS::gui::CompositeWidget
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.
 

Protected Attributes

uint32_t m_mouse_x
 The horizontal position of the mouse cursor.
 
uint32_t m_mouse_y
 The vertical position of the mouse cursor.
 
common::GraphicsContextm_graphics_context
 Where to draw the desktop to.
 
Widgetm_focussed_widget { nullptr }
 The widget that currently is receiving keyboard input and is at the front.
 
drivers::peripherals::MouseEventHandlerm_dragged_widget { nullptr }
 The widget that the mouse is currently dragging.
 
common::Vector< common::Rectangle< int32_t > > m_invalid_areas
 The areas of the desktop that need to be redrawn.
 
- Protected Attributes inherited from MaxOS::gui::CompositeWidget
common::Vector< Widget * > m_children
 Widgets contained within this composite widget.
 
- 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.
 

Detailed Description

The desktop that contains all the windows, handles the drawing of the screen and the mouse on every tick.

Todo:
It is not a good idea to hardcode the mouse into the desktop as a tablet or touch screen device won't have a mouse cursor

Definition at line 27 of file desktop.h.

Constructor & Destructor Documentation

◆ Desktop()

Desktop::Desktop ( common::GraphicsContext gc)
explicit

Creates a new desktop with the given graphics context.

Parameters
gcThe graphics context to use

Definition at line 25 of file desktop.cpp.

26 : CompositeWidget(0, 0, gc->width(), gc->height()),
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}
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
Type height
The height of the rectangle.
Definition rectangle.h:27
Type width
The width of the rectangle.
Definition rectangle.h:26
Handles events that are triggered by the mouse driver.
Definition mouse.h:72
A widget that can contain other widgets.
Definition widget.h:88
common::Colour colour
The background colour of the desktop (.
Definition desktop.h:47
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
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
common::GraphicsContext * m_graphics_context
Where to draw the desktop to.
Definition desktop.h:33
void invalidate()
Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.
Definition widget.cpp:55

References MaxOS::common::Rectangle< Type >::height, MaxOS::gui::Widget::invalidate(), invert_mouse_cursor(), m_mouse_x, m_mouse_y, and MaxOS::common::Rectangle< Type >::width.

Member Function Documentation

◆ add_child()

void Desktop::add_child ( Widget child_widget)
finalvirtual

Adds a child widget to the desktop.

Parameters
child_widgetThe widget to add

Reimplemented from MaxOS::gui::CompositeWidget.

Definition at line 154 of file desktop.cpp.

154 {
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}
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
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

References MaxOS::gui::CompositeWidget::add_child(), m_mouse_x, m_mouse_y, MaxOS::gui::CompositeWidget::on_mouse_enter_widget(), and MaxOS::gui::CompositeWidget::on_mouse_leave_widget().

◆ bring_to_front()

void Desktop::bring_to_front ( Widget front_widget)
finalprotectedvirtual

Brings the given widget to the front of the desktop.

Parameters
front_widgetThe widget to bring to the front

Reimplemented from MaxOS::gui::Widget.

Definition at line 65 of file desktop.cpp.

65 {
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}
common::Vector< Widget * > m_children
Widgets contained within this composite widget.
Definition widget.h:92

References MaxOS::gui::CompositeWidget::m_children.

◆ draw_self()

void Desktop::draw_self ( common::GraphicsContext gc,
common::Rectangle< int32_t > &  area 
)
finalprotectedvirtual

Draws a certain area of the desktop.

Parameters
gcThe graphics context to draw with
areaThe area to draw

Reimplemented from MaxOS::gui::CompositeWidget.

Definition at line 137 of file desktop.cpp.

137 {
138
139 // Calculate the rectangle
144
145 // Draw the background, a rectangle the size of the desktop of the given colour
147}
Type top
The top coordinate of the rectangle.
Definition rectangle.h:25
Type left
The left coordinate of the rectangle.
Definition rectangle.h:24

References colour, MaxOS::common::Rectangle< Type >::height, MaxOS::common::Rectangle< Type >::left, MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

◆ internal_invalidate()

void Desktop::internal_invalidate ( common::Rectangle< int32_t > &  area,
common::Vector< common::Rectangle< int32_t > >::iterator  start,
common::Vector< common::Rectangle< int32_t > >::iterator  stop 
)
protected

Goes through the passed areas and invalidates the areas that are covered by the given area.

Parameters
areaThe area that is now invalid
startThe start of the invalid areas
stopThe end of the invalid areas

Definition at line 97 of file desktop.cpp.

97 {
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}
bool intersects(const Rectangle< Type > &)
Definition rectangle.h:88
Vector< Rectangle< Type > > subtract(const Rectangle< Type > &)
Definition rectangle.h:142
Dynamically stores an array of elements.
Definition vector.h:39
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
common::Vector< common::Rectangle< int32_t > > m_invalid_areas
The areas of the desktop that need to be redrawn.
Definition desktop.h:42

References internal_invalidate(), MaxOS::common::Rectangle< Type >::intersects(), MaxOS::gui::Widget::invalidate(), m_invalid_areas, and MaxOS::common::Rectangle< Type >::subtract().

Referenced by internal_invalidate(), and invalidate().

◆ invalidate()

void Desktop::invalidate ( common::Rectangle< int32_t > &  area)
finalvirtual

Invalidate the given area of the desktop.

Parameters
areaThe area that is now invalid

Reimplemented from MaxOS::gui::Widget.

Definition at line 205 of file desktop.cpp.

205 {
206
207 // Invalidate the area
209
210}

References internal_invalidate(), and m_invalid_areas.

◆ invert_mouse_cursor()

void Desktop::invert_mouse_cursor ( )
protected

Draws the mouse cursor at the current mouse m_position by inverting the pixels (mouse is a plus sign)

Definition at line 77 of file desktop.cpp.

77 {
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}
void invert_pixel(uint32_t x, uint32_t y)
Inverts a pixel.

References MaxOS::common::GraphicsContext::invert_pixel(), m_graphics_context, m_mouse_x, and m_mouse_y.

Referenced by Desktop(), on_mouse_move_event(), and on_time().

◆ on_key_down()

void Desktop::on_key_down ( drivers::peripherals::KeyCode  key_down_code,
const drivers::peripherals::KeyboardState key_down_state 
)
finalvirtual

When a key is pressed pass the event to the widget that is currently focussed.

Parameters
key_down_codeThe key that was pressed
key_down_stateThe state of the keyboard

Reimplemented from MaxOS::drivers::peripherals::KeyboardEventHandler.

Definition at line 279 of file desktop.cpp.

279 {
280
281 // Pass the event to the widget that is in focus
282 if(m_focussed_widget != nullptr)
284}
virtual void on_key_down(KeyCode, const KeyboardState &)
Handle the key down event.
Definition keyboard.cpp:27
Widget * m_focussed_widget
The widget that currently is receiving keyboard input and is at the front.
Definition desktop.h:35

References m_focussed_widget, and MaxOS::drivers::peripherals::KeyboardEventHandler::on_key_down().

◆ on_key_up()

void Desktop::on_key_up ( drivers::peripherals::KeyCode  key_up_code,
const drivers::peripherals::KeyboardState key_up_state 
)
finalvirtual

When a key is pressed pass the event to the widget that is currently focussed.

Parameters
key_up_codeThe key that was pressed
key_up_stateThe state of the keyboard

Reimplemented from MaxOS::drivers::peripherals::KeyboardEventHandler.

Definition at line 291 of file desktop.cpp.

291 {
292
293 // Pass the event to the widget that is in focus
294 if(m_focussed_widget != nullptr)
296}
virtual void on_key_up(KeyCode, const KeyboardState &)
Handle the key up event.
Definition keyboard.cpp:36

References m_focussed_widget, and MaxOS::drivers::peripherals::KeyboardEventHandler::on_key_up().

◆ on_mouse_down_event()

void Desktop::on_mouse_down_event ( uint8_t  button)
finalvirtual

When the mouse button is pressed pass the event to the widget that the mouse is over.

Parameters
buttonThe button that was pressed

Reimplemented from MaxOS::drivers::peripherals::MouseEventHandler.

Definition at line 254 of file desktop.cpp.

254 {
255
256 // The widget that handled the event becomes the widget being dragged
258}
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
drivers::peripherals::MouseEventHandler * m_dragged_widget
The widget that the mouse is currently dragging.
Definition desktop.h:36

References m_dragged_widget, m_mouse_x, m_mouse_y, and MaxOS::gui::CompositeWidget::on_mouse_button_pressed().

◆ on_mouse_move_event()

void Desktop::on_mouse_move_event ( int8_t  x,
int8_t  y 
)
finalvirtual

When the mouse moves on the desktop update the m_position of the mouse and redraw the cursor. Pass the event to the widget that the mouse is over.

Parameters
xThe x m_position of the mouse
yThe y m_position of the mouse

Reimplemented from MaxOS::drivers::peripherals::MouseEventHandler.

Definition at line 219 of file desktop.cpp.

219 {
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}
virtual void on_mouse_move_event(int8_t x, int8_t y)
Called when the mouse is moved.
Definition mouse.cpp:70
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
common::Rectangle< int32_t > position()
Get the position of the widget.
Definition widget.cpp:127

References MaxOS::common::Rectangle< Type >::height, invert_mouse_cursor(), m_dragged_widget, m_mouse_x, m_mouse_y, MaxOS::drivers::peripherals::MouseEventHandler::on_mouse_move_event(), MaxOS::gui::CompositeWidget::on_mouse_move_widget(), MaxOS::gui::Widget::position(), and MaxOS::common::Rectangle< Type >::width.

◆ on_mouse_up_event()

void Desktop::on_mouse_up_event ( uint8_t  button)
finalvirtual

When the mouse button is released pass the event to the widget that the mouse is over.

Parameters
buttonThe button that was released

Reimplemented from MaxOS::drivers::peripherals::MouseEventHandler.

Definition at line 265 of file desktop.cpp.

265 {
266
267 // Pass the event to the widget
269
270 // Dragging has stopped
271 m_dragged_widget = nullptr;
272}
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

References m_dragged_widget, m_mouse_x, m_mouse_y, and MaxOS::gui::CompositeWidget::on_mouse_button_released().

◆ on_time()

void Desktop::on_time ( const common::Time time)

Redraws the desktop when a time event occurs.

Parameters
timeThe time when the event occurred

Definition at line 176 of file desktop.cpp.

176 {
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}
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

References MaxOS::gui::CompositeWidget::draw(), invert_mouse_cursor(), m_graphics_context, and m_invalid_areas.

◆ set_focus()

void Desktop::set_focus ( Widget widget)
finalprotectedvirtual

Updates the currently focussed widget to be the given widget.

Parameters
widgetThe widget that is now in focus

Reimplemented from MaxOS::gui::Widget.

Definition at line 49 of file desktop.cpp.

49 {
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}
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

References m_focussed_widget, MaxOS::gui::Widget::on_focus(), and MaxOS::gui::Widget::on_focus_lost().

Member Data Documentation

◆ colour

common::Colour MaxOS::gui::Desktop::colour

The background colour of the desktop (.

Todo:
: replace with image, make priv)

Definition at line 47 of file desktop.h.

Referenced by draw_self().

◆ m_dragged_widget

drivers::peripherals::MouseEventHandler* MaxOS::gui::Desktop::m_dragged_widget { nullptr }
protected

The widget that the mouse is currently dragging.

Definition at line 36 of file desktop.h.

36{ nullptr };

Referenced by on_mouse_down_event(), on_mouse_move_event(), and on_mouse_up_event().

◆ m_focussed_widget

Widget* MaxOS::gui::Desktop::m_focussed_widget { nullptr }
protected

The widget that currently is receiving keyboard input and is at the front.

Definition at line 35 of file desktop.h.

35{ nullptr };

Referenced by on_key_down(), on_key_up(), and set_focus().

◆ m_graphics_context

common::GraphicsContext* MaxOS::gui::Desktop::m_graphics_context
protected

Where to draw the desktop to.

Definition at line 33 of file desktop.h.

Referenced by invert_mouse_cursor(), and on_time().

◆ m_invalid_areas

common::Vector<common::Rectangle<int32_t> > MaxOS::gui::Desktop::m_invalid_areas
protected

The areas of the desktop that need to be redrawn.

Definition at line 42 of file desktop.h.

Referenced by internal_invalidate(), invalidate(), and on_time().

◆ m_mouse_x

uint32_t MaxOS::gui::Desktop::m_mouse_x
protected

The horizontal position of the mouse cursor.

Definition at line 30 of file desktop.h.

Referenced by add_child(), Desktop(), invert_mouse_cursor(), on_mouse_down_event(), on_mouse_move_event(), and on_mouse_up_event().

◆ m_mouse_y

uint32_t MaxOS::gui::Desktop::m_mouse_y
protected

The vertical position of the mouse cursor.

Definition at line 31 of file desktop.h.

Referenced by add_child(), Desktop(), invert_mouse_cursor(), on_mouse_down_event(), on_mouse_move_event(), and on_mouse_up_event().


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