From d470092952d9b0d2a400bc4ee20fc42ec9550b7f Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Sun, 25 Apr 2021 09:28:58 +0200 Subject: [PATCH] Improved SelectFabButton --- radio/src/gui/colorlcd/CMakeLists.txt | 1 + radio/src/gui/colorlcd/fab_button.cpp | 23 ++- radio/src/gui/colorlcd/fab_button.h | 2 + radio/src/gui/colorlcd/select_fab_button.cpp | 76 ++++++++ radio/src/gui/colorlcd/select_fab_button.h | 39 +++++ radio/src/gui/colorlcd/view_main.cpp | 2 +- .../src/gui/colorlcd/view_main_decoration.cpp | 1 + radio/src/gui/colorlcd/view_main_menu.cpp | 163 +++++++----------- 8 files changed, 196 insertions(+), 111 deletions(-) create mode 100644 radio/src/gui/colorlcd/select_fab_button.cpp create mode 100644 radio/src/gui/colorlcd/select_fab_button.h diff --git a/radio/src/gui/colorlcd/CMakeLists.txt b/radio/src/gui/colorlcd/CMakeLists.txt index b2944c4e2..5fcd7f548 100644 --- a/radio/src/gui/colorlcd/CMakeLists.txt +++ b/radio/src/gui/colorlcd/CMakeLists.txt @@ -21,6 +21,7 @@ set(GUI_SRC tabsgroup.cpp page.cpp fab_button.cpp + select_fab_button.cpp topbar.cpp layout.cpp widget.cpp diff --git a/radio/src/gui/colorlcd/fab_button.cpp b/radio/src/gui/colorlcd/fab_button.cpp index c060552a3..89bd8cdad 100644 --- a/radio/src/gui/colorlcd/fab_button.cpp +++ b/radio/src/gui/colorlcd/fab_button.cpp @@ -32,18 +32,27 @@ const uint8_t __alpha_button_off[] { }; Bitmap ALPHA_BUTTON_OFF(BMP_ARGB4444, (const uint16_t*)__alpha_button_off); - -FabButton::FabButton(FormGroup * parent, coord_t x, coord_t y, uint8_t icon, std::function pressHandler, WindowFlags windowFlags): - Button(parent, { x - 34, y - 34, 68, 68 }, pressHandler, windowFlags), - icon(icon) +FabButton::FabButton(FormGroup* parent, coord_t x, coord_t y, uint8_t icon, + std::function pressHandler, + WindowFlags windowFlags) : + Button(parent, + {x - FAB_BUTTON_SIZE / 2, y - FAB_BUTTON_SIZE / 2, FAB_BUTTON_SIZE, + FAB_BUTTON_SIZE}, + pressHandler, windowFlags), + icon(icon) { } void FabButton::paint(BitmapBuffer * dc) { - dc->drawBitmap(0, 0, checked() ? &ALPHA_BUTTON_ON : &ALPHA_BUTTON_OFF); - const BitmapBuffer * mask = theme->getIconMask(icon); + auto bitmap = checked() ? &ALPHA_BUTTON_ON : &ALPHA_BUTTON_OFF; + + dc->drawBitmap((width() - bitmap->width()) / 2, + (FAB_BUTTON_SIZE - bitmap->height()) / 2, bitmap); + + const BitmapBuffer* mask = theme->getIconMask(icon); if (mask) { - dc->drawMask((68 - mask->width()) / 2, (68 - mask->height()) / 2, mask, DEFAULT_BGCOLOR); + dc->drawMask((width() - mask->width()) / 2, + (FAB_BUTTON_SIZE - mask->height()) / 2, mask, DEFAULT_BGCOLOR); } } diff --git a/radio/src/gui/colorlcd/fab_button.h b/radio/src/gui/colorlcd/fab_button.h index bf5654289..34ee06c19 100644 --- a/radio/src/gui/colorlcd/fab_button.h +++ b/radio/src/gui/colorlcd/fab_button.h @@ -22,6 +22,8 @@ #include "button.h" +constexpr coord_t FAB_BUTTON_SIZE = 68; + class FabButton: public Button { public: FabButton(FormGroup * parent, coord_t x, coord_t y, uint8_t icon, std::function pressHandler, WindowFlags windowFlags = 0); diff --git a/radio/src/gui/colorlcd/select_fab_button.cpp b/radio/src/gui/colorlcd/select_fab_button.cpp new file mode 100644 index 000000000..6c90e6b71 --- /dev/null +++ b/radio/src/gui/colorlcd/select_fab_button.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "select_fab_button.h" + +SelectFabButton::SelectFabButton(FormGroup* parent, coord_t x, coord_t y, + uint8_t icon, const char* title, + std::function pressHandler, + WindowFlags windowFlags) : + // FabButton uses center coordinates, we want top left corner: + FabButton(parent, x + FAB_BUTTON_SIZE / 2, y + FAB_BUTTON_SIZE / 2, icon, + pressHandler, windowFlags), + title(title) +{ + // Add some space on either side on the button + setWidth(width() + SELECT_BUTTON_BORDER); + + // 2 Lines extra for the text + half border + setHeight(height() + 2 * PAGE_LINE_HEIGHT + SELECT_BUTTON_BORDER / 2); + + // if (height() < (width() / 2) * 3) { + // setHeight((width() / 2) * 3); + // } +} + +void SelectFabButton::paint(BitmapBuffer* dc) +{ + FabButton::paint(dc); + auto pos = title.find('\n'); + + auto y_pos = FAB_BUTTON_SIZE;// + (height() - FAB_BUTTON_SIZE) / 5; + dc->drawSizedText(width() / 2, y_pos, + title.c_str(), pos, + FOCUS_COLOR | CENTERED | VCENTERED); + + if (pos != std::string::npos) { + // y_pos += (height() - FAB_BUTTON_SIZE) / 3; + y_pos += PAGE_LINE_HEIGHT; + dc->drawSizedText(width() / 2, y_pos, title.substr(pos+1).c_str(), + 255, FOCUS_COLOR | CENTERED | VCENTERED); + } + + if (hasFocus()) { + dc->drawSolidRect(0, 0, width(), height(), 2, CHECKBOX_COLOR); + } +} + +void SelectFabButton::onEvent(event_t event) +{ +#if defined(HARDWARE_KEYS) + switch (event) { + case EVT_KEY_BREAK(KEY_EXIT): + killEvents(event); + parent->deleteLater(); + return; + } +#endif + FabButton::onEvent(event); +} diff --git a/radio/src/gui/colorlcd/select_fab_button.h b/radio/src/gui/colorlcd/select_fab_button.h new file mode 100644 index 000000000..7b45d1b28 --- /dev/null +++ b/radio/src/gui/colorlcd/select_fab_button.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#pragma once + +#include "fab_button.h" + +constexpr coord_t SELECT_BUTTON_BORDER = 12; + +class SelectFabButton : public FabButton +{ + public: + SelectFabButton(FormGroup* parent, coord_t x, coord_t y, uint8_t icon, + const char* title, std::function pressHandler, + WindowFlags windowFlags = 0); + + void paint(BitmapBuffer* dc) override; + void onEvent(event_t event) override; + + protected: + std::string title; +}; diff --git a/radio/src/gui/colorlcd/view_main.cpp b/radio/src/gui/colorlcd/view_main.cpp index 90c1a9bf1..c4f77e5de 100644 --- a/radio/src/gui/colorlcd/view_main.cpp +++ b/radio/src/gui/colorlcd/view_main.cpp @@ -34,7 +34,7 @@ ViewMain * ViewMain::_instance = nullptr; ViewMain::ViewMain(): - Window(MainWindow::instance(), MainWindow::instance()->getRect()), + Window(MainWindow::instance(), MainWindow::instance()->getRect(), NO_SCROLLBAR), topbar(dynamic_cast(TopbarFactory::create(this))) { setPageWidth(getParent()->width()); diff --git a/radio/src/gui/colorlcd/view_main_decoration.cpp b/radio/src/gui/colorlcd/view_main_decoration.cpp index 3e0bcbfbc..19684d928 100644 --- a/radio/src/gui/colorlcd/view_main_decoration.cpp +++ b/radio/src/gui/colorlcd/view_main_decoration.cpp @@ -204,6 +204,7 @@ void ViewMainDecoration::adjustDecoration() pos = trims[TRIMS_RH]->left() - pos; flightMode->setWidth(pos); + flightMode->setInnerWidth(pos); pos = sliders[SLIDERS_POT1]->top(); pos -= flightMode->height(); diff --git a/radio/src/gui/colorlcd/view_main_menu.cpp b/radio/src/gui/colorlcd/view_main_menu.cpp index 032700777..9de8e1390 100644 --- a/radio/src/gui/colorlcd/view_main_menu.cpp +++ b/radio/src/gui/colorlcd/view_main_menu.cpp @@ -26,68 +26,19 @@ #include "menu_model.h" #include "menu_radio.h" #include "menu_screen.h" -//#include "radio_setup.h" #include "view_channels.h" #include "view_statistics.h" -#include "fab_button.h" - -constexpr coord_t FAB_BUTTON_WIDTH = 68; -constexpr coord_t FAB_BUTTON_BORDER = 12; -constexpr coord_t MENU_BORDER = 16; - -class SelectFabButton : public FabButton -{ - public: - SelectFabButton(FormGroup* parent, coord_t x, coord_t y, uint8_t icon, - const char* title, std::function pressHandler, - WindowFlags windowFlags = 0) : - FabButton(parent, x + FAB_BUTTON_WIDTH / 2, y + FAB_BUTTON_WIDTH / 2, - icon, pressHandler, windowFlags), - title(title) - { - setHeight(height() + 2 * PAGE_LINE_HEIGHT); - } - - void paint(BitmapBuffer* dc) override - { - FabButton::paint(dc); - auto pos = title.find('\n'); - dc->drawSizedText(width() / 2, FAB_BUTTON_WIDTH, title.c_str(), pos, - FOCUS_COLOR | CENTERED); - dc->drawSizedText(width() / 2, FAB_BUTTON_WIDTH, title.substr(pos).c_str(), 255, - FOCUS_COLOR | CENTERED); - if (hasFocus()) { - dc->drawSolidRect(0, 0, width(), height(), 2, CHECKBOX_COLOR); - } - } - - void onEvent(event_t event) override; - - protected: - std::string title; -}; - -void SelectFabButton::onEvent(event_t event) -{ -#if defined(HARDWARE_KEYS) - switch (event) { - case EVT_KEY_BREAK(KEY_EXIT): - killEvents(event); - parent->deleteLater(); - return; - } -#endif - FabButton::onEvent(event); -} +#include "select_fab_button.h" ViewMainMenu::ViewMainMenu(Window* parent) : FormGroup(parent->getFullScreenWindow(), - { 0, 0, 350, 180 }, OPAQUE | FORM_FORWARD_FOCUS) + {}, + OPAQUE | FORM_FORWARD_FOCUS | NO_SCROLLBAR) { Layer::push(this); - coord_t y_pos = MENU_BORDER; - coord_t x_pos = MENU_BORDER; + coord_t y_pos = SELECT_BUTTON_BORDER; + coord_t x_pos = SELECT_BUTTON_BORDER / 2; // Disabled Title // @@ -96,94 +47,100 @@ ViewMainMenu::ViewMainMenu(Window* parent) : // "Tasks", 0, FOCUS_COLOR | FONT(XL) | CENTERED); // pos += title->height() + PAGE_LINE_SPACING; - new SelectFabButton(this, x_pos, y_pos, ICON_RADIO_TOOLS, "Model\nSettings", + auto button = new SelectFabButton(this, x_pos, y_pos, ICON_RADIO_TOOLS, "Model\nSettings", [=]() -> uint8_t { deleteLater(); new ModelMenu(); return 0; }); - x_pos += FAB_BUTTON_WIDTH + FAB_BUTTON_BORDER; + x_pos += button->width() + SELECT_BUTTON_BORDER / 2; + setPageWidth(x_pos); + setWidth(pageWidth * 4); + + x_pos += SELECT_BUTTON_BORDER / 2; - new SelectFabButton(this, x_pos, y_pos, ICON_MODEL, "Select\nModel", + button = new SelectFabButton(this, x_pos, y_pos, ICON_MODEL, "Select\nModel", [=]() -> uint8_t { deleteLater(); new ModelSelectMenu(); return 0; }); - x_pos += FAB_BUTTON_WIDTH + FAB_BUTTON_BORDER; + x_pos += pageWidth; - new SelectFabButton(this, x_pos, y_pos, ICON_RADIO, "Radio\nSettings", + button = new SelectFabButton(this, x_pos, y_pos, ICON_RADIO, "Radio\nSettings", [=]() -> uint8_t { deleteLater(); new RadioMenu(); return 0; }); - x_pos += FAB_BUTTON_WIDTH + FAB_BUTTON_BORDER; + x_pos += pageWidth; - auto button = new SelectFabButton(this, x_pos, y_pos, ICON_THEME, "Screens\nSetup", + button = new SelectFabButton(this, x_pos, y_pos, ICON_THEME, "Screens\nSetup", [=]() -> uint8_t { deleteLater(); new ScreenMenu(); return 0; }); - x_pos += FAB_BUTTON_WIDTH + MENU_BORDER; - y_pos += button->height() + MENU_BORDER; + x_pos += pageWidth; - setWidth(x_pos); - setInnerHeight(x_pos); + button = new SelectFabButton(this, x_pos, y_pos, ICON_MONITOR, "Channel\nMonitor", + [=]() -> uint8_t { + deleteLater(); + new ChannelsViewMenu(); + return 0; + }); + x_pos += pageWidth; + + button = new SelectFabButton(this, x_pos, y_pos, ICON_MODEL_TELEMETRY, "Reset\nTelemetry", + [=]() -> uint8_t { + deleteLater(); + Menu* resetMenu = new Menu(parent); + resetMenu->addLine(STR_RESET_FLIGHT, []() { flightReset(); }); + resetMenu->addLine(STR_RESET_TIMER1, []() { timerReset(0); }); + resetMenu->addLine(STR_RESET_TIMER2, []() { timerReset(1); }); + resetMenu->addLine(STR_RESET_TIMER3, []() { timerReset(2); }); + resetMenu->addLine(STR_RESET_TELEMETRY, []() { telemetryReset(); }); + return 0; + }); + + x_pos += pageWidth; + + button = new SelectFabButton(this, x_pos, y_pos, ICON_STATS, "Statistics", + [=]() -> uint8_t { + deleteLater(); + new StatisticsViewPageGroup(); + return 0; + }); + + x_pos += pageWidth; + + button = new SelectFabButton(this, x_pos, y_pos, ICON_OPENTX, "About\nOpenTx", + [=]() -> uint8_t { + deleteLater(); + //TODO: new AboutUs(); + return 0; + }); + + x_pos += pageWidth; + setInnerWidth(x_pos + SELECT_BUTTON_BORDER / 2); + + y_pos += button->height() + SELECT_BUTTON_BORDER; setHeight(y_pos); setInnerHeight(y_pos); setWindowCentered(); - // new IconButton( - // this, {0, 0, MENU_HEADER_BUTTON_WIDTH, MENU_HEADER_BUTTON_WIDTH}, - // ICON_BACK, - // [=]() -> uint8_t { - // // openMenu(); - // return 0; - // }); - - //TODO: adjust to whatever is in this window - //setRect() - - - // Menu* menu = new Menu(this); - // menu->addLine(STR_MODEL_SELECT, []() { new ModelSelectMenu(); }); - // // if (modelHasNotes()) { - // // menu->addLine(STR_VIEW_NOTES, [=]() { - // // // TODO - // // }); - // // } - // menu->addLine(STR_MONITOR_SCREENS, []() { new ChannelsViewMenu(); }); - // menu->addLine(STR_RESET_SUBMENU, [this]() { - // Menu* resetMenu = new Menu(this); - // resetMenu->addLine(STR_RESET_FLIGHT, []() { flightReset(); }); - // resetMenu->addLine(STR_RESET_TIMER1, []() { timerReset(0); }); - // resetMenu->addLine(STR_RESET_TIMER2, []() { timerReset(1); }); - // resetMenu->addLine(STR_RESET_TIMER3, []() { timerReset(2); }); - // resetMenu->addLine(STR_RESET_TELEMETRY, []() { telemetryReset(); }); - // }); - // menu->addLine(STR_STATISTICS, []() { new StatisticsViewPageGroup(); }); - // menu->addLine(STR_ABOUT_US, []() { - // // TODO - // }); - - // // Place at current position - // menu->setLeft(menu->left() + getScrollPositionX()); - // menu->setTop(menu->top() + getScrollPositionY()); - setFocus(); } void ViewMainMenu::paint(BitmapBuffer* dc) { - dc->drawFilledRect(0, 0, width(), height(), SOLID, OVERLAY_COLOR, OPACITY(5)); + dc->drawFilledRect(0, 0, getInnerWidth(), height(), SOLID, OVERLAY_COLOR, OPACITY(5)); } void ViewMainMenu::deleteLater(bool detach, bool trash) {