ZKBase.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * ZKBase.h
  3. *
  4. * Created on: Jun 8, 2017
  5. * Author: guoxs
  6. */
  7. #ifndef _CONTROL_ZKBASE_H_
  8. #define _CONTROL_ZKBASE_H_
  9. #include <string>
  10. #include <string.h>
  11. #include "Common.h"
  12. #include "json/json.h"
  13. using namespace std;
  14. class ZKCanvas;
  15. class ZKBase;
  16. class ZKBasePrivate;
  17. class ZKBaseData {
  18. public:
  19. virtual ~ZKBaseData() = 0;
  20. ZKBase *q_ptr;
  21. };
  22. /**
  23. * @brief 控件基类
  24. */
  25. class ZKBase {
  26. ZK_DECLARE_PRIVATE(ZKBase)
  27. friend class ControlFactory;
  28. public:
  29. ZKBase(ZKBase *pParent);
  30. virtual ~ZKBase();
  31. bool create(const Json::Value &json);
  32. /**
  33. * @brief 获取控件ID值
  34. */
  35. int getID() const;
  36. /**
  37. * @brief 设置控件显示位置
  38. */
  39. void setPosition(const LayoutPosition &position);
  40. /**
  41. * @brief 获取控件位置
  42. */
  43. const LayoutPosition& getPosition() const;
  44. /**
  45. * @brief 设置无效状态
  46. */
  47. void setInvalid(bool isInvalid);
  48. /**
  49. * @brief 是否是无效状态
  50. */
  51. bool isInvalid() const;
  52. /**
  53. * @brief 设置显示状态
  54. */
  55. void setVisible(bool isVisible);
  56. /**
  57. * @brief 是否是显示状态
  58. */
  59. bool isVisible() const;
  60. /**
  61. * @brief 设置选中状态
  62. */
  63. void setSelected(bool isSelected);
  64. /**
  65. * @brief 是否是选中状态
  66. */
  67. bool isSelected() const;
  68. /**
  69. * @brief 设置按下状态
  70. */
  71. void setPressed(bool isPressed);
  72. /**
  73. * @brief 是否是按下状态
  74. */
  75. bool isPressed() const;
  76. /**
  77. * @brief 设置触摸状态
  78. */
  79. void setTouchable(bool isTouchable);
  80. /**
  81. * @brief 是否是可触摸状态
  82. */
  83. bool isTouchable() const;
  84. /**
  85. * @brief 设置触摸穿透
  86. */
  87. void setTouchPass(bool isPass);
  88. /**
  89. * @brief 是否是触摸穿透状态
  90. */
  91. bool isTouchPass() const;
  92. bool isWndValid() const;
  93. /**
  94. * @brief 设置背景图
  95. * @param pPicPath 图片路径
  96. */
  97. void setBackgroundPic(const char *pPicPath);
  98. void setBackgroundBmp(struct _bitmap_t *pBmp);
  99. /**
  100. * @brief 设置背景颜色
  101. * @param color 颜色值为0x ARGB
  102. */
  103. void setBackgroundColor(uint32_t color);
  104. /**
  105. * @brief 设置背景状态颜色
  106. * @param status 状态
  107. * 正常状态: ZK_CONTROL_STATUS_NORMAL
  108. * 按下状态: ZK_CONTROL_STATUS_PRESSED
  109. * 选中状态: ZK_CONTROL_STATUS_SELECTED
  110. * 选中按下状态: ZK_CONTROL_STATUS_PRESSED | ZK_CONTROL_STATUS_SELECTED
  111. * 无效状态: ZK_CONTROL_STATUS_INVALID
  112. * @param color 颜色值为0x ARGB
  113. */
  114. void setBgStatusColor(int status, uint32_t color);
  115. /**
  116. * @brief 设置控件透明度
  117. * @param alpha 透明度值,范围:0 ~ 255,0 表示全透, 255 表示不透明
  118. */
  119. void setAlpha(uint8_t alpha);
  120. /**
  121. * @brief 出蜂鸣器声音
  122. */
  123. void beep();
  124. virtual const char* getClassName() const { return NULL; }
  125. public:
  126. /**
  127. * @brief 点击事件监听接口
  128. */
  129. class IClickListener {
  130. public:
  131. virtual ~IClickListener() { }
  132. virtual void onClick(ZKBase *pBase) = 0;
  133. };
  134. /**
  135. * @brief 长按事件监听接口
  136. */
  137. class ILongClickListener {
  138. public:
  139. virtual ~ILongClickListener() { }
  140. virtual void onLongClick(ZKBase *pBase) = 0;
  141. };
  142. /**
  143. * @brief 触摸监听接口
  144. */
  145. class ITouchListener {
  146. public:
  147. virtual ~ITouchListener() { }
  148. virtual void onTouchEvent(ZKBase *pBase, const MotionEvent &ev) = 0;
  149. };
  150. void setClickListener(IClickListener *pListener);
  151. void setLongClickListener(ILongClickListener *pListener);
  152. void setTouchListener(ITouchListener *pListener);
  153. protected:
  154. ZKBase(ZKBase *pParent, ZKBasePrivate *pBP);
  155. virtual bool createWindow();
  156. virtual void onBeforeCreateWindow(const Json::Value &json);
  157. virtual void onAfterCreateWindow(const Json::Value &json);
  158. virtual ret_t procCtrlFun(const struct _message_t *pMsg);
  159. static ret_t ctrlFun(const struct _message_t *pMsg);
  160. virtual bool onInterceptMessage(const struct _message_t *pMsg);
  161. virtual void onDraw(ZKCanvas *pCanvas);
  162. virtual bool onTouchEvent(const MotionEvent &ev);
  163. virtual void onTimer(int id);
  164. void startTimer(int id, uint32_t time);
  165. void stopTimer(int id);
  166. void resetTimer(int id, uint32_t time);
  167. bool hasTimer(int id);
  168. private:
  169. void parseBaseAttributeFromJson(const Json::Value &json);
  170. protected:
  171. ZKBaseData *d_ptr;
  172. };
  173. #include <vector>
  174. class ControlFactory {
  175. public:
  176. static ControlFactory* getInstance();
  177. bool registerControl(const char *name, ZKBase* (*create)(ZKBase *pParent));
  178. ZKBase* create(const char *name, ZKBase *pParent);
  179. private:
  180. ControlFactory() { }
  181. private:
  182. typedef struct {
  183. std::string ctrlName;
  184. ZKBase* (*create)(ZKBase *pParent);
  185. } SControlInfo;
  186. std::vector<SControlInfo> mControlInfos;
  187. };
  188. #define CONTROLFACTORY ControlFactory::getInstance()
  189. #define REGISTER_CONTROL(name, _ctrl) \
  190. static struct _ControlFactory_##_ctrl { \
  191. static ZKBase* create(ZKBase *pParent) { \
  192. return new _ctrl(pParent); \
  193. } \
  194. _ControlFactory_##_ctrl() { \
  195. CONTROLFACTORY->registerControl(name, create); \
  196. } \
  197. } _autoRegister_Control_##_ctrl
  198. #endif /* _CONTROL_ZKBASE_H_ */