From d7a6ede1ca71ad0ef4bc7b2845b624a4c35d4b9b Mon Sep 17 00:00:00 2001 From: Pavel Alexandrov Date: Wed, 1 Nov 2023 10:48:12 +0300 Subject: [PATCH] SDL: Add drag&drop support (#512) --- libs/sdl/sdl.c | 28 ++++++++++++++++++++++++++++ libs/sdl/sdl/Event.hx | 5 +++++ libs/sdl/sdl/Sdl.hx | 9 +++++++++ 3 files changed, 42 insertions(+) diff --git a/libs/sdl/sdl.c b/libs/sdl/sdl.c index 4a4e956c5..af50fca3b 100644 --- a/libs/sdl/sdl.c +++ b/libs/sdl/sdl.c @@ -56,6 +56,10 @@ typedef enum { JoystickButtonUp, JoystickAdded, JoystickRemoved, + DropStart = 400, + DropFile, + DropText, + DropEnd, } event_type; typedef enum { @@ -91,6 +95,7 @@ typedef struct { int value; int fingerId; int joystick; + vbyte* dropFile; } event_data; HL_PRIM bool HL_NAME(init_once)() { @@ -329,6 +334,19 @@ HL_PRIM bool HL_NAME(event_loop)( event_data *event ) { event->type = JoystickRemoved; event->joystick = e.jdevice.which; break; + case SDL_DROPBEGIN: + event->type = DropStart; + break; + case SDL_DROPFILE: case SDL_DROPTEXT: { + vbyte* bytes = hl_copy_bytes(e.drop.file, (int)strlen(e.drop.file) + 1); + SDL_free(e.drop.file); + event->type = e.type == SDL_DROPFILE ? DropFile : DropText; + event->dropFile = bytes; + break; + } + case SDL_DROPCOMPLETE: + event->type = DropEnd; + break; default: //printf("Unknown event type 0x%X\\n", e.type); continue; @@ -836,6 +854,14 @@ HL_PRIM char* HL_NAME(get_clipboard_text)() { return bytes; } +HL_PRIM void HL_NAME(set_drag_and_drop_enabled)( bool enabled ) { + SDL_EventState(SDL_DROPFILE, enabled ? SDL_ENABLE : SDL_DISABLE); +} + +HL_PRIM bool HL_NAME(get_drag_and_drop_enabled)() { + return SDL_EventState(SDL_DROPFILE, SDL_QUERY); +} + HL_PRIM varray* HL_NAME(get_displays)() { int n = SDL_GetNumVideoDisplays(); if (n < 0) @@ -916,6 +942,8 @@ DEFINE_PRIM(_VOID, free_cursor, _CURSOR); DEFINE_PRIM(_VOID, set_cursor, _CURSOR); DEFINE_PRIM(_BOOL, set_clipboard_text, _BYTES); DEFINE_PRIM(_BYTES, get_clipboard_text, _NO_ARG); +DEFINE_PRIM(_VOID, set_drag_and_drop_enabled, _BOOL); +DEFINE_PRIM(_BOOL, get_drag_and_drop_enabled, _NO_ARG); DEFINE_PRIM(_ARR, get_displays, _NO_ARG); DEFINE_PRIM(_ARR, get_display_modes, _I32); DEFINE_PRIM(_DYN, get_current_display_mode, _I32 _BOOL); diff --git a/libs/sdl/sdl/Event.hx b/libs/sdl/sdl/Event.hx index 6f0db476e..b77b47cdc 100644 --- a/libs/sdl/sdl/Event.hx +++ b/libs/sdl/sdl/Event.hx @@ -16,6 +16,7 @@ package sdl; public var value : Int; public var fingerId : Int; public var joystick : Int; + public var dropFile: hl.Bytes; public function new() { } } @@ -46,6 +47,10 @@ enum abstract EventType(Int) { var JoystickButtonUp = 304; var JoystickAdded = 305; var JoystickRemoved = 306; + var DropStart = 400; + var DropFile = 401; + var DropText = 402; + var DropEnd = 403; } enum abstract WindowStateChange(Int) { diff --git a/libs/sdl/sdl/Sdl.hx b/libs/sdl/sdl/Sdl.hx index 333ce19a1..55e5fdfa3 100644 --- a/libs/sdl/sdl/Sdl.hx +++ b/libs/sdl/sdl/Sdl.hx @@ -233,6 +233,15 @@ class Sdl { private static function _getClipboardText() : hl.Bytes { return null; } + + @:hlNative("?sdl", "set_drag_and_drop_enabled") + public static function setDragAndDropEnabled( v : Bool ): Void { + } + + @:hlNative("?sdl", "get_drag_and_drop_enabled") + public static function getDragAndDropEnabled(): Bool { + return false; + } } enum abstract SDLHint(String) from String to String {