From a0186dddf3f61b21be892119d6142f6f927dc14e Mon Sep 17 00:00:00 2001 From: NotPike <18655435+notpike@users.noreply.github.com> Date: Fri, 21 Jan 2022 00:09:12 -0500 Subject: [PATCH] Widgets init --- Widgets.md | 554 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 554 insertions(+) create mode 100644 Widgets.md diff --git a/Widgets.md b/Widgets.md new file mode 100644 index 0000000..7a30026 --- /dev/null +++ b/Widgets.md @@ -0,0 +1,554 @@ +## About + +Widgets are the elements that compose the UI of your custom app. Widgets are defined inside [`firmware\common\ui_widget.hpp`](https://github.com/eried/portapack-mayhem/blob/next/firmware/common/ui_widget.hpp) and widget functions can be found inside [`firmware\common\ui_widget.cpp`](https://github.com/eried/portapack-mayhem/blob/next/firmware/common/ui_widget.cpp). In order to be able to use them, you must `#include "ui_widget.hpp"` into your app .hpp file. +There are different type of widget. here you will find a list of available widget, with their respecting constructor. For all the methods available, you should go and see the [ui_widget.hpp](https://github.com/eried/portapack-mayhem/blob/next/firmware/common/ui_widget.hpp) file. + +## Attach a Generic Widget to Your Application + +In order to display a widget into your app, you can either use the function `add_child()` or `add_children()`. +Both those functions shall be called within the code of your `NewAppGameView(NavigationView &nav){}` constructor. The difference between the two function is simple: the first one allows you to add a single widget, while the second one allows you to add an undefined number of widgets. +Widgets must be passed as pointers to the functions. A correct way of calling the two functions would then be: +``` +add_child(&my_widget); +``` +or +``` +add_children({ + &widget_1, + &widget_2 +}); +``` + +## Available widgets +There are several different widgets, and more might be added, so you should always go and check whether new widgets have been added or not. Here you will find a list of most basic widgets. + +#### Text +The text widgets add a simple text area to your app. Here you can find it's declaration and prototype: +``` +Text my_text_widget{ + Rect parent_rect, + std::string text +}; + +``` +To be noted that `Rect parent_rect` has it's own definition inside another file, but let's say that you would like to add a text widget with the text "Hello World", positioned at the top left corner (spaced 10 from both top margin and left margin), with width 100 and height 24, you cold do it in this way: +``` +Text hello_world_text_widget( + {10, 10, 100, 24}, // Coordinates are: int:x (px), int:y (px), int:width (px), int:height (px) + "Hello world!" +); +``` +#### Button + +Buttons allows you to do something when you press them. Here you can find it's declaration and prototype: +``` +Button my_button_widget{ + Rect parent_rect, + std::string text +}; +``` +Be aware that every time you create a button, you then have to implement this method: `my_button_widget.on_select = [&nav](Button &){}`. You can leave it empty (even though it should not, as here you define what action the button should perform), but it must be present in your code. + +For example, let's say you want a button called `my_button`, with the same dimensions as the previous widget. You will then do: +``` +Button my_button( + {10, 10, 100, 24}, // Coordinates are: int:x (px), int:y (px), int:width (px), int:height (px) + "my_button_text" +); +``` + +#### Labels + +Labels are a text element that can be used to describe other widgets. Here you can find it's declaration and prototype: +``` +Labels my_label_widget{ + std::initializer_list