Browse Source

Rework file handling example button, add a note for later

Signed-off-by: falkTX <falktx@falktx.com>
pull/309/head
falkTX 4 years ago
parent
commit
e5df7030c2
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
4 changed files with 82 additions and 72 deletions
  1. +11
    -0
      dgl/EventHandlers.hpp
  2. +12
    -8
      examples/FileHandling/FileHandlingUI.cpp
  3. +41
    -43
      examples/FileHandling/NanoButton.cpp
  4. +18
    -21
      examples/FileHandling/NanoButton.hpp

+ 11
- 0
dgl/EventHandlers.hpp View File

@@ -21,6 +21,17 @@

START_NAMESPACE_DGL

/* NOTE none of these classes get assigned to a widget automatically
Manual plugging into Widget events is needed, like so:

```
bool onMouse(const MouseEvent& ev) override
{
return ButtonEventHandler::mouseEvent(ev);
}
```
*/

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

class ButtonEventHandler


+ 12
- 8
examples/FileHandling/FileHandlingUI.cpp View File

@@ -1,6 +1,6 @@
/*
* DISTRHO Plugin Framework (DPF)
* Copyright (C) 2012-2020 Filipe Coelho <falktx@falktx.com>
* 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
@@ -33,7 +33,7 @@ const char* kStateKeys[kStateCount] = {

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

static void setupButton(Button& btn, int y)
inline void setupButton(Button& btn, const int y)
{
btn.setAbsolutePos(5, y);
btn.setLabel("Open...");
@@ -41,7 +41,7 @@ static void setupButton(Button& btn, int y)
}

class FileHandlingExampleUI : public UI,
public Button::Callback
public ButtonEventHandler::Callback
{
public:
static const uint kInitialWidth = 600;
@@ -67,7 +67,7 @@ public:
loadSharedResources();
#endif

setGeometryConstraints(kInitialWidth, kInitialHeight, true);
setGeometryConstraints(kInitialWidth, kInitialHeight, false);
}

protected:
@@ -193,18 +193,22 @@ protected:
fButton2.setSize(100*fScale, 30*fScale);
fButton3.setSize(100*fScale, 30*fScale);

fButton1.setFontScale(fScale);
fButton2.setFontScale(fScale);
fButton3.setFontScale(fScale);

UI::onResize(ev);
}

void buttonClicked(Button* const button, bool) override
void buttonClicked(SubWidget* const widget, int) override
{
States stateId;

/**/ if (button == &fButton1)
/**/ if (widget == &fButton1)
stateId = kStateFile1;
else if (button == &fButton2)
else if (widget == &fButton2)
stateId = kStateFile2;
else if (button == &fButton3)
else if (widget == &fButton3)
stateId = kStateFile3;
else
return;


+ 41
- 43
examples/FileHandling/NanoButton.cpp View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2018-2019 Rob van den Berg <rghvdberg at gmail dot org>
* Copyright (C) 2020-2021 Filipe Coelho <falktx@falktx.com>
*
* This file is part of CharacterCompressor
*
@@ -22,16 +23,40 @@

START_NAMESPACE_DGL

Button::Button(Widget *parent, Callback *cb)
Button::Button(Widget* const parent, ButtonEventHandler::Callback* const cb)
: NanoWidget(parent),
fCallback(cb),
buttonActive(false)
ButtonEventHandler(this),
backgroundColor(32, 32, 32),
labelColor(255, 255, 255),
label("button"),
fontScale(1.0f)
{
#ifdef DGL_NO_SHARED_RESOURCES
createFontFromFile("sans", "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf");
#else
loadSharedResources();
fNanoFont = findFont(NANOVG_DEJAVU_SANS_TTF);
labelColor = Color(255, 255, 255);
backgroundColor = Color(32,32,32);
Label = "button";
#endif
ButtonEventHandler::setCallback(cb);
}

void Button::setBackgroundColor(const Color color)
{
backgroundColor = color;
}

void Button::setFontScale(const float scale)
{
fontScale = scale;
}

void Button::setLabel(const std::string& label2)
{
label = label2;
}

void Button::setLabelColor(const Color color)
{
labelColor = color;
}

void Button::onNanoDisplay()
@@ -43,62 +68,35 @@ void Button::onNanoDisplay()
// Background
beginPath();
fillColor(backgroundColor);
strokeColor(borderColor);
strokeColor(labelColor);
rect(margin, margin, w - 2 * margin, h - 2 * margin);
fill();
stroke();
closePath();

//Label
// Label
beginPath();
fontFaceId(fNanoFont);
fontSize(14);
fontSize(14 * fontScale);
fillColor(labelColor);
Rectangle<float> bounds;
textBounds(0, 0, Label.c_str(), NULL, bounds);
// float tw = bounds.getWidth();
// float th = bounds.getHeight();
textBounds(0, 0, label.c_str(), NULL, bounds);
float tx = w / 2.0f ;
float ty = h / 2.0f;
textAlign(ALIGN_CENTER | ALIGN_MIDDLE);

fillColor(255, 255, 255, 255);
text(tx, ty, Label.c_str(), NULL);
text(tx, ty, label.c_str(), NULL);
closePath();
}

void Button::setLabel(std::string label)
bool Button::onMouse(const MouseEvent& ev)
{
Label = label;
return ButtonEventHandler::mouseEvent(ev);
}

void Button::setLabelColor(const Color color)
bool Button::onMotion(const MotionEvent& ev)
{
labelColor = color;
borderColor = color;
}
void Button::setBackgroundColor(const Color color)
{
backgroundColor = color;
}

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

return false;
return ButtonEventHandler::motionEvent(ev);
}

END_NAMESPACE_DGL

+ 18
- 21
examples/FileHandling/NanoButton.hpp View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2018-2019 Rob van den Berg <rghvdberg at gmail dot org>
* Copyright (C) 2020-2021 Filipe Coelho <falktx@falktx.com>
*
* This file is part of CharacterCompressor
*
@@ -17,44 +18,40 @@
* along with CharacterCompressor. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef BUTTON_HPP_INCLUDED
#define BUTTON_HPP_INCLUDED
#ifndef NANO_BUTTON_HPP_INCLUDED
#define NANO_BUTTON_HPP_INCLUDED

#include "Widget.hpp"
#include "NanoVG.hpp"

#include <string>

START_NAMESPACE_DGL

class Button : public NanoSubWidget
class Button : public NanoSubWidget,
public ButtonEventHandler
{
public:
class Callback
{
public:
virtual ~Callback() {}
virtual void buttonClicked(Button *button, bool value) = 0;
};
explicit Button(Widget *parent, Callback *cb);

void setLabel(std::string label);
void setLabelColor(Color color);
explicit Button(Widget* parent, ButtonEventHandler::Callback* cb);

void setBackgroundColor(Color color);
void setFontScale(float scale);
void setLabel(const std::string& label);
void setLabelColor(Color color);

protected:
void onNanoDisplay() override;
bool onMouse(const MouseEvent &ev) override;
bool onMouse(const MouseEvent& ev) override;
bool onMotion(const MotionEvent& ev) override;

private:
std::string Label;
Color labelColor,backgroundColor,borderColor;
Callback *const fCallback;
bool buttonActive;
FontId fNanoFont;
Color backgroundColor;
Color labelColor;
std::string label;
float fontScale;
DISTRHO_LEAK_DETECTOR(Button)
};

END_NAMESPACE_DGL

#endif
#endif // NANO_BUTTON_HPP_INCLUDED

Loading…
Cancel
Save