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.

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