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.

157 lines
3.7KB

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