Преглед на файлове

Clean up + included a by-pass in UI.cpp to allow for drag move and drag end to occur when Input::IsMouseGrabbed() is true (allowing existing events to continue, preventing new events from triggering).

hdunderscore преди 11 години
родител
ревизия
9fabafec87

+ 2 - 0
Source/Engine/LuaScript/pkgs/Math/MathDefs.pkg

@@ -52,6 +52,8 @@ bool IsPowerOfTwo(unsigned value);
 
 
 unsigned NextPowerOfTwo(unsigned value);
 unsigned NextPowerOfTwo(unsigned value);
 
 
+unsigned CountSetBits(unsigned value);
+
 unsigned SDBMHash(unsigned hash, unsigned char c);
 unsigned SDBMHash(unsigned hash, unsigned char c);
 float Random();
 float Random();
 float Random(float range);
 float Random(float range);

+ 0 - 1
Source/Engine/LuaScript/pkgs/UI/UI.pkg

@@ -36,7 +36,6 @@ class UI : public Object
 
 
     UIElement* GetFocusElement() const;
     UIElement* GetFocusElement() const;
     UIElement* GetFrontElement() const;
     UIElement* GetFrontElement() const;
-    unsigned GetNumDragElements() const;
     UIElement* GetDragElement(unsigned index);
     UIElement* GetDragElement(unsigned index);
     const String GetClipboardText() const;
     const String GetClipboardText() const;
     float GetDoubleClickInterval() const;
     float GetDoubleClickInterval() const;

+ 10 - 0
Source/Engine/Math/MathDefs.h

@@ -141,6 +141,16 @@ inline unsigned NextPowerOfTwo(unsigned value)
     return ret;
     return ret;
 }
 }
 
 
+/// Count the number of set bits in a mask.
+inline unsigned CountSetBits(unsigned value)
+{
+    // Brian Kernighan's method
+    unsigned count = 0;
+    for (count = 0; value; count++)
+        value &= value - 1;
+    return count;
+}
+
 /// Update a hash with the given 8-bit value using the SDBM algorithm.
 /// Update a hash with the given 8-bit value using the SDBM algorithm.
 inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
 inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
 /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
 /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)

+ 0 - 1
Source/Engine/Script/InputAPI.cpp

@@ -23,7 +23,6 @@
 #include "Precompiled.h"
 #include "Precompiled.h"
 #include "APITemplates.h"
 #include "APITemplates.h"
 #include "Input.h"
 #include "Input.h"
-#include "Cursor.h"
 
 
 namespace Urho3D
 namespace Urho3D
 {
 {

+ 1 - 0
Source/Engine/Script/MathAPI.cpp

@@ -79,6 +79,7 @@ static void RegisterMathFunctions(asIScriptEngine* engine)
     engine->RegisterGlobalFunction("float Ceil(float)", asFUNCTION(ceilf), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Ceil(float)", asFUNCTION(ceilf), asCALL_CDECL);
     engine->RegisterGlobalFunction("bool IsPowerOfTwo(uint)", asFUNCTION(IsPowerOfTwo), asCALL_CDECL);
     engine->RegisterGlobalFunction("bool IsPowerOfTwo(uint)", asFUNCTION(IsPowerOfTwo), asCALL_CDECL);
     engine->RegisterGlobalFunction("uint NextPowerOfTwo(uint)", asFUNCTION(NextPowerOfTwo), asCALL_CDECL);
     engine->RegisterGlobalFunction("uint NextPowerOfTwo(uint)", asFUNCTION(NextPowerOfTwo), asCALL_CDECL);
+    engine->RegisterGlobalFunction("uint CountSetBits(uint)", asFUNCTION(CountSetBits), asCALL_CDECL);
     engine->RegisterGlobalFunction("uint SDBMHash(uint, uint8)", asFUNCTION(SDBMHash), asCALL_CDECL);
     engine->RegisterGlobalFunction("uint SDBMHash(uint, uint8)", asFUNCTION(SDBMHash), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Random()", asFUNCTIONPR(Random, (), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Random()", asFUNCTIONPR(Random, (), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Random(float)", asFUNCTIONPR(Random, (float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Random(float)", asFUNCTIONPR(Random, (float), float), asCALL_CDECL);

+ 0 - 1
Source/Engine/Script/UIAPI.cpp

@@ -719,7 +719,6 @@ static void RegisterUI(asIScriptEngine* engine)
     engine->RegisterObjectMethod("UI", "bool get_useMutableGlyphs() const", asMETHOD(UI, GetUseMutableGlyphs), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "bool get_useMutableGlyphs() const", asMETHOD(UI, GetUseMutableGlyphs), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "void set_forceAutoHint(bool)", asMETHOD(UI, SetForceAutoHint), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "void set_forceAutoHint(bool)", asMETHOD(UI, SetForceAutoHint), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "bool get_forceAutoHint() const", asMETHOD(UI, GetForceAutoHint), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "bool get_forceAutoHint() const", asMETHOD(UI, GetForceAutoHint), asCALL_THISCALL);
-    engine->RegisterObjectMethod("UI", "uint get_numDragButtons(int buttons) const", asMETHOD(UI, GetNumDragButtons), asCALL_THISCALL);
     engine->RegisterGlobalFunction("UI@+ get_ui()", asFUNCTION(GetUI), asCALL_CDECL);
     engine->RegisterGlobalFunction("UI@+ get_ui()", asFUNCTION(GetUI), asCALL_CDECL);
 }
 }
 
 

+ 21 - 27
Source/Engine/UI/UI.cpp

@@ -59,6 +59,8 @@
 
 
 #include "DebugNew.h"
 #include "DebugNew.h"
 
 
+#define TOUCHID_MASK(id) (1 << id)
+
 namespace Urho3D
 namespace Urho3D
 {
 {
 
 
@@ -302,12 +304,14 @@ void UI::Update(float timeStep)
         i->second_ = false;
         i->second_ = false;
 
 
     Input* input = GetSubsystem<Input>();
     Input* input = GetSubsystem<Input>();
+    bool mouseGrabbed = input->IsMouseGrabbed();
+
     IntVector2 cursorPos;
     IntVector2 cursorPos;
     bool cursorVisible;
     bool cursorVisible;
     GetCursorPositionAndVisible(cursorPos, cursorVisible);
     GetCursorPositionAndVisible(cursorPos, cursorVisible);
 
 
     // Drag begin based on time
     // Drag begin based on time
-    if (dragElementsCount_ > 0)
+    if (dragElementsCount_ > 0 && !mouseGrabbed)
     {
     {
         for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End(); )
         for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End(); )
         {
         {
@@ -344,7 +348,7 @@ void UI::Update(float timeStep)
     }
     }
 
 
     // Mouse hover
     // Mouse hover
-    if (!input->IsMouseGrabbed() && !input->GetTouchEmulation())
+    if (!mouseGrabbed && !input->GetTouchEmulation())
     {
     {
         if (!usingTouchInput_ && cursorVisible)
         if (!usingTouchInput_ && cursorVisible)
             ProcessHover(cursorPos, mouseButtons_, qualifiers_, cursor_);
             ProcessHover(cursorPos, mouseButtons_, qualifiers_, cursor_);
@@ -648,7 +652,7 @@ UIElement* UI::GetDragElement(unsigned index)
     GetDragElements();
     GetDragElements();
     if (index >= dragElementsConfirmed_.Size())
     if (index >= dragElementsConfirmed_.Size())
         return (UIElement*)0;
         return (UIElement*)0;
-        
+
     return dragElementsConfirmed_[index];
     return dragElementsConfirmed_[index];
 }
 }
 
 
@@ -670,16 +674,6 @@ bool UI::HasModalElement() const
     return rootModalElement_->GetNumChildren() > 0;
     return rootModalElement_->GetNumChildren() > 0;
 }
 }
 
 
-unsigned UI::GetNumDragButtons(unsigned buttons)
-{
-    // Counting set bits in mask
-    // Brian Kernighan's method
-    unsigned count = 0;
-    for (count = 0; buttons; count++)
-        buttons &= buttons - 1;
-    return count;
-}
-
 void UI::Initialize()
 void UI::Initialize()
 {
 {
     Graphics* graphics = GetSubsystem<Graphics>();
     Graphics* graphics = GetSubsystem<Graphics>();
@@ -1125,7 +1119,7 @@ void UI::ProcessClickBegin(const IntVector2& cursorPos, int button, int buttons,
                 dragData->dragBeginSumPos = cursorPos;
                 dragData->dragBeginSumPos = cursorPos;
                 dragData->dragBeginTimer.Reset();
                 dragData->dragBeginTimer.Reset();
                 dragData->dragButtons = button;
                 dragData->dragButtons = button;
-                dragData->numDragButtons = GetNumDragButtons(dragData->dragButtons);
+                dragData->numDragButtons = CountSetBits(dragData->dragButtons);
                 dragElementsCount_++;
                 dragElementsCount_++;
 
 
                 dragElementsContain = dragElements_.Contains(element);
                 dragElementsContain = dragElements_.Contains(element);
@@ -1136,7 +1130,7 @@ void UI::ProcessClickBegin(const IntVector2& cursorPos, int button, int buttons,
                 dragData->sumPos += cursorPos;
                 dragData->sumPos += cursorPos;
                 dragData->dragBeginSumPos += cursorPos;
                 dragData->dragBeginSumPos += cursorPos;
                 dragData->dragButtons |= button;
                 dragData->dragButtons |= button;
-                dragData->numDragButtons = GetNumDragButtons(dragData->dragButtons);
+                dragData->numDragButtons = CountSetBits(dragData->dragButtons);
             }
             }
         }
         }
         else
         else
@@ -1218,8 +1212,9 @@ void UI::ProcessClickEnd(const IntVector2& cursorPos, int button, int buttons, i
 void UI::ProcessMove(const IntVector2& cursorPos, const IntVector2& cursorDeltaPos, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible)
 void UI::ProcessMove(const IntVector2& cursorPos, const IntVector2& cursorDeltaPos, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible)
 {
 {
     if (cursorVisible && dragElementsCount_ > 0 && buttons)
     if (cursorVisible && dragElementsCount_ > 0 && buttons)
-    if (dragElementsCount_ > 0 && buttons)
     {
     {
+        Input* input = GetSubsystem<Input>();
+        bool mouseGrabbed = input->IsMouseGrabbed();
         for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End();)
         for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End();)
         {
         {
             WeakPtr<UIElement> dragElement = i->first_;
             WeakPtr<UIElement> dragElement = i->first_;
@@ -1254,7 +1249,8 @@ void UI::ProcessMove(const IntVector2& cursorPos, const IntVector2& cursorDeltaP
             if (dragElement->IsEnabled() && dragElement->IsVisible())
             if (dragElement->IsEnabled() && dragElement->IsVisible())
             {
             {
                 // Signal drag begin if distance threshold was exceeded
                 // Signal drag begin if distance threshold was exceeded
-                if (dragData->dragBeginPending)
+
+                if (dragData->dragBeginPending && !mouseGrabbed)
                 {
                 {
                     IntVector2 beginSendPos;
                     IntVector2 beginSendPos;
                     beginSendPos.x_ = dragData->dragBeginSumPos.x_ / dragData->numDragButtons;
                     beginSendPos.x_ = dragData->dragBeginSumPos.x_ / dragData->numDragButtons;
@@ -1351,11 +1347,6 @@ void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
 
 
 void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
 void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
 {
 {
-    Input* input = GetSubsystem<Input>();
-    bool mouseGrabbed = input->IsMouseGrabbed();
-    if (dragElementsCount_ == 0 && mouseGrabbed)
-        return;
-
     using namespace MouseButtonDown;
     using namespace MouseButtonDown;
 
 
     mouseButtons_ = eventData[P_BUTTONS].GetInt();
     mouseButtons_ = eventData[P_BUTTONS].GetInt();
@@ -1369,16 +1360,14 @@ void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
     // Handle drag cancelling
     // Handle drag cancelling
     ProcessDragCancel();
     ProcessDragCancel();
 
 
-    if (!mouseGrabbed)
+    Input* input = GetSubsystem<Input>();
+
+    if (!input->IsMouseGrabbed())
         ProcessClickBegin(cursorPos, eventData[P_BUTTON].GetInt(), mouseButtons_, qualifiers_, cursor_, cursorVisible);
         ProcessClickBegin(cursorPos, eventData[P_BUTTON].GetInt(), mouseButtons_, qualifiers_, cursor_, cursorVisible);
 }
 }
 
 
 void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
 void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
 {
 {
-    Input* input = GetSubsystem<Input>();
-    if (input->IsMouseGrabbed())
-        return;
-
     using namespace MouseButtonUp;
     using namespace MouseButtonUp;
 
 
     mouseButtons_ = eventData[P_BUTTONS].GetInt();
     mouseButtons_ = eventData[P_BUTTONS].GetInt();
@@ -1483,6 +1472,10 @@ void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
 
 
 void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
 void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
 {
 {
+    Input* input = GetSubsystem<Input>();
+    if (input->IsMouseGrabbed())
+        return;
+
     using namespace TouchBegin;
     using namespace TouchBegin;
 
 
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
     IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
@@ -1490,6 +1483,7 @@ void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
 
 
     int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
     int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
     WeakPtr<UIElement> element(GetElementAt(pos));
     WeakPtr<UIElement> element(GetElementAt(pos));
+
     if (element)
     if (element)
     {
     {
         ProcessClickBegin(pos, touchId, touchDragElements_[element], 0, 0, true);
         ProcessClickBegin(pos, touchId, touchDragElements_[element], 0, 0, true);

+ 0 - 6
Source/Engine/UI/UI.h

@@ -144,8 +144,6 @@ public:
     bool HasModalElement() const;
     bool HasModalElement() const;
     /// Return whether a drag is in progress.
     /// Return whether a drag is in progress.
     bool IsDragging() const { return dragConfirmedCount_ > 0; };
     bool IsDragging() const { return dragConfirmedCount_ > 0; };
-    /// Return number of buttons pressed in a button mask.
-    unsigned GetNumDragButtons(unsigned buttons);
 
 
     /// Data structure used to represent the drag data associated to a UIElement.
     /// Data structure used to represent the drag data associated to a UIElement.
     struct DragData
     struct DragData
@@ -292,8 +290,6 @@ private:
     unsigned nonModalBatchSize_;
     unsigned nonModalBatchSize_;
     /// Timer used to trigger double click.
     /// Timer used to trigger double click.
     Timer clickTimer_;
     Timer clickTimer_;
-    /// UI element last clicked for tracking click end.
-    /*WeakPtr<UIElement> clickElement_;*/
     /// UI element last clicked for tracking double clicks.
     /// UI element last clicked for tracking double clicks.
     WeakPtr<UIElement> doubleClickElement_;
     WeakPtr<UIElement> doubleClickElement_;
     /// Currently hovered elements.
     /// Currently hovered elements.
@@ -314,5 +310,3 @@ private:
 void URHO3D_API RegisterUILibrary(Context* context);
 void URHO3D_API RegisterUILibrary(Context* context);
 
 
 }
 }
-
-#define TOUCHID_MASK(id) (1 << id)

+ 1 - 2
Source/Engine/UI/UIElement.cpp

@@ -502,8 +502,7 @@ void UIElement::OnDoubleClick(const IntVector2& position, const IntVector2& scre
 void UIElement::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
 void UIElement::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
 {
 {
     dragButtonCombo_ = buttons;
     dragButtonCombo_ = buttons;
-    UI* ui = GetSubsystem<UI>();
-    dragButtonCount_ = ui->GetNumDragButtons(dragButtonCombo_);
+    dragButtonCount_ = CountSetBits(dragButtonCombo_);
 }
 }
 
 
 void UIElement::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, int buttons, int qualifiers, Cursor* cursor)
 void UIElement::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, int buttons, int qualifiers, Cursor* cursor)