DISTRHO Plugin Framework
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.

214 lines
4.9KB

  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. // ------------------------------------------------------
  17. // DGL Stuff
  18. #include "App.hpp"
  19. #include "CairoWidget.hpp"
  20. #include "Window.hpp"
  21. #include <cstdio>
  22. // ------------------------------------------------------
  23. // use namespace
  24. using namespace DGL;
  25. // ------------------------------------------------------
  26. // Background widget (cairo will be painted on top)
  27. class BackgroundWidget : public Widget
  28. {
  29. public:
  30. BackgroundWidget(Window& parent)
  31. : Widget(parent)
  32. {
  33. }
  34. private:
  35. void onDisplay() override
  36. {
  37. int x = 0;
  38. int y = 0;
  39. int width = getWidth();
  40. int height = getHeight();
  41. // paint bg color (in full size)
  42. glColor3b(20, 80, 20);
  43. glBegin(GL_QUADS);
  44. glTexCoord2f(0.0f, 0.0f);
  45. glVertex2i(x, y);
  46. glTexCoord2f(1.0f, 0.0f);
  47. glVertex2i(x+width, y);
  48. glTexCoord2f(1.0f, 1.0f);
  49. glVertex2i(x+width, y+height);
  50. glTexCoord2f(0.0f, 1.0f);
  51. glVertex2i(x, y+height);
  52. glEnd();
  53. }
  54. void onReshape(int width, int height) override
  55. {
  56. // make this widget same size as window
  57. setSize(width, height);
  58. Widget::onReshape(width, height);
  59. }
  60. };
  61. // ------------------------------------------------------
  62. // Custom Cairo Widget
  63. class CustomCairoWidget : public App::IdleCallback,
  64. CairoWidget
  65. {
  66. public:
  67. CustomCairoWidget(Window& parent)
  68. : CairoWidget(parent),
  69. value(0.0f),
  70. pressed(false)
  71. {
  72. setSize(100, 100);
  73. }
  74. private:
  75. void idleCallback() override
  76. {
  77. value += 0.001f;
  78. if (value > 1.0f)
  79. value = 0;
  80. repaint();
  81. }
  82. void cairoDisplay(cairo_t* const context) override
  83. {
  84. const int w = getWidth();
  85. const int h = getHeight();
  86. float radius = 40.0f;
  87. // * 0.9 for line width to remain inside redraw area
  88. if (w > h)
  89. radius = (h / 2.0f)*0.9f;
  90. else
  91. radius = (w / 2.0f)*0.9f;
  92. cairo_save(context);
  93. cairo_rectangle(context, 0, 0, w, h );
  94. cairo_set_source_rgba(context, 1.1, 0.1, 0.1, 0 );
  95. cairo_fill(context);
  96. cairo_set_line_join(context, CAIRO_LINE_JOIN_ROUND);
  97. cairo_set_line_cap(context, CAIRO_LINE_CAP_ROUND);
  98. cairo_set_line_width(context, 5-0.2);
  99. cairo_move_to(context, w/2, h/2);
  100. cairo_line_to(context, w/2, h/2);
  101. cairo_set_source_rgba(context, 0.1, 0.1, 0.1, 0 );
  102. cairo_stroke(context);
  103. cairo_arc(context, w/2, h/2, radius, 2.46, 0.75 );
  104. cairo_set_source_rgb(context, 0.1, 0.1, 0.1 );
  105. cairo_stroke(context);
  106. float angle = 2.46 + ( 4.54 * value );
  107. cairo_set_line_width(context, 5);
  108. cairo_arc(context, w/2, h/2, radius, 2.46, angle );
  109. cairo_line_to(context, w/2, h/2);
  110. cairo_set_source_rgba(context, 1.0, 0.48, 0, 0.8);
  111. cairo_stroke(context);
  112. cairo_restore(context);
  113. }
  114. bool onMouse(int button, bool press, int x, int y)
  115. {
  116. if (button == 1)
  117. {
  118. pressed = press;
  119. if (press)
  120. {
  121. setX(x-100/2);
  122. setY(y-100/2);
  123. }
  124. return true;
  125. }
  126. return false;
  127. }
  128. bool onMotion(int x, int y)
  129. {
  130. if (pressed)
  131. {
  132. setX(x-100/2);
  133. setY(y-100/2);
  134. return true;
  135. }
  136. return false;
  137. }
  138. float value;
  139. bool pressed;
  140. };
  141. // ------------------------------------------------------
  142. // Custom window, with bg + cairo + image
  143. class CustomWindow : public Window
  144. {
  145. public:
  146. CustomWindow(App& app)
  147. : Window(app),
  148. bg(*this),
  149. cairo(*this)
  150. {
  151. app.addIdleCallback(&cairo);
  152. }
  153. private:
  154. BackgroundWidget bg;
  155. CustomCairoWidget cairo;
  156. };
  157. // ------------------------------------------------------
  158. // main entry point
  159. int main()
  160. {
  161. App app;
  162. CustomWindow win(app);
  163. win.setSize(300, 300);
  164. win.setTitle("Cairo");
  165. win.show();
  166. app.exec();
  167. return 0;
  168. }
  169. // ------------------------------------------------------