Audio plugin host https://kx.studio/carla
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.

203 lines
4.7KB

  1. /*
  2. * Neko widget animation
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the LICENSE file.
  16. */
  17. #ifndef NEKO_WIDGET_HPP_INCLUDED
  18. #define NEKO_WIDGET_HPP_INCLUDED
  19. #include "DistrhoArtworkNekobi.hpp"
  20. #include "Image.hpp"
  21. #include "Widget.hpp"
  22. #include <cstdlib> // rand
  23. using DGL::Image;
  24. // -----------------------------------------------------------------------
  25. class NekoWidget
  26. {
  27. public:
  28. NekoWidget()
  29. : fPos(0),
  30. fTimer(0),
  31. fTimerSpeed(20),
  32. fCurAction(kActionNone),
  33. fCurImage(&fImages.sit)
  34. {
  35. // load images
  36. {
  37. using namespace DistrhoArtworkNekobi;
  38. #define JOIN(a, b) a ## b
  39. #define LOAD_IMAGE(NAME) fImages.NAME.loadFromMemory(JOIN(NAME, Data), JOIN(NAME, Width), JOIN(NAME, Height));
  40. LOAD_IMAGE(sit)
  41. LOAD_IMAGE(tail)
  42. LOAD_IMAGE(claw1)
  43. LOAD_IMAGE(claw2)
  44. LOAD_IMAGE(scratch1)
  45. LOAD_IMAGE(scratch2)
  46. LOAD_IMAGE(run1)
  47. LOAD_IMAGE(run2)
  48. LOAD_IMAGE(run3)
  49. LOAD_IMAGE(run4)
  50. #undef JOIN
  51. #undef LOAD_IMAGE
  52. }
  53. }
  54. void draw()
  55. {
  56. int x = fPos+108;
  57. int y = -2;
  58. if (fCurImage == &fImages.claw1 || fCurImage == &fImages.claw2)
  59. {
  60. x += 2;
  61. y += 12;
  62. }
  63. fCurImage->drawAt(x, y);
  64. }
  65. // returns true if needs repaint
  66. bool idle()
  67. {
  68. if (++fTimer % fTimerSpeed != 0) // target is 20ms
  69. return false;
  70. if (fTimer == fTimerSpeed*9)
  71. {
  72. if (fCurAction == kActionNone)
  73. fCurAction = static_cast<Action>(std::rand() % kActionCount);
  74. else
  75. fCurAction = kActionNone;
  76. fTimer = 0;
  77. }
  78. switch (fCurAction)
  79. {
  80. case kActionNone:
  81. if (fCurImage == &fImages.sit)
  82. fCurImage = &fImages.tail;
  83. else
  84. fCurImage = &fImages.sit;
  85. break;
  86. case kActionClaw:
  87. if (fCurImage == &fImages.claw1)
  88. fCurImage = &fImages.claw2;
  89. else
  90. fCurImage = &fImages.claw1;
  91. break;
  92. case kActionScratch:
  93. if (fCurImage == &fImages.scratch1)
  94. fCurImage = &fImages.scratch2;
  95. else
  96. fCurImage = &fImages.scratch1;
  97. break;
  98. case kActionRunRight:
  99. if (fTimer == 0 && fPos > 20*9)
  100. {
  101. // run the other way
  102. --fTimer;
  103. fCurAction = kActionRunLeft;
  104. idle();
  105. break;
  106. }
  107. fPos += 20;
  108. if (fCurImage == &fImages.run1)
  109. fCurImage = &fImages.run2;
  110. else
  111. fCurImage = &fImages.run1;
  112. break;
  113. case kActionRunLeft:
  114. if (fTimer == 0 && fPos < 20*9)
  115. {
  116. // run the other way
  117. --fTimer;
  118. fCurAction = kActionRunRight;
  119. idle();
  120. break;
  121. }
  122. fPos -= 20;
  123. if (fCurImage == &fImages.run3)
  124. fCurImage = &fImages.run4;
  125. else
  126. fCurImage = &fImages.run3;
  127. break;
  128. case kActionCount:
  129. break;
  130. }
  131. return true;
  132. }
  133. void setTimerSpeed(int speed)
  134. {
  135. fTimer = 0;
  136. fTimerSpeed = speed;
  137. }
  138. // -------------------------------------------------------------------
  139. private:
  140. enum Action {
  141. kActionNone, // bounce tail
  142. kActionClaw,
  143. kActionScratch,
  144. kActionRunRight,
  145. kActionRunLeft,
  146. kActionCount
  147. };
  148. struct Images {
  149. Image sit;
  150. Image tail;
  151. Image claw1;
  152. Image claw2;
  153. Image scratch1;
  154. Image scratch2;
  155. Image run1;
  156. Image run2;
  157. Image run3;
  158. Image run4;
  159. } fImages;
  160. int fPos;
  161. int fTimer;
  162. int fTimerSpeed;
  163. Action fCurAction;
  164. Image* fCurImage;
  165. };
  166. // -----------------------------------------------------------------------
  167. #endif // NEKO_WIDGET_HPP_INCLUDED