Create button/menuitem states class and table
Open, Needs TriagePublic


create table for the button/menuitem states

add a class for states as menuitems and buttons are more or less equal

Button states:

  • Default
  • Active
  • Disabled
  • Highlighted
BAndiT1983 updated the task description. (Show Details)Apr 29 2020, 7:24 AM
BAndiT1983 edited subscribers, added: MarkusEngsner; removed: sebastian.
BAndiT1983 added subscribers: sebastian, BAndiT1983.
MarkusEngsner added a comment.EditedSun, Mar 28, 1:52 PM

Hello, I've started working on this.

Here is the progress this far:

It's not finished yet, but I figured it might be a good idea to get some feedback on it.

This ended up being quite a wall of text. Read New functions first, the rest is mostly implementation details.

About the implementation

I've gone with implementing the state switching as part of the IButton interface.
Since the only functionality associated with different ButtonTypes at the moment
is what color to use, this has been integrated into IButton as well.

What differs between different button type is how many colors they need
per state, and (perhaps?) how many states they have.
The way this is implemented is that IButton holds a pointer
to an array of the correct size in the derived class.

Changes for derived classes
All derived classes need to implement and specify the following,
in order to use the color and state management:

  • How many valid states there are
  • How many colors there are per state
  • An array for storing the colors
  • An enum defining what index in the color array is to be used for what.

Regarding the valid states:
At the moment, there is no way to specify which states are valid,
and it's instead assumed that the first n states in enum class ButtonState are to be used.

New functions
A couple of new functions are exposed for users of IButton derived classes:

  • SetColor(ButtonState state, uint8_t index, uint16_t color):
    • This replaces all previous colors setters.
    • index: one of the values defined in the enum in the derived class .
  • SetState(ButtonState state):
    • This is meant to replace all functions like SetHighlighted(), SetChecked()...
    • Should probably be made virtual, to allow derived classes to have greater control over state transitions
  • GetColor(ButtonState state, uint8_t index):
    • Returns the corresponding color in the specified state.
  • GetCurrentColor(uint8_t index):
    • Returns the corresponding color in the current state.

Possible problems with this implementation
Adding this functionality to IButton directly does bloat what should
be just an interface class. At the moment though, all buttons need
this functionality anyway, so this doesn't seem to be that much of an issue.
If need be, one can always extract the implementations in IButton
into a separate class at a later date.

Currently, the allowed states use the same enum class for all buttons.
One might want to do something similar to the current solution with color definitions,
that is to allow each Button to define its own states using an enum.
This would sacrifice some type safety.

Work left

  • Make ToggleButton use this functionality.
  • Add default colors.

I had some issues with the MainPageButtons in MainPage.
Since they weren't initialized in the initializer list,
but rather in SetupButtons(), the pointers to their respective
arrays didn't update properly. This has been temporarily fixed
by initializing the buttons in the initializer list, but this bug should definitely be investigated.
Until then, I've made some member variables in IButton const,
which forces all button initializations to be done with initializer lists.