From 4b07bd7c51d5996d6bbfdfbd0554ba34470cd3f0 Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 23 May 2021 16:34:30 +0100 Subject: [PATCH] Add a file browser dialog test tool Signed-off-by: falkTX --- dgl/Geometry.hpp | 6 ++ dgl/src/Geometry.cpp | 19 ++++ dgl/src/pugl.cpp | 2 +- examples/FileHandling/NanoButton.cpp | 4 +- tests/FileBrowserDialog.cpp | 151 +++++++++++++++++++++++++++ tests/Makefile | 5 + 6 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 tests/FileBrowserDialog.cpp diff --git a/dgl/Geometry.hpp b/dgl/Geometry.hpp index 1bd02282..76d9c158 100644 --- a/dgl/Geometry.hpp +++ b/dgl/Geometry.hpp @@ -751,6 +751,12 @@ public: */ bool contains(const Point& pos) const noexcept; + /** + Check if this rectangle contains the point @a pos of another type. + */ + template + bool contains(const Point& pos) const noexcept; + /** Check if this rectangle contains X. */ diff --git a/dgl/src/Geometry.cpp b/dgl/src/Geometry.cpp index f2c44c44..8b1ba7d1 100644 --- a/dgl/src/Geometry.cpp +++ b/dgl/src/Geometry.cpp @@ -885,6 +885,25 @@ bool Rectangle::contains(const Point& p) const noexcept return contains(p.x, p.y); } +template +template +bool Rectangle::contains(const Point& 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::contains(const Point& 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::contains(const Point& 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 bool Rectangle::containsX(const T& x) const noexcept { diff --git a/dgl/src/pugl.cpp b/dgl/src/pugl.cpp index 3902a199..5a013bda 100644 --- 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 index a3850403..6b77dccd 100644 --- 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 new file mode 100644 index 00000000..e3a15847 --- /dev/null +++ b/tests/FileBrowserDialog.cpp @@ -0,0 +1,151 @@ +/* + * DISTRHO Plugin Framework (DPF) + * Copyright (C) 2012-2021 Filipe Coelho + * + * 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 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 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(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 index 1cf83350..a0a9c31e 100644 --- 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 $@