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.

175 lines
4.3KB

  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 (clicked[0+i])
  67. glColor3f(0.8f, 0.5f, 0.3f);
  68. else
  69. glColor3f(0.3f, 0.5f, 0.8f);
  70. r.draw();
  71. // 2nd
  72. r.setY(3 + height/3);
  73. if (clicked[3+i])
  74. glColor3f(0.8f, 0.5f, 0.3f);
  75. else
  76. glColor3f(0.3f, 0.5f, 0.8f);
  77. r.draw();
  78. // 3rd
  79. r.setY(3 + height*2/3);
  80. if (clicked[6+i])
  81. glColor3f(0.8f, 0.5f, 0.3f);
  82. else
  83. glColor3f(0.3f, 0.5f, 0.8f);
  84. r.draw();
  85. }
  86. }
  87. bool onMouse(const MouseEvent& ev) override
  88. {
  89. if (ev.button != 1 || ! ev.press)
  90. return false;
  91. const uint width = BaseWidget::getWidth();
  92. const uint height = BaseWidget::getHeight();
  93. Rectangle<double> r;
  94. r.setWidth(width/3 - 6);
  95. r.setHeight(height/3 - 6);
  96. // draw a 3x3 grid
  97. for (int i=0; i<3; ++i)
  98. {
  99. r.setX(3 + i*width/3);
  100. // 1st
  101. r.setY(3);
  102. if (r.contains(ev.pos))
  103. {
  104. clicked[0+i] = !clicked[0+i];
  105. BaseWidget::repaint();
  106. break;
  107. }
  108. // 2nd
  109. r.setY(3 + height/3);
  110. if (r.contains(ev.pos))
  111. {
  112. clicked[3+i] = !clicked[3+i];
  113. BaseWidget::repaint();
  114. break;
  115. }
  116. // 3rd
  117. r.setY(3 + height*2/3);
  118. if (r.contains(ev.pos))
  119. {
  120. clicked[6+i] = !clicked[6+i];
  121. BaseWidget::repaint();
  122. break;
  123. }
  124. }
  125. return true;
  126. }
  127. };
  128. typedef ExampleRectanglesWidget<SubWidget> ExampleRectanglesSubWidget;
  129. typedef ExampleRectanglesWidget<TopLevelWidget> ExampleRectanglesTopLevelWidget;
  130. typedef ExampleRectanglesWidget<StandaloneWindow> ExampleRectanglesStandaloneWindow;
  131. // ------------------------------------------------------
  132. END_NAMESPACE_DGL
  133. #endif // EXAMPLE_RECTANGLES_WIDGET_HPP_INCLUDED