commit 4b07bd7c51d5996d6bbfdfbd0554ba34470cd3f0
parent d36f61a78a99694cb8a8d58ae0c0cbbdf503c605
Author: falkTX <falktx@falktx.com>
Date: Sun, 23 May 2021 16:34:03 +0100
Add a file browser dialog test tool
Signed-off-by: falkTX <falktx@falktx.com>
Diffstat:
6 files changed, 184 insertions(+), 3 deletions(-)
diff --git a/dgl/Geometry.hpp b/dgl/Geometry.hpp
@@ -752,6 +752,12 @@ public:
bool contains(const Point<T>& pos) const noexcept;
/**
+ Check if this rectangle contains the point @a pos of another type.
+ */
+ template<typename T2>
+ bool contains(const Point<T2>& pos) const noexcept;
+
+ /**
Check if this rectangle contains X.
*/
bool containsX(const T& x) const noexcept;
diff --git a/dgl/src/Geometry.cpp b/dgl/src/Geometry.cpp
@@ -886,6 +886,25 @@ bool Rectangle<T>::contains(const Point<T>& p) const noexcept
}
template<typename T>
+template<typename T2>
+bool Rectangle<T>::contains(const Point<T2>& p) const noexcept
+{
+ return (p.x >= pos.x && p.y >= pos.y && p.x <= pos.x+size.fWidth && p.y <= pos.y+size.fHeight);
+}
+
+template<> template<>
+bool Rectangle<int>::contains(const Point<double>& p) const noexcept
+{
+ return (p.x >= pos.x && p.y >= pos.y && p.x <= pos.x+size.fWidth && p.y <= pos.y+size.fHeight);
+}
+
+template<> template<>
+bool Rectangle<uint>::contains(const Point<double>& p) const noexcept
+{
+ return (p.x >= pos.x && p.y >= pos.y && p.x <= pos.x+size.fWidth && p.y <= pos.y+size.fHeight);
+}
+
+template<typename T>
bool Rectangle<T>::containsX(const T& x) const noexcept
{
return (x >= pos.x && x <= pos.x + size.fWidth);
diff --git a/dgl/src/pugl.cpp b/dgl/src/pugl.cpp
@@ -90,7 +90,7 @@
#endif
#ifdef HAVE_X11
-# define 400
+# define DBLCLKTME 400
# include "sofd/libsofd.h"
# include "sofd/libsofd.c"
#endif
diff --git a/examples/FileHandling/NanoButton.cpp b/examples/FileHandling/NanoButton.cpp
@@ -84,10 +84,10 @@ void Button::setBackgroundColor(const Color color)
bool Button::onMouse(const MouseEvent &ev)
{
- if (ev.press & contains(ev.pos))
+ if (ev.press && contains(ev.pos))
{
buttonActive = true;
- setLabelColor(labelColor);
+ setLabelColor(labelColor);
fCallback->buttonClicked(this, true);
return true;
}
diff --git a/tests/FileBrowserDialog.cpp b/tests/FileBrowserDialog.cpp
@@ -0,0 +1,151 @@
+/*
+ * DISTRHO Plugin Framework (DPF)
+ * Copyright (C) 2012-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.
+ */
+
+#include "tests.hpp"
+
+#include "dgl/NanoVG.hpp"
+
+START_NAMESPACE_DGL
+
+// --------------------------------------------------------------------------------------------------------------------
+
+class NanoFilePicker : public NanoStandaloneWindow
+{
+ Rectangle<uint> buttonBounds;
+ bool buttonClick = false;
+ bool buttonHover = false;
+
+public:
+ NanoFilePicker(Application& app)
+ : NanoStandaloneWindow(app)
+ {
+#ifndef DGL_NO_SHARED_RESOURCES
+ loadSharedResources();
+#endif
+
+ }
+
+protected:
+ void onNanoDisplay() override
+ {
+ Color labelColor(255, 255, 255);
+ Color backgroundColor(32,
+ buttonClick ? 128 : 32,
+ buttonHover ? 128 : 32);
+ Color borderColor;
+
+ // Button background
+ beginPath();
+ fillColor(backgroundColor);
+ strokeColor(borderColor);
+ rect(buttonBounds.getX(), buttonBounds.getY(), buttonBounds.getWidth(), buttonBounds.getHeight());
+ fill();
+ stroke();
+ closePath();
+
+ // Label
+ beginPath();
+ fontSize(14);
+ fillColor(labelColor);
+ Rectangle<float> buttonTextBounds;
+ textBounds(0, 0, "Press me", NULL, buttonTextBounds);
+ textAlign(ALIGN_CENTER | ALIGN_MIDDLE);
+
+ fillColor(255, 255, 255, 255);
+ text(buttonBounds.getX() + buttonBounds.getWidth()/2,
+ buttonBounds.getY() + buttonBounds.getHeight()/2,
+ "Press me", NULL);
+ closePath();
+ }
+
+ bool onMotion(const MotionEvent& ev) override
+ {
+ const bool newButtonHover = buttonBounds.contains(ev.pos);
+
+ if (newButtonHover != buttonHover)
+ {
+ buttonHover = newButtonHover;
+ repaint();
+ return true;
+ }
+
+ return newButtonHover;
+ }
+
+ bool onMouse(const MouseEvent& ev) override
+ {
+ if (ev.button != 1)
+ return false;
+
+ if (! buttonBounds.contains(ev.pos))
+ {
+ if (buttonClick)
+ {
+ buttonClick = false;
+ repaint();
+ return true;
+ }
+ return false;
+ }
+
+ const bool newButtonClick = ev.press;
+
+ if (newButtonClick != buttonClick)
+ {
+ buttonClick = newButtonClick;
+ repaint();
+
+ if (newButtonClick)
+ {
+ FileBrowserOptions opts;
+ opts.title = "Look at me";
+ openFileBrowser(opts);
+ }
+
+ return true;
+ }
+
+ return newButtonClick;
+ }
+
+ void onResize(const ResizeEvent& ev) override
+ {
+ const uint width = ev.size.getWidth();
+ const uint height = ev.size.getHeight();
+
+ buttonBounds = Rectangle<uint>(width - 120, height/2 - 20, 100, 40);
+ }
+};
+
+// --------------------------------------------------------------------------------------------------------------------
+
+END_NAMESPACE_DGL
+
+int main()
+{
+ USE_NAMESPACE_DGL;
+
+ Application app(true);
+ NanoFilePicker win(app);
+ win.setSize(500, 200);
+ win.setTitle("FileBrowserDialog");
+ win.show();
+ app.exec();
+
+ return 0;
+}
+
+// --------------------------------------------------------------------------------------------------------------------
diff --git a/tests/Makefile b/tests/Makefile
@@ -30,6 +30,7 @@ UNIT_TESTS += Window.cairo
endif
ifeq ($(HAVE_OPENGL),true)
MANUAL_TESTS += Demo.opengl
+MANUAL_TESTS += FileBrowserDialog
MANUAL_TESTS += NanoSubWidgets
UNIT_TESTS += Window.opengl
endif
@@ -138,6 +139,10 @@ clean:
@echo "Linking Demo (OpenGL)"
$(SILENT)$(CXX) $^ $(LINK_FLAGS) $(DGL_SYSTEM_LIBS) $(VULKAN_LIBS) -o $@
+../build/tests/FileBrowserDialog$(APP_EXT): ../build/tests/FileBrowserDialog.cpp.o ../build/libdgl-opengl.a
+ @echo "Linking Demo (OpenGL)"
+ $(SILENT)$(CXX) $^ $(LINK_FLAGS) $(DGL_SYSTEM_LIBS) $(OPENGL_LIBS) -o $@
+
../build/tests/NanoSubWidgets$(APP_EXT): ../build/tests/NanoSubWidgets.cpp.o ../build/libdgl-opengl.a
@echo "Linking Demo (OpenGL)"
$(SILENT)$(CXX) $^ $(LINK_FLAGS) $(DGL_SYSTEM_LIBS) $(OPENGL_LIBS) -o $@