DISTRHO Nekobi
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.

191 lines
4.5KB

  1. /*
  2. * Neko widget animation
  3. * Copyright (C) 2013-2021 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 <cstdlib> // rand
  22. using DGL_NAMESPACE::Image;
  23. // -----------------------------------------------------------------------
  24. class NekoWidget
  25. {
  26. public:
  27. NekoWidget()
  28. : fPos(0),
  29. fTimer(0),
  30. fCurAction(kActionNone),
  31. fCurImage(&fImages.sit)
  32. {
  33. // load images
  34. {
  35. using namespace DistrhoArtworkNekobi;
  36. #define JOIN(a, b) a ## b
  37. #define LOAD_IMAGE(NAME) fImages.NAME.loadFromMemory(JOIN(NAME, Data), JOIN(NAME, Width), JOIN(NAME, Height));
  38. LOAD_IMAGE(sit)
  39. LOAD_IMAGE(tail)
  40. LOAD_IMAGE(claw1)
  41. LOAD_IMAGE(claw2)
  42. LOAD_IMAGE(scratch1)
  43. LOAD_IMAGE(scratch2)
  44. LOAD_IMAGE(run1)
  45. LOAD_IMAGE(run2)
  46. LOAD_IMAGE(run3)
  47. LOAD_IMAGE(run4)
  48. #undef JOIN
  49. #undef LOAD_IMAGE
  50. }
  51. }
  52. void draw(const GraphicsContext& context)
  53. {
  54. int x = fPos+108;
  55. int y = -2;
  56. if (fCurImage == &fImages.claw1 || fCurImage == &fImages.claw2)
  57. {
  58. x += 2;
  59. y += 12;
  60. }
  61. fCurImage->drawAt(context, x, y);
  62. }
  63. // returns true if needs repaint
  64. bool idle()
  65. {
  66. if (++fTimer == 10)
  67. {
  68. if (fCurAction == kActionNone)
  69. fCurAction = static_cast<Action>(std::rand() % kActionCount);
  70. else
  71. fCurAction = kActionNone;
  72. fTimer = 0;
  73. }
  74. switch (fCurAction)
  75. {
  76. case kActionNone:
  77. if (fCurImage == &fImages.sit)
  78. fCurImage = &fImages.tail;
  79. else
  80. fCurImage = &fImages.sit;
  81. break;
  82. case kActionClaw:
  83. if (fCurImage == &fImages.claw1)
  84. fCurImage = &fImages.claw2;
  85. else
  86. fCurImage = &fImages.claw1;
  87. break;
  88. case kActionScratch:
  89. if (fCurImage == &fImages.scratch1)
  90. fCurImage = &fImages.scratch2;
  91. else
  92. fCurImage = &fImages.scratch1;
  93. break;
  94. case kActionRunRight:
  95. if (fTimer == 0 && fPos > 20*9)
  96. {
  97. // run the other way
  98. --fTimer;
  99. fCurAction = kActionRunLeft;
  100. idle();
  101. break;
  102. }
  103. fPos += 20;
  104. if (fCurImage == &fImages.run1)
  105. fCurImage = &fImages.run2;
  106. else
  107. fCurImage = &fImages.run1;
  108. break;
  109. case kActionRunLeft:
  110. if (fTimer == 0 && fPos < 20*9)
  111. {
  112. // run the other way
  113. --fTimer;
  114. fCurAction = kActionRunRight;
  115. idle();
  116. break;
  117. }
  118. fPos -= 20;
  119. if (fCurImage == &fImages.run3)
  120. fCurImage = &fImages.run4;
  121. else
  122. fCurImage = &fImages.run3;
  123. break;
  124. case kActionCount:
  125. break;
  126. }
  127. return true;
  128. }
  129. // -------------------------------------------------------------------
  130. private:
  131. enum Action {
  132. kActionNone, // bounce tail
  133. kActionClaw,
  134. kActionScratch,
  135. kActionRunRight,
  136. kActionRunLeft,
  137. kActionCount
  138. };
  139. struct Images {
  140. Image sit;
  141. Image tail;
  142. Image claw1;
  143. Image claw2;
  144. Image scratch1;
  145. Image scratch2;
  146. Image run1;
  147. Image run2;
  148. Image run3;
  149. Image run4;
  150. } fImages;
  151. int fPos;
  152. int fTimer;
  153. Action fCurAction;
  154. Image* fCurImage;
  155. };
  156. // -----------------------------------------------------------------------
  157. #endif // NEKO_WIDGET_HPP_INCLUDED