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.

159 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. protected:
  40. void onDisplay() override
  41. {
  42. const int cx = getX();
  43. const int cy = getY();
  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(cx + 3 + i*width/3);
  53. // 1st
  54. r.setY(cy + 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(cy + 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(cy + 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(int button, bool press, int x, int y) override
  77. {
  78. if (button != 1 || ! press)
  79. return false;
  80. const int cx = getX();
  81. const int cy = getY();
  82. const int width = getWidth();
  83. const int height = getHeight();
  84. Rectangle<int> r;
  85. r.setWidth(width/3 - 6);
  86. r.setHeight(height/3 - 6);
  87. // draw a 3x3 grid
  88. for (int i=0; i<3; ++i)
  89. {
  90. r.setX(cx + 3 + i*width/3);
  91. // 1st
  92. r.setY(cy + 3);
  93. if (r.contains(x, y))
  94. {
  95. fClicked[0+i] = !fClicked[0+i];
  96. repaint();
  97. break;
  98. }
  99. // 2nd
  100. r.setY(cy + 3 + height/3);
  101. if (r.contains(x, y))
  102. {
  103. fClicked[3+i] = !fClicked[3+i];
  104. repaint();
  105. break;
  106. }
  107. // 3rd
  108. r.setY(cy + 3 + height*2/3);
  109. if (r.contains(x, y))
  110. {
  111. fClicked[6+i] = !fClicked[6+i];
  112. repaint();
  113. break;
  114. }
  115. }
  116. return true;
  117. }
  118. private:
  119. bool fClicked[9];
  120. };
  121. // ------------------------------------------------------
  122. #endif // EXAMPLE_RECTANGLES_WIDGET_HPP_INCLUDED