From 02b3efc958fba7d016325812813d89c987c9c2c8 Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sat, 27 Mar 2021 15:51:38 +0100 Subject: [PATCH 01/11] Add enum state to 3 kinds of buttons --- Firmware/UI/Widgets/ButtonState.h | 14 ++++++++ Firmware/UI/Widgets/ImageButton.h | 48 ++++++++++++++----------- Firmware/UI/Widgets/MainPageButton.h | 11 +++--- Firmware/UI/Widgets/PushButton.h | 52 ++++++++++++++++------------ 4 files changed, 76 insertions(+), 49 deletions(-) create mode 100644 Firmware/UI/Widgets/ButtonState.h diff --git a/Firmware/UI/Widgets/ButtonState.h b/Firmware/UI/Widgets/ButtonState.h new file mode 100644 index 00000000..2766409d --- /dev/null +++ b/Firmware/UI/Widgets/ButtonState.h @@ -0,0 +1,14 @@ + +#ifndef BUTTON_STATE_H +#define BUTTON_STATE_H + +enum class ButtonState +{ + Default = 0, + Highlighted = 1, + Pressed = 2, // same as active? + Disabled = 3, + Checked = 4, + +}; +#endif // BUTTON_STATE_H diff --git a/Firmware/UI/Widgets/ImageButton.h b/Firmware/UI/Widgets/ImageButton.h index 9e2a21da..c1cacc90 100644 --- a/Firmware/UI/Widgets/ImageButton.h +++ b/Firmware/UI/Widgets/ImageButton.h @@ -2,6 +2,7 @@ #define IMAGEBUTTON_H #include "IButton.h" +#include "ButtonState.h" #include "../Painter/Painter.h" #include "Icon.h" @@ -18,7 +19,7 @@ class ImageButton : public IButton const char* _label; uint8_t _cornerRadius; - bool _highlighted; + ButtonState _state; // Color Defintions uint16_t _textColor; @@ -41,9 +42,25 @@ class ImageButton : public IButton ButtonStyle _buttonStyle; + void UpdateCurrentColors() + { + if (_state == ButtonState::Highlighted) + { + _currentImageColor = _imageHighlightColor; + _currentBackgroundColor = _backgroundHighlightColor; + _currentTextColor = _textHighlightColor; + + } else + { + _currentImageColor = _imageColor; + _currentBackgroundColor = _backgroundColor; + _currentTextColor = _textColor; + } + } + public: explicit ImageButton(const Icon* icon, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IButton(x, y, width, height), _image(icon), _cornerRadius(3), _highlighted(false), + IButton(x, y, width, height), _image(icon), _cornerRadius(3), _state(ButtonState::Default), _imageColor((uint16_t)Color565::Black), _currentImageColor(_imageColor), _backgroundHighlightColor((uint16_t)Color565::AXIOM_Blue), _currentBackgroundColor(utils::RGB565(220, 220, 220)), _backgroundColor(utils::RGB565(220, 220, 220)), @@ -112,54 +129,43 @@ class ImageButton : public IButton void SetBackgroundColor(uint16_t color) { _backgroundColor = color; - SetHighlighted(_highlighted); + UpdateCurrentColors(); } void SetImageColor(uint16_t color) { _imageColor = color; - SetHighlighted(_highlighted); + UpdateCurrentColors(); } void SetTextColor(uint16_t color) { _textColor = color; - SetHighlighted(_highlighted); + UpdateCurrentColors(); } void SetHighlightBackgroundColor(uint16_t color) { _backgroundHighlightColor = color; - SetHighlighted(_highlighted); + UpdateCurrentColors(); } void SetHighlightImageColor(uint16_t color) { _imageHighlightColor = color; - SetHighlighted(_highlighted); + UpdateCurrentColors(); } void SetHighlightTextColor(uint16_t color) { _textHighlightColor = color; - SetHighlighted(_highlighted); + UpdateCurrentColors(); } void SetHighlighted(bool highlighted) { - _highlighted = highlighted; - if (highlighted) - { - _currentImageColor = _imageHighlightColor; - _currentBackgroundColor = _backgroundHighlightColor; - _currentTextColor = _textHighlightColor; - - } else - { - _currentImageColor = _imageColor; - _currentBackgroundColor = _backgroundColor; - _currentTextColor = _textColor; - } + _state = highlighted ? ButtonState::Highlighted : ButtonState::Default; + UpdateCurrentColors(); } }; diff --git a/Firmware/UI/Widgets/MainPageButton.h b/Firmware/UI/Widgets/MainPageButton.h index c82b8c76..a136f2d0 100755 --- a/Firmware/UI/Widgets/MainPageButton.h +++ b/Firmware/UI/Widgets/MainPageButton.h @@ -3,6 +3,7 @@ #define MAINPAGEBUTTON_H #include "IButton.h" +#include "ButtonState.h" #include "../Painter/Painter.h" #include "../Color565.h" @@ -27,13 +28,14 @@ class MainPageButton : public IButton char* _value; bool _invertOrder; - bool _highlighted; Font _labelFont; Font _valueFont; ButtonType _type; + ButtonState _currentState; + // Color Defintions uint16_t labelTextColor; uint16_t labelBackgroundColor; @@ -60,7 +62,8 @@ class MainPageButton : public IButton ButtonType type = ButtonType::VALUE_AND_LABEL) : _x(x), _y(y), _width(width), _labelHeight(20), _valueHeight(40), _label((char*)label), _value((char*)"..."), - _invertOrder(invertOrder), _labelFont(Font::FreeSans9pt7b), _valueFont(Font::FreeSans12pt7b), _type(type) + _invertOrder(invertOrder), _labelFont(Font::FreeSans9pt7b), _valueFont(Font::FreeSans12pt7b), _type(type), + _currentState(ButtonState::Default) { currentLabelTextColor = labelTextColor = (uint16_t)Color565::White; currentLabelBackgroundColor = labelBackgroundColor = (uint16_t)Color565::Black; @@ -69,8 +72,6 @@ class MainPageButton : public IButton backgroundHighlightColor = (uint16_t)Color565::AXIOM_Orange; textHighlightColor = (uint16_t)Color565::Black; - - _highlighted = false; } void Draw(IPainter* painter) override @@ -169,7 +170,7 @@ class MainPageButton : public IButton void SetHighlighted(bool highlighted) { - _highlighted = highlighted; + _currentState = highlighted ? ButtonState::Highlighted : ButtonState::Default; if (highlighted) { currentLabelTextColor = textHighlightColor; diff --git a/Firmware/UI/Widgets/PushButton.h b/Firmware/UI/Widgets/PushButton.h index cedd7c40..a12a187f 100644 --- a/Firmware/UI/Widgets/PushButton.h +++ b/Firmware/UI/Widgets/PushButton.h @@ -4,17 +4,18 @@ #include #include "IButton.h" +#include "ButtonState.h" #include "../Painter/Painter.h" class PushButton : public IButton { const char* _label; uint8_t _cornerRadius; - bool _highlighted; + ButtonState _state; // Color Defintions - uint16_t _TextColor; - uint16_t _BackgroundColor; + uint16_t _textColor; + uint16_t _backgroundColor; uint16_t _textHighlightColor; uint16_t _backgroundHighlightColor; @@ -22,12 +23,26 @@ class PushButton : public IButton uint16_t _currentTextColor; uint16_t _currentBackgroundColor; + void UpdateCurrentColors() + { + if (_state == ButtonState::Highlighted) + { + _currentBackgroundColor = _backgroundHighlightColor; + _currentTextColor = _textHighlightColor; + + } else + { + _currentBackgroundColor = _backgroundColor; + _currentTextColor = _textColor; + } + } + public: explicit PushButton(const char* label, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IButton(x, y, width, height), _label(label), _cornerRadius(3), _highlighted(false) + IButton(x, y, width, height), _label(label), _cornerRadius(3), _state(ButtonState::Default) { - _currentTextColor = _TextColor = (uint16_t)Color565::Black; - _currentBackgroundColor = _BackgroundColor = utils::RGB565(220, 220, 220); + _currentTextColor = _textColor = (uint16_t)Color565::Black; + _currentBackgroundColor = _backgroundColor = utils::RGB565(220, 220, 220); _backgroundHighlightColor = (uint16_t)Color565::AXIOM_Blue; _textHighlightColor = (uint16_t)Color565::Black; @@ -55,41 +70,32 @@ class PushButton : public IButton void SetBackgroundColor(uint16_t color) { - _BackgroundColor = color; - SetHighlighted(_highlighted); + _backgroundColor = color; + UpdateCurrentColors(); } void SetTextColor(uint16_t color) { - _TextColor = color; - SetHighlighted(_highlighted); + _textColor = color; + UpdateCurrentColors(); } void SetHighlightBackgroundColor(uint16_t color) { _backgroundHighlightColor = color; - SetHighlighted(_highlighted); + UpdateCurrentColors(); } void SetHighlightTextColor(uint16_t color) { _textHighlightColor = color; - SetHighlighted(_highlighted); + UpdateCurrentColors(); } void SetHighlighted(bool highlighted) { - _highlighted = highlighted; - if (highlighted) - { - _currentTextColor = _textHighlightColor; - _currentBackgroundColor = _backgroundHighlightColor; - - } else - { - _currentTextColor = _TextColor; - _currentBackgroundColor = _BackgroundColor; - } + _state = highlighted ? ButtonState::Highlighted : ButtonState::Default; + UpdateCurrentColors(); } }; From a6a5a6abdefb769b05bed18eed77a33d5a27054a Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sat, 27 Mar 2021 17:45:37 +0100 Subject: [PATCH 02/11] Add statemachine to IButton --- Firmware/UI/Screens/MainPage.cpp | 20 +++---- Firmware/UI/Widgets/ButtonState.h | 9 ---- Firmware/UI/Widgets/IButton.h | 42 ++++++++++++++- Firmware/UI/Widgets/MainPageButton.h | 80 ++++++++++++---------------- 4 files changed, 86 insertions(+), 65 deletions(-) diff --git a/Firmware/UI/Screens/MainPage.cpp b/Firmware/UI/Screens/MainPage.cpp index 363db28a..a55634f5 100644 --- a/Firmware/UI/Screens/MainPage.cpp +++ b/Firmware/UI/Screens/MainPage.cpp @@ -81,45 +81,45 @@ void MainPage::Update(Button button, int8_t knob, IMenuSystem* menuSystem) { case Button::BUTTON_1_UP: _fpsButton.SetValue((char*)"1U"); - _fpsButton.SetHighlighted(false); + _fpsButton.SetState(ButtonState::Default); break; case Button::BUTTON_1_DOWN: _fpsButton.SetValue((char*)"1D"); - _fpsButton.SetHighlighted(true); + _fpsButton.SetState(ButtonState::Highlighted); break; case Button::BUTTON_2_UP: //_fpsButton.SetValue((char*)"2"); - _analogGainButton.SetHighlighted(false); + _analogGainButton.SetState(ButtonState::Default); _analogGainButton.Activate(this); // _usbDevice->Send((uint8_t*)"Button 2\r\n", 10); break; case Button::BUTTON_2_DOWN: - _analogGainButton.SetHighlighted(true); + _analogGainButton.SetState(ButtonState::Highlighted); break; case Button::BUTTON_3_UP: //_fpsButton.SetValue((char*)"3"); - _digitalGainButton.SetHighlighted(false); + _digitalGainButton.SetState(ButtonState::Default); _digitalGainButton.Activate(this); break; case Button::BUTTON_3_DOWN: _fpsButton.SetValue((char*)"3"); - _digitalGainButton.SetHighlighted(true); + _digitalGainButton.SetState(ButtonState::Highlighted); break; case Button::BUTTON_4_UP: //_menuButton.Activate(this); - _menuButton.SetHighlighted(false); + _menuButton.SetState(ButtonState::Default); menuSystem->SetCurrentScreen(AvailableScreens::MainMenu); break; case Button::BUTTON_4_DOWN: - _menuButton.SetHighlighted(true); + _menuButton.SetState(ButtonState::Highlighted); break; case Button::BUTTON_6_UP: //_menuButton.Activate(this); - _whiteBalanceButton.SetHighlighted(false); + _whiteBalanceButton.SetState(ButtonState::Default); menuSystem->SetCurrentScreen(AvailableScreens::WhiteBalance); break; case Button::BUTTON_6_DOWN: - _whiteBalanceButton.SetHighlighted(true); + _whiteBalanceButton.SetState(ButtonState::Highlighted); break; default: break; diff --git a/Firmware/UI/Widgets/ButtonState.h b/Firmware/UI/Widgets/ButtonState.h index 2766409d..6715e74d 100644 --- a/Firmware/UI/Widgets/ButtonState.h +++ b/Firmware/UI/Widgets/ButtonState.h @@ -2,13 +2,4 @@ #ifndef BUTTON_STATE_H #define BUTTON_STATE_H -enum class ButtonState -{ - Default = 0, - Highlighted = 1, - Pressed = 2, // same as active? - Disabled = 3, - Checked = 4, - -}; #endif // BUTTON_STATE_H diff --git a/Firmware/UI/Widgets/IButton.h b/Firmware/UI/Widgets/IButton.h index 35091ac1..99ca3552 100755 --- a/Firmware/UI/Widgets/IButton.h +++ b/Firmware/UI/Widgets/IButton.h @@ -10,14 +10,27 @@ #include "IWidget.h" +enum class ButtonState : uint8_t +{ + Default = 0, + Highlighted = 1, + Disabled = 2, +}; class IButton : public IWidget { // void* -> sender, e.g. MainPage void (*_handlerPtr)(void*); + // Might want to move these three into implementers instead, + // to allow for correct sizes + static constexpr uint8_t _colorsPerState = 4; + static constexpr uint8_t _stateCount = 3; + uint16_t _colors[_stateCount][_colorsPerState]; + ButtonState _currentState; + public: IButton(uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IWidget(x, y, width, height), _handlerPtr(nullptr) + IWidget(x, y, width, height), _handlerPtr(nullptr), _currentState(ButtonState::Default) { } @@ -30,6 +43,33 @@ class IButton : public IWidget { _handlerPtr(sender); } + + // index: every subclass is supposed to define own enum for colorMeanings + void SetColor(ButtonState state, uint8_t index, uint16_t color) + { + _colors[static_cast(state)][index] = color; + } + + uint16_t GetColor(ButtonState state, uint8_t index) + { + return _colors[static_cast(state)][index]; + } + + uint16_t GetCurrentColor(uint8_t index) + { + return _colors[static_cast(_currentState)][index]; + } + + // uint16_t[_colorsPerState] GetCurrentColors() + // { + // return _colors[static_cast(_currentState)]; + // } + + void SetState(ButtonState state) + { + // TODO: checks to see if valid transition, transition function? + _currentState = state; + } }; #endif /* IBUTTON_H */ diff --git a/Firmware/UI/Widgets/MainPageButton.h b/Firmware/UI/Widgets/MainPageButton.h index a136f2d0..49848f64 100755 --- a/Firmware/UI/Widgets/MainPageButton.h +++ b/Firmware/UI/Widgets/MainPageButton.h @@ -3,7 +3,6 @@ #define MAINPAGEBUTTON_H #include "IButton.h" -#include "ButtonState.h" #include "../Painter/Painter.h" #include "../Color565.h" @@ -34,21 +33,27 @@ class MainPageButton : public IButton ButtonType _type; - ButtonState _currentState; - // Color Defintions - uint16_t labelTextColor; - uint16_t labelBackgroundColor; - uint16_t valueTextColor; - uint16_t valueBackgroundColor; - uint16_t textHighlightColor; - uint16_t backgroundHighlightColor; + enum Colors : uint8_t + { + LabelText = 0, + LabelBackground = 1, + ValueText = 2, + ValueBackground = 2, + }; + // uint16_t labelTextColor; + // uint16_t labelBackgroundColor; + // uint16_t valueTextColor; + // uint16_t valueBackgroundColor; - uint16_t currentLabelTextColor; - uint16_t currentLabelBackgroundColor; - uint16_t currentValueTextColor; - uint16_t currentValueBackgroundColor; + // uint16_t textHighlightColor; + // uint16_t backgroundHighlightColor; + + // uint16_t currentLabelTextColor; + // uint16_t currentLabelBackgroundColor; + // uint16_t currentValueTextColor; + // uint16_t currentValueBackgroundColor; // bool _hideValue; @@ -62,16 +67,15 @@ class MainPageButton : public IButton ButtonType type = ButtonType::VALUE_AND_LABEL) : _x(x), _y(y), _width(width), _labelHeight(20), _valueHeight(40), _label((char*)label), _value((char*)"..."), - _invertOrder(invertOrder), _labelFont(Font::FreeSans9pt7b), _valueFont(Font::FreeSans12pt7b), _type(type), - _currentState(ButtonState::Default) + _invertOrder(invertOrder), _labelFont(Font::FreeSans9pt7b), _valueFont(Font::FreeSans12pt7b), _type(type) { - currentLabelTextColor = labelTextColor = (uint16_t)Color565::White; - currentLabelBackgroundColor = labelBackgroundColor = (uint16_t)Color565::Black; - currentValueTextColor = valueTextColor = (uint16_t)Color565::Black; - currentValueBackgroundColor = valueBackgroundColor = (uint16_t)Color565::White; + SetColor(ButtonState::Default, Colors::LabelText, static_cast(Color565::White)); + SetColor(ButtonState::Default, Colors::LabelBackground, static_cast(Color565::Black)); + SetColor(ButtonState::Default, Colors::ValueText, static_cast(Color565::Black)); + SetColor(ButtonState::Default, Colors::ValueBackground, static_cast(Color565::White)); - backgroundHighlightColor = (uint16_t)Color565::AXIOM_Orange; - textHighlightColor = (uint16_t)Color565::Black; + SetColor(ButtonState::Highlighted, Colors::ValueBackground, static_cast(Color565::AXIOM_Orange)); + SetColor(ButtonState::Highlighted, Colors::ValueText, static_cast(Color565::Black)); } void Draw(IPainter* painter) override @@ -103,24 +107,28 @@ class MainPageButton : public IButton void DrawButton(IPainter* painter) { - painter->DrawFillRoundRectangle(_x, _y, _width, _labelHeight, 3, currentLabelBackgroundColor); + // const uint16_t current[] = GetCurrentColor(); + painter->DrawFillRoundRectangle(_x, _y, _width, _labelHeight, 3, GetCurrentColor(Colors::LabelBackground)); painter->SetFont(_labelFont); - painter->DrawText(_x, _y + 24, _label, currentLabelTextColor, TextAlign::TEXT_ALIGN_CENTER, _width); + painter->DrawText(_x, _y + 24, _label, GetCurrentColor(Colors::LabelText), TextAlign::TEXT_ALIGN_CENTER, + _width); } void DrawLabelBox(IPainter* painter, int8_t verticaloffset, int8_t verticaltextoffset) { - painter->DrawFillRoundRectangle(_x, _y + verticaloffset, _width, _labelHeight, 3, currentLabelBackgroundColor); + painter->DrawFillRoundRectangle(_x, _y + verticaloffset, _width, _labelHeight, 3, + GetCurrentColor(Colors::LabelBackground)); painter->SetFont(_labelFont); - painter->DrawText(_x, _y + verticaloffset + verticaltextoffset, _label, currentLabelTextColor, + painter->DrawText(_x, _y + verticaloffset + verticaltextoffset, _label, GetCurrentColor(Colors::LabelText), TextAlign::TEXT_ALIGN_CENTER, _width); } void DrawValueBox(IPainter* painter, int8_t verticaloffset, int8_t verticaltextoffset) { - painter->DrawFillRoundRectangle(_x, _y + verticaloffset, _width, _valueHeight, 3, currentValueBackgroundColor); + painter->DrawFillRoundRectangle(_x, _y + verticaloffset, _width, _valueHeight, 3, + GetCurrentColor(Colors::ValueBackground)); painter->SetFont(_valueFont); - painter->DrawText(_x, _y + verticaloffset + verticaltextoffset, _value, currentValueTextColor, + painter->DrawText(_x, _y + verticaloffset + verticaltextoffset, _value, GetCurrentColor(Colors::ValueText), TextAlign::TEXT_ALIGN_CENTER, _width); } @@ -167,24 +175,6 @@ class MainPageButton : public IButton { _labelHeight = height; } - - void SetHighlighted(bool highlighted) - { - _currentState = highlighted ? ButtonState::Highlighted : ButtonState::Default; - if (highlighted) - { - currentLabelTextColor = textHighlightColor; - currentLabelBackgroundColor = backgroundHighlightColor; - currentValueTextColor = valueBackgroundColor; - currentValueBackgroundColor = valueTextColor; - } else - { - currentLabelTextColor = labelTextColor; - currentLabelBackgroundColor = labelBackgroundColor; - currentValueTextColor = valueTextColor; - currentValueBackgroundColor = valueBackgroundColor; - } - }; }; #endif /* MAINPAGEBUTTON_H */ From 48d53040426444c9d724dad0b2f6854cf73d8df6 Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sat, 27 Mar 2021 18:14:41 +0100 Subject: [PATCH 03/11] Make PushButton use IButton state machine --- Firmware/UI/Screens/NumericValueScreen.h | 7 ++- Firmware/UI/Screens/ParameterListScreen.h | 7 ++- Firmware/UI/Screens/WhiteBalanceScreen.cpp | 19 +++--- Firmware/UI/Widgets/IButton.h | 1 + Firmware/UI/Widgets/PushButton.h | 73 ++++------------------ 5 files changed, 32 insertions(+), 75 deletions(-) diff --git a/Firmware/UI/Screens/NumericValueScreen.h b/Firmware/UI/Screens/NumericValueScreen.h index 5c796b3e..c0f12a9d 100644 --- a/Firmware/UI/Screens/NumericValueScreen.h +++ b/Firmware/UI/Screens/NumericValueScreen.h @@ -42,7 +42,8 @@ class NumericValueScreen : public IScreen // This is the primary button in this menu //_setButton.SetHandler(&SetButtonHandler); - _setButton.SetBackgroundColor((uint16_t)Color565::AXIOM_Orange); + _setButton.SetColor(ButtonState::Default, PushButton::Colors::Background, + static_cast(Color565::AXIOM_Orange)); _bottomButtonBar.SetButton(ButtonPosition::Right, &_setButton); _bottomButtonBar.SetButton(ButtonPosition::Center, &_liveButton); @@ -119,12 +120,12 @@ class NumericValueScreen : public IScreen void SetSetButtonPressed(bool pressed) { - _setButton.SetHighlighted(pressed); + _setButton.SetState(pressed ? ButtonState::Highlighted : ButtonState::Default); } void SetCancelButtonPressed(bool pressed) { - _cancelButton.SetHighlighted(pressed); + _cancelButton.SetState(pressed ? ButtonState::Highlighted : ButtonState::Default); } void Draw(IPainter* painter) override diff --git a/Firmware/UI/Screens/ParameterListScreen.h b/Firmware/UI/Screens/ParameterListScreen.h index 0d1d905a..2f50c81f 100644 --- a/Firmware/UI/Screens/ParameterListScreen.h +++ b/Firmware/UI/Screens/ParameterListScreen.h @@ -47,7 +47,8 @@ class ParameterListScreen : public IScreen // This is the primary button in this menu //_setButton.SetHandler(&SetButtonHandler); - _setButton.SetBackgroundColor((uint16_t)Color565::AXIOM_Orange); + _setButton.SetColor(ButtonState::Default, PushButton::Colors::Background, + static_cast(Color565::AXIOM_Orange)); _bottomButtonBar.SetButton(ButtonPosition::Right, &_setButton); } @@ -63,12 +64,12 @@ class ParameterListScreen : public IScreen void SetSetButtonPressed(bool pressed) { - _setButton.SetHighlighted(pressed); + _setButton.SetState(pressed ? ButtonState::Highlighted : ButtonState::Default); } void SetCancelButtonPressed(bool pressed) { - _cancelButton.SetHighlighted(pressed); + _cancelButton.SetState(pressed ? ButtonState::Highlighted : ButtonState::Default); } void SetHighlighted(uint8_t highlightindex) diff --git a/Firmware/UI/Screens/WhiteBalanceScreen.cpp b/Firmware/UI/Screens/WhiteBalanceScreen.cpp index 4198d563..0f5cd4ac 100644 --- a/Firmware/UI/Screens/WhiteBalanceScreen.cpp +++ b/Firmware/UI/Screens/WhiteBalanceScreen.cpp @@ -19,7 +19,8 @@ WhiteBalanceScreen::WhiteBalanceScreen(IUSBDevice* usbDevice) : // This is the primary button in this menu _setButton.SetHandler(&SetButtonHandler); - _setButton.SetBackgroundColor((uint16_t)Color565::AXIOM_Orange); + _setButton.SetColor(ButtonState::Default, PushButton::Colors::Background, + static_cast(Color565::AXIOM_Orange)); _bottomButtonBar.SetButton(ButtonPosition::Right, &_setButton); //_leftButtonBar.SetButton(ButtonPosition::Left, &_homeButton); //already done in IScreen @@ -74,30 +75,30 @@ void WhiteBalanceScreen::Update(Button button, int8_t knob, IMenuSystem* menuSys switch (button) { case Button::BUTTON_4_DOWN: - _cancelButton.SetHighlighted(true); + _cancelButton.SetState(ButtonState::Highlighted); break; case Button::BUTTON_4_UP: - _cancelButton.SetHighlighted(false); + _cancelButton.SetState(ButtonState::Default); menuSystem->SetCurrentScreen(AvailableScreens::MainPage); break; case Button::BUTTON_5_DOWN: - _addPresetButton.SetHighlighted(true); + _addPresetButton.SetState(ButtonState::Highlighted); break; case Button::BUTTON_5_UP: - _addPresetButton.SetHighlighted(false); + _addPresetButton.SetState(ButtonState::Default); break; case Button::BUTTON_6_DOWN: - _setButton.SetHighlighted(true); + _setButton.SetState(ButtonState::Highlighted); break; case Button::BUTTON_6_UP: - _setButton.SetHighlighted(false); + _setButton.SetState(ButtonState::Default); menuSystem->SetCurrentScreen(AvailableScreens::MainPage); break; case Button::BUTTON_7_DOWN: - _homeButton.SetHighlighted(true); + _homeButton.SetState(ButtonState::Highlighted); break; case Button::BUTTON_7_UP: - _homeButton.SetHighlighted(false); + _homeButton.SetState(ButtonState::Default); menuSystem->SetCurrentScreen(AvailableScreens::MainPage); break; default: diff --git a/Firmware/UI/Widgets/IButton.h b/Firmware/UI/Widgets/IButton.h index 99ca3552..bd514527 100755 --- a/Firmware/UI/Widgets/IButton.h +++ b/Firmware/UI/Widgets/IButton.h @@ -29,6 +29,7 @@ class IButton : public IWidget ButtonState _currentState; public: + // TODO: add startState to constructor? IButton(uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : IWidget(x, y, width, height), _handlerPtr(nullptr), _currentState(ButtonState::Default) { diff --git a/Firmware/UI/Widgets/PushButton.h b/Firmware/UI/Widgets/PushButton.h index a12a187f..479f53f4 100644 --- a/Firmware/UI/Widgets/PushButton.h +++ b/Firmware/UI/Widgets/PushButton.h @@ -11,41 +11,24 @@ class PushButton : public IButton { const char* _label; uint8_t _cornerRadius; - ButtonState _state; // Color Defintions - uint16_t _textColor; - uint16_t _backgroundColor; - uint16_t _textHighlightColor; - uint16_t _backgroundHighlightColor; - - uint16_t _currentTextColor; - uint16_t _currentBackgroundColor; - - void UpdateCurrentColors() + public: + enum Colors : uint8_t { - if (_state == ButtonState::Highlighted) - { - _currentBackgroundColor = _backgroundHighlightColor; - _currentTextColor = _textHighlightColor; + Text = 0, + Background = 1, + }; - } else - { - _currentBackgroundColor = _backgroundColor; - _currentTextColor = _textColor; - } - } - - public: explicit PushButton(const char* label, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IButton(x, y, width, height), _label(label), _cornerRadius(3), _state(ButtonState::Default) + IButton(x, y, width, height), _label(label), _cornerRadius(3) { - _currentTextColor = _textColor = (uint16_t)Color565::Black; - _currentBackgroundColor = _backgroundColor = utils::RGB565(220, 220, 220); + SetColor(ButtonState::Default, Colors::Text, (uint16_t)Color565::Black); + SetColor(ButtonState::Default, Colors::Background, (uint16_t)utils::RGB565(220, 220, 220)); - _backgroundHighlightColor = (uint16_t)Color565::AXIOM_Blue; - _textHighlightColor = (uint16_t)Color565::Black; + SetColor(ButtonState::Highlighted, Colors::Text, (uint16_t)Color565::Black); + SetColor(ButtonState::Highlighted, Colors::Background, (uint16_t)Color565::AXIOM_Blue); } void SetCornerRadius(uint8_t cornerRadius) @@ -60,42 +43,12 @@ class PushButton : public IButton virtual void Draw(IPainter* painter) override { - painter->DrawFillRoundRectangle(_x, _y, _width, _height, _cornerRadius, _currentBackgroundColor); + painter->DrawFillRoundRectangle(_x, _y, _width, _height, _cornerRadius, GetCurrentColor(Colors::Background)); painter->SetFont(Font::FreeSans12pt7b); uint8_t textPosY = _height / 2 + painter->GetCurrentFontHeight() / 2; - painter->DrawText(_x + _width / 2, _y + textPosY, _label, _currentTextColor, TextAlign::TEXT_ALIGN_CENTER, - strlen(_label)); - } - - void SetBackgroundColor(uint16_t color) - { - _backgroundColor = color; - UpdateCurrentColors(); - } - - void SetTextColor(uint16_t color) - { - _textColor = color; - UpdateCurrentColors(); - } - - void SetHighlightBackgroundColor(uint16_t color) - { - _backgroundHighlightColor = color; - UpdateCurrentColors(); - } - - void SetHighlightTextColor(uint16_t color) - { - _textHighlightColor = color; - UpdateCurrentColors(); - } - - void SetHighlighted(bool highlighted) - { - _state = highlighted ? ButtonState::Highlighted : ButtonState::Default; - UpdateCurrentColors(); + painter->DrawText(_x + _width / 2, _y + textPosY, _label, GetCurrentColor(Colors::Text), + TextAlign::TEXT_ALIGN_CENTER, strlen(_label)); } }; From 8ab9ca2a573f7a42e5d40fd6855530dc2e3c000c Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sat, 27 Mar 2021 18:28:19 +0100 Subject: [PATCH 04/11] Port ImageButton.h to use IButton state machine --- Firmware/UI/Widgets/ImageButton.h | 95 ++++++------------------------- 1 file changed, 16 insertions(+), 79 deletions(-) diff --git a/Firmware/UI/Widgets/ImageButton.h b/Firmware/UI/Widgets/ImageButton.h index c1cacc90..e2fac6d1 100644 --- a/Firmware/UI/Widgets/ImageButton.h +++ b/Firmware/UI/Widgets/ImageButton.h @@ -19,20 +19,8 @@ class ImageButton : public IButton const char* _label; uint8_t _cornerRadius; - ButtonState _state; - // Color Defintions - uint16_t _textColor; - uint16_t _imageColor; - uint16_t _backgroundColor; - - uint16_t _textHighlightColor; - uint16_t _imageHighlightColor; - uint16_t _backgroundHighlightColor; - - uint16_t _currentTextColor; - uint16_t _currentImageColor; - uint16_t _currentBackgroundColor; + // Color Definitions uint8_t _imagePositionX; uint8_t _textPositionX; @@ -42,30 +30,20 @@ class ImageButton : public IButton ButtonStyle _buttonStyle; - void UpdateCurrentColors() + public: + enum Colors : uint8_t { - if (_state == ButtonState::Highlighted) - { - _currentImageColor = _imageHighlightColor; - _currentBackgroundColor = _backgroundHighlightColor; - _currentTextColor = _textHighlightColor; - - } else - { - _currentImageColor = _imageColor; - _currentBackgroundColor = _backgroundColor; - _currentTextColor = _textColor; - } - } + Text = 0, + Background = 1, + Image = 2, + }; - public: explicit ImageButton(const Icon* icon, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IButton(x, y, width, height), _image(icon), _cornerRadius(3), _state(ButtonState::Default), - _imageColor((uint16_t)Color565::Black), _currentImageColor(_imageColor), - _backgroundHighlightColor((uint16_t)Color565::AXIOM_Blue), - _currentBackgroundColor(utils::RGB565(220, 220, 220)), _backgroundColor(utils::RGB565(220, 220, 220)), - _buttonStyle(ButtonStyle::Icon) + IButton(x, y, width, height), _image(icon), _cornerRadius(3), _buttonStyle(ButtonStyle::Icon) { + SetColor(ButtonState::Default, Colors::Image, static_cast(Color565::Black)); + SetColor(ButtonState::Default, Colors::Background, utils::RGB565(220, 220, 220)); + SetColor(ButtonState::Highlighted, Colors::Background, static_cast(Color565::AXIOM_Blue)); _totalWidth = _image->Width; _textPositionY = _height / 2; _imagePositionX = _width / 2 - _totalWidth / 2; @@ -105,7 +83,7 @@ class ImageButton : public IButton virtual void Draw(IPainter* painter) override { - painter->DrawFillRoundRectangle(_x, _y, _width, _height, _cornerRadius, _currentBackgroundColor); + painter->DrawFillRoundRectangle(_x, _y, _width, _height, _cornerRadius, GetCurrentColor(Colors::Background)); if (_buttonStyle == ButtonStyle::IconAndText) { @@ -116,57 +94,16 @@ class ImageButton : public IButton _textPositionY += painter->GetCurrentFontHeight() / 2; // TODO: This should not be recalculated with every redraw - painter->DrawIcon(_image, _x + _imagePositionX, _y + _height / 2 - _image->Height / 2, _currentImageColor); - painter->DrawText(_x + _textPositionX, _y + _textPositionY, _label, _currentTextColor, + painter->DrawIcon(_image, _x + _imagePositionX, _y + _height / 2 - _image->Height / 2, + GetCurrentColor(Colors::Image)); + painter->DrawText(_x + _textPositionX, _y + _textPositionY, _label, GetCurrentColor(Colors::Text), TextAlign::TEXT_ALIGN_LEFT, strlen(_label)); } else if (_buttonStyle == ButtonStyle::Icon) { painter->DrawIcon(_image, _x + _width / 2 - _image->Width / 2, _y + _height / 2 - _image->Height / 2, - _currentImageColor); + GetCurrentColor(Colors::Image)); } } - - void SetBackgroundColor(uint16_t color) - { - _backgroundColor = color; - UpdateCurrentColors(); - } - - void SetImageColor(uint16_t color) - { - _imageColor = color; - UpdateCurrentColors(); - } - - void SetTextColor(uint16_t color) - { - _textColor = color; - UpdateCurrentColors(); - } - - void SetHighlightBackgroundColor(uint16_t color) - { - _backgroundHighlightColor = color; - UpdateCurrentColors(); - } - - void SetHighlightImageColor(uint16_t color) - { - _imageHighlightColor = color; - UpdateCurrentColors(); - } - - void SetHighlightTextColor(uint16_t color) - { - _textHighlightColor = color; - UpdateCurrentColors(); - } - - void SetHighlighted(bool highlighted) - { - _state = highlighted ? ButtonState::Highlighted : ButtonState::Default; - UpdateCurrentColors(); - } }; #endif // IMAGEBUTTON_H From 383c3963092d46255da5a277923b08c8f98f6478 Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sat, 27 Mar 2021 18:40:25 +0100 Subject: [PATCH 05/11] Remove commented out function --- Firmware/UI/Widgets/IButton.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Firmware/UI/Widgets/IButton.h b/Firmware/UI/Widgets/IButton.h index bd514527..a2af2d3f 100755 --- a/Firmware/UI/Widgets/IButton.h +++ b/Firmware/UI/Widgets/IButton.h @@ -61,11 +61,6 @@ class IButton : public IWidget return _colors[static_cast(_currentState)][index]; } - // uint16_t[_colorsPerState] GetCurrentColors() - // { - // return _colors[static_cast(_currentState)]; - // } - void SetState(ButtonState state) { // TODO: checks to see if valid transition, transition function? From 30c5ab55e4230f5dcdf8a307698244c4356bd5da Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sun, 28 Mar 2021 10:53:34 +0200 Subject: [PATCH 06/11] Add temp solution for custom array sizes in subclasses --- Firmware/UI/Widgets/IButton.h | 37 ++++++++++++++++++------- Firmware/UI/Widgets/ImageButton.h | 12 +++++++- Firmware/UI/Widgets/MainPageButton.h | 41 ++++++++++++---------------- Firmware/UI/Widgets/PushButton.h | 11 +++++++- Firmware/UI/Widgets/ToggleButton.h | 10 +++++-- 5 files changed, 74 insertions(+), 37 deletions(-) diff --git a/Firmware/UI/Widgets/IButton.h b/Firmware/UI/Widgets/IButton.h index a2af2d3f..095b1e66 100755 --- a/Firmware/UI/Widgets/IButton.h +++ b/Firmware/UI/Widgets/IButton.h @@ -16,6 +16,7 @@ enum class ButtonState : uint8_t Highlighted = 1, Disabled = 2, }; + class IButton : public IWidget { // void* -> sender, e.g. MainPage @@ -23,15 +24,22 @@ class IButton : public IWidget // Might want to move these three into implementers instead, // to allow for correct sizes - static constexpr uint8_t _colorsPerState = 4; - static constexpr uint8_t _stateCount = 3; - uint16_t _colors[_stateCount][_colorsPerState]; + + uint16_t* _colors; + uint8_t _colorsPerState; // Should really be const. + uint8_t _stateCount; // See above. + + protected: ButtonState _currentState; + virtual uint16_t* GetStatePtr() = 0; public: // TODO: add startState to constructor? - IButton(uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IWidget(x, y, width, height), _handlerPtr(nullptr), _currentState(ButtonState::Default) + IButton(uint8_t stateCount, uint8_t colorsPerState, uint16_t* colorPointer, uint16_t x = 0, uint16_t y = 0, + uint16_t width = 0, uint16_t height = 0) : + IWidget(x, y, width, height), + _handlerPtr(nullptr), _currentState(ButtonState::Default), _colors(colorPointer), + _colorsPerState(colorsPerState), _stateCount(colorsPerState) { } @@ -45,27 +53,36 @@ class IButton : public IWidget _handlerPtr(sender); } - // index: every subclass is supposed to define own enum for colorMeanings + // index: every derived class is supposed to define its own enum for colorMeanings + + uint8_t Index(ButtonState state, uint8_t index) + { + return static_cast(state) * _colorsPerState + index; + } void SetColor(ButtonState state, uint8_t index, uint16_t color) { - _colors[static_cast(state)][index] = color; + _colors[static_cast(state) * _colorsPerState + index] = color; } uint16_t GetColor(ButtonState state, uint8_t index) { - return _colors[static_cast(state)][index]; + return GetStatePtr()[static_cast(state) * _colorsPerState + index]; } uint16_t GetCurrentColor(uint8_t index) { - return _colors[static_cast(_currentState)][index]; + return GetColor(_currentState, index); } void SetState(ButtonState state) { - // TODO: checks to see if valid transition, transition function? _currentState = state; } + + ButtonState GetState() + { + return _currentState; + } }; #endif /* IBUTTON_H */ diff --git a/Firmware/UI/Widgets/ImageButton.h b/Firmware/UI/Widgets/ImageButton.h index e2fac6d1..60fdb703 100644 --- a/Firmware/UI/Widgets/ImageButton.h +++ b/Firmware/UI/Widgets/ImageButton.h @@ -21,6 +21,9 @@ class ImageButton : public IButton uint8_t _cornerRadius; // Color Definitions + static constexpr uint8_t _stateCount = 3; + static constexpr uint8_t _colorsPerState = 3; + uint16_t colors[_stateCount * _colorsPerState]; uint8_t _imagePositionX; uint8_t _textPositionX; @@ -30,6 +33,12 @@ class ImageButton : public IButton ButtonStyle _buttonStyle; + protected: + uint16_t* GetStatePtr() override + { + return colors; + } + public: enum Colors : uint8_t { @@ -39,7 +48,8 @@ class ImageButton : public IButton }; explicit ImageButton(const Icon* icon, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IButton(x, y, width, height), _image(icon), _cornerRadius(3), _buttonStyle(ButtonStyle::Icon) + IButton(_stateCount, _colorsPerState, colors, x, y, width, height), _image(icon), _cornerRadius(3), + _buttonStyle(ButtonStyle::Icon) { SetColor(ButtonState::Default, Colors::Image, static_cast(Color565::Black)); SetColor(ButtonState::Default, Colors::Background, utils::RGB565(220, 220, 220)); diff --git a/Firmware/UI/Widgets/MainPageButton.h b/Firmware/UI/Widgets/MainPageButton.h index 49848f64..b37f8455 100755 --- a/Firmware/UI/Widgets/MainPageButton.h +++ b/Firmware/UI/Widgets/MainPageButton.h @@ -35,38 +35,35 @@ class MainPageButton : public IButton // Color Defintions + static constexpr uint8_t _stateCount = 3; + static constexpr uint8_t _colorsPerState = 4; + uint16_t colors[_stateCount * _colorsPerState]; + + // bool _hideValue; + protected: + uint16_t* GetStatePtr() override + { + return colors; + } + + public: enum Colors : uint8_t { LabelText = 0, LabelBackground = 1, ValueText = 2, - ValueBackground = 2, + ValueBackground = 3, }; - // uint16_t labelTextColor; - // uint16_t labelBackgroundColor; - // uint16_t valueTextColor; - // uint16_t valueBackgroundColor; - // uint16_t textHighlightColor; - // uint16_t backgroundHighlightColor; - - // uint16_t currentLabelTextColor; - // uint16_t currentLabelBackgroundColor; - // uint16_t currentValueTextColor; - // uint16_t currentValueBackgroundColor; - - // bool _hideValue; - - public: - MainPageButton() : IButton() + MainPageButton() : IButton(_stateCount, _colorsPerState, colors) { } // TODO: Check if customizable height is required for this button, if yes, add it later MainPageButton(uint16_t x, uint16_t y, uint16_t width, const char* label = "...", bool invertOrder = false, ButtonType type = ButtonType::VALUE_AND_LABEL) : - _x(x), - _y(y), _width(width), _labelHeight(20), _valueHeight(40), _label((char*)label), _value((char*)"..."), + IButton(_stateCount, _colorsPerState, colors), + _x(x), _y(y), _width(width), _labelHeight(20), _valueHeight(40), _label((char*)label), _value((char*)"..."), _invertOrder(invertOrder), _labelFont(Font::FreeSans9pt7b), _valueFont(Font::FreeSans12pt7b), _type(type) { SetColor(ButtonState::Default, Colors::LabelText, static_cast(Color565::White)); @@ -107,11 +104,9 @@ class MainPageButton : public IButton void DrawButton(IPainter* painter) { - // const uint16_t current[] = GetCurrentColor(); - painter->DrawFillRoundRectangle(_x, _y, _width, _labelHeight, 3, GetCurrentColor(Colors::LabelBackground)); + painter->DrawFillRoundRectangle(_x, _y, _width, _labelHeight, 3, colors[Colors::LabelBackground]); painter->SetFont(_labelFont); - painter->DrawText(_x, _y + 24, _label, GetCurrentColor(Colors::LabelText), TextAlign::TEXT_ALIGN_CENTER, - _width); + painter->DrawText(_x, _y + 24, _label, colors[Colors::LabelText], TextAlign::TEXT_ALIGN_CENTER, _width); } void DrawLabelBox(IPainter* painter, int8_t verticaloffset, int8_t verticaltextoffset) diff --git a/Firmware/UI/Widgets/PushButton.h b/Firmware/UI/Widgets/PushButton.h index 479f53f4..63d6742e 100644 --- a/Firmware/UI/Widgets/PushButton.h +++ b/Firmware/UI/Widgets/PushButton.h @@ -13,6 +13,15 @@ class PushButton : public IButton uint8_t _cornerRadius; // Color Defintions + static constexpr uint8_t _stateCount = 3; + static constexpr uint8_t _colorsPerState = 2; + uint16_t colors[_stateCount * _colorsPerState]; + + protected: + uint16_t* GetStatePtr() override + { + return colors; + } public: enum Colors : uint8_t @@ -22,7 +31,7 @@ class PushButton : public IButton }; explicit PushButton(const char* label, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IButton(x, y, width, height), _label(label), _cornerRadius(3) + IButton(_stateCount, _colorsPerState, colors, x, y, width, height), _label(label), _cornerRadius(3) { SetColor(ButtonState::Default, Colors::Text, (uint16_t)Color565::Black); SetColor(ButtonState::Default, Colors::Background, (uint16_t)utils::RGB565(220, 220, 220)); diff --git a/Firmware/UI/Widgets/ToggleButton.h b/Firmware/UI/Widgets/ToggleButton.h index e092b23f..c2063b34 100644 --- a/Firmware/UI/Widgets/ToggleButton.h +++ b/Firmware/UI/Widgets/ToggleButton.h @@ -26,10 +26,16 @@ class ToggleButton : public IButton const char* _label; const Icon* _checkboxIcon; + protected: + uint16_t* GetStatePtr() override + { + return nullptr; + } + public: explicit ToggleButton(const char* label, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IButton(x, y, width, height), _label(label), _cornerRadius(3), _highlighted(false), _checked(true), - _currentTextColor((uint16_t)Color565::Black), _textColor(_currentTextColor), + IButton(0, 0, nullptr, x, y, width, height), _label(label), _cornerRadius(3), _highlighted(false), + _checked(true), _currentTextColor((uint16_t)Color565::Black), _textColor(_currentTextColor), _currentBackgroundColor(utils::RGB565(220, 220, 220)), _backgroundColor(_currentBackgroundColor), _textDisabledColor(utils::RGB565(180, 180, 180)), _backgroundHighlightColor((uint16_t)Color565::AXIOM_Blue), _textHighlightColor((uint16_t)Color565::Black) From 7e1db76319d2d70f9e9d5001e2a973191405e50f Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sun, 28 Mar 2021 10:57:36 +0200 Subject: [PATCH 07/11] Init MainPage buttons using Initializer list --- Firmware/UI/Screens/MainPage.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Firmware/UI/Screens/MainPage.cpp b/Firmware/UI/Screens/MainPage.cpp index a55634f5..b719ef66 100644 --- a/Firmware/UI/Screens/MainPage.cpp +++ b/Firmware/UI/Screens/MainPage.cpp @@ -4,7 +4,11 @@ #include "../MenuSystem.h" // TODO: Add assignment of menu system to IMenu -MainPage::MainPage(IUSBDevice* cdcDevice) : IMenu(cdcDevice), _backgroundColor(Color565::MenuBackground) +MainPage::MainPage(IUSBDevice* cdcDevice) : + IMenu(cdcDevice), _backgroundColor(Color565::MenuBackground), _fpsButton(10, 0, 90, "FPS", false), + _analogGainButton(115, 0, 90, "A. Gain", false), _digitalGainButton(220, 0, 90, "D. Gain", false), + _menuButton(10, 210, 90, "MENU", true, ButtonType::BUTTON), _shutterButton(115, 179, 90, "Shutter", true), + _whiteBalanceButton(220, 179, 90, "WB", true) { SetupButtons(); } @@ -12,22 +16,15 @@ MainPage::MainPage(IUSBDevice* cdcDevice) : IMenu(cdcDevice), _backgroundColor(C void MainPage::SetupButtons() { // Top row - _fpsButton = MainPageButton(10, 0, 90, "FPS", false); - _analogGainButton = MainPageButton(115, 0, 90, "A. Gain", false); _analogGainButton.SetHandler(&AnalogGainButtonHandler); - _digitalGainButton = MainPageButton(220, 0, 90, "D. Gain", false), _digitalGainButton.SetHandler(&DigitalGainButtonHandler); // Bottom row - _menuButton = MainPageButton(10, 210, 90, "MENU", true, ButtonType::BUTTON); _menuButton.SetLabelHeight(30); _menuButton.SetLabelFont(Font::FreeSans12pt7b); _menuButton.SetHandler(&MenuButtonHandler); - - _shutterButton = MainPageButton(115, 179, 90, "Shutter", true); - _whiteBalanceButton = MainPageButton(220, 179, 90, "WB", true); } Color565 MainPage::GetBackgroundColor() From 043052fa51dbc81120f2e6878a5b80d76ba45131 Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sun, 28 Mar 2021 11:02:55 +0200 Subject: [PATCH 08/11] Make IButton use color pointer --- Firmware/UI/Widgets/IButton.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Firmware/UI/Widgets/IButton.h b/Firmware/UI/Widgets/IButton.h index 095b1e66..3a98cfff 100755 --- a/Firmware/UI/Widgets/IButton.h +++ b/Firmware/UI/Widgets/IButton.h @@ -26,8 +26,14 @@ class IButton : public IWidget // to allow for correct sizes uint16_t* _colors; - uint8_t _colorsPerState; // Should really be const. - uint8_t _stateCount; // See above. + const uint8_t _colorsPerState; // Should really be const. + const uint8_t _stateCount; // See above. + + // index: every derived class is supposed to define its own enum for colorMeanings + uint8_t Index(ButtonState state, uint8_t index) + { + return static_cast(state) * _colorsPerState + index; + } protected: ButtonState _currentState; @@ -53,20 +59,14 @@ class IButton : public IWidget _handlerPtr(sender); } - // index: every derived class is supposed to define its own enum for colorMeanings - - uint8_t Index(ButtonState state, uint8_t index) - { - return static_cast(state) * _colorsPerState + index; - } void SetColor(ButtonState state, uint8_t index, uint16_t color) { - _colors[static_cast(state) * _colorsPerState + index] = color; + _colors[Index(state, index)] = color; } uint16_t GetColor(ButtonState state, uint8_t index) { - return GetStatePtr()[static_cast(state) * _colorsPerState + index]; + return _colors[Index(state, index)]; } uint16_t GetCurrentColor(uint8_t index) From 6ae9519315f1273c05bab121765ad980460b121b Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sun, 28 Mar 2021 11:12:17 +0200 Subject: [PATCH 09/11] Removed virtual Color Array Pointer solution --- Firmware/UI/Widgets/IButton.h | 14 +++----------- Firmware/UI/Widgets/ImageButton.h | 6 ------ Firmware/UI/Widgets/MainPageButton.h | 5 ----- Firmware/UI/Widgets/PushButton.h | 6 ------ Firmware/UI/Widgets/ToggleButton.h | 6 ------ 5 files changed, 3 insertions(+), 34 deletions(-) diff --git a/Firmware/UI/Widgets/IButton.h b/Firmware/UI/Widgets/IButton.h index 3a98cfff..0d652f7e 100755 --- a/Firmware/UI/Widgets/IButton.h +++ b/Firmware/UI/Widgets/IButton.h @@ -26,8 +26,9 @@ class IButton : public IWidget // to allow for correct sizes uint16_t* _colors; - const uint8_t _colorsPerState; // Should really be const. - const uint8_t _stateCount; // See above. + const uint8_t _colorsPerState; + const uint8_t _stateCount; + ButtonState _currentState; // index: every derived class is supposed to define its own enum for colorMeanings uint8_t Index(ButtonState state, uint8_t index) @@ -35,10 +36,6 @@ class IButton : public IWidget return static_cast(state) * _colorsPerState + index; } - protected: - ButtonState _currentState; - virtual uint16_t* GetStatePtr() = 0; - public: // TODO: add startState to constructor? IButton(uint8_t stateCount, uint8_t colorsPerState, uint16_t* colorPointer, uint16_t x = 0, uint16_t y = 0, @@ -78,11 +75,6 @@ class IButton : public IWidget { _currentState = state; } - - ButtonState GetState() - { - return _currentState; - } }; #endif /* IBUTTON_H */ diff --git a/Firmware/UI/Widgets/ImageButton.h b/Firmware/UI/Widgets/ImageButton.h index 60fdb703..ea22f0b2 100644 --- a/Firmware/UI/Widgets/ImageButton.h +++ b/Firmware/UI/Widgets/ImageButton.h @@ -33,12 +33,6 @@ class ImageButton : public IButton ButtonStyle _buttonStyle; - protected: - uint16_t* GetStatePtr() override - { - return colors; - } - public: enum Colors : uint8_t { diff --git a/Firmware/UI/Widgets/MainPageButton.h b/Firmware/UI/Widgets/MainPageButton.h index b37f8455..60033c4d 100755 --- a/Firmware/UI/Widgets/MainPageButton.h +++ b/Firmware/UI/Widgets/MainPageButton.h @@ -40,11 +40,6 @@ class MainPageButton : public IButton uint16_t colors[_stateCount * _colorsPerState]; // bool _hideValue; - protected: - uint16_t* GetStatePtr() override - { - return colors; - } public: enum Colors : uint8_t diff --git a/Firmware/UI/Widgets/PushButton.h b/Firmware/UI/Widgets/PushButton.h index 63d6742e..6ff2fec5 100644 --- a/Firmware/UI/Widgets/PushButton.h +++ b/Firmware/UI/Widgets/PushButton.h @@ -17,12 +17,6 @@ class PushButton : public IButton static constexpr uint8_t _colorsPerState = 2; uint16_t colors[_stateCount * _colorsPerState]; - protected: - uint16_t* GetStatePtr() override - { - return colors; - } - public: enum Colors : uint8_t { diff --git a/Firmware/UI/Widgets/ToggleButton.h b/Firmware/UI/Widgets/ToggleButton.h index c2063b34..678fcf85 100644 --- a/Firmware/UI/Widgets/ToggleButton.h +++ b/Firmware/UI/Widgets/ToggleButton.h @@ -26,12 +26,6 @@ class ToggleButton : public IButton const char* _label; const Icon* _checkboxIcon; - protected: - uint16_t* GetStatePtr() override - { - return nullptr; - } - public: explicit ToggleButton(const char* label, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : IButton(0, 0, nullptr, x, y, width, height), _label(label), _cornerRadius(3), _highlighted(false), From 476fd5ac94193784f507601f9d924fbf0732b46c Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sun, 28 Mar 2021 11:23:23 +0200 Subject: [PATCH 10/11] Rename color array to adhere to naming conventions --- Firmware/UI/Widgets/IButton.h | 8 ++++---- Firmware/UI/Widgets/ImageButton.h | 4 ++-- Firmware/UI/Widgets/MainPageButton.h | 11 ++++++----- Firmware/UI/Widgets/PushButton.h | 4 ++-- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Firmware/UI/Widgets/IButton.h b/Firmware/UI/Widgets/IButton.h index 0d652f7e..7a472b35 100755 --- a/Firmware/UI/Widgets/IButton.h +++ b/Firmware/UI/Widgets/IButton.h @@ -25,7 +25,7 @@ class IButton : public IWidget // Might want to move these three into implementers instead, // to allow for correct sizes - uint16_t* _colors; + uint16_t* _colorPtr; const uint8_t _colorsPerState; const uint8_t _stateCount; ButtonState _currentState; @@ -41,7 +41,7 @@ class IButton : public IWidget IButton(uint8_t stateCount, uint8_t colorsPerState, uint16_t* colorPointer, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : IWidget(x, y, width, height), - _handlerPtr(nullptr), _currentState(ButtonState::Default), _colors(colorPointer), + _handlerPtr(nullptr), _currentState(ButtonState::Default), _colorPtr(colorPointer), _colorsPerState(colorsPerState), _stateCount(colorsPerState) { } @@ -58,12 +58,12 @@ class IButton : public IWidget void SetColor(ButtonState state, uint8_t index, uint16_t color) { - _colors[Index(state, index)] = color; + _colorPtr[Index(state, index)] = color; } uint16_t GetColor(ButtonState state, uint8_t index) { - return _colors[Index(state, index)]; + return _colorPtr[Index(state, index)]; } uint16_t GetCurrentColor(uint8_t index) diff --git a/Firmware/UI/Widgets/ImageButton.h b/Firmware/UI/Widgets/ImageButton.h index ea22f0b2..1236d7e3 100644 --- a/Firmware/UI/Widgets/ImageButton.h +++ b/Firmware/UI/Widgets/ImageButton.h @@ -23,7 +23,7 @@ class ImageButton : public IButton // Color Definitions static constexpr uint8_t _stateCount = 3; static constexpr uint8_t _colorsPerState = 3; - uint16_t colors[_stateCount * _colorsPerState]; + uint16_t _colors[_stateCount * _colorsPerState]; uint8_t _imagePositionX; uint8_t _textPositionX; @@ -42,7 +42,7 @@ class ImageButton : public IButton }; explicit ImageButton(const Icon* icon, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IButton(_stateCount, _colorsPerState, colors, x, y, width, height), _image(icon), _cornerRadius(3), + IButton(_stateCount, _colorsPerState, _colors, x, y, width, height), _image(icon), _cornerRadius(3), _buttonStyle(ButtonStyle::Icon) { SetColor(ButtonState::Default, Colors::Image, static_cast(Color565::Black)); diff --git a/Firmware/UI/Widgets/MainPageButton.h b/Firmware/UI/Widgets/MainPageButton.h index 60033c4d..ba6cdf3f 100755 --- a/Firmware/UI/Widgets/MainPageButton.h +++ b/Firmware/UI/Widgets/MainPageButton.h @@ -37,7 +37,7 @@ class MainPageButton : public IButton static constexpr uint8_t _stateCount = 3; static constexpr uint8_t _colorsPerState = 4; - uint16_t colors[_stateCount * _colorsPerState]; + uint16_t _colors[_stateCount * _colorsPerState]; // bool _hideValue; @@ -50,14 +50,14 @@ class MainPageButton : public IButton ValueBackground = 3, }; - MainPageButton() : IButton(_stateCount, _colorsPerState, colors) + MainPageButton() : IButton(_stateCount, _colorsPerState, _colors) { } // TODO: Check if customizable height is required for this button, if yes, add it later MainPageButton(uint16_t x, uint16_t y, uint16_t width, const char* label = "...", bool invertOrder = false, ButtonType type = ButtonType::VALUE_AND_LABEL) : - IButton(_stateCount, _colorsPerState, colors), + IButton(_stateCount, _colorsPerState, _colors), _x(x), _y(y), _width(width), _labelHeight(20), _valueHeight(40), _label((char*)label), _value((char*)"..."), _invertOrder(invertOrder), _labelFont(Font::FreeSans9pt7b), _valueFont(Font::FreeSans12pt7b), _type(type) { @@ -99,9 +99,10 @@ class MainPageButton : public IButton void DrawButton(IPainter* painter) { - painter->DrawFillRoundRectangle(_x, _y, _width, _labelHeight, 3, colors[Colors::LabelBackground]); + painter->DrawFillRoundRectangle(_x, _y, _width, _labelHeight, 3, GetCurrentColor(Colors::LabelBackground)); painter->SetFont(_labelFont); - painter->DrawText(_x, _y + 24, _label, colors[Colors::LabelText], TextAlign::TEXT_ALIGN_CENTER, _width); + painter->DrawText(_x, _y + 24, _label, GetCurrentColor(Colors::LabelText), TextAlign::TEXT_ALIGN_CENTER, + _width); } void DrawLabelBox(IPainter* painter, int8_t verticaloffset, int8_t verticaltextoffset) diff --git a/Firmware/UI/Widgets/PushButton.h b/Firmware/UI/Widgets/PushButton.h index 6ff2fec5..47d2ed28 100644 --- a/Firmware/UI/Widgets/PushButton.h +++ b/Firmware/UI/Widgets/PushButton.h @@ -15,7 +15,7 @@ class PushButton : public IButton // Color Defintions static constexpr uint8_t _stateCount = 3; static constexpr uint8_t _colorsPerState = 2; - uint16_t colors[_stateCount * _colorsPerState]; + uint16_t _colors[_stateCount * _colorsPerState]; public: enum Colors : uint8_t @@ -25,7 +25,7 @@ class PushButton : public IButton }; explicit PushButton(const char* label, uint16_t x = 0, uint16_t y = 0, uint16_t width = 0, uint16_t height = 0) : - IButton(_stateCount, _colorsPerState, colors, x, y, width, height), _label(label), _cornerRadius(3) + IButton(_stateCount, _colorsPerState, _colors, x, y, width, height), _label(label), _cornerRadius(3) { SetColor(ButtonState::Default, Colors::Text, (uint16_t)Color565::Black); SetColor(ButtonState::Default, Colors::Background, (uint16_t)utils::RGB565(220, 220, 220)); From d2729adc66d079a6e136ba85d0e580c63c71adf3 Mon Sep 17 00:00:00 2001 From: Markus Engsner Date: Sun, 28 Mar 2021 11:28:28 +0200 Subject: [PATCH 11/11] Remove empty ButtonState.h file --- Firmware/UI/Widgets/ButtonState.h | 5 ----- Firmware/UI/Widgets/ImageButton.h | 1 - Firmware/UI/Widgets/PushButton.h | 1 - 3 files changed, 7 deletions(-) delete mode 100644 Firmware/UI/Widgets/ButtonState.h diff --git a/Firmware/UI/Widgets/ButtonState.h b/Firmware/UI/Widgets/ButtonState.h deleted file mode 100644 index 6715e74d..00000000 --- a/Firmware/UI/Widgets/ButtonState.h +++ /dev/null @@ -1,5 +0,0 @@ - -#ifndef BUTTON_STATE_H -#define BUTTON_STATE_H - -#endif // BUTTON_STATE_H diff --git a/Firmware/UI/Widgets/ImageButton.h b/Firmware/UI/Widgets/ImageButton.h index 1236d7e3..5090a0cd 100644 --- a/Firmware/UI/Widgets/ImageButton.h +++ b/Firmware/UI/Widgets/ImageButton.h @@ -2,7 +2,6 @@ #define IMAGEBUTTON_H #include "IButton.h" -#include "ButtonState.h" #include "../Painter/Painter.h" #include "Icon.h" diff --git a/Firmware/UI/Widgets/PushButton.h b/Firmware/UI/Widgets/PushButton.h index 47d2ed28..db98ed09 100644 --- a/Firmware/UI/Widgets/PushButton.h +++ b/Firmware/UI/Widgets/PushButton.h @@ -4,7 +4,6 @@ #include #include "IButton.h" -#include "ButtonState.h" #include "../Painter/Painter.h" class PushButton : public IButton