Collection of DPF-based plugins for packaging
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.

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