1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*
* Resize handle for DPF
* Copyright (C) 2021-2022 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include "NanoVG.hpp"
START_NAMESPACE_DGL
/** Resize handle for DPF windows, will sit on bottom-right. */
class ResizeHandle : public NanoTopLevelWidget
{
public:
/** Overloaded constructor, will fetch the window from an existing top-level widget. */
explicit ResizeHandle(TopLevelWidget* const tlw)
: NanoTopLevelWidget(tlw->getWindow()),
handleSize(16),
hasCursor(false),
isResizing(false)
{
resetArea();
}
/** Set the handle size, minimum 16.
* Scale factor is automatically applied on top of this size as needed */
void setHandleSize(const uint size)
{
handleSize = std::max(16u, size);
resetArea();
}
protected:
void onNanoDisplay() override
{
const double lineWidth = 1.0 * getScaleFactor();
strokeWidth(lineWidth);
// draw white lines, 1px wide
strokeColor(Color(1.0f, 1.0f, 1.0f));
drawLine(l1);
drawLine(l2);
drawLine(l3);
// draw black lines, offset by 1px and 1px wide
strokeColor(Color(0.0f, 0.0f, 0.0f));
Line<double> l1b(l1), l2b(l2), l3b(l3);
l1b.moveBy(lineWidth, lineWidth);
l2b.moveBy(lineWidth, lineWidth);
l3b.moveBy(lineWidth, lineWidth);
drawLine(l1b);
drawLine(l2b);
drawLine(l3b);
}
void drawLine(const Line<double>& line)
{
beginPath();
moveTo(line.getStartPos().getX(), line.getStartPos().getY());
lineTo(line.getEndPos().getX(), line.getEndPos().getY());
stroke();
}
bool onMouse(const MouseEvent& ev) override
{
if (ev.button != 1)
return false;
if (ev.press && area.contains(ev.pos))
{
isResizing = true;
resizingSize = Size<double>(getWidth(), getHeight());
lastResizePoint = ev.pos;
return true;
}
if (isResizing && ! ev.press)
{
isResizing = false;
recheckCursor(ev.pos);
return true;
}
return false;
}
bool onMotion(const MotionEvent& ev) override
{
if (! isResizing)
{
recheckCursor(ev.pos);
return false;
}
const Size<double> offset(ev.pos.getX() - lastResizePoint.getX(),
ev.pos.getY() - lastResizePoint.getY());
resizingSize += offset;
lastResizePoint = ev.pos;
// TODO keepAspectRatio
bool keepAspectRatio;
const Size<uint> minSize(getWindow().getGeometryConstraints(keepAspectRatio));
const uint minWidth = minSize.getWidth();
const uint minHeight = minSize.getHeight();
if (resizingSize.getWidth() < minWidth)
resizingSize.setWidth(minWidth);
if (resizingSize.getWidth() > 16384)
resizingSize.setWidth(16384);
if (resizingSize.getHeight() < minHeight)
resizingSize.setHeight(minHeight);
if (resizingSize.getHeight() > 16384)
resizingSize.setHeight(16384);
setSize(resizingSize.getWidth(), resizingSize.getHeight());
return true;
}
void onResize(const ResizeEvent& ev) override
{
TopLevelWidget::onResize(ev);
resetArea();
}
private:
Rectangle<uint> area;
Line<double> l1, l2, l3;
uint handleSize;
// event handling state
bool hasCursor, isResizing;
Point<double> lastResizePoint;
Size<double> resizingSize;
void recheckCursor(const Point<double>& pos)
{
const bool shouldHaveCursor = area.contains(pos);
if (shouldHaveCursor == hasCursor)
return;
hasCursor = shouldHaveCursor;
setCursor(shouldHaveCursor ? kMouseCursorDiagonal : kMouseCursorArrow);
}
void resetArea()
{
const double scaleFactor = getScaleFactor();
const uint margin = 1.5 * scaleFactor;
const uint size = handleSize * scaleFactor;
area = Rectangle<uint>(getWidth() - size - margin,
getHeight() - size - margin,
size, size);
recreateLines(area.getX(), area.getY(), size);
}
void recreateLines(const uint x, const uint y, const uint size)
{
uint linesize = size;
uint offset = 0;
// 1st line, full diagonal size
l1.setStartPos(x + size, y);
l1.setEndPos(x, y + size);
// 2nd line, bit more to the right and down, cropped
offset += size / 3;
linesize -= size / 3;
l2.setStartPos(x + linesize + offset, y + offset);
l2.setEndPos(x + offset, y + linesize + offset);
// 3rd line, even more right and down
offset += size / 3;
linesize -= size / 3;
l3.setStartPos(x + linesize + offset, y + offset);
l3.setEndPos(x + offset, y + linesize + offset);
}
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ResizeHandle)
};
END_NAMESPACE_DGL
|