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

A box that can be used to input text. More...

#include <inputbox.h>

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

Public Member Functions

 InputBox (int32_t left, int32_t top, uint32_t width, uint32_t height)
 Construct a new Input Box object at a specific position and size.
 
 InputBox (int32_t left, int32_t top, uint32_t width, uint32_t height, const string &text)
 Construct a new Input Box object at a specific position and size with initial text.
 
void draw (common::GraphicsContext *gc, common::Rectangle< int32_t > &area) override
 Draw an area of the input box onto a graphics context.
 
void on_focus () override
 Update the border when the input box is focussed.
 
void on_focus_lost () override
 Reset to the original input box style when it is no longer focussed.
 
void on_key_down (drivers::peripherals::KeyCode key_down_code, const drivers::peripherals::KeyboardState &key_down_state) override
 Handles a keypress event by updating the rendered text in the input box.
 
void update_text (const string &)
 Update the text in the input box.
 
string text ()
 Get the text currently in the input box.
 
- 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.
 
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.
 
virtual drivers::peripherals::MouseEventHandleron_mouse_button_pressed (uint32_t x, uint32_t y, uint8_t button)
 Handles the event when the mouse is pressed on the widget.
 
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.
 
- 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_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< InputBoxEvents >
void connect_event_handler (EventHandler< InputBoxEvents > *handler)
 connect an event handler to the event manager if it is not already connected
 
void disconnect_event_handler (EventHandler< InputBoxEvents > *handler)
 disconnect an event handler from the event manager if it is connected
 
Vector< Event< InputBoxEvents > * > raise_event (Event< InputBoxEvents > *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 background colour of the input box.
 
common::Colour foreground_colour
 The colour of the text in the input box.
 
common::Colour border_colour
 The colour of the bar around the input box.
 
gui::Font font
 The font to use for the input box text.
 
uint32_t cursor_position { 0 }
 How many characters into the text the cursor is.
 

Protected Attributes

string m_widget_text = " "
 The text in the input box.
 
- 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< InputBoxEvents >
Vector< EventHandler< InputBoxEvents > * > m_handlers
 

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.
 

Detailed Description

A box that can be used to input text.

Definition at line 58 of file inputbox.h.

Constructor & Destructor Documentation

◆ InputBox() [1/2]

InputBox::InputBox ( int32_t  left,
int32_t  top,
uint32_t  width,
uint32_t  height 
)

Construct a new Input Box object at a specific position and size.

Parameters
leftHow many pixels from the left of the parent widget the input box is
topHow many pixels from the top of the parent widget the input box is
widthHow many pixels wide the input box is
heightHow many pixels tall the input box is

Definition at line 56 of file inputbox.cpp.

57: Widget(left, top, width, height),
58 background_colour(Colour(0xFF, 0xFF, 0xFF)),
59 foreground_colour(Colour(0x00, 0x00, 0x00)),
60 border_colour(Colour(0x57, 0x57, 0x57)),
61 font((uint8_t*) AMIGA_FONT)
62{
63
64}
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 background_colour
The background colour of the input box.
Definition inputbox.h:80
common::Colour foreground_colour
The colour of the text in the input box.
Definition inputbox.h:81
common::Colour border_colour
The colour of the bar around the input box.
Definition inputbox.h:82
gui::Font font
The font to use for the input box text.
Definition inputbox.h:83

◆ InputBox() [2/2]

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

Construct a new Input Box object at a specific position and size with initial text.

Parameters
leftThe left position of the input box
topThe top position of the input box
widthThe width of the input box
heightThe height of the input box
textThe initial text in the input box

Definition at line 75 of file inputbox.cpp.

76: Widget(left, top, width, height),
77 background_colour(Colour(0xFF, 0xFF, 0xFF)),
78 foreground_colour(Colour(0x00, 0x00, 0x00)),
79 border_colour(Colour(0x57, 0x57, 0x57)),
80 font((uint8_t*) AMIGA_FONT)
81{
82
83 // Update the text
85
86}
void update_text(const string &)
Update the text in the input box.
Definition inputbox.cpp:264
string text()
Get the text currently in the input box.
Definition inputbox.cpp:281

References text(), and update_text().

Member Function Documentation

◆ draw()

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

Draw an area of the input box onto a graphics context.

Parameters
gcThe context to draw onto
areaWhat part of the input box to draw

Reimplemented from MaxOS::gui::Widget.

Definition at line 96 of file inputbox.cpp.

96 {
97
98 // Default Draw
100
101 // Get the absolute m_position of the input box
104
105 // Get the x and y m_position of the input box
108
109 // Draw the background for the input box
110 gc->fill_rectangle(x + area.left, y + area.top, x + area.left + area.width,
112
113 // Draw the border
114
115 // Top Border
117
118 // Start in the top left corner of the button and end in the top right corner
119 gc->draw_line(x + area.left, y, x + area.left + area.width - 1, y,
121 }
122
123 // Left Border
125
126 // Start in the top left corner and end in the bottom left corner
127 gc->draw_line(x, y + area.top, x, y + area.top + area.height - 1,
129 }
130
131 // Right Border
133
134 // Start in the top right corner and end in the bottom right corner
135 gc->draw_line(x + area.left, y + input_box_position.height - 1,
136 x + area.left + area.width - 1,
138 }
139
140 // Bottom Border
142
143 // Start in the bottom left corner and end in the bottom right corner
144 gc->draw_line(x + input_box_position.width - 1, y + area.top,
146 y + area.top + area.height - 1, border_colour);
147 }
148
149 // Draw the text
150 common::Rectangle<int32_t> text_area(area.left - 1, area.top - 1, area.width, area.height);
152}
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
string m_widget_text
The text in the input box.
Definition inputbox.h:62

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, m_widget_text, MaxOS::gui::Widget::position(), MaxOS::common::Rectangle< Type >::top, and MaxOS::common::Rectangle< Type >::width.

◆ on_focus()

void InputBox::on_focus ( )
overridevirtual

Update the border when the input box is focussed.

Reimplemented from MaxOS::gui::Widget.

Definition at line 157 of file inputbox.cpp.

157 {
158
159 // Make the border black on focus
160 border_colour = Colour(0x00, 0x00, 0x00);
161 invalidate();
162}
void invalidate()
Invalidates the entire widget. This forces the widget to be redrawn on the next screen update.
Definition widget.cpp:55

References border_colour, and MaxOS::gui::Widget::invalidate().

◆ on_focus_lost()

void InputBox::on_focus_lost ( )
overridevirtual

Reset to the original input box style when it is no longer focussed.

Reimplemented from MaxOS::gui::Widget.

Definition at line 167 of file inputbox.cpp.

167 {
168
169 // Reset the border colour
170 border_colour = Colour(0x57, 0x57, 0x57);
171 invalidate();
172}

References border_colour, and MaxOS::gui::Widget::invalidate().

◆ on_key_down()

void InputBox::on_key_down ( drivers::peripherals::KeyCode  key_down_code,
const drivers::peripherals::KeyboardState key_down_state 
)
overridevirtual

Handles a keypress event by updating the rendered text in the input box.

Parameters
key_down_codeThe key being pressed
key_down_stateThe state of the key being pressed

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

Definition at line 180 of file inputbox.cpp.

180 {
181
182 // Handle the key press
183 switch (key_down_code) {
184 case KeyCode::backspace: {
185 if (cursor_position == 0)
186 break;
187
189 // no break - we move the cursor to the left and use the <Delete> code
190 [[fallthrough]];
191 }
192 case KeyCode::deleteKey: {
193 // Move the text to the left
194 for (size_t i = cursor_position; i < m_widget_text.length(); ++i)
195 m_widget_text[i] = m_widget_text[i + 1];
196
197 // Put a null character at the end of the string
198 m_widget_text[m_widget_text.length() - 1] = '\0';
199
200 break;
201 }
202 case KeyCode::leftArrow: {
203 // If the cursor is not at the beginning of the text, move it to the left
204 if (cursor_position > 0)
206 break;
207 }
208 case KeyCode::rightArrow: {
209
210 // If the cursor is not at the end of the text, move it to the right
211 if (m_widget_text[cursor_position] != '\0')
213 break;
214 }
215 default: {
216
217 // If the key is a printable character, add it to the text
218 if (31 < (int) key_down_code && (int) key_down_code < 127) {
219 uint32_t length = cursor_position;
220
221 // Find the length of the text buffer
222 while (m_widget_text[length] != '\0') {
223 ++length;
224 }
225
226 // Check if we need to make space for the new character
227 if (length >= (uint32_t) m_widget_text.length()) {
228 m_widget_text += " ";
229 }
230
231 // Shift elements to the right
232 while (length > cursor_position) {
233 m_widget_text[length + 1] = m_widget_text[length];
234 --length;
235 }
236
237 // Insert the new character
241 } else {
242
243 // Don't want to redraw the widget if nothing has changed
244 return;
245 }
246 break;
247 }
248 }
249
250 // Redraw the widget
251 invalidate();
252
253 // Fire the text changed event
254 if (key_down_code != KeyCode::leftArrow && key_down_code != KeyCode::rightArrow)
256
257}
Vector< Event< InputBoxEvents > * > raise_event(Event< InputBoxEvents > *event)
Calls the on_event function of all the event m_handlers connected to the event manager and returns a ...
Event that is triggered when the text in an input box is changed.
Definition inputbox.h:32
uint32_t cursor_position
How many characters into the text the cursor is.
Definition inputbox.h:84

References cursor_position, MaxOS::gui::Widget::invalidate(), m_widget_text, and MaxOS::common::EventManager< InputBoxEvents >::raise_event().

◆ text()

string InputBox::text ( )

Get the text currently in the input box.

Returns
The text in the input box

Definition at line 281 of file inputbox.cpp.

281 {
282
283 return m_widget_text;
284}

References m_widget_text.

Referenced by InputBox().

◆ update_text()

void InputBox::update_text ( const string new_text)

Update the text in the input box.

Parameters
new_textThe new text to display

Definition at line 264 of file inputbox.cpp.

264 {
265
266 m_widget_text.copy(new_text);
268
269 // Redraw the widget
270 invalidate();
271
272 // Fire the text changed event
274}

References cursor_position, MaxOS::gui::Widget::invalidate(), m_widget_text, and MaxOS::common::EventManager< InputBoxEvents >::raise_event().

Referenced by InputBox().

Member Data Documentation

◆ background_colour

common::Colour MaxOS::gui::widgets::InputBox::background_colour

The background colour of the input box.

Definition at line 80 of file inputbox.h.

Referenced by draw().

◆ border_colour

common::Colour MaxOS::gui::widgets::InputBox::border_colour

The colour of the bar around the input box.

Definition at line 82 of file inputbox.h.

Referenced by draw(), on_focus(), and on_focus_lost().

◆ cursor_position

uint32_t MaxOS::gui::widgets::InputBox::cursor_position { 0 }

How many characters into the text the cursor is.

Definition at line 84 of file inputbox.h.

84{ 0 };

Referenced by on_key_down(), and update_text().

◆ font

gui::Font MaxOS::gui::widgets::InputBox::font

The font to use for the input box text.

Definition at line 83 of file inputbox.h.

Referenced by draw().

◆ foreground_colour

common::Colour MaxOS::gui::widgets::InputBox::foreground_colour

The colour of the text in the input box.

Definition at line 81 of file inputbox.h.

Referenced by draw().

◆ m_widget_text

string MaxOS::gui::widgets::InputBox::m_widget_text = " "
protected

The text in the input box.

Definition at line 62 of file inputbox.h.

Referenced by draw(), on_key_down(), text(), and update_text().


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