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.

155 lines
3.6KB

  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 width = getWidth();
  43. const int height = getHeight();
  44. Rectangle<int> r;
  45. r.setWidth(width/3 - 6);
  46. r.setHeight(height/3 - 6);
  47. // draw a 3x3 grid
  48. for (int i=0; i<3; ++i)
  49. {
  50. r.setX(3 + i*width/3);
  51. // 1st
  52. r.setY(3);
  53. if (fClicked[0+i])
  54. glColor3f(0.8f, 0.5f, 0.3f);
  55. else
  56. glColor3f(0.3f, 0.5f, 0.8f);
  57. r.draw();
  58. // 2nd
  59. r.setY(3 + height/3);
  60. if (fClicked[3+i])
  61. glColor3f(0.8f, 0.5f, 0.3f);
  62. else
  63. glColor3f(0.3f, 0.5f, 0.8f);
  64. r.draw();
  65. // 3rd
  66. r.setY(3 + height*2/3);
  67. if (fClicked[6+i])
  68. glColor3f(0.8f, 0.5f, 0.3f);
  69. else
  70. glColor3f(0.3f, 0.5f, 0.8f);
  71. r.draw();
  72. }
  73. }
  74. bool onMouse(const MouseEvent& ev) override
  75. {
  76. if (ev.button != 1 || ! ev.press)
  77. return false;
  78. const int width = getWidth();
  79. const int height = getHeight();
  80. Rectangle<int> r;
  81. r.setWidth(width/3 - 6);
  82. r.setHeight(height/3 - 6);
  83. // draw a 3x3 grid
  84. for (int i=0; i<3; ++i)
  85. {
  86. r.setX(3 + i*width/3);
  87. // 1st
  88. r.setY(3);
  89. if (r.contains(ev.pos))
  90. {
  91. fClicked[0+i] = !fClicked[0+i];
  92. repaint();
  93. break;
  94. }
  95. // 2nd
  96. r.setY(3 + height/3);
  97. if (r.contains(ev.pos))
  98. {
  99. fClicked[3+i] = !fClicked[3+i];
  100. repaint();
  101. break;
  102. }
  103. // 3rd
  104. r.setY(3 + height*2/3);
  105. if (r.contains(ev.pos))
  106. {
  107. fClicked[6+i] = !fClicked[6+i];
  108. repaint();
  109. break;
  110. }
  111. }
  112. return true;
  113. }
  114. private:
  115. bool fClicked[9];
  116. };
  117. // ------------------------------------------------------
  118. #endif // EXAMPLE_RECTANGLES_WIDGET_HPP_INCLUDED