pm_stringwheel.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * File : pm_stringwheel.h
  3. * COPYRIGHT (C) 2012-2017, Shanghai Real-Thread Technology Co., Ltd
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2017-11-05 realthread the first version
  8. */
  9. #pragma once
  10. #include <vector>
  11. #include <pm_widget.h>
  12. #include <sigslot.h>
  13. #include <pm_timer.h>
  14. namespace Persimmon
  15. {
  16. class StringWheel : public Widget
  17. {
  18. public:
  19. enum type
  20. {
  21. VERTICAL = 0,
  22. HORIZONTAL = 1 << 0,
  23. CYCLE = 1 << 1,
  24. };
  25. DEFINE_CLASS_ENUM_FLAG_OPERATORS(type);
  26. StringWheel(const Rect& rect, int norSize, int selSize, enum type tp = HORIZONTAL);
  27. virtual ~StringWheel();
  28. Signal<int> clicked;
  29. void updateStrSize(void)
  30. {
  31. if (mtype & HORIZONTAL)
  32. {
  33. strWidth = norExtentSize;
  34. strHeight = getRect().getHeight();
  35. }
  36. else
  37. {
  38. strWidth = getRect().getHeight();
  39. strHeight = norExtentSize;
  40. }
  41. }
  42. void addStr(const char *text);
  43. void addStr(char *fmt, ...);
  44. void emptyStringWheel(void)
  45. {
  46. int size = str.size();
  47. for (int i = 0; i < size; i++)
  48. {
  49. if (str[i])
  50. rt_free(str[i]);
  51. }
  52. str.clear();
  53. extent.clear();
  54. number.clear();
  55. cycleEnable = 0;
  56. selItemNum = -1;
  57. currentItemNum = -1;
  58. itemMovePitch = 0;
  59. oldPitch = 0;
  60. _progress = 10;
  61. }
  62. void cycleMandatoryEnable(void)
  63. {
  64. int wWidth, size = str.size();
  65. cycleEnable = 1;
  66. mtype = mtype | CYCLE;
  67. if (mtype & HORIZONTAL)
  68. {
  69. wWidth = getRect().getWidth();
  70. }
  71. else
  72. {
  73. wWidth = getRect().getHeight();
  74. }
  75. do
  76. {
  77. cycleEnable ++;
  78. for (int i = 0; i < size; i++)
  79. {
  80. if (str[i])
  81. {
  82. str.push_back(rt_strdup(str[i]));
  83. extent.push_back(extent[i]);
  84. number.push_back(str.size() - 1);
  85. }
  86. }
  87. }
  88. while(wWidth > selExtentSize + (str.size() - 2) * norExtentSize);
  89. }
  90. void setSlideType(enum type tp = HORIZONTAL)
  91. {
  92. mtype = tp;
  93. }
  94. void fixItemExtent(int itemNum, bool ref);
  95. int getSelItemNum(void)
  96. {
  97. return selItemNum;
  98. }
  99. void setAdjustSpeed(int value) //调整滑动手势结束后,控件继续滑动的速度,即距离
  100. {
  101. if (value >= 0)
  102. adjustSpeed = value;
  103. }
  104. void animationEnable(bool enable = true)
  105. {
  106. animEnable = enable;
  107. }
  108. void tapSelEnable(bool enable = true)
  109. {
  110. tapEnable = enable;
  111. }
  112. virtual bool handleGestureEvent(struct rtgui_event_gesture *gev,
  113. const struct rtgui_gesture *gest);
  114. virtual void render(struct rtgui_dc* dc, const Point &dcPoint = Point(),
  115. const Rect &srcRect = Rect(),
  116. RenderFlag flags = DrawNormal);
  117. protected:
  118. std::vector<char *> str;
  119. std::vector<Rect> extent;
  120. std::vector<int> number;
  121. int norExtentSize, selExtentSize;
  122. int strWidth, strHeight;
  123. int selItemNum, currentItemNum, selProgress;
  124. private:
  125. void moveItem(bool ref);
  126. void filterSelItem(void); //筛选出当前选中项
  127. void animationStart(void); //开始动画
  128. void onAnimation(void); //动画刷新
  129. enum type mtype; //滑动类型 水平或垂直
  130. int cycleEnable;
  131. int itemMovePitch, oldPitch, adjustSpeed;
  132. Timer *animationTimer; //动画效果使用的定时器
  133. bool animationMoving, animEnable, tapEnable; //true 则为动画进行时 false 则否
  134. int animationMovePitch, _progress; //分别为未选中项和选中项动画需移动的总距离
  135. int animationPitch, animationProgress; //分别为未选中项和选中项动画中当前移动的距离,以及 animationProgress 动画进度
  136. };
  137. }
  138. #endif