Browse Source

Fix mouse handling for ImageButton; Send motion event when modal closes

pull/6/head
falkTX 9 years ago
parent
commit
655aabc0e1
4 changed files with 79 additions and 51 deletions
  1. +1
    -1
      dgl/ImageWidgets.hpp
  2. +7
    -2
      dgl/Widget.hpp
  3. +55
    -48
      dgl/src/ImageWidgets.cpp
  4. +16
    -0
      dgl/src/Window.cpp

+ 1
- 1
dgl/ImageWidgets.hpp View File

@@ -77,8 +77,8 @@ private:
Image fImageNormal;
Image fImageHover;
Image fImageDown;
Image& fCurImage;
int fCurButton;
int fCurState;

Callback* fCallback;



+ 7
- 2
dgl/Widget.hpp View File

@@ -21,11 +21,15 @@

#include <vector>

START_NAMESPACE_DGL

// -----------------------------------------------------------------------
// Forward class names

START_NAMESPACE_DISTRHO
class UI;
END_NAMESPACE_DISTRHO

START_NAMESPACE_DGL

class App;
class ImageSlider;
class NanoWidget;
@@ -379,6 +383,7 @@ private:
friend class NanoWidget;
friend class Window;
friend class StandaloneWindow;
friend class DISTRHO_NAMESPACE::UI;

DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Widget)
};


+ 55
- 48
dgl/src/ImageWidgets.cpp View File

@@ -93,12 +93,12 @@ ImageButton::ImageButton(Window& parent, const Image& image) noexcept
fImageNormal(image),
fImageHover(image),
fImageDown(image),
fCurImage(fImageNormal),
fCurButton(-1),
fCurState(0),
fCallback(nullptr),
leakDetector_ImageButton()
{
setSize(fCurImage.getSize());
setSize(image.getSize());
}

ImageButton::ImageButton(Window& parent, const Image& imageNormal, const Image& imageDown) noexcept
@@ -106,14 +106,14 @@ ImageButton::ImageButton(Window& parent, const Image& imageNormal, const Image&
fImageNormal(imageNormal),
fImageHover(imageNormal),
fImageDown(imageDown),
fCurImage(fImageNormal),
fCurButton(-1),
fCurState(0),
fCallback(nullptr),
leakDetector_ImageButton()
{
DISTRHO_SAFE_ASSERT(fImageNormal.getSize() == fImageDown.getSize());
DISTRHO_SAFE_ASSERT(imageNormal.getSize() == imageDown.getSize());

setSize(fCurImage.getSize());
setSize(imageNormal.getSize());
}

ImageButton::ImageButton(Window& parent, const Image& imageNormal, const Image& imageHover, const Image& imageDown) noexcept
@@ -121,14 +121,14 @@ ImageButton::ImageButton(Window& parent, const Image& imageNormal, const Image&
fImageNormal(imageNormal),
fImageHover(imageHover),
fImageDown(imageDown),
fCurImage(fImageNormal),
fCurButton(-1),
fCurState(0),
fCallback(nullptr),
leakDetector_ImageButton()
{
DISTRHO_SAFE_ASSERT(fImageNormal.getSize() == fImageHover.getSize() && fImageHover.getSize() == fImageDown.getSize());
DISTRHO_SAFE_ASSERT(imageNormal.getSize() == imageHover.getSize() && imageHover.getSize() == imageDown.getSize());

setSize(fCurImage.getSize());
setSize(imageNormal.getSize());
}

ImageButton::ImageButton(Widget* widget, const Image& image) noexcept
@@ -136,12 +136,12 @@ ImageButton::ImageButton(Widget* widget, const Image& image) noexcept
fImageNormal(image),
fImageHover(image),
fImageDown(image),
fCurImage(fImageNormal),
fCurButton(-1),
fCurState(0),
fCallback(nullptr),
leakDetector_ImageButton()
{
setSize(fCurImage.getSize());
setSize(image.getSize());
}

ImageButton::ImageButton(Widget* widget, const Image& imageNormal, const Image& imageDown) noexcept
@@ -149,14 +149,14 @@ ImageButton::ImageButton(Widget* widget, const Image& imageNormal, const Image&
fImageNormal(imageNormal),
fImageHover(imageNormal),
fImageDown(imageDown),
fCurImage(fImageNormal),
fCurButton(-1),
fCurState(0),
fCallback(nullptr),
leakDetector_ImageButton()
{
DISTRHO_SAFE_ASSERT(fImageNormal.getSize() == fImageDown.getSize());
DISTRHO_SAFE_ASSERT(imageNormal.getSize() == imageDown.getSize());

setSize(fCurImage.getSize());
setSize(imageNormal.getSize());
}

ImageButton::ImageButton(Widget* widget, const Image& imageNormal, const Image& imageHover, const Image& imageDown) noexcept
@@ -164,14 +164,14 @@ ImageButton::ImageButton(Widget* widget, const Image& imageNormal, const Image&
fImageNormal(imageNormal),
fImageHover(imageHover),
fImageDown(imageDown),
fCurImage(fImageNormal),
fCurButton(-1),
fCurState(0),
fCallback(nullptr),
leakDetector_ImageButton()
{
DISTRHO_SAFE_ASSERT(fImageNormal.getSize() == fImageHover.getSize() && fImageHover.getSize() == fImageDown.getSize());
DISTRHO_SAFE_ASSERT(imageNormal.getSize() == imageHover.getSize() && imageHover.getSize() == imageDown.getSize());

setSize(fCurImage.getSize());
setSize(imageNormal.getSize());
}

void ImageButton::setCallback(Callback* callback) noexcept
@@ -181,50 +181,54 @@ void ImageButton::setCallback(Callback* callback) noexcept

void ImageButton::onDisplay()
{
fCurImage.draw();
switch (fCurState)
{
case 2:
fImageDown.draw();
break;
case 1:
fImageHover.draw();
break;
default:
fImageNormal.draw();
break;
}
}

bool ImageButton::onMouse(const MouseEvent& ev)
{
// button was released, handle it now
if (fCurButton != -1 && ! ev.press)
{
if (&fCurImage != &fImageNormal)
{
fCurImage = fImageNormal;
repaint();
}
DISTRHO_SAFE_ASSERT(fCurState == 2);

// release button
fCurButton = -1;

// cursor was moved outside the button bounds, ignore click
if (! contains(ev.pos))
{
fCurButton = -1;
return false;
fCurState = 0;
repaint();
return true;
}

// still on bounds, register click
fCurState = 1;
repaint();

if (fCallback != nullptr)
fCallback->imageButtonClicked(this, fCurButton);

#if 0
if (contains(ev.pos))
{
fCurImage = &fImageHover;
repaint();
}
#endif

fCurButton = -1;

return true;
}

// button was pressed, wait for release
if (ev.press && contains(ev.pos))
{
if (&fCurImage != &fImageDown)
{
fCurImage = fImageDown;
repaint();
}

fCurButton = ev.button;
fCurState = 2;
repaint();
return true;
}

@@ -233,29 +237,32 @@ bool ImageButton::onMouse(const MouseEvent& ev)

bool ImageButton::onMotion(const MotionEvent& ev)
{
// keep pressed
if (fCurButton != -1)
return true;

if (contains(ev.pos))
{
if (&fCurImage != &fImageHover)
// check if entering hover
if (fCurState == 0)
{
fCurImage = fImageHover;
fCurState = 1;
repaint();
return true;
}

return true;
}
else
{
if (&fCurImage != &fImageNormal)
// check if exiting hover
if (fCurState == 1)
{
fCurImage = fImageNormal;
fCurState = 0;
repaint();
return true;
}

return false;
}

return false;
}

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


+ 16
- 0
dgl/src/Window.cpp View File

@@ -355,8 +355,24 @@ struct Window::PrivateData {
fModal.enabled = false;

if (fModal.parent != nullptr)
{
fModal.parent->fModal.childFocus = nullptr;

// the mouse position probably changed since the modal appeared,
// so send a mouse motion event to the modal's parent window
#if defined(DISTRHO_OS_WINDOWS)
// TODO
#elif defined(DISTRHO_OS_MAC)
// TODO
#elif defined(DISTRHO_OS_LINUX)
int i, wx, wy;
uint u;
::Window w;
if (XQueryPointer(fModal.parent->xDisplay, fModal.parent->xWindow, &w, &w, &i, &i, &wx, &wy, &u) == True)
fModal.parent->onPuglMotion(wx, wy);
#endif
}

DBG("Ok\n");
}



Loading…
Cancel
Save