From ead4402d5fcfe5cf234104d0225c1c2f692e2e64 Mon Sep 17 00:00:00 2001 From: Shahar Marcus <88977041+ShaharMS@users.noreply.github.com> Date: Tue, 4 Jun 2024 20:46:24 +0300 Subject: [PATCH] Implement `onDropStart`, `onDropEnd` for file dropping (#1583) * native side * Theoretically, things should work * brurh forgot a letter * another quick cpp fix * everything works now * onDropText isnt fully supported * Fix typo. * Remove accidental cSpell addition to settings.json * Dispatch `onDropStart` and `onDropEnd` events in HTML5. --------- Co-authored-by: player-03 --- project/include/ui/DropEvent.h | 5 +++- project/src/backend/sdl/SDLApplication.cpp | 29 +++++++++++++++++-- project/src/ui/DropEvent.cpp | 14 +++++---- .../_internal/backend/html5/HTML5Window.hx | 2 ++ .../backend/native/NativeApplication.hx | 10 ++++++- src/lime/ui/Window.hx | 4 +++ 6 files changed, 54 insertions(+), 10 deletions(-) diff --git a/project/include/ui/DropEvent.h b/project/include/ui/DropEvent.h index 67b65e072..18a180377 100644 --- a/project/include/ui/DropEvent.h +++ b/project/include/ui/DropEvent.h @@ -11,7 +11,10 @@ namespace lime { enum DropEventType { - DROP_FILE + DROP_FILE, + DROP_TEXT, + DROP_BEGIN, + DROP_COMPLETE }; diff --git a/project/src/backend/sdl/SDLApplication.cpp b/project/src/backend/sdl/SDLApplication.cpp index 53674f53a..c778e29ac 100644 --- a/project/src/backend/sdl/SDLApplication.cpp +++ b/project/src/backend/sdl/SDLApplication.cpp @@ -60,6 +60,9 @@ namespace lime { WindowEvent windowEvent; SDL_EventState (SDL_DROPFILE, SDL_ENABLE); + SDL_EventState (SDL_DROPTEXT, SDL_ENABLE); + SDL_EventState (SDL_DROPBEGIN, SDL_ENABLE); + SDL_EventState (SDL_DROPCOMPLETE, SDL_ENABLE); SDLJoystick::Init (); #ifdef HX_MACOS @@ -184,6 +187,9 @@ namespace lime { break; case SDL_DROPFILE: + case SDL_DROPTEXT: + case SDL_DROPBEGIN: + case SDL_DROPCOMPLETE: ProcessDropEvent (event); break; @@ -352,8 +358,27 @@ namespace lime { if (DropEvent::callback) { - dropEvent.type = DROP_FILE; - dropEvent.file = (vbyte*)event->drop.file; + switch (event->type) + { + case SDL_DROPFILE: + dropEvent.type = DROP_FILE; + dropEvent.file = (vbyte*)event->drop.file; + break; + case SDL_DROPTEXT: + dropEvent.type = DROP_TEXT; + dropEvent.file = (vbyte*)event->drop.file; + break; + case SDL_DROPBEGIN: + dropEvent.type = DROP_BEGIN; + dropEvent.file = 0; + break; + case SDL_DROPCOMPLETE: + dropEvent.type = DROP_COMPLETE; + dropEvent.file = 0; + break; + default: + break; + } DropEvent::Dispatch (&dropEvent); SDL_free (dropEvent.file); diff --git a/project/src/ui/DropEvent.cpp b/project/src/ui/DropEvent.cpp index 91e40077e..d4f7f1d95 100644 --- a/project/src/ui/DropEvent.cpp +++ b/project/src/ui/DropEvent.cpp @@ -44,14 +44,16 @@ namespace lime { DropEvent* eventObject = (DropEvent*)DropEvent::eventObject->Get (); - int length = strlen ((const char*)event->file); - char* file = (char*)malloc (length + 1); - strcpy (file, (const char*)event->file); - eventObject->file = (vbyte*)file; + if (event->type == DROP_FILE || event->type == DROP_TEXT) { + int length = strlen ((const char*)event->file); + char* file = (char*)malloc (length + 1); + strcpy (file, (const char*)event->file); + eventObject->file = (vbyte*)file; + } else { + eventObject->file = 0; + } eventObject->type = event->type; - } - DropEvent::callback->Call (); } diff --git a/src/lime/_internal/backend/html5/HTML5Window.hx b/src/lime/_internal/backend/html5/HTML5Window.hx index 5d1beb7d0..a38e4399f 100644 --- a/src/lime/_internal/backend/html5/HTML5Window.hx +++ b/src/lime/_internal/backend/html5/HTML5Window.hx @@ -507,10 +507,12 @@ class HTML5Window // TODO: Create a formal API that supports HTML5 file objects if (event.dataTransfer != null && event.dataTransfer.files.length > 0) { + parent.onDropStart.dispatch(); for (file in event.dataTransfer.files) { parent.onDropFile.dispatch(URL.createObjectURL(file)); } + parent.onDropEnd.dispatch(); event.preventDefault(); return false; } diff --git a/src/lime/_internal/backend/native/NativeApplication.hx b/src/lime/_internal/backend/native/NativeApplication.hx index 2208624bf..2acce07dd 100644 --- a/src/lime/_internal/backend/native/NativeApplication.hx +++ b/src/lime/_internal/backend/native/NativeApplication.hx @@ -188,7 +188,12 @@ class NativeApplication { for (window in parent.windows) { - window.onDropFile.dispatch(CFFI.stringValue(dropEventInfo.file)); + switch dropEventInfo.type { + case DROP_FILE: window.onDropFile.dispatch(CFFI.stringValue(dropEventInfo.file)); + case DROP_TEXT: //window.onDropText.dispatch(CFFI.stringValue(dropEventInfo.file)); + case DROP_BEGIN: window.onDropStart.dispatch(); + case DROP_COMPLETE: window.onDropEnd.dispatch(); + } } } @@ -686,6 +691,9 @@ class NativeApplication #if (haxe_ver >= 4.0) private enum #else @:enum private #end abstract DropEventType(Int) { var DROP_FILE = 0; + var DROP_TEXT = 1; + var DROP_BEGIN = 2; + var DROP_COMPLETE = 3; } @:keep /*private*/ class GamepadEventInfo diff --git a/src/lime/ui/Window.hx b/src/lime/ui/Window.hx index 64810eba3..f5aa29b6b 100644 --- a/src/lime/ui/Window.hx +++ b/src/lime/ui/Window.hx @@ -61,6 +61,10 @@ class Window public var onClose(default, null) = new EventVoid>(); public var onDeactivate(default, null) = new EventVoid>(); public var onDropFile(default, null) = new EventVoid>(); + // SDL_DROPTEXT is only implemented for X11 on Linux + //public var onDropText(default, null) = new EventVoid>(); + public var onDropStart(default, null) = new EventVoid>(); + public var onDropEnd(default, null) = new EventVoid>(); public var onEnter(default, null) = new EventVoid>(); public var onExpose(default, null) = new EventVoid>(); public var onFocusIn(default, null) = new EventVoid>();