Răsfoiți Sursa

Refactored slider page event parameters to be simpler and fixed unintended scrolling while hovering without mouse button pressed.
Change CheckBox to react to click begin (as it was before) so that ListView hierarchy toggling doesn't break.

Lasse Öörni 12 ani în urmă
părinte
comite
e234e9e8b6

+ 2 - 2
Source/Engine/UI/CheckBox.cpp

@@ -66,9 +66,9 @@ void CheckBox::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexD
     BorderImage::GetBatches(batches, vertexData, currentScissor, offset);
 }
 
-void CheckBox::OnClickEnd(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor, UIElement* beginElement)
+void CheckBox::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor)
 {
-    if (button == MOUSEB_LEFT && beginElement == this)
+    if (button == MOUSEB_LEFT)
         SetChecked(!checked_);
 }
 

+ 2 - 2
Source/Engine/UI/CheckBox.h

@@ -42,8 +42,8 @@ public:
     
     /// Return UI rendering batches.
     virtual void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
-    /// React to mouse click end.
-    virtual void OnClickEnd(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor, UIElement* beginElement);
+    /// React to mouse click begin.
+    virtual void OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor);
     
     /// Set checked state.
     void SetChecked(bool enable);

+ 16 - 9
Source/Engine/UI/ScrollBar.cpp

@@ -281,30 +281,37 @@ void ScrollBar::HandleSliderPaged(StringHash eventType, VariantMap& eventData)
 {
     using namespace SliderPaged;
 
-    if (eventData[P_BUTTONS].GetInt() & MOUSEB_LEFT)
+    // Synthesize hover event to the forward/back buttons
+    if (eventData[P_OFFSET].GetInt() < 0)
+        backButton_->OnHover(IntVector2::ZERO, backButton_->ElementToScreen(IntVector2::ZERO), 0, 0, 0);
+    else
+        forwardButton_->OnHover(IntVector2::ZERO, forwardButton_->ElementToScreen(IntVector2::ZERO), 0, 0, 0);
+
+    // Synthesize click / release events to the buttons
+    if (eventData[P_PRESSED].GetBool())
     {
         if (eventData[P_OFFSET].GetInt() < 0)
         {
-            backButton_->OnClickBegin(IntVector2::ZERO, backButton_->ElementToScreen(IntVector2::ZERO),
-                eventData[P_BUTTON].GetInt(), eventData[P_BUTTONS].GetInt(), eventData[P_QUALIFIERS].GetInt(), 0);
+            backButton_->OnClickBegin(IntVector2::ZERO, backButton_->ElementToScreen(IntVector2::ZERO), MOUSEB_LEFT, MOUSEB_LEFT,
+                0, 0);
         }
         else
         {
-            forwardButton_->OnClickBegin(IntVector2::ZERO, backButton_->ElementToScreen(IntVector2::ZERO),
-                eventData[P_BUTTON].GetInt(), eventData[P_BUTTONS].GetInt(), eventData[P_QUALIFIERS].GetInt(), 0);
+            forwardButton_->OnClickBegin(IntVector2::ZERO, forwardButton_->ElementToScreen(IntVector2::ZERO), MOUSEB_LEFT,
+                MOUSEB_LEFT, 0, 0);
         }
     }
     else
     {
         if (eventData[P_OFFSET].GetInt() < 0)
         {
-            backButton_->OnHover(IntVector2::ZERO, backButton_->ElementToScreen(IntVector2::ZERO), eventData[P_BUTTONS].GetInt(),
-                eventData[P_QUALIFIERS].GetInt(), 0);
+            backButton_->OnClickEnd(IntVector2::ZERO, backButton_->ElementToScreen(IntVector2::ZERO), MOUSEB_LEFT, 0, 0, 0,
+                backButton_);
         }
         else
         {
-            forwardButton_->OnHover(IntVector2::ZERO, backButton_->ElementToScreen(IntVector2::ZERO),
-                eventData[P_BUTTONS].GetInt(), eventData[P_QUALIFIERS].GetInt(), 0);
+            forwardButton_->OnClickEnd(IntVector2::ZERO, forwardButton_->ElementToScreen(IntVector2::ZERO), MOUSEB_LEFT, 0, 0, 0,
+                forwardButton_);
         }
     }
 }

+ 19 - 25
Source/Engine/UI/Slider.cpp

@@ -96,15 +96,22 @@ void Slider::OnHover(const IntVector2& position, const IntVector2& screenPositio
 
     // If not hovering on the knob, send it as page event
     if (!hovering_)
-        Page(position, 0, buttons, qualifiers);
+        Page(position, buttons & MOUSEB_LEFT);
 }
 
 void Slider::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor)
 {
     selected_ = true;
     hovering_ = knob_->IsInside(screenPosition, true);
-    if (!hovering_)
-        Page(position, button, buttons, qualifiers);
+    if (!hovering_ && button == MOUSEB_LEFT)
+        Page(position, true);
+}
+
+void Slider::OnClickEnd(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor, UIElement* beginElement)
+{
+    hovering_ = knob_->IsInside(screenPosition, true);
+    if (!hovering_ && button == MOUSEB_LEFT)
+        Page(position, false);
 }
 
 void Slider::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
@@ -238,7 +245,7 @@ void Slider::UpdateSlider()
     }
 }
 
-void Slider::Page(const IntVector2& position, int button, int buttons, int qualifiers)
+void Slider::Page(const IntVector2& position, bool pressed)
 {
     IntVector2 offsetXY = position - knob_->GetPosition() - knob_->GetSize() / 2;
     int offset = orientation_ == O_HORIZONTAL ? offsetXY.x_ : offsetXY.y_;
@@ -246,31 +253,18 @@ void Slider::Page(const IntVector2& position, int button, int buttons, int quali
 
     using namespace SliderPaged;
     
-    // If button is 0 (passed from hover event) assume it's the lowest pressed bit in buttons
-    if (!button && buttons)
-    {
-        if (buttons & MOUSEB_LEFT)
-            button = MOUSEB_LEFT;
-        else if (buttons & MOUSEB_MIDDLE)
-            button = MOUSEB_MIDDLE;
-        else if (buttons & MOUSEB_RIGHT)
-            button = MOUSEB_RIGHT;
-    }
-    
     VariantMap eventData;
     eventData[P_ELEMENT] = (void*)this;
     eventData[P_OFFSET] = offset;
-    // Only generate the 'click' variant of the event when the slider is selected
-    // i.e. when it has received the first initial click.
-    if (selected_ && repeatRate_ > 0.0f && repeatTimer_.GetMSec(false) >= Lerp(1000.0f / repeatRate_, 0, Abs(offset) / length))
-    {
+
+    // Start transmitting repeated pages after the initial press
+    if (selected_ && pressed && repeatRate_ > 0.0f && repeatTimer_.GetMSec(false) >= Lerp(1000.0f / repeatRate_, 0, Abs(offset) / length))
         repeatTimer_.Reset();
-        eventData[P_BUTTON] = button;
-        eventData[P_BUTTONS] = buttons;
-        eventData[P_QUALIFIERS] = qualifiers;
-    }
-    // When without buttons & qualifiers parameters, the receiver should interpret
-    // this event as just mouse hovering on slider's 'paging' area instead
+    else
+        pressed = false;
+
+    eventData[P_PRESSED] = pressed;
+
     SendEvent(E_SLIDERPAGED, eventData);
 }
 

+ 3 - 1
Source/Engine/UI/Slider.h

@@ -46,6 +46,8 @@ public:
     virtual void OnHover(const IntVector2& position, const IntVector2& screenPosition,  int buttons, int qualifiers, Cursor* cursor);
     /// React to mouse click begin.
     virtual void OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor);
+    /// React to mouse click end.
+    virtual void OnClickEnd(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor, UIElement* beginElement);
     /// React to mouse drag begin.
     virtual void OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
     /// React to mouse drag motion.
@@ -83,7 +85,7 @@ protected:
     /// Update slider knob position & size.
     void UpdateSlider();
     /// Send slider page event.
-    void Page(const IntVector2& position, int button, int buttons, int qualifiers);
+    void Page(const IntVector2& position, bool pressed);
 
     /// Slider knob.
     SharedPtr<BorderImage> knob_;

+ 1 - 3
Source/Engine/UI/UIEvents.h

@@ -162,9 +162,7 @@ EVENT(E_SLIDERPAGED, SliderPaged)
 {
     PARAM(P_ELEMENT, Element);              // UIElement pointer
     PARAM(P_OFFSET, Offset);                // int
-    PARAM(P_BUTTON, Button);                // int
-    PARAM(P_BUTTONS, Buttons);              // int
-    PARAM(P_QUALIFIERS, Qualifiers);        // int
+    PARAM(P_PRESSED, Pressed);              // bool
 }
 
 /// UI scrollbar value changed.