From 766c1f6b798627c30d41fce05e844fe3243e2461 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 5 Apr 2021 08:40:49 +0200 Subject: [PATCH] WiP widget settings --- radio/src/gui/colorlcd/CMakeLists.txt | 1 + radio/src/gui/colorlcd/screen_setup.cpp | 13 ++- radio/src/gui/colorlcd/widget.h | 5 + radio/src/gui/colorlcd/widget_settings.cpp | 129 +++++++++++++++++++++ radio/src/gui/colorlcd/widget_settings.h | 32 +++++ 5 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 radio/src/gui/colorlcd/widget_settings.cpp create mode 100644 radio/src/gui/colorlcd/widget_settings.h diff --git a/radio/src/gui/colorlcd/CMakeLists.txt b/radio/src/gui/colorlcd/CMakeLists.txt index 3151e92d6..9a2c1db3a 100644 --- a/radio/src/gui/colorlcd/CMakeLists.txt +++ b/radio/src/gui/colorlcd/CMakeLists.txt @@ -23,6 +23,7 @@ set(GUI_SRC topbar.cpp layout.cpp widget.cpp + widget_settings.cpp ${THEMES_SRC} ${LAYOUTS_SRC} ${WIDGETS_SRC} diff --git a/radio/src/gui/colorlcd/screen_setup.cpp b/radio/src/gui/colorlcd/screen_setup.cpp index 576ef13a8..1d88962be 100644 --- a/radio/src/gui/colorlcd/screen_setup.cpp +++ b/radio/src/gui/colorlcd/screen_setup.cpp @@ -22,6 +22,7 @@ #include "screen_setup.h" #include "opentx.h" #include "view_main.h" +#include "widget_settings.h" #define SET_DIRTY() storageDirty(EE_MODEL) @@ -158,7 +159,7 @@ class SetupWidgetsPageSlot: public Button widget = screen->getWidget(slotIndex); setPressHandler([=](){ Menu * menu = new Menu(this); - menu->addLine("Select a widget", [=]() { + menu->addLine(TR_SELECT_WIDGET, [=]() { Menu * menu = new Menu(this); for (auto factory: getRegisteredWidgets()) { menu->addLine(factory->getName(), [=]() { @@ -170,7 +171,15 @@ class SetupWidgetsPageSlot: public Button return 0; }); if (widget) { - menu->addLine(STR_DELETE, [=]() { + menu->addLine(TR_WIDGET_SETTINGS, [=]() { + new WidgetSettings(this, widget, [=]() { + TRACE("WidgetSettings confirmed"); + deleteLater(); + return; + }); + }); + + menu->addLine(STR_REMOVE_WIDGET, [=]() { widget->deleteLater(); widget = nullptr; return 0; diff --git a/radio/src/gui/colorlcd/widget.h b/radio/src/gui/colorlcd/widget.h index e5c4f54f5..e1bb59aae 100644 --- a/radio/src/gui/colorlcd/widget.h +++ b/radio/src/gui/colorlcd/widget.h @@ -71,6 +71,11 @@ class Widget : public FormField return &persistentData->options[index].value; } + inline void setOptionValue(unsigned int index, const ZoneOptionValue& value) + { + persistentData->options[index].value = value; + } + inline PersistentData * getPersistentData() { return persistentData; diff --git a/radio/src/gui/colorlcd/widget_settings.cpp b/radio/src/gui/colorlcd/widget_settings.cpp new file mode 100644 index 000000000..6659fe915 --- /dev/null +++ b/radio/src/gui/colorlcd/widget_settings.cpp @@ -0,0 +1,129 @@ +/* + * 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 "opentx.h" +#include "widget_settings.h" + +static const rect_t widgetSettingsDialogRect = { + LCD_W / 10, // x + LCD_H / 4, // y + LCD_W - 2 * LCD_W / 10, // width + LCD_H - 2 * LCD_H / 4 // height +}; + +WidgetSettings::WidgetSettings(Window * parent, Widget * widget, std::function confirmHandler) : + Dialog(parent, TR_WIDGET_SETTINGS, widgetSettingsDialogRect), + confirmHandler(std::move(confirmHandler)) +{ + auto form = &content->form; + FormGridLayout grid(content->form.width()); + form->clear(); + + uint8_t optIdx = 0; + auto optPtr = widget->getOptions(); + + while (optPtr->name != nullptr) { + + auto option = *optPtr; + new StaticText(form, grid.getLabelSlot(), option.name); + + switch (option.type) { + + case ZoneOption::Integer: + new NumberEdit(form, grid.getFieldSlot(), + option.min.signedValue, // Minimum + option.max.signedValue, // Maximum + [=]() -> int { // getValue + return widget->getOptionValue(optIdx)->signedValue; + }, + [=](int32_t newValue) { // setValue + widget->setOptionValue(optIdx, OPTION_VALUE_SIGNED(newValue)); + }); + break; + + case ZoneOption::Source: + new SourceChoice(form, grid.getFieldSlot(), + option.min.unsignedValue, // min + option.max.unsignedValue, // max + [=]() -> int16_t { // getValue + return (int16_t)widget->getOptionValue(optIdx)->unsignedValue; + }, + [=](int16_t newValue) { // setValue + widget->setOptionValue(optIdx, OPTION_VALUE_UNSIGNED((uint32_t)newValue)); + }); + break; + + case ZoneOption::Bool: + new CheckBox(form, grid.getFieldSlot(), + [=]() -> uint8_t { // getValue + return (uint8_t)widget->getOptionValue(optIdx)->boolValue; + }, + [=](int8_t newValue) { // setValue + widget->setOptionValue(optIdx, OPTION_VALUE_BOOL((uint32_t)newValue)); + }); + break; + + case ZoneOption::String: + break; + + case ZoneOption::File: + break; + + case ZoneOption::TextSize: + break; + + case ZoneOption::Timer: // Unsigned + { + auto tmChoice = new Choice(form, grid.getFieldSlot(), + 0, TIMERS - 1, + [=]() -> int { // getValue + return (int)widget->getOptionValue(optIdx)->unsignedValue; + }, + [=](int newValue) { // setValue + widget->setOptionValue(optIdx, OPTION_VALUE_UNSIGNED((uint32_t)newValue)); + }); + + tmChoice->setTextHandler([](int value) { + return std::string(STR_TIMER) + std::to_string(value + 1); + }); + } + break; + + case ZoneOption::Switch: + new SwitchChoice(form, grid.getFieldSlot(), + option.min.unsignedValue, // min + option.max.unsignedValue, // max + [=]() -> int16_t { // getValue + return (uint8_t)widget->getOptionValue(optIdx)->unsignedValue; + }, + [=](int16_t newValue) { // setValue + widget->setOptionValue(optIdx, OPTION_VALUE_UNSIGNED((uint32_t)newValue)); + }); + break; + + case ZoneOption::Color: + break; + } + + optIdx++; + optPtr++; + grid.nextLine(); + } +} diff --git a/radio/src/gui/colorlcd/widget_settings.h b/radio/src/gui/colorlcd/widget_settings.h new file mode 100644 index 000000000..9f59e95db --- /dev/null +++ b/radio/src/gui/colorlcd/widget_settings.h @@ -0,0 +1,32 @@ +/* + * 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 "dialog.h" +#include "widget.h" + +class WidgetSettings: public Dialog +{ + std::function confirmHandler; + + public: + WidgetSettings(Window * parent, Widget * widget, std::function confirmHandler); +};