Collection of DPF-based plugins for packaging
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.

185 lines
5.3KB

  1. /*
  2. * Resize handle for DPF
  3. * Copyright (C) 2021 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. #pragma once
  17. #include "TopLevelWidget.hpp"
  18. #include "Color.hpp"
  19. START_NAMESPACE_DGL
  20. /** Resize handle for DPF windows, will sit on bottom-right. */
  21. class ResizeHandle : public TopLevelWidget
  22. {
  23. public:
  24. /** Constructor for placing this handle on top of a window. */
  25. explicit ResizeHandle(Window& window)
  26. : TopLevelWidget(window),
  27. handleSize(16),
  28. resizing(false)
  29. {
  30. resetArea();
  31. }
  32. /** Overloaded constructor, will fetch the window from an existing top-level widget. */
  33. explicit ResizeHandle(TopLevelWidget* const tlw)
  34. : TopLevelWidget(tlw->getWindow()),
  35. handleSize(16),
  36. resizing(false)
  37. {
  38. resetArea();
  39. }
  40. /** Set the handle size, minimum 16. */
  41. void setHandleSize(const uint size)
  42. {
  43. handleSize = std::max(16u, size);
  44. resetArea();
  45. }
  46. protected:
  47. void onDisplay() override
  48. {
  49. const GraphicsContext& context(getGraphicsContext());
  50. const double lineWidth = 1.0 * getScaleFactor();
  51. #ifdef DGL_OPENGL
  52. glMatrixMode(GL_MODELVIEW);
  53. #endif
  54. // draw white lines, 1px wide
  55. Color(1.0f, 1.0f, 1.0f).setFor(context);
  56. l1.draw(context, lineWidth);
  57. l2.draw(context, lineWidth);
  58. l3.draw(context, lineWidth);
  59. // draw black lines, offset by 1px and 1px wide
  60. Color(0.0f, 0.0f, 0.0f).setFor(context);
  61. Line<double> l1b(l1), l2b(l2), l3b(l3);
  62. l1b.moveBy(lineWidth, lineWidth);
  63. l2b.moveBy(lineWidth, lineWidth);
  64. l3b.moveBy(lineWidth, lineWidth);
  65. l1b.draw(context, lineWidth);
  66. l2b.draw(context, lineWidth);
  67. l3b.draw(context, lineWidth);
  68. }
  69. bool onMouse(const MouseEvent& ev) override
  70. {
  71. if (ev.button != 1)
  72. return false;
  73. if (ev.press && area.contains(ev.pos))
  74. {
  75. resizing = true;
  76. resizingSize = Size<double>(getWidth(), getHeight());
  77. lastResizePoint = ev.pos;
  78. return true;
  79. }
  80. if (resizing && ! ev.press)
  81. {
  82. resizing = false;
  83. return true;
  84. }
  85. return false;
  86. }
  87. bool onMotion(const MotionEvent& ev) override
  88. {
  89. if (! resizing)
  90. return false;
  91. const Size<double> offset(ev.pos.getX() - lastResizePoint.getX(),
  92. ev.pos.getY() - lastResizePoint.getY());
  93. resizingSize += offset;
  94. lastResizePoint = ev.pos;
  95. // TODO min width, min height
  96. const uint minWidth = 16;
  97. const uint minHeight = 16;
  98. if (resizingSize.getWidth() < minWidth)
  99. resizingSize.setWidth(minWidth);
  100. if (resizingSize.getWidth() > 16384)
  101. resizingSize.setWidth(16384);
  102. if (resizingSize.getHeight() < minHeight)
  103. resizingSize.setHeight(minHeight);
  104. if (resizingSize.getHeight() > 16384)
  105. resizingSize.setHeight(16384);
  106. setSize(resizingSize.getWidth(), resizingSize.getHeight());
  107. return true;
  108. }
  109. void onResize(const ResizeEvent& ev) override
  110. {
  111. TopLevelWidget::onResize(ev);
  112. resetArea();
  113. }
  114. private:
  115. Rectangle<uint> area;
  116. Line<double> l1, l2, l3;
  117. uint handleSize;
  118. // event handling state
  119. bool resizing;
  120. Point<double> lastResizePoint;
  121. Size<double> resizingSize;
  122. void resetArea()
  123. {
  124. const double scaleFactor = getScaleFactor();
  125. const uint margin = 0.0 * scaleFactor;
  126. const uint size = handleSize * scaleFactor;
  127. area = Rectangle<uint>(getWidth() - size - margin,
  128. getHeight() - size - margin,
  129. size, size);
  130. recreateLines(area.getX(), area.getY(), size);
  131. }
  132. void recreateLines(const uint x, const uint y, const uint size)
  133. {
  134. uint linesize = size;
  135. uint offset = 0;
  136. // 1st line, full diagonal size
  137. l1.setStartPos(x + size, y);
  138. l1.setEndPos(x, y + size);
  139. // 2nd line, bit more to the right and down, cropped
  140. offset += size / 3;
  141. linesize -= size / 3;
  142. l2.setStartPos(x + linesize + offset, y + offset);
  143. l2.setEndPos(x + offset, y + linesize + offset);
  144. // 3rd line, even more right and down
  145. offset += size / 3;
  146. linesize -= size / 3;
  147. l3.setStartPos(x + linesize + offset, y + offset);
  148. l3.setEndPos(x + offset, y + linesize + offset);
  149. }
  150. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ResizeHandle)
  151. };
  152. END_NAMESPACE_DGL