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.

181 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_RECTANGLES_WIDGET_HPP_INCLUDED
  17. #define EXAMPLE_RECTANGLES_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 ExampleRectanglesWidget : public BaseWidget
  27. {
  28. bool clicked[9];
  29. public:
  30. static constexpr const char* const kExampleWidgetName = "Rectangles";
  31. explicit ExampleRectanglesWidget(Widget* const parentWidget)
  32. : BaseWidget(parentWidget)
  33. {
  34. init();
  35. }
  36. explicit ExampleRectanglesWidget(Window& windowToMapTo)
  37. : BaseWidget(windowToMapTo)
  38. {
  39. init();
  40. }
  41. explicit ExampleRectanglesWidget(Application& app)
  42. : BaseWidget(app)
  43. {
  44. init();
  45. }
  46. void init()
  47. {
  48. BaseWidget::setSize(300, 300);
  49. for (int i=0; i<9; ++i)
  50. clicked[i] = false;
  51. }
  52. protected:
  53. void onDisplay() override
  54. {
  55. const uint width = BaseWidget::getWidth();
  56. const uint height = BaseWidget::getHeight();
  57. Rectangle<double> r;
  58. r.setWidth(width/3 - 6);
  59. r.setHeight(height/3 - 6);
  60. // draw a 3x3 grid
  61. for (int i=0; i<3; ++i)
  62. {
  63. r.setX(3 + i*width/3);
  64. // 1st
  65. r.setY(3);
  66. #if 0 /* TODO make generic */
  67. if (clicked[0+i])
  68. glColor3f(0.8f, 0.5f, 0.3f);
  69. else
  70. glColor3f(0.3f, 0.5f, 0.8f);
  71. #endif
  72. r.draw();
  73. // 2nd
  74. r.setY(3 + height/3);
  75. #if 0 /* TODO make generic */
  76. if (clicked[3+i])
  77. glColor3f(0.8f, 0.5f, 0.3f);
  78. else
  79. glColor3f(0.3f, 0.5f, 0.8f);
  80. #endif
  81. r.draw();
  82. // 3rd
  83. r.setY(3 + height*2/3);
  84. #if 0 /* TODO make generic */
  85. if (clicked[6+i])
  86. glColor3f(0.8f, 0.5f, 0.3f);
  87. else
  88. glColor3f(0.3f, 0.5f, 0.8f);
  89. #endif
  90. r.draw();
  91. }
  92. }
  93. bool onMouse(const MouseEvent& ev) override
  94. {
  95. if (ev.button != 1 || ! ev.press)
  96. return false;
  97. const uint width = BaseWidget::getWidth();
  98. const uint height = BaseWidget::getHeight();
  99. Rectangle<double> r;
  100. r.setWidth(width/3 - 6);
  101. r.setHeight(height/3 - 6);
  102. // draw a 3x3 grid
  103. for (int i=0; i<3; ++i)
  104. {
  105. r.setX(3 + i*width/3);
  106. // 1st
  107. r.setY(3);
  108. if (r.contains(ev.pos))
  109. {
  110. clicked[0+i] = !clicked[0+i];
  111. BaseWidget::repaint();
  112. break;
  113. }
  114. // 2nd
  115. r.setY(3 + height/3);
  116. if (r.contains(ev.pos))
  117. {
  118. clicked[3+i] = !clicked[3+i];
  119. BaseWidget::repaint();
  120. break;
  121. }
  122. // 3rd
  123. r.setY(3 + height*2/3);
  124. if (r.contains(ev.pos))
  125. {
  126. clicked[6+i] = !clicked[6+i];
  127. BaseWidget::repaint();
  128. break;
  129. }
  130. }
  131. return true;
  132. }
  133. };
  134. typedef ExampleRectanglesWidget<SubWidget> ExampleRectanglesSubWidget;
  135. typedef ExampleRectanglesWidget<TopLevelWidget> ExampleRectanglesTopLevelWidget;
  136. typedef ExampleRectanglesWidget<StandaloneWindow> ExampleRectanglesStandaloneWindow;
  137. // ------------------------------------------------------
  138. END_NAMESPACE_DGL
  139. #endif // EXAMPLE_RECTANGLES_WIDGET_HPP_INCLUDED