Skip to content

Instantly share code, notes, and snippets.

@ffAudio
Last active August 13, 2019 14:09
Show Gist options
  • Save ffAudio/602abfbaa422156dcd6fb808c997914c to your computer and use it in GitHub Desktop.
Save ffAudio/602abfbaa422156dcd6fb808c997914c to your computer and use it in GitHub Desktop.
/*
==============================================================================
MouseOverChecker.h
Created: 13 Aug 2019 1:26:42pm
Author: Daniel Walz
==============================================================================
*/
#pragma once
class MouseOverChecker : public MouseListener
{
public:
MouseOverChecker (Component& componentToWatch) : watchedComponent (componentToWatch)
{
watchedComponent.addMouseListener (this, true);
}
~MouseOverChecker()
{
watchedComponent.removeMouseListener (this);
}
void mouseEnter (const MouseEvent&) override
{
if (mouseIsOver == false)
{
mouseIsOver = true;
if (onMouseEnter != nullptr)
onMouseEnter();
}
}
void mouseExit (const MouseEvent&) override
{
if (mouseIsOver == true)
{
mouseIsOver = false;
if (onMouseExit != nullptr)
onMouseExit();
}
}
std::function<void()> onMouseEnter;
std::function<void()> onMouseExit;
private:
Component& watchedComponent;
bool mouseIsOver = false;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseOverChecker);
};
// example usage:
/*
// members:
bool highlight = false;
MouseOverChecker overChecker {*this};
// in Constructor after all "addAndMakeVisible()" calls:
overChecker.onMouseEnter = [&]
{
highlight = true;
Timer::callAfterDelay (50, [this] { repaint(); });
};
overChecker.onMouseExit = [&]
{
highlight = false;
// the delay allows the mouseEnter of the other child to occur
Timer::callAfterDelay (50, [this] { repaint(); });
};
// and use the "highlight" flag in your "paint()"
*/
@ffAudio
Copy link
Author

ffAudio commented Aug 13, 2019

Ah well, there are different views about that. It is a header, and I usually have the #include "JuceHeader.h" at the beginning of each cpp file. I aim for no includes in headers, but there are different opinions on that, I know...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment