自定义容器
自定义容器
假设某个模块是可以复用的,那么可以用自定义的容器装载这些控件,从而不必在每个UI中都重复定义一套UI。
自定义触发和动作(Trigger & Action)
自定义容器的优势:
custom triggers – callbacks
custom actions – methods
以上两个特点,允许用户自定义该容器的行为模式,而不仅仅只是装载不同控件的可重复的容器,从而能与应用的其他部分进行交互。
编码实现
- 创建一个容器子类:自定义的容器类,
- 声明Children, 作为自定义容器的member
- 设置容器的宽度和高度
- 设置每个child
- 将每个child添加至container内
- 通过method和callbacks完成用户自定义方法
#include <gui/common/FrontendApplication.hpp>
#include <touchgfx/containers/Container.hpp>
## MyCustomContainer.hpp类定义
class MyCustomContainer : public touchgfx::Container //继承自标准的Container
{
public:
MyCustomContainer();
virtual ~MyCustomContainer() {}
virtual void initialize();
protected:
FrontendApplication& application() {
return *static_cast<FrontendApplication*>(touchgfx::Application::getInstance());
}
private:
/*
* Member Declarations
* 添加children, 此处为控件Box和Button
*/
touchgfx::Box myBox;
touchgfx::Button myButton;
};
MyCustomContainer.cpp类声明
#include <gui/include/containers/MyCustomContainer.hpp>
MyCustomContainer::MyCustomContainer()
{
//set width and height
setWidth(250);
setHeight(250);
//set box property
myBox.setPosition(0, 0, 250, 250);
myBox.setColor(touchgfx::Color::getColorFrom24BitRGB(255, 255, 255));
//set button property, set button press and release img
myButton.setXY(40, 95);
myButton.setBitmaps(touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_ID), touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_PRESSED_ID));
//add box and button into the current container
add(myBox);
add(myButton);
}
void MyCustomContainer::initialize()
{
}
实现自定义的方法和回调机制
在自定义的容器中增加自定义行为,在MyCustomContainer.hpp中定义一些method 和 callback。在该例中,定义“dosomething()”方法,该方法的目的是触发回调函数“somethingHappened”。 注意这里和上面的有什么不同。
#include <gui/common/FrontendApplication.hpp>
#include <touchgfx/containers/Container.hpp>
class MyCustomContainer : public touchgfx::Container
{
public:
MyCustomContainerBase();
virtual ~MyCustomContainerBase() {}
virtual void initialize();
/*
* Callback Setters
* 注册回调函数
*/
void setSomethingHappenedCallback(touchgfx::GenericCallback<>& callback)
{
somethingHappenedCallback = &callback;
}
/*
* Methods
* 自定义的方法
*/
virtual void doSomething();
protected:
FrontendApplication& application() {
return *static_cast<FrontendApplication*>(touchgfx::Application::getInstance());
}
/*
* Callback Emitters
*/
virtual void emitSomethingHappenedCallback()
{
if (somethingHappenedCallback && somethingHappenedCallback->isValid())
{
somethingHappenedCallback->execute();
}
}
/*
* Member Declarations
*/
touchgfx::Box myBox;
touchgfx::Button myButton;
private:
/*
* Callback Declarations
* 回调函数声明
*/
touchgfx::GenericCallback<>* somethingHappenedCallback;
};
注册回调函数
/*
* Callback Setters
* 注册回调函数
*/
void setSomethingHappenedCallback(touchgfx::GenericCallback<>& callback)
{
somethingHappenedCallback = &callback;
}
方法
/*
* Methods
* 自定义的方法
*/
virtual void doSomething();
回调函数发射
/*
* Callback Emitters
*/
virtual void emitSomethingHappenedCallback()
{
if (somethingHappenedCallback && somethingHappenedCallback->isValid())
{
somethingHappenedCallback->execute();
}
}
回调函数声明
/*
* Callback Declarations
* 回调函数声明
*/
touchgfx::GenericCallback<>* somethingHappenedCallback;
在MyCustomContainer.cpp中实现doSomthing方法
#include <gui/containers/MyCustomContainer.hpp>
MyCustomContainer::MyCustomContainer()
{
setWidth(250);
setHeight(250);
myBox.setPosition(0, 0, 250, 250);
myBox.setColor(touchgfx::Color::getColorFrom24BitRGB(255, 255, 255));
myButton.setXY(40, 95);
myButton.setBitmaps(touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_ID), touchgfx::Bitmap(BITMAP_BLUE_BUTTONS_ROUND_EDGE_SMALL_PRESSED_ID));
add(myBox);
add(myButton);
}
void MyCustomContainer::initialize()
{
}
void MyCustomContainer::doSomething()
{
MyCustomContainer::emitSomethingHappenedCallback();
}
在上述.cpp中,并没有注册回调函数,及没有调用setSomethingHappenedCallback将具体函数的指针赋给somethingHappenedCallback,所以在emitSomethingHappenedCallback后,并不会执行,因为somethingHappenedCallback->isValid()会返回失败。
那么在哪里去初始化呢,MyCustomContainer::MyCustomContainer()的构造函数里。