DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
5.5KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef EXAMPLE_IMAGES_WIDGET_HPP_INCLUDED
  17. #define EXAMPLE_IMAGES_WIDGET_HPP_INCLUDED
  18. // ------------------------------------------------------
  19. // DGL Stuff
  20. #include "../../dgl/ImageBase.hpp"
  21. #include "../../dgl/StandaloneWindow.hpp"
  22. #include "../../dgl/SubWidget.hpp"
  23. // ------------------------------------------------------
  24. // Images
  25. #include "../images_res/CatPics.hpp"
  26. START_NAMESPACE_DGL
  27. // ------------------------------------------------------
  28. // our widget
  29. template <class BaseWidget, class BaseImage>
  30. class ExampleImagesWidget : public BaseWidget,
  31. public IdleCallback
  32. {
  33. static const int kImg1y = 0;
  34. static const int kImg2y = 500/2-CatPics::cat2Height/2;
  35. static const int kImg3x = 400/3-CatPics::cat3Width/3;
  36. static const int kImg1max = 500-CatPics::cat1Width;
  37. static const int kImg2max = 500-CatPics::cat2Width;
  38. static const int kImg3max = 400-CatPics::cat3Height;
  39. int imgTop1st, imgTop2nd, imgTop3rd;
  40. int img1x, img2x, img3y;
  41. bool img1rev, img2rev, img3rev;
  42. BaseImage img1, img2, img3;
  43. public:
  44. static constexpr const char* kExampleWidgetName = "Images";
  45. // SubWidget
  46. explicit ExampleImagesWidget(Widget* const parent);
  47. // TopLevelWidget
  48. explicit ExampleImagesWidget(Window& windowToMapTo);
  49. // StandaloneWindow
  50. explicit ExampleImagesWidget(Application& app);
  51. protected:
  52. void init(Application& app)
  53. {
  54. imgTop1st = 1;
  55. imgTop2nd = 2;
  56. imgTop3rd = 3;
  57. img1x = 0;
  58. img2x = kImg2max;
  59. img3y = kImg3max;
  60. img1rev = false;
  61. img2rev = true;
  62. img3rev = true;
  63. img1 = BaseImage(CatPics::cat1Data, CatPics::cat1Width, CatPics::cat1Height, kImageFormatBGR);
  64. img2 = BaseImage(CatPics::cat2Data, CatPics::cat2Width, CatPics::cat2Height, kImageFormatBGR);
  65. img3 = BaseImage(CatPics::cat3Data, CatPics::cat3Width, CatPics::cat3Height, kImageFormatBGR);
  66. BaseWidget::setSize(500, 400);
  67. app.addIdleCallback(this);
  68. }
  69. void idleCallback() noexcept override
  70. {
  71. if (img1rev)
  72. {
  73. img1x -= 2;
  74. if (img1x <= -50)
  75. {
  76. img1rev = false;
  77. setNewTopImg(1);
  78. }
  79. }
  80. else
  81. {
  82. img1x += 2;
  83. if (img1x >= kImg1max+50)
  84. {
  85. img1rev = true;
  86. setNewTopImg(1);
  87. }
  88. }
  89. if (img2rev)
  90. {
  91. img2x -= 1;
  92. if (img2x <= -50)
  93. {
  94. img2rev = false;
  95. setNewTopImg(2);
  96. }
  97. }
  98. else
  99. {
  100. img2x += 4;
  101. if (img2x >= kImg2max+50)
  102. {
  103. img2rev = true;
  104. setNewTopImg(2);
  105. }
  106. }
  107. if (img3rev)
  108. {
  109. img3y -= 3;
  110. if (img3y <= -50)
  111. {
  112. img3rev = false;
  113. setNewTopImg(3);
  114. }
  115. }
  116. else
  117. {
  118. img3y += 3;
  119. if (img3y >= kImg3max+50)
  120. {
  121. img3rev = true;
  122. setNewTopImg(3);
  123. }
  124. }
  125. BaseWidget::repaint();
  126. }
  127. void onDisplay() override
  128. {
  129. const GraphicsContext& context(BaseWidget::getGraphicsContext());
  130. switch (imgTop3rd)
  131. {
  132. case 1:
  133. img1.drawAt(context, img1x, kImg1y);
  134. break;
  135. case 2:
  136. img2.drawAt(context, img2x, kImg2y);
  137. break;
  138. case 3:
  139. img3.drawAt(context, kImg3x, img3y);
  140. break;
  141. };
  142. switch (imgTop2nd)
  143. {
  144. case 1:
  145. img1.drawAt(context, img1x, kImg1y);
  146. break;
  147. case 2:
  148. img2.drawAt(context, img2x, kImg2y);
  149. break;
  150. case 3:
  151. img3.drawAt(context, kImg3x, img3y);
  152. break;
  153. };
  154. switch (imgTop1st)
  155. {
  156. case 1:
  157. img1.drawAt(context, img1x, kImg1y);
  158. break;
  159. case 2:
  160. img2.drawAt(context, img2x, kImg2y);
  161. break;
  162. case 3:
  163. img3.drawAt(context, kImg3x, img3y);
  164. break;
  165. };
  166. }
  167. private:
  168. void setNewTopImg(const int imgId) noexcept
  169. {
  170. if (imgTop1st == imgId)
  171. return;
  172. if (imgTop2nd == imgId)
  173. {
  174. imgTop2nd = imgTop1st;
  175. imgTop1st = imgId;
  176. return;
  177. }
  178. imgTop3rd = imgTop2nd;
  179. imgTop2nd = imgTop1st;
  180. imgTop1st = imgId;
  181. }
  182. };
  183. // ------------------------------------------------------
  184. END_NAMESPACE_DGL
  185. #endif // EXAMPLE_IMAGES_WIDGET_HPP_INCLUDED