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.

182 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_RECTANGLES_WIDGET_HPP_INCLUDED
  17. #define EXAMPLE_RECTANGLES_WIDGET_HPP_INCLUDED
  18. // ------------------------------------------------------
  19. // DGL Stuff
  20. #include "../../dgl/Color.hpp"
  21. #include "../../dgl/SubWidget.hpp"
  22. #include "../../dgl/TopLevelWidget.hpp"
  23. START_NAMESPACE_DGL
  24. // ------------------------------------------------------
  25. // our widget
  26. template <class BaseWidget>
  27. class ExampleRectanglesWidget : public BaseWidget
  28. {
  29. bool clicked[9];
  30. public:
  31. static constexpr const char* const kExampleWidgetName = "Rectangles";
  32. // SubWidget
  33. explicit ExampleRectanglesWidget(Widget* const parentWidget)
  34. : BaseWidget(parentWidget)
  35. {
  36. init();
  37. }
  38. // TopLevelWidget
  39. explicit ExampleRectanglesWidget(Window& windowToMapTo)
  40. : BaseWidget(windowToMapTo)
  41. {
  42. init();
  43. }
  44. // StandaloneWindow
  45. explicit ExampleRectanglesWidget(Application& app)
  46. : BaseWidget(app)
  47. {
  48. init();
  49. done();
  50. }
  51. void init()
  52. {
  53. BaseWidget::setSize(300, 300);
  54. for (int i=0; i<9; ++i)
  55. clicked[i] = false;
  56. }
  57. protected:
  58. void onDisplay() override
  59. {
  60. const GraphicsContext& context(BaseWidget::getGraphicsContext());
  61. const uint width = BaseWidget::getWidth();
  62. const uint height = BaseWidget::getHeight();
  63. Rectangle<double> r;
  64. r.setWidth(width/3 - 6);
  65. r.setHeight(height/3 - 6);
  66. // draw a 3x3 grid
  67. for (int i=0; i<3; ++i)
  68. {
  69. r.setX(3 + i*width/3);
  70. // 1st
  71. r.setY(3);
  72. if (clicked[0+i])
  73. Color(0.8f, 0.5f, 0.3f).setFor(context);
  74. else
  75. Color(0.3f, 0.5f, 0.8f).setFor(context);
  76. r.draw(context);
  77. // 2nd
  78. r.setY(3 + height/3);
  79. if (clicked[3+i])
  80. Color(0.8f, 0.5f, 0.3f).setFor(context);
  81. else
  82. Color(0.3f, 0.5f, 0.8f).setFor(context);
  83. r.draw(context);
  84. // 3rd
  85. r.setY(3 + height*2/3);
  86. if (clicked[6+i])
  87. Color(0.8f, 0.5f, 0.3f).setFor(context);
  88. else
  89. Color(0.3f, 0.5f, 0.8f).setFor(context);
  90. r.draw(context);
  91. }
  92. }
  93. bool onMouse(const Widget::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