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.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_COLOR_WIDGET_HPP_INCLUDED
  17. #define EXAMPLE_COLOR_WIDGET_HPP_INCLUDED
  18. // ------------------------------------------------------
  19. // DGL Stuff
  20. #include "../../dgl/Color.hpp"
  21. #include "../../dgl/StandaloneWindow.hpp"
  22. #include "../../dgl/SubWidget.hpp"
  23. START_NAMESPACE_DGL
  24. // ------------------------------------------------------
  25. // our widget
  26. template <class BaseWidget>
  27. class ExampleColorWidget : public BaseWidget,
  28. public IdleCallback
  29. {
  30. char cur;
  31. bool reverse;
  32. int r, g, b;
  33. Rectangle<uint> bgFull, bgSmall;
  34. public:
  35. static constexpr const char* kExampleWidgetName = "Color";
  36. // SubWidget
  37. explicit ExampleColorWidget(Widget* const parent);
  38. // TopLevelWidget
  39. explicit ExampleColorWidget(Window& windowToMapTo);
  40. // StandaloneWindow
  41. explicit ExampleColorWidget(Application& app);
  42. protected:
  43. void idleCallback() noexcept override
  44. {
  45. switch (cur)
  46. {
  47. case 'r':
  48. if (reverse)
  49. {
  50. if (--r == 0)
  51. cur = 'g';
  52. }
  53. else
  54. {
  55. if (++r == 100)
  56. cur = 'g';
  57. }
  58. break;
  59. case 'g':
  60. if (reverse)
  61. {
  62. if (--g == 0)
  63. cur = 'b';
  64. }
  65. else
  66. {
  67. if (++g == 100)
  68. cur = 'b';
  69. }
  70. break;
  71. case 'b':
  72. if (reverse)
  73. {
  74. if (--b == 0)
  75. {
  76. cur = 'r';
  77. reverse = false;
  78. }
  79. }
  80. else
  81. {
  82. if (++b == 100)
  83. {
  84. cur = 'r';
  85. reverse = true;
  86. }
  87. }
  88. break;
  89. }
  90. BaseWidget::repaint();
  91. }
  92. void onDisplay() override
  93. {
  94. const GraphicsContext& context(BaseWidget::getGraphicsContext());
  95. // paint bg color (in full size)
  96. Color(r, g, b).setFor(context);
  97. bgFull.draw(context);
  98. // paint inverted color (in 2/3 size)
  99. Color(100-r, 100-g, 100-b).setFor(context);
  100. bgSmall.draw(context);
  101. }
  102. void onResize(const Widget::ResizeEvent& ev) override
  103. {
  104. const uint width = ev.size.getWidth();
  105. const uint height = ev.size.getHeight();
  106. // full bg
  107. bgFull = Rectangle<uint>(0, 0, width, height);
  108. // small bg, centered 2/3 size
  109. bgSmall = Rectangle<uint>(width/6, height/6, width*2/3, height*2/3);
  110. }
  111. };
  112. // SubWidget
  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