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.

159 lines
3.9KB

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