Browse Source

Add a resize handle, remove custom keyboard input for resizing

Signed-off-by: falkTX <falktx@falktx.com>
master
falkTX 3 years ago
parent
commit
2cdb9c7b68
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
3 changed files with 194 additions and 34 deletions
  1. +7
    -32
      plugins/glBars/DistrhoUIGLBars.cpp
  2. +3
    -2
      plugins/glBars/DistrhoUIGLBars.hpp
  3. +184
    -0
      plugins/glBars/ResizeHandle.hpp

+ 7
- 32
plugins/glBars/DistrhoUIGLBars.cpp View File

@@ -3,7 +3,7 @@
* Copyright (C) 1998-2000 Peter Alm, Mikael Alm, Olle Hallnas, Thomas Nilsson and 4Front Technologies
* Copyright (C) 2000 Christian Zander <phoenix@minion.de>
* Copyright (C) 2015 Nedko Arnaudov
* Copyright (C) 2016-2019 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2016-2021 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -27,9 +27,14 @@ START_NAMESPACE_DISTRHO

DistrhoUIGLBars::DistrhoUIGLBars()
: UI(512, 512),
fInitialized(false)
fInitialized(false),
fResizeHandle(this)
{
setGeometryConstraints(256, 256, true);

// no need to show resize handle if window is user-resizable
if (isResizable())
fResizeHandle.hide();
}

DistrhoUIGLBars::~DistrhoUIGLBars()
@@ -100,36 +105,6 @@ void DistrhoUIGLBars::onDisplay()
fState.Render();
}

bool DistrhoUIGLBars::onKeyboard(const KeyboardEvent& ev)
{
if (ev.press && (ev.key == '1' || ev.key == '+' || ev.key == '-'))
{
if (ev.key == '1')
{
if (getWidth() != 512 || getHeight() != 512)
setSize(512, 512);
}
else if (ev.key == '+')
{
/**/ if (getWidth() < 1100 && getHeight() < 1100)
setSize(std::min(getWidth()+100, 1100U), std::min(getHeight()+100, 1100U));
else if (getWidth() != 1100 || getHeight() != 1100)
setSize(1100, 1100);
}
else if (ev.key == '-')
{
/**/ if (getWidth() > 100 && getHeight() > 100)
setSize(std::max(getWidth()-100, 100U), std::max(getHeight()-100, 100U));
else if (getWidth() != 100 || getHeight() != 100)
setSize(100, 100);
}

return true;
}

return true;
}

// -----------------------------------------------------------------------

UI* createUI()


+ 3
- 2
plugins/glBars/DistrhoUIGLBars.hpp View File

@@ -3,7 +3,7 @@
* Copyright (C) 1998-2000 Peter Alm, Mikael Alm, Olle Hallnas, Thomas Nilsson and 4Front Technologies
* Copyright (C) 2000 Christian Zander <phoenix@minion.de>
* Copyright (C) 2015 Nedko Arnaudov
* Copyright (C) 2016-2019 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2016-2021 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -24,6 +24,7 @@
#include "DistrhoUI.hpp"

#include "glBars.hpp"
#include "ResizeHandle.hpp"

START_NAMESPACE_DISTRHO

@@ -50,11 +51,11 @@ protected:
// Widget Callbacks

void onDisplay() override;
bool onKeyboard(const KeyboardEvent&) override;

private:
bool fInitialized;
glBarsState fState;
ResizeHandle fResizeHandle;

DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(DistrhoUIGLBars)
};


+ 184
- 0
plugins/glBars/ResizeHandle.hpp View File

@@ -0,0 +1,184 @@
/*
* Resize handle for DPF
* Copyright (C) 2021 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 "TopLevelWidget.hpp"
#include "Color.hpp"

START_NAMESPACE_DGL

/** Resize handle for DPF windows, will sit on bottom-right. */
class ResizeHandle : public TopLevelWidget
{
public:
/** Constructor for placing this handle on top of a window. */
explicit ResizeHandle(Window& window)
: TopLevelWidget(window),
handleSize(16),
resizing(false)
{
resetArea();
}

/** Overloaded constructor, will fetch the window from an existing top-level widget. */
explicit ResizeHandle(TopLevelWidget* const tlw)
: TopLevelWidget(tlw->getWindow()),
handleSize(16),
resizing(false)
{
resetArea();
}

/** Set the handle size, minimum 16. */
void setHandleSize(const uint size)
{
handleSize = std::max(16u, size);
resetArea();
}

protected:
void onDisplay() override
{
const GraphicsContext& context(getGraphicsContext());
const double lineWidth = 1.0 * getScaleFactor();

#ifdef DGL_OPENGL
glMatrixMode(GL_MODELVIEW);
#endif

// draw white lines, 1px wide
Color(1.0f, 1.0f, 1.0f).setFor(context);
l1.draw(context, lineWidth);
l2.draw(context, lineWidth);
l3.draw(context, lineWidth);

// draw black lines, offset by 1px and 1px wide
Color(0.0f, 0.0f, 0.0f).setFor(context);
Line<double> l1b(l1), l2b(l2), l3b(l3);
l1b.moveBy(lineWidth, lineWidth);
l2b.moveBy(lineWidth, lineWidth);
l3b.moveBy(lineWidth, lineWidth);
l1b.draw(context, lineWidth);
l2b.draw(context, lineWidth);
l3b.draw(context, lineWidth);
}

bool onMouse(const MouseEvent& ev) override
{
if (ev.button != 1)
return false;

if (ev.press && area.contains(ev.pos))
{
resizing = true;
resizingSize = Size<double>(getWidth(), getHeight());
lastResizePoint = ev.pos;
return true;
}

if (resizing && ! ev.press)
{
resizing = false;
return true;
}

return false;
}

bool onMotion(const MotionEvent& ev) override
{
if (! resizing)
return false;

const Size<double> offset(ev.pos.getX() - lastResizePoint.getX(),
ev.pos.getY() - lastResizePoint.getY());

resizingSize += offset;
lastResizePoint = ev.pos;

// TODO min width, min height
const uint minWidth = 16;
const uint minHeight = 16;

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 resizing;
Point<double> lastResizePoint;
Size<double> resizingSize;

void resetArea()
{
const double scaleFactor = getScaleFactor();
const uint margin = 0.0 * 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

Loading…
Cancel
Save