tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_MouseHoverDetector.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
15{
16public:
17 MouseHoverDetector (int hoverTimeMillisecs_ = 400)
18 : hoverTimeMillisecs (hoverTimeMillisecs_)
19 {
20 internalTimer.owner = this;
21 }
22
23 virtual ~MouseHoverDetector()
24 {
25 setHoverComponent (nullptr, false);
26 }
27
28 void setHoverTimeMillisecs (int newTimeInMillisecs)
29 {
30 hoverTimeMillisecs = newTimeInMillisecs;
31 }
32
33 void setHoverComponent (juce::Component* newSourceComponent,
34 bool wantsEventsForAllNestedChildComponents)
35 {
36 if (source != newSourceComponent)
37 {
38 internalTimer.stopTimer();
39 hasJustHovered = false;
40 wantsHoversForAllNestedChildComponents = wantsEventsForAllNestedChildComponents;
41
42 if (source != nullptr)
43 source->removeMouseListener (&internalTimer);
44
45 source = newSourceComponent;
46
47 if (newSourceComponent != nullptr)
48 newSourceComponent->addMouseListener (&internalTimer, wantsHoversForAllNestedChildComponents);
49 }
50 }
51
52 void hoverTimerCallback()
53 {
54 internalTimer.stopTimer();
55
56 if (source != nullptr)
57 {
58 auto pos = source->getMouseXYRelative();
59
60 if (source->reallyContains (pos, wantsHoversForAllNestedChildComponents))
61 {
62 hasJustHovered = true;
63 mouseHovered (pos.getX(), pos.getY());
64 }
65 }
66 }
67
68 void checkJustHoveredCallback()
69 {
70 if (hasJustHovered)
71 {
72 hasJustHovered = false;
73 mouseMovedAfterHover();
74 }
75 }
76
77protected:
78 virtual void mouseHovered (int mouseX, int mouseY) = 0;
79 virtual void mouseMovedAfterHover() = 0;
80
81private:
82 //==============================================================================
83 class HoverDetectorInternal : public juce::MouseListener,
84 public juce::Timer
85 {
86 public:
87 HoverDetectorInternal() = default;
88
89 MouseHoverDetector* owner = nullptr;
90 juce::Point<int> lastPos;
91
92 void timerCallback() override
93 {
94 owner->hoverTimerCallback();
95 }
96
97 void mouseEnter (const juce::MouseEvent&) override
98 {
99 stopTimer();
100 owner->checkJustHoveredCallback();
101 }
102
103 void mouseExit (const juce::MouseEvent&) override
104 {
105 stopTimer();
106 owner->checkJustHoveredCallback();
107 }
108
109 void mouseDown (const juce::MouseEvent&) override
110 {
111 stopTimer();
112 owner->checkJustHoveredCallback();
113 }
114
115 void mouseUp (const juce::MouseEvent&) override
116 {
117 stopTimer();
118 owner->checkJustHoveredCallback();
119 }
120
121 void mouseMove (const juce::MouseEvent& e) override
122 {
123 if (lastPos != e.getPosition()) // to avoid fake mouse-moves setting it off
124 {
125 lastPos = e.getPosition();
126
127 if (owner->source != nullptr)
128 startTimer (owner->hoverTimeMillisecs);
129
130 owner->checkJustHoveredCallback();
131 }
132 }
133
134 void mouseWheelMove (const juce::MouseEvent&, const juce::MouseWheelDetails&) override
135 {
136 stopTimer();
137 owner->checkJustHoveredCallback();
138 }
139
141 };
142
143 HoverDetectorInternal internalTimer;
144 juce::Component* source = nullptr;
145 int hoverTimeMillisecs;
146 bool hasJustHovered = false;
147 bool wantsHoversForAllNestedChildComponents = false;
148
150};
151
152}} // namespace tracktion { inline namespace engine
bool reallyContains(Point< int > localPoint, bool returnTrueIfWithinAChild)
void addMouseListener(MouseListener *newListener, bool wantsEventsForAllNestedChildComponents)
Point< int > getMouseXYRelative() const
void removeMouseListener(MouseListener *listenerToRemove)
Point< int > getPosition() const noexcept
void stopTimer() noexcept
void startTimer(int intervalInMilliseconds) noexcept
#define JUCE_DECLARE_NON_COPYABLE(className)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)