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.

181 lines
5.2KB

  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. // draw white lines, 1px wide
  52. Color(1.0f, 1.0f, 1.0f).setFor(context);
  53. l1.draw(context, lineWidth);
  54. l2.draw(context, lineWidth);
  55. l3.draw(context, lineWidth);
  56. // draw black lines, offset by 1px and 1px wide
  57. Color(0.0f, 0.0f, 0.0f).setFor(context);
  58. Line<double> l1b(l1), l2b(l2), l3b(l3);
  59. l1b.moveBy(lineWidth, lineWidth);
  60. l2b.moveBy(lineWidth, lineWidth);
  61. l3b.moveBy(lineWidth, lineWidth);
  62. l1b.draw(context, lineWidth);
  63. l2b.draw(context, lineWidth);
  64. l3b.draw(context, lineWidth);
  65. }
  66. bool onMouse(const MouseEvent& ev) override
  67. {
  68. if (ev.button != 1)
  69. return false;
  70. if (ev.press && area.contains(ev.pos))
  71. {
  72. resizing = true;
  73. resizingSize = Size<double>(getWidth(), getHeight());
  74. lastResizePoint = ev.pos;
  75. return true;
  76. }
  77. if (resizing && ! ev.press)
  78. {
  79. resizing = false;
  80. return true;
  81. }
  82. return false;
  83. }
  84. bool onMotion(const MotionEvent& ev) override
  85. {
  86. if (! resizing)
  87. return false;
  88. const Size<double> offset(ev.pos.getX() - lastResizePoint.getX(),
  89. ev.pos.getY() - lastResizePoint.getY());
  90. resizingSize += offset;
  91. lastResizePoint = ev.pos;
  92. // TODO min width, min height
  93. const uint minWidth = 16;
  94. const uint minHeight = 16;
  95. if (resizingSize.getWidth() < minWidth)
  96. resizingSize.setWidth(minWidth);
  97. if (resizingSize.getWidth() > 16384)
  98. resizingSize.setWidth(16384);
  99. if (resizingSize.getHeight() < minHeight)
  100. resizingSize.setHeight(minHeight);
  101. if (resizingSize.getHeight() > 16384)
  102. resizingSize.setHeight(16384);
  103. setSize(resizingSize.getWidth(), resizingSize.getHeight());
  104. return true;
  105. }
  106. void onResize(const ResizeEvent& ev) override
  107. {
  108. TopLevelWidget::onResize(ev);
  109. resetArea();
  110. }
  111. private:
  112. Rectangle<uint> area;
  113. Line<double> l1, l2, l3;
  114. uint handleSize;
  115. // event handling state
  116. bool resizing;
  117. Point<double> lastResizePoint;
  118. Size<double> resizingSize;
  119. void resetArea()
  120. {
  121. const double scaleFactor = getScaleFactor();
  122. const uint margin = 0.0 * scaleFactor;
  123. const uint size = handleSize * scaleFactor;
  124. area = Rectangle<uint>(getWidth() - size - margin,
  125. getHeight() - size - margin,
  126. size, size);
  127. recreateLines(area.getX(), area.getY(), size);
  128. }
  129. void recreateLines(const uint x, const uint y, const uint size)
  130. {
  131. uint linesize = size;
  132. uint offset = 0;
  133. // 1st line, full diagonal size
  134. l1.setStartPos(x + size, y);
  135. l1.setEndPos(x, y + size);
  136. // 2nd line, bit more to the right and down, cropped
  137. offset += size / 3;
  138. linesize -= size / 3;
  139. l2.setStartPos(x + linesize + offset, y + offset);
  140. l2.setEndPos(x + offset, y + linesize + offset);
  141. // 3rd line, even more right and down
  142. offset += size / 3;
  143. linesize -= size / 3;
  144. l3.setStartPos(x + linesize + offset, y + offset);
  145. l3.setEndPos(x + offset, y + linesize + offset);
  146. }
  147. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ResizeHandle)
  148. };
  149. END_NAMESPACE_DGL