TouchGFX버전: 4.18.0
목표
1. Button 클래스를 상속받는 CustomButton을 동적 생성
2. CustomButton은 누르고 있으면 Gauge Up, 누르지 않고 있으면 Gauge Down
TouchGFX의 Button 클래스는 기본적으로 AbstractButton클래스로부터 상속받고 있고, 마우스 클릭을 땔 때 Click 이벤트가 발생되도록 프로그래밍 되어있습니다.
AbstractButton 클래스에 정의되어있는 handleClickEvent
AbstractButton 클래스를 상속받는 Button
목표 2와 같이 구현하려면 handleClickEvent를 재정의 해주어 상태가 바뀔때마다 excuteAction()가 호출되도록 프로그래밍 되어야 합니다.
CustomButton이라는 Button을 상속받는 클래스를 생성하여 handleClickEvent를 재정의 하도록 하겠습니다.
#ifndef CUSTOMBUTTON_HPP
#define CUSTOMBUTTON_HPP
#include <touchgfx/widgets/Button.hpp>
namespace touchgfx
{
class CustomButton: public Button
{
public:
virtual void handleClickEvent(const ClickEvent& event);
CustomButton() :Button(){}
};
}
#endif
#include <touchgfx/events/ClickEvent.hpp>
#include <gui/common/CustomButton.hpp>
namespace touchgfx
{
void CustomButton::handleClickEvent(const ClickEvent& event)
{
bool wasPressed = pressed;
pressed = (event.getType() == ClickEvent::PRESSED);
if ((pressed && !wasPressed) || (!pressed && wasPressed))
{
executeAction();
invalidate();
}
}
}
위와 같이 원하는 동작을 하는 CustomButton 클래스를 만들었습니다.
이제 CustomButton을 Screen1에 동적으로 추가하겠습니다.
#ifndef SCREEN1VIEW_HPP
#define SCREEN1VIEW_HPP
#include <gui_generated/screen1_screen/Screen1ViewBase.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
#include <gui/common/CustomButton.hpp>
class Screen1View : public Screen1ViewBase
{
public:
Screen1View();
virtual ~Screen1View() {}
virtual void setupScreen();
virtual void tearDownScreen();
CustomButton gasButton;
void handleTickEvent();
protected:
bool isPressed;
touchgfx::Callback<Screen1View, const touchgfx::AbstractButton&> gasButtonCallback;
void gasButtonCallbackHandler(const touchgfx::AbstractButton& src);
};
#endif
#include <BitmapDatabase.hpp>
#include <gui/screen1_screen/Screen1View.hpp>
Screen1View::Screen1View():
gasButtonCallback(this, &Screen1View::gasButtonCallbackHandler)
{
isPressed = false;
}
void Screen1View::setupScreen()
{
Screen1ViewBase::setupScreen();
gasButton.setPosition(347, 106, 60, 60);
gasButton.setBitmaps(Bitmap(BITMAP_DARK_BUTTONS_ROUND_EDGE_ICON_BUTTON_ID), Bitmap(BITMAP_DARK_BUTTONS_ROUND_EDGE_ICON_BUTTON_PRESSED_ID));
gasButton.setAction(gasButtonCallback);
add(gasButton);
}
void Screen1View::tearDownScreen()
{
Screen1ViewBase::tearDownScreen();
}
void Screen1View::gasButtonCallbackHandler(const touchgfx::AbstractButton& src)
{
isPressed ^= 1;
}
void Screen1View::handleTickEvent()
{
static int incr = 1;
if (isPressed)
{
incr = +1;
}
else
{
incr = -1;
}
gauge1.setValue(gauge1.getValue() + incr);
}
Callback을 추가하기 위해서
hpp에
touchgfx::Callback<Screen1View, const touchgfx::AbstractButton&> gasButtonCallback;
void gasButtonCallbackHandler(const touchgfx::AbstractButton& src);
cpp에
Screen1View::Screen1View(): gasButtonCallback(this, &Screen1View::gasButtonCallbackHandler)
를 해주어야 한다는 점을 기억해야 합니다.
reference
1. https://make.e4ds.com/st-touchGFX/quest_detail_4.asp
2. Example on how to swipe to change a screen (st.com)