touchGFX 之 Custom Container

自定义容器

假设某个模块是可以复用的,那么可以用自定义的容器装载这些控件,从而不必在每个UI中都重复定义一套UI。

自定义触发和动作(Trigger & Action)

自定义容器的优势:

custom triggers – callbacks
custom actions – methods

以上两个特点,允许用户自定义该容器的行为模式,而不仅仅只是装载不同控件的可重复的容器,从而能与应用的其他部分进行交互。

编码实现

  1. 创建一个容器子类:自定义的容器类,
  2. 声明Children, 作为自定义容器的member
  3. 设置容器的宽度和高度
  4. 设置每个child
  5. 将每个child添加至container内
  6. 通过methodcallbacks完成用户自定义方法
#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()的构造函数里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值