DPF OpenGL examples
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.

164 lines
3.8KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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 "Widget.hpp"
  21. #include "Window.hpp"
  22. // ------------------------------------------------------
  23. // use namespace
  24. using DGL::Rectangle;
  25. using DGL::Widget;
  26. using DGL::Window;
  27. // ------------------------------------------------------
  28. // our widget
  29. class ExampleRectanglesWidget : public Widget
  30. {
  31. public:
  32. ExampleRectanglesWidget(Window& parent)
  33. : Widget(parent)
  34. {
  35. setSize(300, 300);
  36. for (int i=0; i<9; ++i)
  37. fClicked[i] = false;
  38. }
  39. ExampleRectanglesWidget(Widget* groupWidget)
  40. : Widget(groupWidget)
  41. {
  42. setSize(300, 300);
  43. for (int i=0; i<9; ++i)
  44. fClicked[i] = false;
  45. }
  46. protected:
  47. void onDisplay() override
  48. {
  49. const int width = getWidth();
  50. const int height = getHeight();
  51. Rectangle<int> r;
  52. r.setWidth(width/3 - 6);
  53. r.setHeight(height/3 - 6);
  54. // draw a 3x3 grid
  55. for (int i=0; i<3; ++i)
  56. {
  57. r.setX(3 + i*width/3);
  58. // 1st
  59. r.setY(3);
  60. if (fClicked[0+i])
  61. glColor3f(0.8f, 0.5f, 0.3f);
  62. else
  63. glColor3f(0.3f, 0.5f, 0.8f);
  64. r.draw();
  65. // 2nd
  66. r.setY(3 + height/3);
  67. if (fClicked[3+i])
  68. glColor3f(0.8f, 0.5f, 0.3f);
  69. else
  70. glColor3f(0.3f, 0.5f, 0.8f);
  71. r.draw();
  72. // 3rd
  73. r.setY(3 + height*2/3);
  74. if (fClicked[6+i])
  75. glColor3f(0.8f, 0.5f, 0.3f);
  76. else
  77. glColor3f(0.3f, 0.5f, 0.8f);
  78. r.draw();
  79. }
  80. }
  81. bool onMouse(const MouseEvent& ev) override
  82. {
  83. if (ev.button != 1 || ! ev.press)
  84. return false;
  85. const int width = getWidth();
  86. const int height = getHeight();
  87. Rectangle<int> r;
  88. r.setWidth(width/3 - 6);
  89. r.setHeight(height/3 - 6);
  90. // draw a 3x3 grid
  91. for (int i=0; i<3; ++i)
  92. {
  93. r.setX(3 + i*width/3);
  94. // 1st
  95. r.setY(3);
  96. if (r.contains(ev.pos))
  97. {
  98. fClicked[0+i] = !fClicked[0+i];
  99. repaint();
  100. break;
  101. }
  102. // 2nd
  103. r.setY(3 + height/3);
  104. if (r.contains(ev.pos))
  105. {
  106. fClicked[3+i] = !fClicked[3+i];
  107. repaint();
  108. break;
  109. }
  110. // 3rd
  111. r.setY(3 + height*2/3);
  112. if (r.contains(ev.pos))
  113. {
  114. fClicked[6+i] = !fClicked[6+i];
  115. repaint();
  116. break;
  117. }
  118. }
  119. return true;
  120. }
  121. private:
  122. bool fClicked[9];
  123. };
  124. // ------------------------------------------------------
  125. #endif // EXAMPLE_RECTANGLES_WIDGET_HPP_INCLUDED