aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorConfuSomu2021-03-16 18:13:39 -0400
committerConfuSomu2021-03-16 18:13:39 -0400
commitf5b82cacfb2712837a97999837e90e23e33e3ea5 (patch)
tree6029ab9274d6d29358bf2e5c3bbcd9c5b1f51fd2
parent54467fed75930ea005d7fb1e7099d5397e11a308 (diff)
downloadpico-watch-f5b82cacfb2712837a97999837e90e23e33e3ea5.tar
pico-watch-f5b82cacfb2712837a97999837e90e23e33e3ea5.tar.gz
pico-watch-f5b82cacfb2712837a97999837e90e23e33e3ea5.zip
Implement API method for popup for
choosing number
-rw-r--r--api.cpp52
-rw-r--r--api.hpp11
2 files changed, 62 insertions, 1 deletions
diff --git a/api.cpp b/api.cpp
index a0d8ae0..96a942c 100644
--- a/api.cpp
+++ b/api.cpp
@@ -68,7 +68,7 @@ void Api::gui_popup_generic(std::string &title, std::string &body, int max_title
oledRectangle(&m_oled, 9,7, 119,63, 0, 1); // Background
oledRectangle(&m_oled, 9,7, 119,63, 1, 0); // Popup border
oledRectangle(&m_oled, 9,7, 119,16, 1, 1); // Title background, FIXME pixel bleeding
- m_writebb_needed = true; this->display_write_backbuffer(); // Display rectangle and anything else behind it (drawn before), could be moved after writing strings
+ m_writebb_needed = true; this->display_write_backbuffer(); // Display rectangle and anything else behind it (drawn before), could be moved after writing strings (but not done for debugging)
// Truncate longer strings to avoid wasting time in for loop and drawing on OLED
if (max_title_length > 13) max_title_length = 13;
@@ -135,6 +135,56 @@ bool Api::gui_popup_booleanchoice(std::string title, std::string body){
return choice;
}
+void Api::gui_popup_intchoice_footer(int current_num, int min_num, int max_num) {
+ char buf[30];
+ snprintf(&buf[0], sizeof(buf),
+ "Select: %d (%d/%d)",
+ current_num,
+ min_num,
+ max_num);
+ oledRectangle(&m_oled, 9,55, 119,63, 1, 1); // Footer background, FIXME pixel bleeding
+ oledWriteString(&m_oled, 0,10,7, buf, FONT_6x8, 1, 0);
+ m_writebb_needed = true; display_write_backbuffer();
+}
+
+int Api::gui_popup_intchoice(std::string title, std::string body, int min_num, int max_num, int default_num, int step){
+ m_button_last_pressed = BUTTON_NONE;
+ m_send_button_press_to_app = false;
+
+ int current_num = default_num;
+
+ title.insert(0, "Number|"); // TODO: Could be made nicer with a custom char instead of pipe char
+ gui_popup_generic(title, body, 13, 39); // 39: 3 lines of body text, to leave space for number
+ gui_popup_intchoice_footer(current_num, min_num, max_num);
+
+ do {
+ m_button_last_pressed = BUTTON_NONE;
+ sleep_ms(50); // TODO: use _wfi()
+ switch (m_button_last_pressed) {
+ case BUTTON_UP:
+ current_num += step;
+ if (current_num > max_num)
+ current_num = max_num;
+ break;
+ case BUTTON_DOWN:
+ current_num -= step;
+ if (current_num < min_num)
+ current_num = min_num;
+ break;
+ case BUTTON_MODE:
+ current_num = default_num;
+ break;
+ }
+ if (m_button_last_pressed)
+ gui_popup_intchoice_footer(current_num, min_num, max_num);
+ } while (m_button_last_pressed != BUTTON_SELECT);
+
+ // Give back control to running app
+ oledFill(&m_oled, 0, 1);
+ m_send_button_press_to_app = true;
+ return current_num;
+}
+
bool Api::gui_footer_text(std::string text, int offset_x, int offset_row, bool invert, bool no_bg) {
// Max chars per line for FONT_8x8 is 16 chars
// Max chars per line for FONT_6x8 is 21 chars
diff --git a/api.hpp b/api.hpp
index 2930759..d6f9c30 100644
--- a/api.hpp
+++ b/api.hpp
@@ -20,6 +20,7 @@ class Api {
// When the string cannot be created, this comes from the fact that we, on some runs, allocation can be very close to the SRAM's boundry: 0x20041f88 with boundry at 0x20042000. The heap is nearly full. Even just appending to a string moves the allocation lower. I think that the best course of action would be to have more static variables and pull in less things to save SRAM.
// When char* directly passed as parameter: The memory is possibly fragmented as long strings (>215) push the allocation downwards, into lower (higher address) in the heap. title is at 0x20041f90 and body at 0x200045e8, for length of 215 chars.
void gui_popup_generic(std::string &title, std::string &body, int max_title_length = 13, int max_body_length = 78);
+ void gui_popup_intchoice_footer(int current_num, int min_num, int max_num);
public:
bool m_send_button_press_to_app = true;
enum perf_modes {
@@ -53,6 +54,16 @@ class Api {
// \param body String containing the popup's body. The zone has a size of 13×6 characters, so body should not be longer than 78 characters. Newline allows going to the next line and the text is automatically wrapped.
// \note Strings longer than 13 and 78 respectively will be truncated.
bool gui_popup_booleanchoice(std::string title, std::string body);
+ // Display a popup over the current view and wait for user to choose (with left and right) a number between min_num and max_num. The default choice is default_num and the user can reset back to it with mode/cancel button. After confirming with select, the choice is returned.
+ // This is a blocking function and should be used only in the app's render method.
+ // \param title Popup's title. The title is prefixed with "Number|", so the `title` argument cannot exceed 6 characters.
+ // \param body String containing the popup's body. The zone has a size of 13×3 characters, so body should not be longer than 39 characters. Newline allows going to the next line and the text is automatically wrapped. Under the body is displayed the current choosen number with the min and max in parenthesis.
+ // \param min_num The smallest number that can be choosen.
+ // \param max_num Biggest number that can be choosen.
+ // \param default_num This should be between min_num and max_num, else user may be able to return a number out of range
+ // \param step Value to increment/decrement from when user changes number. This cannot result in an out-of-bounds as the number is clipped to the min/max when this happens. This maybe undesirable behaviour.
+ // \note Strings longer than 13 and 39 respectively will be truncated.
+ int gui_popup_intchoice(std::string title, std::string body, int min_num = 0, int max_num = 10, int default_num = 5, int step = 1);
// Display text at the bottom of the screen.
// The font size is automatically choosen based on the text lenght.
// \param text Text to display. Text longer than 21 will be truncated.