Selaa lähdekoodia

Added support for SDL_DROPFILE event. Input system sends event with filename only, and UI subsystem interprets that to include the element that was dropped onto. The UI variant of the event is only posted when the OS mouse cursor is visible (such as in the editor.)

Lasse Öörni 12 vuotta sitten
vanhempi
sitoutus
04bf45e463

+ 13 - 0
Source/Engine/Input/Input.cpp

@@ -22,6 +22,7 @@
 
 #include "Context.h"
 #include "CoreEvents.h"
+#include "FileSystem.h"
 #include "Graphics.h"
 #include "GraphicsEvents.h"
 #include "GraphicsImpl.h"
@@ -850,6 +851,18 @@ void Input::HandleSDLEvent(void* sdlEvent)
         }
         break;
 
+    case SDL_DROPFILE:
+        {
+            using namespace DropFile;
+            
+            VariantMap eventData;
+            eventData[P_FILENAME] = GetInternalPath(String(evt.drop.file));
+            SDL_free(evt.drop.file);
+            
+            SendEvent(E_DROPFILE, eventData);
+        }
+        break;
+        
     case SDL_QUIT:
         SendEvent(E_EXITREQUESTED);
         break;

+ 6 - 0
Source/Engine/Input/InputEvents.h

@@ -148,6 +148,12 @@ EVENT(E_JOYSTICKHATMOVE, JoystickHatMove)
     PARAM(P_POSITION, Position);            // int
 }
 
+/// A file was drag-dropped into the application window.
+EVENT(E_DROPFILE, DropFile)
+{
+    PARAM(P_FILENAME, FileName);            // String
+}
+
 /// Application input focus or minimization changed.
 EVENT(E_INPUTFOCUS, InputFocus)
 {

+ 30 - 0
Source/Engine/UI/UI.cpp

@@ -100,6 +100,7 @@ UI::UI(Context* context) :
     SubscribeToEvent(E_TOUCHMOVE, HANDLER(UI, HandleTouchMove));
     SubscribeToEvent(E_KEYDOWN, HANDLER(UI, HandleKeyDown));
     SubscribeToEvent(E_CHAR, HANDLER(UI, HandleChar));
+    SubscribeToEvent(E_DROPFILE, HANDLER(UI, HandleDropFile));
 
     // Try to initialize right now, but skip if screen mode is not yet set
     Initialize();
@@ -1203,6 +1204,35 @@ void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
     RenderUpdate();
 }
 
+void UI::HandleDropFile(StringHash eventType, VariantMap& eventData)
+{
+    Input* input = GetSubsystem<Input>();
+    
+    // Sending the UI variant of the event only makes sense if the OS cursor is visible (not locked to window center)
+    if (input->IsMouseVisible())
+    {
+        IntVector2 screenPos = input->GetMousePosition();
+        UIElement* element = GetElementAt(screenPos);
+        
+        using namespace UIDropFile;
+        
+        VariantMap uiEventData;
+        uiEventData[P_FILENAME] = eventData[P_FILENAME];
+        uiEventData[P_X] = screenPos.x_;
+        uiEventData[P_Y] = screenPos.y_;
+        uiEventData[P_ELEMENT] = (void*)element;
+        
+        if (element)
+        {
+            IntVector2 relativePos = element->ScreenToElement(screenPos);
+            uiEventData[P_ELEMENTX] = relativePos.x_;
+            uiEventData[P_ELEMENTY] = relativePos.y_;
+        }
+        
+        SendEvent(E_UIDROPFILE, uiEventData);
+    }
+}
+
 void RegisterUILibrary(Context* context)
 {
     Font::RegisterObject(context);

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

@@ -160,6 +160,8 @@ private:
     void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
     /// Handle render update event.
     void HandleRenderUpdate(StringHash eventType, VariantMap& eventData);
+    /// Handle a file being drag-dropped into the application window.
+    void HandleDropFile(StringHash eventType, VariantMap& eventData);
 
     /// Graphics subsystem.
     WeakPtr<Graphics> graphics_;

+ 11 - 0
Source/Engine/UI/UIEvents.h

@@ -296,4 +296,15 @@ EVENT(E_DRAGEND, DragEnd)
     PARAM(P_ELEMENTY, ElementY);            // int
 }
 
+/// A file was drag-dropped into the application window. Includes also coordinates and UI element if applicable
+EVENT(E_UIDROPFILE, UIDropFile)
+{
+    PARAM(P_FILENAME, FileName);            // String
+    PARAM(P_ELEMENT, Element);              // UIElement pointer
+    PARAM(P_X, X);                          // int
+    PARAM(P_Y, Y);                          // int
+    PARAM(P_ELEMENTX, ElementX);            // int (only if element is non-null)
+    PARAM(P_ELEMENTY, ElementY);            // int (only if element is non-null)
+}
+
 }