The default button type hooks into the FastLED EngineEvents to monitor whether the button is pressed or not. You do not need to run an update function. If you need more control, use ButtonLowLevel directly.
| 51 | // whether the button is pressed or not. You do not need to run an update |
| 52 | // function. If you need more control, use ButtonLowLevel directly. |
| 53 | class Button : public IButtonInput { |
| 54 | public: |
| 55 | Button(int pin, |
| 56 | ButtonStrategy strategy = ButtonStrategy::kHighLowFloating); |
| 57 | |
| 58 | int onClick(fl::function<void()> callback); |
| 59 | void removeOnClick(int id) { |
| 60 | mOnClickCallbacks.remove(id); |
| 61 | } |
| 62 | |
| 63 | void setStrategy(ButtonStrategy strategy) { |
| 64 | mButton.setStrategy(strategy); |
| 65 | } |
| 66 | |
| 67 | bool isPressed() const FL_NOEXCEPT override { |
| 68 | return mButton.isPressed(); |
| 69 | } |
| 70 | |
| 71 | bool clicked() const FL_NOEXCEPT override { |
| 72 | return mClickedThisFrame; |
| 73 | } |
| 74 | |
| 75 | protected: |
| 76 | struct Listener : public EngineEvents::Listener { |
| 77 | Listener(Button *owner); |
| 78 | ~Listener() FL_NOEXCEPT; |
| 79 | void addToEngineEventsOnce(); |
| 80 | |
| 81 | // We do an experiment here, what about listening to the end frame event |
| 82 | // instea do of the begin frame event? This will put the activation of |
| 83 | // this button **before** the next frame. I think this pattern should be |
| 84 | // used for all UI elements, so that the button state is updated before |
| 85 | // the next frame is drawn. This seems like the only way to do this, or |
| 86 | // by using platform pre loop, but not all platforms support that. |
| 87 | void onEndFrame() override; |
| 88 | |
| 89 | private: |
| 90 | Button *mOwner; |
| 91 | bool added = false; |
| 92 | }; |
| 93 | |
| 94 | private: |
| 95 | // mButton is mutable because querying button state requires toggling pin |
| 96 | // mode/level on platforms using kHighLowFloating strategy. The const-ness |
| 97 | // refers to the logical observable state of the Button object, not the |
| 98 | // underlying hardware probe. |
| 99 | mutable ButtonLowLevel mButton; |
| 100 | Listener mListener; |
| 101 | bool mPressedLastFrame = false; // Don't read this variale, it's used internally. |
| 102 | bool mClickedThisFrame = false; // This is true if clicked this frame. |
| 103 | |
| 104 | fl::function_list<void()> mOnClickCallbacks; |
| 105 | // fl::function_list<void(Button&)> mOnChangedCallbacks; |
| 106 | }; |
| 107 | |
| 108 | } // namespace fl |