From 674c833c84ba24bf4ca45ee8943c4c143124a0fd Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 16 Feb 2016 12:59:49 +0000 Subject: [PATCH] Added method MouseEvent::mouseWasDraggedSinceMouseDown() --- .../Source/GraphEditorPanel.cpp | 20 ++++++------- .../paintelements/jucer_PaintElement.cpp | 2 +- .../paintelements/jucer_PaintElementPath.cpp | 2 +- .../ui/jucer_ComponentLayoutEditor.cpp | 2 +- .../ui/jucer_ComponentOverlayComponent.cpp | 2 +- .../ui/jucer_PaintRoutineEditor.cpp | 2 +- .../juce_gui_basics/mouse/juce_MouseEvent.cpp | 7 ++++- .../juce_gui_basics/mouse/juce_MouseEvent.h | 30 ++++++++++--------- .../juce_gui_basics/widgets/juce_ComboBox.cpp | 2 +- .../juce_gui_basics/widgets/juce_Label.cpp | 3 +- .../juce_gui_basics/widgets/juce_ListBox.cpp | 2 +- .../juce_gui_basics/widgets/juce_Slider.cpp | 4 +-- .../widgets/juce_TableHeaderComponent.cpp | 6 ++-- .../widgets/juce_TableListBox.cpp | 5 +++- .../widgets/juce_ToolbarItemComponent.cpp | 2 +- 15 files changed, 51 insertions(+), 40 deletions(-) diff --git a/examples/audio plugin host/Source/GraphEditorPanel.cpp b/examples/audio plugin host/Source/GraphEditorPanel.cpp index af70cebacc..099423a3c8 100644 --- a/examples/audio plugin host/Source/GraphEditorPanel.cpp +++ b/examples/audio plugin host/Source/GraphEditorPanel.cpp @@ -408,16 +408,16 @@ public: void mouseUp (const MouseEvent& e) override { - if (e.mouseWasClicked() && e.getNumberOfClicks() == 2) + if (e.mouseWasDraggedSinceMouseDown()) + { + graph.setChangedFlag (true); + } + else if (e.getNumberOfClicks() == 2) { if (const AudioProcessorGraph::Node::Ptr f = graph.getNodeForId (filterID)) if (PluginWindow* const w = PluginWindow::getWindowFor (f, PluginWindow::Normal)) w->toFront (true); } - else if (! e.mouseWasClicked()) - { - graph.setChangedFlag (true); - } } bool hitTest (int x, int y) override @@ -699,7 +699,11 @@ public: void mouseDrag (const MouseEvent& e) { - if ((! dragging) && ! e.mouseWasClicked()) + if (dragging) + { + getGraphPanel()->dragConnector (e); + } + else if (e.mouseWasDraggedSinceMouseDown()) { dragging = true; @@ -715,10 +719,6 @@ public: destFilterChannel, e); } - else if (dragging) - { - getGraphPanel()->dragConnector (e); - } } void mouseUp (const MouseEvent& e) diff --git a/extras/Introjucer/Source/ComponentEditor/paintelements/jucer_PaintElement.cpp b/extras/Introjucer/Source/ComponentEditor/paintelements/jucer_PaintElement.cpp index 9e55b77c62..844b6c0c76 100644 --- a/extras/Introjucer/Source/ComponentEditor/paintelements/jucer_PaintElement.cpp +++ b/extras/Introjucer/Source/ComponentEditor/paintelements/jucer_PaintElement.cpp @@ -296,7 +296,7 @@ void PaintElement::mouseDrag (const MouseEvent& e) if (selected && ! dragging) { - dragging = ! e.mouseWasClicked(); + dragging = e.mouseWasDraggedSinceMouseDown(); if (dragging) owner->startDragging (area); diff --git a/extras/Introjucer/Source/ComponentEditor/paintelements/jucer_PaintElementPath.cpp b/extras/Introjucer/Source/ComponentEditor/paintelements/jucer_PaintElementPath.cpp index 34b89aa647..75d08f1576 100644 --- a/extras/Introjucer/Source/ComponentEditor/paintelements/jucer_PaintElementPath.cpp +++ b/extras/Introjucer/Source/ComponentEditor/paintelements/jucer_PaintElementPath.cpp @@ -1565,7 +1565,7 @@ void PathPointComponent::mouseDrag (const MouseEvent& e) if (! e.mods.isPopupMenu()) { if (selected && ! dragging) - dragging = ! e.mouseWasClicked(); + dragging = e.mouseWasDraggedSinceMouseDown(); if (dragging) { diff --git a/extras/Introjucer/Source/ComponentEditor/ui/jucer_ComponentLayoutEditor.cpp b/extras/Introjucer/Source/ComponentEditor/ui/jucer_ComponentLayoutEditor.cpp index f08fb3448d..bbeb39f631 100644 --- a/extras/Introjucer/Source/ComponentEditor/ui/jucer_ComponentLayoutEditor.cpp +++ b/extras/Introjucer/Source/ComponentEditor/ui/jucer_ComponentLayoutEditor.cpp @@ -298,7 +298,7 @@ void ComponentLayoutEditor::mouseUp (const MouseEvent& e) lassoComp.endLasso(); removeChildComponent (&lassoComp); - if (e.mouseWasClicked() && ! e.mods.isAnyModifierKeyDown()) + if (! (e.mouseWasDraggedSinceMouseDown() || e.mods.isAnyModifierKeyDown())) layout.getSelectedSet().deselectAll(); } diff --git a/extras/Introjucer/Source/ComponentEditor/ui/jucer_ComponentOverlayComponent.cpp b/extras/Introjucer/Source/ComponentEditor/ui/jucer_ComponentOverlayComponent.cpp index 0ed781f618..423831bcec 100644 --- a/extras/Introjucer/Source/ComponentEditor/ui/jucer_ComponentOverlayComponent.cpp +++ b/extras/Introjucer/Source/ComponentEditor/ui/jucer_ComponentOverlayComponent.cpp @@ -113,7 +113,7 @@ void ComponentOverlayComponent::mouseDrag (const MouseEvent& e) { if (selected && ! dragging) { - dragging = ! e.mouseWasClicked(); + dragging = e.mouseWasDraggedSinceMouseDown(); if (dragging) layout.startDragging(); diff --git a/extras/Introjucer/Source/ComponentEditor/ui/jucer_PaintRoutineEditor.cpp b/extras/Introjucer/Source/ComponentEditor/ui/jucer_PaintRoutineEditor.cpp index c86b2978c5..ce965f5ab4 100644 --- a/extras/Introjucer/Source/ComponentEditor/ui/jucer_PaintRoutineEditor.cpp +++ b/extras/Introjucer/Source/ComponentEditor/ui/jucer_PaintRoutineEditor.cpp @@ -231,7 +231,7 @@ void PaintRoutineEditor::mouseUp (const MouseEvent& e) { lassoComp.endLasso(); - if (e.mouseWasClicked() && ! e.mods.isAnyModifierKeyDown()) + if (! (e.mouseWasDraggedSinceMouseDown() || e.mods.isAnyModifierKeyDown())) { graphics.getSelectedElements().deselectAll(); graphics.getSelectedPoints().deselectAll(); diff --git a/modules/juce_gui_basics/mouse/juce_MouseEvent.cpp b/modules/juce_gui_basics/mouse/juce_MouseEvent.cpp index bf5cb87a75..8d558cac82 100644 --- a/modules/juce_gui_basics/mouse/juce_MouseEvent.cpp +++ b/modules/juce_gui_basics/mouse/juce_MouseEvent.cpp @@ -79,9 +79,14 @@ MouseEvent MouseEvent::withNewPosition (Point newPosition) const noexcept } //============================================================================== +bool MouseEvent::mouseWasDraggedSinceMouseDown() const noexcept +{ + return wasMovedSinceMouseDown != 0; +} + bool MouseEvent::mouseWasClicked() const noexcept { - return wasMovedSinceMouseDown == 0; + return ! mouseWasDraggedSinceMouseDown(); } int MouseEvent::getLengthOfMousePress() const noexcept diff --git a/modules/juce_gui_basics/mouse/juce_MouseEvent.h b/modules/juce_gui_basics/mouse/juce_MouseEvent.h index 9d993e31da..b9b06bd819 100644 --- a/modules/juce_gui_basics/mouse/juce_MouseEvent.h +++ b/modules/juce_gui_basics/mouse/juce_MouseEvent.h @@ -156,19 +156,19 @@ public: //============================================================================== /** Returns the x coordinate of the last place that a mouse was pressed. The coordinate is relative to the component specified in MouseEvent::component. - @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasClicked + @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasDraggedSinceMouseDown */ int getMouseDownX() const noexcept; /** Returns the y coordinate of the last place that a mouse was pressed. The coordinate is relative to the component specified in MouseEvent::component. - @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasClicked + @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasDraggedSinceMouseDown */ int getMouseDownY() const noexcept; /** Returns the coordinates of the last place that a mouse was pressed. The coordinates are relative to the component specified in MouseEvent::component. - @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasClicked + @see getDistanceFromDragStart, getDistanceFromDragStartX, mouseWasDraggedSinceMouseDown */ Point getMouseDownPosition() const noexcept; @@ -203,25 +203,27 @@ public: */ Point getOffsetFromDragStart() const noexcept; - /** Returns true if the mouse has just been clicked. + /** Returns true if the user seems to be performing a drag gesture. - Used in either your mouseUp() or mouseDrag() methods, this will tell you whether - the user has dragged the mouse more than a few pixels from the place where the - mouse-down occurred. + This is only meaningful if called in either a mouseUp() or mouseDrag() method. - Once they have dragged it far enough for this method to return false, it will continue - to return false until the mouse-up, even if they move the mouse back to the same - position where they originally pressed it. This means that it's very handy for + It will return true if the user has dragged the mouse more than a few pixels + from the place where the mouse-down occurred. + + Once they have dragged it far enough for this method to return true, it will continue + to return true until the mouse-up, even if they move the mouse back to the same + location at which the mouse-down happened. This means that it's very handy for objects that can either be clicked on or dragged, as you can use it in the mouseDrag() - callback to ignore any small movements they might make while clicking. + callback to ignore small movements they might make while trying to click. + */ + bool mouseWasDraggedSinceMouseDown() const noexcept; - @returns true if the mouse wasn't dragged by more than a few pixels between - the last time the button was pressed and released. + /** Returns true if the mouse event is part of a click gesture rather than a drag. + This is effectively the opposite of mouseWasDraggedSinceMouseDown() */ bool mouseWasClicked() const noexcept; /** For a click event, the number of times the mouse was clicked in succession. - So for example a double-click event will return 2, a triple-click 3, etc. */ int getNumberOfClicks() const noexcept { return numberOfClicks; } diff --git a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp index 409969ee59..68e1c23890 100644 --- a/modules/juce_gui_basics/widgets/juce_ComboBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_ComboBox.cpp @@ -579,7 +579,7 @@ void ComboBox::mouseDrag (const MouseEvent& e) { beginDragAutoRepeat (50); - if (isButtonDown && ! e.mouseWasClicked()) + if (isButtonDown && e.mouseWasDraggedSinceMouseDown()) showPopupIfNotActive(); } diff --git a/modules/juce_gui_basics/widgets/juce_Label.cpp b/modules/juce_gui_basics/widgets/juce_Label.cpp index 2fb4b6d0a8..0393dda6ef 100644 --- a/modules/juce_gui_basics/widgets/juce_Label.cpp +++ b/modules/juce_gui_basics/widgets/juce_Label.cpp @@ -329,9 +329,8 @@ void Label::mouseUp (const MouseEvent& e) { if (editSingleClick && isEnabled() - && e.mouseWasClicked() && contains (e.getPosition()) - && ! e.mods.isPopupMenu()) + && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu())) { showEditor(); } diff --git a/modules/juce_gui_basics/widgets/juce_ListBox.cpp b/modules/juce_gui_basics/widgets/juce_ListBox.cpp index 91487420bd..6445402f7e 100644 --- a/modules/juce_gui_basics/widgets/juce_ListBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_ListBox.cpp @@ -104,7 +104,7 @@ public: { if (ListBoxModel* m = owner.getModel()) { - if (isEnabled() && ! (e.mouseWasClicked() || isDragging)) + if (isEnabled() && e.mouseWasDraggedSinceMouseDown() && ! isDragging) { SparseSet rowsToDrag; diff --git a/modules/juce_gui_basics/widgets/juce_Slider.cpp b/modules/juce_gui_basics/widgets/juce_Slider.cpp index 8214833ae3..75aaddc932 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.cpp +++ b/modules/juce_gui_basics/widgets/juce_Slider.cpp @@ -693,7 +693,7 @@ public: while (angle < 0.0) angle += double_Pi * 2.0; - if (rotaryParams.stopAtEnd && ! e.mouseWasClicked()) + if (rotaryParams.stopAtEnd && e.mouseWasDraggedSinceMouseDown()) { if (std::abs (angle - lastAngle) > double_Pi) { @@ -883,7 +883,7 @@ public: { if (style == IncDecButtons && ! incDecDragged) { - if (e.getDistanceFromDragStart() < 10 || e.mouseWasClicked()) + if (e.getDistanceFromDragStart() < 10 || ! e.mouseWasDraggedSinceMouseDown()) return; incDecDragged = true; diff --git a/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp b/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp index 96482c776d..13fee7e23d 100644 --- a/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp +++ b/modules/juce_gui_basics/widgets/juce_TableHeaderComponent.cpp @@ -590,7 +590,8 @@ void TableHeaderComponent::mouseDrag (const MouseEvent& e) { if (columnIdBeingResized == 0 && columnIdBeingDragged == 0 - && ! (e.mouseWasClicked() || e.mods.isPopupMenu())) + && e.mouseWasDraggedSinceMouseDown() + && ! e.mods.isPopupMenu()) { dragOverlayComp = nullptr; @@ -599,6 +600,7 @@ void TableHeaderComponent::mouseDrag (const MouseEvent& e) if (columnIdBeingResized != 0) { const ColumnInfo* const ci = getInfoForId (columnIdBeingResized); + jassert (ci != nullptr); initialColumnWidth = ci->width; } else @@ -767,7 +769,7 @@ void TableHeaderComponent::mouseUp (const MouseEvent& e) updateColumnUnderMouse (e); - if (columnIdUnderMouse != 0 && e.mouseWasClicked() && ! e.mods.isPopupMenu()) + if (columnIdUnderMouse != 0 && ! (e.mouseWasDraggedSinceMouseDown() || e.mods.isPopupMenu())) columnClicked (columnIdUnderMouse, e.mods); dragOverlayComp = nullptr; diff --git a/modules/juce_gui_basics/widgets/juce_TableListBox.cpp b/modules/juce_gui_basics/widgets/juce_TableListBox.cpp index cef3a62c4b..deb7785bba 100644 --- a/modules/juce_gui_basics/widgets/juce_TableListBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_TableListBox.cpp @@ -153,7 +153,10 @@ public: void mouseDrag (const MouseEvent& e) override { - if (isEnabled() && owner.getModel() != nullptr && ! (e.mouseWasClicked() || isDragging)) + if (isEnabled() + && owner.getModel() != nullptr + && e.mouseWasDraggedSinceMouseDown() + && ! isDragging) { SparseSet rowsToDrag; diff --git a/modules/juce_gui_basics/widgets/juce_ToolbarItemComponent.cpp b/modules/juce_gui_basics/widgets/juce_ToolbarItemComponent.cpp index 5e5ce3f2e1..c11cec139d 100644 --- a/modules/juce_gui_basics/widgets/juce_ToolbarItemComponent.cpp +++ b/modules/juce_gui_basics/widgets/juce_ToolbarItemComponent.cpp @@ -64,7 +64,7 @@ public: void mouseDrag (const MouseEvent& e) override { - if (! (isDragging || e.mouseWasClicked())) + if (e.mouseWasDraggedSinceMouseDown() && ! isDragging) { isDragging = true;