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 <player3.14@gmail.com>
This commit is contained in:
Shahar Marcus
2024-06-04 20:46:24 +03:00
committed by GitHub
parent e4782cb1b2
commit ead4402d5f
6 changed files with 54 additions and 10 deletions

View File

@@ -11,7 +11,10 @@ namespace lime {
enum DropEventType {
DROP_FILE
DROP_FILE,
DROP_TEXT,
DROP_BEGIN,
DROP_COMPLETE
};

View File

@@ -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);

View File

@@ -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 ();
}

View File

@@ -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;
}

View File

@@ -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

View File

@@ -61,6 +61,10 @@ class Window
public var onClose(default, null) = new Event<Void->Void>();
public var onDeactivate(default, null) = new Event<Void->Void>();
public var onDropFile(default, null) = new Event<String->Void>();
// SDL_DROPTEXT is only implemented for X11 on Linux
//public var onDropText(default, null) = new Event<String->Void>();
public var onDropStart(default, null) = new Event<Void->Void>();
public var onDropEnd(default, null) = new Event<Void->Void>();
public var onEnter(default, null) = new Event<Void->Void>();
public var onExpose(default, null) = new Event<Void->Void>();
public var onFocusIn(default, null) = new Event<Void->Void>();