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를 재정의 하도록 하겠습니다.
//TouchGFX/gui/include/gui/common/CustomButton.hpp
#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
// TouchGFX/gui/src/common/CustomButton.cpp
#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);
// 버튼 Press상태가 바뀌었다면
if ((pressed && !wasPressed) || (!pressed && wasPressed))
{
// Action 실행
executeAction();
// invalidate를 해주어야 Release 될 때 이미지가 정상 적용됨
invalidate();
}
}
} // namespace touchgfx
위와 같이 원하는 동작을 하는 CustomButton 클래스를 만들었습니다.
이제 CustomButton을 Screen1에 동적으로 추가하겠습니다.
// TouchGFX/gui/include/gui/screen1_screen/Screen1View.hpp
#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
CustomButton gasButton;
// 매 Tick마다 호출되는 함수
void handleTickEvent();
protected:
// 현재 버튼 누르고 있는 상태
bool isPressed;
// Action Callback
touchgfx::Callback<Screen1View, const touchgfx::AbstractButton&> gasButtonCallback;
void gasButtonCallbackHandler(const touchgfx::AbstractButton& src);
};
#endif // SCREEN1VIEW_HPP
// TouchGFX/gui/src/screen1_screen/Screen1View.cpp
#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));
// Action 설정
gasButton.setAction(gasButtonCallback);
// Screen에 추가
add(gasButton);
}
void Screen1View::tearDownScreen()
{
Screen1ViewBase::tearDownScreen();
}
// 버튼 상태가 변하면 호출되는 Callback
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)