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.

208 lines
6.1KB

  1. /*
  2. * Resize handle for DPF
  3. * Copyright (C) 2021-2022 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. hasCursor(false),
  29. isResizing(false)
  30. {
  31. resetArea();
  32. }
  33. /** Overloaded constructor, will fetch the window from an existing top-level widget. */
  34. explicit ResizeHandle(TopLevelWidget* const tlw)
  35. : TopLevelWidget(tlw->getWindow()),
  36. handleSize(16),
  37. hasCursor(false),
  38. isResizing(false)
  39. {
  40. resetArea();
  41. }
  42. /** Set the handle size, minimum 16.
  43. * Scale factor is automatically applied on top of this size as needed */
  44. void setHandleSize(const uint size)
  45. {
  46. handleSize = std::max(16u, size);
  47. resetArea();
  48. }
  49. protected:
  50. void onDisplay() override
  51. {
  52. // TODO implement gl3 stuff in DPF
  53. #ifndef DGL_USE_OPENGL3
  54. const GraphicsContext& context(getGraphicsContext());
  55. const double lineWidth = 1.0 * getScaleFactor();
  56. #if defined(DGL_OPENGL) && !defined(DGL_USE_OPENGL3)
  57. glMatrixMode(GL_MODELVIEW);
  58. #endif
  59. // draw white lines, 1px wide
  60. Color(1.0f, 1.0f, 1.0f).setFor(context);
  61. l1.draw(context, lineWidth);
  62. l2.draw(context, lineWidth);
  63. l3.draw(context, lineWidth);
  64. // draw black lines, offset by 1px and 1px wide
  65. Color(0.0f, 0.0f, 0.0f).setFor(context);
  66. Line<double> l1b(l1), l2b(l2), l3b(l3);
  67. l1b.moveBy(lineWidth, lineWidth);
  68. l2b.moveBy(lineWidth, lineWidth);
  69. l3b.moveBy(lineWidth, lineWidth);
  70. l1b.draw(context, lineWidth);
  71. l2b.draw(context, lineWidth);
  72. l3b.draw(context, lineWidth);
  73. #endif
  74. }
  75. bool onMouse(const MouseEvent& ev) override
  76. {
  77. if (ev.button != 1)
  78. return false;
  79. if (ev.press && area.contains(ev.pos))
  80. {
  81. isResizing = true;
  82. resizingSize = Size<double>(getWidth(), getHeight());
  83. lastResizePoint = ev.pos;
  84. return true;
  85. }
  86. if (isResizing && ! ev.press)
  87. {
  88. isResizing = false;
  89. recheckCursor(ev.pos);
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool onMotion(const MotionEvent& ev) override
  95. {
  96. if (! isResizing)
  97. {
  98. recheckCursor(ev.pos);
  99. return false;
  100. }
  101. const Size<double> offset(ev.pos.getX() - lastResizePoint.getX(),
  102. ev.pos.getY() - lastResizePoint.getY());
  103. resizingSize += offset;
  104. lastResizePoint = ev.pos;
  105. // TODO keepAspectRatio
  106. bool keepAspectRatio;
  107. const Size<uint> minSize(getWindow().getGeometryConstraints(keepAspectRatio));
  108. const uint minWidth = minSize.getWidth();
  109. const uint minHeight = minSize.getHeight();
  110. if (resizingSize.getWidth() < minWidth)
  111. resizingSize.setWidth(minWidth);
  112. if (resizingSize.getWidth() > 16384)
  113. resizingSize.setWidth(16384);
  114. if (resizingSize.getHeight() < minHeight)
  115. resizingSize.setHeight(minHeight);
  116. if (resizingSize.getHeight() > 16384)
  117. resizingSize.setHeight(16384);
  118. setSize(resizingSize.getWidth(), resizingSize.getHeight());
  119. return true;
  120. }
  121. void onResize(const ResizeEvent& ev) override
  122. {
  123. TopLevelWidget::onResize(ev);
  124. resetArea();
  125. }
  126. private:
  127. Rectangle<uint> area;
  128. Line<double> l1, l2, l3;
  129. uint handleSize;
  130. // event handling state
  131. bool hasCursor, isResizing;
  132. Point<double> lastResizePoint;
  133. Size<double> resizingSize;
  134. void recheckCursor(const Point<double>& pos)
  135. {
  136. const bool shouldHaveCursor = area.contains(pos);
  137. if (shouldHaveCursor == hasCursor)
  138. return;
  139. hasCursor = shouldHaveCursor;
  140. setCursor(shouldHaveCursor ? kMouseCursorDiagonal : kMouseCursorArrow);
  141. }
  142. void resetArea()
  143. {
  144. const double scaleFactor = getScaleFactor();
  145. const uint margin = 0.0 * scaleFactor;
  146. const uint size = handleSize * scaleFactor;
  147. area = Rectangle<uint>(getWidth() - size - margin,
  148. getHeight() - size - margin,
  149. size, size);
  150. recreateLines(area.getX(), area.getY(), size);
  151. }
  152. void recreateLines(const uint x, const uint y, const uint size)
  153. {
  154. uint linesize = size;
  155. uint offset = 0;
  156. // 1st line, full diagonal size
  157. l1.setStartPos(x + size, y);
  158. l1.setEndPos(x, y + size);
  159. // 2nd line, bit more to the right and down, cropped
  160. offset += size / 3;
  161. linesize -= size / 3;
  162. l2.setStartPos(x + linesize + offset, y + offset);
  163. l2.setEndPos(x + offset, y + linesize + offset);
  164. // 3rd line, even more right and down
  165. offset += size / 3;
  166. linesize -= size / 3;
  167. l3.setStartPos(x + linesize + offset, y + offset);
  168. l3.setEndPos(x + offset, y + linesize + offset);
  169. }
  170. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ResizeHandle)
  171. };
  172. END_NAMESPACE_DGL