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.

179 lines
4.4KB

  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_COLOR_WIDGET_HPP_INCLUDED
  17. #define EXAMPLE_COLOR_WIDGET_HPP_INCLUDED
  18. // ------------------------------------------------------
  19. // DGL Stuff
  20. #include "../../dgl/StandaloneWindow.hpp"
  21. #include "../../dgl/SubWidget.hpp"
  22. START_NAMESPACE_DGL
  23. // ------------------------------------------------------
  24. // our widget
  25. template <class BaseWidget>
  26. class ExampleColorWidget : public BaseWidget,
  27. public IdleCallback
  28. {
  29. char cur;
  30. bool reverse;
  31. int r, g, b;
  32. Rectangle<uint> bgFull, bgSmall;
  33. public:
  34. static constexpr const char* kExampleWidgetName = "Color";
  35. // SubWidget
  36. explicit ExampleColorWidget(Widget* const parent);
  37. // TopLevelWidget
  38. explicit ExampleColorWidget(Window& windowToMapTo);
  39. // StandaloneWindow
  40. explicit ExampleColorWidget(Application& app);
  41. protected:
  42. void idleCallback() noexcept override
  43. {
  44. switch (cur)
  45. {
  46. case 'r':
  47. if (reverse)
  48. {
  49. if (--r == 0)
  50. cur = 'g';
  51. }
  52. else
  53. {
  54. if (++r == 100)
  55. cur = 'g';
  56. }
  57. break;
  58. case 'g':
  59. if (reverse)
  60. {
  61. if (--g == 0)
  62. cur = 'b';
  63. }
  64. else
  65. {
  66. if (++g == 100)
  67. cur = 'b';
  68. }
  69. break;
  70. case 'b':
  71. if (reverse)
  72. {
  73. if (--b == 0)
  74. {
  75. cur = 'r';
  76. reverse = false;
  77. }
  78. }
  79. else
  80. {
  81. if (++b == 100)
  82. {
  83. cur = 'r';
  84. reverse = true;
  85. }
  86. }
  87. break;
  88. }
  89. BaseWidget::repaint();
  90. }
  91. void onDisplay() override
  92. {
  93. const GraphicsContext& context(BaseWidget::getGraphicsContext());
  94. #if 0 /* TODO make generic */
  95. // paint bg color (in full size)
  96. glColor3b(r, g, b);
  97. bgFull.draw(context);
  98. // paint inverted color (in 2/3 size)
  99. glColor3b(100-r, 100-g, 100-b);
  100. bgSmall.draw();
  101. #endif
  102. }
  103. void onResize(const ResizeEvent& ev) override
  104. {
  105. const uint width = ev.size.getWidth();
  106. const uint height = ev.size.getHeight();
  107. // full bg
  108. bgFull = Rectangle<uint>(0, 0, width, height);
  109. // small bg, centered 2/3 size
  110. bgSmall = Rectangle<uint>(width/6, height/6, width*2/3, height*2/3);
  111. }
  112. };
  113. template<> inline
  114. ExampleColorWidget<SubWidget>::ExampleColorWidget(Widget* const parent)
  115. : SubWidget(parent),
  116. cur('r'),
  117. reverse(false),
  118. r(0), g(0), b(0)
  119. {
  120. setSize(300, 300);
  121. parent->getApp().addIdleCallback(this);
  122. }
  123. // TopLevelWidget
  124. template<> inline
  125. ExampleColorWidget<TopLevelWidget>::ExampleColorWidget(Window& windowToMapTo)
  126. : TopLevelWidget(windowToMapTo),
  127. cur('r'),
  128. reverse(false),
  129. r(0), g(0), b(0)
  130. {
  131. setSize(300, 300);
  132. addIdleCallback(this);
  133. }
  134. // StandaloneWindow
  135. template<> inline
  136. ExampleColorWidget<StandaloneWindow>::ExampleColorWidget(Application& app)
  137. : StandaloneWindow(app),
  138. cur('r'),
  139. reverse(false),
  140. r(0), g(0), b(0)
  141. {
  142. setSize(300, 300);
  143. addIdleCallback(this);
  144. }
  145. typedef ExampleColorWidget<SubWidget> ExampleColorSubWidget;
  146. typedef ExampleColorWidget<TopLevelWidget> ExampleColorTopLevelWidget;
  147. typedef ExampleColorWidget<StandaloneWindow> ExampleColorStandaloneWindow;
  148. // ------------------------------------------------------
  149. END_NAMESPACE_DGL
  150. #endif // EXAMPLE_COLOR_WIDGET_HPP_INCLUDED