Browse Source

Add a file browser dialog test tool

Signed-off-by: falkTX <falktx@falktx.com>
pull/272/head
falkTX 4 years ago
parent
commit
4b07bd7c51
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
6 changed files with 184 additions and 3 deletions
  1. +6
    -0
      dgl/Geometry.hpp
  2. +19
    -0
      dgl/src/Geometry.cpp
  3. +1
    -1
      dgl/src/pugl.cpp
  4. +2
    -2
      examples/FileHandling/NanoButton.cpp
  5. +151
    -0
      tests/FileBrowserDialog.cpp
  6. +5
    -0
      tests/Makefile

+ 6
- 0
dgl/Geometry.hpp View File

@@ -751,6 +751,12 @@ public:
*/ */
bool contains(const Point<T>& pos) const noexcept; 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. Check if this rectangle contains X.
*/ */


+ 19
- 0
dgl/src/Geometry.cpp View File

@@ -885,6 +885,25 @@ bool Rectangle<T>::contains(const Point<T>& p) const noexcept
return contains(p.x, p.y); return contains(p.x, p.y);
} }


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> template<typename T>
bool Rectangle<T>::containsX(const T& x) const noexcept bool Rectangle<T>::containsX(const T& x) const noexcept
{ {


+ 1
- 1
dgl/src/pugl.cpp View File

@@ -90,7 +90,7 @@
#endif #endif


#ifdef HAVE_X11 #ifdef HAVE_X11
# define 400
# define DBLCLKTME 400
# include "sofd/libsofd.h" # include "sofd/libsofd.h"
# include "sofd/libsofd.c" # include "sofd/libsofd.c"
#endif #endif


+ 2
- 2
examples/FileHandling/NanoButton.cpp View File

@@ -84,10 +84,10 @@ void Button::setBackgroundColor(const Color color)


bool Button::onMouse(const MouseEvent &ev) bool Button::onMouse(const MouseEvent &ev)
{ {
if (ev.press & contains(ev.pos))
if (ev.press && contains(ev.pos))
{ {
buttonActive = true; buttonActive = true;
setLabelColor(labelColor);
setLabelColor(labelColor);
fCallback->buttonClicked(this, true); fCallback->buttonClicked(this, true);
return true; return true;
} }


+ 151
- 0
tests/FileBrowserDialog.cpp View File

@@ -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;
}

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

+ 5
- 0
tests/Makefile View File

@@ -30,6 +30,7 @@ UNIT_TESTS += Window.cairo
endif endif
ifeq ($(HAVE_OPENGL),true) ifeq ($(HAVE_OPENGL),true)
MANUAL_TESTS += Demo.opengl MANUAL_TESTS += Demo.opengl
MANUAL_TESTS += FileBrowserDialog
MANUAL_TESTS += NanoSubWidgets MANUAL_TESTS += NanoSubWidgets
UNIT_TESTS += Window.opengl UNIT_TESTS += Window.opengl
endif endif
@@ -138,6 +139,10 @@ clean:
@echo "Linking Demo (OpenGL)" @echo "Linking Demo (OpenGL)"
$(SILENT)$(CXX) $^ $(LINK_FLAGS) $(DGL_SYSTEM_LIBS) $(VULKAN_LIBS) -o $@ $(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 ../build/tests/NanoSubWidgets$(APP_EXT): ../build/tests/NanoSubWidgets.cpp.o ../build/libdgl-opengl.a
@echo "Linking Demo (OpenGL)" @echo "Linking Demo (OpenGL)"
$(SILENT)$(CXX) $^ $(LINK_FLAGS) $(DGL_SYSTEM_LIBS) $(OPENGL_LIBS) -o $@ $(SILENT)$(CXX) $^ $(LINK_FLAGS) $(DGL_SYSTEM_LIBS) $(OPENGL_LIBS) -o $@


Loading…
Cancel
Save