实际上,您通常无法使用 Designer 访问一个按钮的 3 个回调。为此,您需要自己实施。
当按钮只有 2 个状态时,我真的不明白 3 个回调的意义:按下和释放。按下和按下按钮不应该是一样的吗?
我尝试了一个按钮的粗略实现,该按钮在当前状态(按下或释放)的屏幕视图中触发回调。为此,我基于 Flex 按钮的 Designer 实现,基于 ImageButtonStyle.hpp、ClickButtonTrigger.hpp 和 AbstractContainer.hpp 的组合创建了我自己的小部件。
这是小部件的代码以及屏幕视图
自定义按钮容器.hpp
- #ifndef CUSTOMBUTTONCONTAINER_HPP
- #define CUSTOMBUTTONCONTAINER_HPP
-
- #include
- #include
-
- namespace touchgfx
- {
- /**
- * @class CustomButtonTrigger CustomButtonTrigger.hpp
- */
- class CustomButtonContainer : public Container
- {
- public:
-
- /**
- * @fn CustomButtonContainer::CustomButtonContainer()
- *
- * @brief Default constructor.
- */
- CustomButtonContainer() : pressed(false), alpha(255), up(), down(), action(0)
- {
- setTouchable(true);
- buttonImage.setXY(0, 0);
- add(buttonImage);
- }
-
- /**
- * @fn virtual CustomButtonContainer::~CustomButtonContainer()
- *
- * @brief Destructor.
- */
- virtual ~CustomButtonContainer() {}
-
- /**
- * @fn virtual void CustomButtonContainer::setBitmaps(const Bitmap& bmpReleased, const Bitmap& bmpPressed)
- *
- * @brief Sets the bitmaps.
- *
- * @param bmpReleased The bitmap released.
- * @param bmpPressed The bitmap pressed.
- */
- virtual void setBitmaps(const Bitmap& bmpReleased, const Bitmap& bmpPressed)
- {
- up = bmpReleased;
- down = bmpPressed;
- setWidth(down.getWidth());
- setHeight(down.getHeight());
-
- handlePressedUpdated();
- }
-
- /**
- * @fn void CustomButtonContainer::setBitmapXY(uint16_t x, uint16_t y)
- *
- * @brief Sets bitmap xy.
- *
- * @param x An uint16_t to process.
- * @param y An uint16_t to process.
- */
- void setBitmapXY(uint16_t x, uint16_t y)
- {
- buttonImage.setXY(x, y);
- }
-
- /**
- * @fn Bitmap CustomButtonContainer::getCurrentlyDisplayedBitmap() const
- *
- * @brief Gets currently displayed bitmap.
- *
- * @return The currently displayed bitmap.
- */
- Bitmap getCurrentlyDisplayedBitmap() const
- {
- return (pressed ? down : up);
- }
-
- /**
- * @fn void CustomButtonContainer::setPressed(bool isPressed)
- *
- * @brief Sets the pressed state.
- *
- * @param isPressed True if is pressed, false if not.
- */
- void setPressed(bool isPressed)
- {
- pressed = isPressed;
- handlePressedUpdated();
- }
-
- /**
- * @fn bool CustomButtonContainer::getPressed()
- *
- * @brief Gets the pressed state.
- *
- * @return True if it succeeds, false if it fails.
- */
- bool getPressed()
- {
- return pressed;
- }
-
- /**
- * @fn void CustomButtonContainer::setAlpha(uint8_t newAlpha)
- *
- * @brief Sets an alpha value.
- *
- * @param newAlpha The new alpha.
- */
- void setAlpha(uint8_t newAlpha)
- {
- alpha = newAlpha;
- handleAlphaUpdated();
- }
-
- /**
- * @fn uint8_t CustomButtonContainer::getAlpha() const
- *
- * @brief Gets the alpha.
- *
- * @return The alpha value.
- */
- uint8_t getAlpha() const
- {
- return alpha;
- }
-
- /**
- * @fn void CustomButtonContainer::setAction(GenericCallback< const CustomButtonContainer&, bool& >& callback)
- *
- * @brief Sets an action callback.
- *
- * @param callback The callback.
- */
- void setAction(GenericCallback< const CustomButtonContainer&, bool& >& callback)
- {
- action = &callback;
- }
-
- /**
- * @fn virtual void CustomButtonContainer::handleClickEvent(const ClickEvent& event)
- *
- * @brief Handles the click event described by event.
- *
- * @param event The event.
- */
- virtual void handleClickEvent(const ClickEvent& event)
- {
- bool wasPressed = getPressed();
- bool newPressedValue = (event.getType() == ClickEvent::PRESSED);
- if ((newPressedValue && !wasPressed) || (!newPressedValue && wasPressed))
- {
- setPressed(newPressedValue);
- invalidate();
- if (action->isValid())
- {
- action->execute(*this, pressed);
- }
- }
- }
-
-
- protected:
- bool pressed; ///< True if pressed
- uint8_t alpha; ///< The current alpha value. 255 denotes solid, 0 denotes completely transparent.
- Image buttonImage; ///< The button image
- Bitmap up; ///< The image to display when button is released.
- Bitmap down; ///< The image to display when button is pressed.
-
- GenericCallback< const CustomButtonContainer&, bool&>* action; ///< The action
-
- /**
- * @fn virtual void CustomButtonContainer::handlePressedUpdated()
- *
- * @brief Handles the pressed updated.
- */
- virtual void handlePressedUpdated()
- {
- buttonImage.setBitmap(pressed ? down : up);
- }
-
- /**
- * @fn virtual void CustomButtonContainer::handleAlphaUpdated()
- *
- * @brief Handles the alpha updated.
- */
- virtual void handleAlphaUpdated()
- {
- buttonImage.setAlpha(alpha);
- }
- };
-
-
- } // namespace touchgfx
-
- #endif // CUSTOMBUTTONCONTAINER_HPP
Screen1View.hpp
- #ifndef SCREEN1VIEW_HPP
- #define SCREEN1VIEW_HPP
-
- #include
- #include
- #include
-
- class Screen1View : public Screen1ViewBase
- {
- public:
- Screen1View();
- virtual ~Screen1View() {}
- virtual void setupScreen();
- virtual void tearDownScreen();
- protected:
- touchgfx::CustomButtonContainer customButton;
-
- Callback buttonEventCallback;
-
- void handleButtonEvent(const CustomButtonContainer& element, bool& buttonState);
- };
-
- #endif // SCREEN1VIEW_HPP
Screen1View.cpp
- #include
- #include "BitmapDatabase.hpp"
-
- Screen1View::Screen1View():
- buttonEventCallback(this, &Screen1View::handleButtonEvent)
- {
- customButton.setBitmaps(Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_MEDIUM_ID), Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_MEDIUM_PRESSED_ID));
- customButton.setBitmapXY(0, 0);
- customButton.setPosition(285, 200, 230, 60);
-
- add(customButton);
-
- customButton.setAction(buttonEventCallback);
- }
-
- void Screen1View::setupScreen()
- {
- Screen1ViewBase::setupScreen();
- }
-
- void Screen1View::tearDownScreen()
- {
- Screen1ViewBase::tearDownScreen();
- }
-
- void Screen1View::handleButtonEvent(const CustomButtonContainer& element, bool& buttonState)
- {
- if(buttonState)
- {
- touchgfx_printf("pressedn");
- }
- else
- {
- touchgfx_printf("releasedn");
- }
- }
您可以先尝试使用此代码,但要确保设计人员知道您为按钮使用的位图。
需要说明的是,这只是一个粗略的例子,而不是官方的解决方案
|
|
|
2022-12-26 15:23:07
评论
举报
|
|
|
|