diff --git a/modules/juce_gui_basics/components/juce_Desktop.cpp b/modules/juce_gui_basics/components/juce_Desktop.cpp index b8058c8bad..602f94334e 100644 --- a/modules/juce_gui_basics/components/juce_Desktop.cpp +++ b/modules/juce_gui_basics/components/juce_Desktop.cpp @@ -28,7 +28,7 @@ Desktop::Desktop() kioskModeComponent (nullptr), allowedOrientations (allOrientations) { - createMouseInputSources(); + addMouseInputSource(); } Desktop::~Desktop() diff --git a/modules/juce_gui_basics/components/juce_Desktop.h b/modules/juce_gui_basics/components/juce_Desktop.h index 266ccc4ea2..22503e3c7e 100644 --- a/modules/juce_gui_basics/components/juce_Desktop.h +++ b/modules/juce_gui_basics/components/juce_Desktop.h @@ -235,7 +235,8 @@ public: //============================================================================== /** Returns the number of MouseInputSource objects the system has at its disposal. In a traditional single-mouse system, there might be only one object. On a multi-touch - system, there could be one input source per potential finger. + system, there could be one input source per potential finger. The number of mouse + sources returned here may increase dynamically as the program runs. To find out how many mouse events are currently happening, use getNumDraggingMouseSources(). @see getMouseSource */ @@ -383,7 +384,7 @@ private: friend class TopLevelWindowManager; OwnedArray mouseSources; - void createMouseInputSources(); + bool addMouseInputSource(); ListenerList mouseListeners; ListenerList focusListeners; diff --git a/modules/juce_gui_basics/native/juce_android_Windowing.cpp b/modules/juce_gui_basics/native/juce_android_Windowing.cpp index aed6291607..124c6cb4d4 100644 --- a/modules/juce_gui_basics/native/juce_android_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_android_Windowing.cpp @@ -600,12 +600,10 @@ Desktop::DisplayOrientation Desktop::getCurrentOrientation() const return upright; } -void Desktop::createMouseInputSources() +bool Desktop::addMouseInputSource() { - // This creates a mouse input source for each possible finger - - for (int i = 0; i < 10; ++i) - mouseSources.add (new MouseInputSource (i, false)); + mouseSources.add (new MouseInputSource (mouseSources.size(), false)); + return true; } Point MouseInputSource::getCurrentMousePosition() diff --git a/modules/juce_gui_basics/native/juce_ios_Windowing.mm b/modules/juce_gui_basics/native/juce_ios_Windowing.mm index db82d002a4..8ef80c319e 100644 --- a/modules/juce_gui_basics/native/juce_ios_Windowing.mm +++ b/modules/juce_gui_basics/native/juce_ios_Windowing.mm @@ -273,10 +273,10 @@ String SystemClipboard::getTextFromClipboard() } //============================================================================== -void Desktop::createMouseInputSources() +bool Desktop::addMouseInputSource() { - for (int i = 0; i < 10; ++i) - mouseSources.add (new MouseInputSource (i, false)); + mouseSources.add (new MouseInputSource (mouseSources.size(), false)); + return true; } bool Desktop::canUseSemiTransparentWindows() noexcept diff --git a/modules/juce_gui_basics/native/juce_linux_Windowing.cpp b/modules/juce_gui_basics/native/juce_linux_Windowing.cpp index 88ee129e73..4cddf5af71 100644 --- a/modules/juce_gui_basics/native/juce_linux_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_linux_Windowing.cpp @@ -3086,9 +3086,15 @@ void Desktop::Displays::findDisplays() } //============================================================================== -void Desktop::createMouseInputSources() +bool Desktop::addMouseInputSource() { - mouseSources.add (new MouseInputSource (0, true)); + if (mouseSources.size() == 0) + { + mouseSources.add (new MouseInputSource (0, true)); + return true; + } + + return false; } bool Desktop::canUseSemiTransparentWindows() noexcept diff --git a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm index e2476340ee..95d57f4fa7 100644 --- a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm +++ b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm @@ -1811,9 +1811,15 @@ void ModifierKeys::updateCurrentModifiers() noexcept //============================================================================== -void Desktop::createMouseInputSources() +bool Desktop::addMouseInputSource() { - mouseSources.add (new MouseInputSource (0, true)); + if (mouseSources.size() == 0) + { + mouseSources.add (new MouseInputSource (0, true)); + return true; + } + + return false; } //============================================================================== diff --git a/modules/juce_gui_basics/native/juce_win32_Windowing.cpp b/modules/juce_gui_basics/native/juce_win32_Windowing.cpp index c3f0fc6615..709ceffaa0 100644 --- a/modules/juce_gui_basics/native/juce_win32_Windowing.cpp +++ b/modules/juce_gui_basics/native/juce_win32_Windowing.cpp @@ -2915,13 +2915,17 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconTy } //============================================================================== -void Desktop::createMouseInputSources() +bool Desktop::addMouseInputSource() { - mouseSources.add (new MouseInputSource (0, true)); + const int numSources = mouseSources.size(); - if (canUseMultiTouch()) - for (int i = 1; i <= 10; ++i) - mouseSources.add (new MouseInputSource (i, false)); + if (numSources == 0 || canUseMultiTouch()) + { + mouseSources.add (new MouseInputSource (numSources, numSources == 0)); + return true; + } + + return false; } Point MouseInputSource::getCurrentMousePosition() diff --git a/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp b/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp index 24a06ffbd0..580d821bf9 100644 --- a/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp +++ b/modules/juce_gui_basics/windows/juce_ComponentPeer.cpp @@ -80,22 +80,37 @@ void ComponentPeer::updateCurrentModifiers() noexcept } //============================================================================== +MouseInputSource* ComponentPeer::getOrCreateMouseInputSource (int touchIndex) +{ + jassert (touchIndex >= 0 && touchIndex < 100); // sanity-check on number of fingers + + Desktop& desktop = Desktop::getInstance(); + + for (;;) + { + if (MouseInputSource* mouse = desktop.getMouseSource (touchIndex)) + return mouse; + + if (! desktop.addMouseInputSource()) + { + jassertfalse; // not enough mouse sources! + return nullptr; + } + } +} + void ComponentPeer::handleMouseEvent (const int touchIndex, const Point& positionWithinPeer, const ModifierKeys& newMods, const int64 time) { - MouseInputSource* const mouse = Desktop::getInstance().getMouseSource (touchIndex); - jassert (mouse != nullptr); // not enough sources! - - mouse->handleEvent (this, positionWithinPeer, time, newMods); + if (MouseInputSource* mouse = getOrCreateMouseInputSource (touchIndex)) + mouse->handleEvent (this, positionWithinPeer, time, newMods); } void ComponentPeer::handleMouseWheel (const int touchIndex, const Point& positionWithinPeer, const int64 time, const MouseWheelDetails& wheel) { - MouseInputSource* const mouse = Desktop::getInstance().getMouseSource (touchIndex); - jassert (mouse != nullptr); // not enough sources! - - mouse->handleWheel (this, positionWithinPeer, time, wheel); + if (MouseInputSource* mouse = getOrCreateMouseInputSource (touchIndex)) + mouse->handleWheel (this, positionWithinPeer, time, wheel); } //============================================================================== diff --git a/modules/juce_gui_basics/windows/juce_ComponentPeer.h b/modules/juce_gui_basics/windows/juce_ComponentPeer.h index 04c07cecf2..98227240dd 100644 --- a/modules/juce_gui_basics/windows/juce_ComponentPeer.h +++ b/modules/juce_gui_basics/windows/juce_ComponentPeer.h @@ -384,6 +384,7 @@ private: const uint32 uniqueID; bool fakeMouseMessageSent, isWindowMinimised; Component* getTargetForKeyPress(); + static MouseInputSource* getOrCreateMouseInputSource (int); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentPeer) };