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.

164 lines
4.2KB

  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/SubWidget.hpp"
  21. #include "../../dgl/TopLevelWidget.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. : BaseWidget(parent),
  38. cur('r'),
  39. reverse(false),
  40. r(0), g(0), b(0)
  41. {
  42. BaseWidget::setSize(300, 300);
  43. parent->getApp().addIdleCallback(this);
  44. }
  45. // TopLevelWidget
  46. explicit ExampleColorWidget(Window& windowToMapTo)
  47. : BaseWidget(windowToMapTo),
  48. cur('r'),
  49. reverse(false),
  50. r(0), g(0), b(0)
  51. {
  52. BaseWidget::setSize(300, 300);
  53. windowToMapTo.getApp().addIdleCallback(this);
  54. }
  55. // StandaloneWindow
  56. explicit ExampleColorWidget(Application& app)
  57. : BaseWidget(app),
  58. cur('r'),
  59. reverse(false),
  60. r(0), g(0), b(0)
  61. {
  62. BaseWidget::setSize(300, 300);
  63. app.addIdleCallback(this);
  64. }
  65. protected:
  66. void idleCallback() noexcept override
  67. {
  68. switch (cur)
  69. {
  70. case 'r':
  71. if (reverse)
  72. {
  73. if (--r == 0)
  74. cur = 'g';
  75. }
  76. else
  77. {
  78. if (++r == 100)
  79. cur = 'g';
  80. }
  81. break;
  82. case 'g':
  83. if (reverse)
  84. {
  85. if (--g == 0)
  86. cur = 'b';
  87. }
  88. else
  89. {
  90. if (++g == 100)
  91. cur = 'b';
  92. }
  93. break;
  94. case 'b':
  95. if (reverse)
  96. {
  97. if (--b == 0)
  98. {
  99. cur = 'r';
  100. reverse = false;
  101. }
  102. }
  103. else
  104. {
  105. if (++b == 100)
  106. {
  107. cur = 'r';
  108. reverse = true;
  109. }
  110. }
  111. break;
  112. }
  113. BaseWidget::repaint();
  114. }
  115. void onDisplay() override
  116. {
  117. // paint bg color (in full size)
  118. glColor3b(r, g, b);
  119. bgFull.draw();
  120. // paint inverted color (in 2/3 size)
  121. glColor3b(100-r, 100-g, 100-b);
  122. bgSmall.draw();
  123. }
  124. void onResize(const ResizeEvent& ev) override
  125. {
  126. const uint width = ev.size.getWidth();
  127. const uint height = ev.size.getHeight();
  128. // full bg
  129. bgFull = Rectangle<uint>(0, 0, width, height);
  130. // small bg, centered 2/3 size
  131. bgSmall = Rectangle<uint>(width/6, height/6, width*2/3, height*2/3);
  132. }
  133. };
  134. typedef ExampleColorWidget<SubWidget> ExampleColorSubWidget;
  135. typedef ExampleColorWidget<TopLevelWidget> ExampleColorTopLevelWidget;
  136. typedef ExampleColorWidget<StandaloneWindow> ExampleColorStandaloneWindow;
  137. // ------------------------------------------------------
  138. END_NAMESPACE_DGL
  139. #endif // EXAMPLE_COLOR_WIDGET_HPP_INCLUDED