From 85ec75fd59ad44d8362d04e0d038d550cd498b1f Mon Sep 17 00:00:00 2001 From: Yanrishatum Date: Sun, 18 Oct 2015 00:15:05 +0300 Subject: [PATCH] Drag&Drop support; SDL_CaptureMode support; Maximize window support; --- lime/_backend/native/NativeApplication.hx | 28 +++++++++++++ lime/_backend/native/NativeMouse.hx | 19 ++++++++- lime/_backend/native/NativeWindow.hx | 12 ++++++ lime/app/Application.hx | 1 + lime/app/Event.hx | 15 ++++--- lime/ui/Mouse.hx | 11 +++++ lime/ui/Window.hx | 18 ++++++++- project/Build.xml | 2 + project/include/ui/DropEvent.h | 39 ++++++++++++++++++ project/include/ui/Mouse.h | 1 + project/include/ui/Window.h | 1 + project/src/ExternalInterface.cpp | 20 ++++++++- project/src/backend/sdl/SDLApplication.cpp | 19 +++++++++ project/src/backend/sdl/SDLApplication.h | 3 ++ project/src/backend/sdl/SDLMouse.cpp | 15 ++++++- project/src/backend/sdl/SDLWindow.cpp | 14 +++++++ project/src/backend/sdl/SDLWindow.h | 1 + project/src/ui/DropEvent.cpp | 47 ++++++++++++++++++++++ 18 files changed, 256 insertions(+), 10 deletions(-) create mode 100644 project/include/ui/DropEvent.h create mode 100644 project/src/ui/DropEvent.cpp diff --git a/lime/_backend/native/NativeApplication.hx b/lime/_backend/native/NativeApplication.hx index 33ac53b68..45e0dcb6a 100644 --- a/lime/_backend/native/NativeApplication.hx +++ b/lime/_backend/native/NativeApplication.hx @@ -45,6 +45,7 @@ class NativeApplication { private var gamepadEventInfo = new GamepadEventInfo (); private var joystickEventInfo = new JoystickEventInfo (); private var keyEventInfo = new KeyEventInfo (); + private var dropEventInfo = new DropEventInfo(); private var mouseEventInfo = new MouseEventInfo (); private var renderEventInfo = new RenderEventInfo (RENDER); private var sensorEventInfo = new SensorEventInfo (); @@ -90,6 +91,7 @@ class NativeApplication { lime_gamepad_event_manager_register (handleGamepadEvent, gamepadEventInfo); lime_joystick_event_manager_register (handleJoystickEvent, joystickEventInfo); lime_key_event_manager_register (handleKeyEvent, keyEventInfo); + lime_drop_event_manager_register(handleDropEvent, dropEventInfo); lime_mouse_event_manager_register (handleMouseEvent, mouseEventInfo); lime_render_event_manager_register (handleRenderEvent, renderEventInfo); lime_text_event_manager_register (handleTextEvent, textEventInfo); @@ -248,6 +250,10 @@ class NativeApplication { } + private function handleDropEvent():Void + { + parent.onDropFile.dispatch(dropEventInfo.file); + } private function handleKeyEvent ():Void { @@ -621,6 +627,7 @@ class NativeApplication { @:cffi private static function lime_gamepad_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; @:cffi private static function lime_joystick_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; @:cffi private static function lime_key_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; + @:cffi private static function lime_drop_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; @:cffi private static function lime_mouse_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; @:cffi private static function lime_render_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; @:cffi private static function lime_sensor_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; @@ -632,6 +639,27 @@ class NativeApplication { } +@:enum private abstract DropEventType(Int) +{ + var DROP_FILE = 0; +} + +private class DropEventInfo +{ + public var type:DropEventType; + public var file:String; + + public function new (type:DropEventType = null, file:String = null) + { + this.file = file; + this.type = type; + } + + public function clone():DropEventInfo + { + return new DropEventInfo(type, file); + } +} private class ApplicationEventInfo { diff --git a/lime/_backend/native/NativeMouse.hx b/lime/_backend/native/NativeMouse.hx index 636e308ce..f40d2a529 100644 --- a/lime/_backend/native/NativeMouse.hx +++ b/lime/_backend/native/NativeMouse.hx @@ -17,6 +17,7 @@ class NativeMouse { private static var __cursor:MouseCursor; private static var __hidden:Bool; private static var __lock:Bool; + private static var __captureMode:Bool; public static function hide ():Void { @@ -63,7 +64,22 @@ class NativeMouse { // Get & Set Methods - + public static function get_captureMode():Bool + { + return __captureMode; + } + + public static function set_captureMode(v:Bool):Bool + { + if (v != __captureMode) + { + #if !macro + lime_mouse_set_capture_mode(v); + #end + __captureMode = v; + } + return v; + } public static function get_cursor ():MouseCursor { @@ -149,6 +165,7 @@ class NativeMouse { @:cffi private static function lime_mouse_set_lock (lock:Bool):Void; @:cffi private static function lime_mouse_show ():Void; @:cffi private static function lime_mouse_warp (x:Int, y:Int, window:Dynamic):Void; + @:cffi private static function lime_mouse_set_capture_mode (capture:Bool):Void; #end diff --git a/lime/_backend/native/NativeWindow.hx b/lime/_backend/native/NativeWindow.hx index 5df5df748..fdf453910 100644 --- a/lime/_backend/native/NativeWindow.hx +++ b/lime/_backend/native/NativeWindow.hx @@ -249,6 +249,17 @@ class NativeWindow { } + public function setMaximized (value:Bool):Bool + { + if (handle != null) + { + #if !macro + return lime_window_set_maximized(handle, value); + #end + } + return value; + } + public function setMinimized (value:Bool):Bool { if (handle != null) { @@ -318,6 +329,7 @@ class NativeWindow { @:cffi private static function lime_window_set_icon (handle:Dynamic, buffer:Dynamic):Void; @:cffi private static function lime_window_set_minimized (handle:Dynamic, minimized:Bool):Bool; @:cffi private static function lime_window_set_resizable (handle:Dynamic, resizable:Bool):Bool; + @:cffi private static function lime_window_set_maximized (handle:Dynamic, maximized:Bool):Bool; @:cffi private static function lime_window_set_title (handle:Dynamic, title:String):Dynamic; #end diff --git a/lime/app/Application.hx b/lime/app/Application.hx index 31aeef208..eae457ad4 100644 --- a/lime/app/Application.hx +++ b/lime/app/Application.hx @@ -34,6 +34,7 @@ class Application extends Module { * Update events are dispatched each frame (usually just before rendering) */ public var onUpdate = new EventVoid> (); + public var onDropFile = new EventVoid> (); public var renderer (get, null):Renderer; public var renderers (default, null):Array; diff --git a/lime/app/Event.hx b/lime/app/Event.hx index fe46ff711..994fd0978 100644 --- a/lime/app/Event.hx +++ b/lime/app/Event.hx @@ -57,6 +57,11 @@ class Event { #end } + + public function stopPropagation():Void + { + interrupt = true; + } #if macro @@ -231,17 +236,17 @@ class Event { // //$ethis.remove (listeners[i]); // - //} else { + // } else { // //i++; // - //} + // } // - //} + // } // - //} + // } // - //} + // } public function has (listener:T):Bool { diff --git a/lime/ui/Mouse.hx b/lime/ui/Mouse.hx index 6ed267d29..a2e56f456 100644 --- a/lime/ui/Mouse.hx +++ b/lime/ui/Mouse.hx @@ -6,6 +6,17 @@ class Mouse { public static var cursor (get, set):MouseCursor; public static var lock (get, set):Bool; + public static var captureMode(get, set):Bool; + + private static function set_captureMode(v:Bool):Bool + { + return MouseBackend.set_captureMode(v); + } + + private static function get_captureMode():Bool + { + return MouseBackend.get_captureMode(); + } public static function hide ():Void { diff --git a/lime/ui/Window.hx b/lime/ui/Window.hx index 27245dc40..2a1dfba3b 100644 --- a/lime/ui/Window.hx +++ b/lime/ui/Window.hx @@ -29,6 +29,7 @@ class Window { public var height (get, set):Int; public var id (default, null):Int; public var minimized (get, set):Bool; + public var maximized (get, set):Bool; public var onActivate = new EventVoid> (); public var onClose = new EventVoid> (); public var onCreate = new EventVoid> (); @@ -65,7 +66,11 @@ class Window { @:noCompletion private var __fullscreen:Bool; @:noCompletion private var __height:Int; @:noCompletion private var __minimized:Bool; +<<<<<<< HEAD @:noCompletion private var __resizable:Bool; +======= + @:noCompletion private var __maximized:Bool; +>>>>>>> 7cee614... Drag&Drop support; SDL_CaptureMode support; Maximize window support; Interrupting Event spread; @:noCompletion private var __scale:Float; @:noCompletion private var __title:String; @:noCompletion private var __width:Int; @@ -378,15 +383,26 @@ class Window { } + private inline function get_maximized():Bool + { + return __maximized; + } + + private inline function set_maximized(value:Bool):Bool + { + __minimized = false; + return __maximized = backend.setMaximized(value); + } + @:noCompletion private inline function get_minimized ():Bool { return __minimized; } - @:noCompletion private function set_minimized (value:Bool):Bool { + __maximized = false; return __minimized = backend.setMinimized (value); } diff --git a/project/Build.xml b/project/Build.xml index 8a97393cd..0b2c257ae 100644 --- a/project/Build.xml +++ b/project/Build.xml @@ -3,6 +3,7 @@ + @@ -230,6 +231,7 @@ + diff --git a/project/include/ui/DropEvent.h b/project/include/ui/DropEvent.h new file mode 100644 index 000000000..8225beb3b --- /dev/null +++ b/project/include/ui/DropEvent.h @@ -0,0 +1,39 @@ +#ifndef LIME_UI_DROP_EVENT_H +#define LIME_UI_DROP_EVENT_H + + +#include +#include + + +namespace lime { + + + enum DropEventType { + + DROP_FILE + + }; + + + class DropEvent { + + public: + + static AutoGCRoot* callback; + static AutoGCRoot* eventObject; + + DropEvent (); + + static void Dispatch (DropEvent* event); + + char* file; + DropEventType type; + + }; + + +} + + +#endif \ No newline at end of file diff --git a/project/include/ui/Mouse.h b/project/include/ui/Mouse.h index 5faf937b0..2290788eb 100644 --- a/project/include/ui/Mouse.h +++ b/project/include/ui/Mouse.h @@ -18,6 +18,7 @@ namespace lime { static void Hide (); static void SetCursor (MouseCursor cursor); static void SetLock (bool lock); + static void SetCaptureMode(bool capture); static void Show (); static void Warp (int x, int y, Window* window); diff --git a/project/include/ui/Window.h b/project/include/ui/Window.h index b09d10d59..ab84533e7 100644 --- a/project/include/ui/Window.h +++ b/project/include/ui/Window.h @@ -37,6 +37,7 @@ namespace lime { virtual void SetIcon (ImageBuffer *imageBuffer) = 0; virtual bool SetMinimized (bool minimized) = 0; virtual bool SetResizable (bool resizable) = 0; + virtual bool SetMaximized (bool minimized) = 0; virtual const char* SetTitle (const char* title) = 0; Application* currentApplication; diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index 91fe125e9..195c82ceb 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -909,6 +910,11 @@ namespace lime { } + void lime_drop_event_manager_register(value callback, value eventObject) + { + DropEvent::callback = new AutoGCRoot(callback); + DropEvent::eventObject = new AutoGCRoot(eventObject); + } value lime_lzma_decode (value buffer) { @@ -963,7 +969,11 @@ namespace lime { } - + void lime_mouse_set_capture_mode(bool capture) + { + Mouse::SetCaptureMode(capture); + } + void lime_mouse_set_lock (bool lock) { Mouse::SetLock (lock); @@ -1412,6 +1422,11 @@ namespace lime { } + bool lime_window_set_maximized (value window, bool fullscreen) + { + Window* targetWindow = (Window*)val_data(window); + return targetWindow->SetMaximized(fullscreen); + } bool lime_window_set_resizable (value window, bool resizable) { @@ -1495,12 +1510,14 @@ namespace lime { DEFINE_PRIME2 (lime_jpeg_decode_bytes); DEFINE_PRIME2 (lime_jpeg_decode_file); DEFINE_PRIME2v (lime_key_event_manager_register); + DEFINE_PRIME2v (lime_drop_event_manager_register); DEFINE_PRIME1 (lime_lzma_decode); DEFINE_PRIME1 (lime_lzma_encode); DEFINE_PRIME2v (lime_mouse_event_manager_register); DEFINE_PRIME0v (lime_mouse_hide); DEFINE_PRIME1v (lime_mouse_set_cursor); DEFINE_PRIME1v (lime_mouse_set_lock); + DEFINE_PRIME1v (lime_mouse_set_capture_mode); DEFINE_PRIME0v (lime_mouse_show); DEFINE_PRIME3v (lime_mouse_warp); DEFINE_PRIME1v (lime_neko_execute); @@ -1550,6 +1567,7 @@ namespace lime { DEFINE_PRIME2v (lime_window_set_icon); DEFINE_PRIME2 (lime_window_set_minimized); DEFINE_PRIME2 (lime_window_set_resizable); + DEFINE_PRIME2 (lime_window_set_maximized); DEFINE_PRIME2 (lime_window_set_title); diff --git a/project/src/backend/sdl/SDLApplication.cpp b/project/src/backend/sdl/SDLApplication.cpp index d9e57d3dc..3712fbc00 100644 --- a/project/src/backend/sdl/SDLApplication.cpp +++ b/project/src/backend/sdl/SDLApplication.cpp @@ -28,6 +28,8 @@ namespace lime { printf ("Could not initialize SDL: %s.\n", SDL_GetError ()); } + + SDL_EventState(SDL_DROPFILE, SDL_ENABLE); SDL_LogSetPriority (SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN); @@ -49,6 +51,7 @@ namespace lime { GamepadEvent gamepadEvent; JoystickEvent joystickEvent; KeyEvent keyEvent; + DropEvent dropEvent; MouseEvent mouseEvent; RenderEvent renderEvent; SensorEvent sensorEvent; @@ -201,6 +204,11 @@ namespace lime { ProcessTextEvent (event); break; + case SDL_DROPFILE: + + ProcessDropEvent (event); + break; + case SDL_WINDOWEVENT: switch (event->window.event) { @@ -454,6 +462,17 @@ namespace lime { } + void SDLApplication::ProcessDropEvent (SDL_Event* event) + { + if (DropEvent::callback) + { + dropEvent.type = DROP_FILE; + dropEvent.file = event->drop.file; + + DropEvent::Dispatch(&dropEvent); + SDL_free(dropEvent.file); + } + } void SDLApplication::ProcessKeyEvent (SDL_Event* event) { diff --git a/project/src/backend/sdl/SDLApplication.h b/project/src/backend/sdl/SDLApplication.h index db1152d9e..a45bafb89 100644 --- a/project/src/backend/sdl/SDLApplication.h +++ b/project/src/backend/sdl/SDLApplication.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,7 @@ namespace lime { void ProcessGamepadEvent (SDL_Event* event); void ProcessJoystickEvent (SDL_Event* event); void ProcessKeyEvent (SDL_Event* event); + void ProcessDropEvent (SDL_Event* event); void ProcessMouseEvent (SDL_Event* event); void ProcessSensorEvent (SDL_Event* event); void ProcessTextEvent (SDL_Event* event); @@ -60,6 +62,7 @@ namespace lime { GamepadEvent gamepadEvent; JoystickEvent joystickEvent; KeyEvent keyEvent; + DropEvent dropEvent; Uint32 lastUpdate; MouseEvent mouseEvent; double nextUpdate; diff --git a/project/src/backend/sdl/SDLMouse.cpp b/project/src/backend/sdl/SDLMouse.cpp index 52cbb97c1..138497a64 100644 --- a/project/src/backend/sdl/SDLMouse.cpp +++ b/project/src/backend/sdl/SDLMouse.cpp @@ -176,8 +176,19 @@ namespace lime { } } - - + + void Mouse::SetCaptureMode(bool capture) + { + if (capture) + { + SDL_CaptureMouse(SDL_TRUE); + } + else + { + SDL_CaptureMouse(SDL_FALSE); + } + } + void Mouse::Show () { SDL_ShowCursor (SDL_ENABLE); diff --git a/project/src/backend/sdl/SDLWindow.cpp b/project/src/backend/sdl/SDLWindow.cpp index fa47de57b..d54d4bb0f 100644 --- a/project/src/backend/sdl/SDLWindow.cpp +++ b/project/src/backend/sdl/SDLWindow.cpp @@ -324,6 +324,20 @@ namespace lime { } + bool SDLWindow::SetMaximized (bool maximized) + { + if (maximized) + { + SDL_MaximizeWindow(sdlWindow); + + } + else + { + SDL_RestoreWindow(sdlWindow); + } + + return maximized; + } bool SDLWindow::SetMinimized (bool minimized) { diff --git a/project/src/backend/sdl/SDLWindow.h b/project/src/backend/sdl/SDLWindow.h index 4a66f8224..dec5eafd5 100644 --- a/project/src/backend/sdl/SDLWindow.h +++ b/project/src/backend/sdl/SDLWindow.h @@ -35,6 +35,7 @@ namespace lime { virtual void SetIcon (ImageBuffer *imageBuffer); virtual bool SetMinimized (bool minimized); virtual bool SetResizable (bool resizable); + virtual bool SetMaximized (bool minimized); virtual const char* SetTitle (const char* title); SDL_Window* sdlWindow; diff --git a/project/src/ui/DropEvent.cpp b/project/src/ui/DropEvent.cpp new file mode 100644 index 000000000..399a9fb2f --- /dev/null +++ b/project/src/ui/DropEvent.cpp @@ -0,0 +1,47 @@ +#include +#include + + +namespace lime { + + + AutoGCRoot* DropEvent::callback = 0; + AutoGCRoot* DropEvent::eventObject = 0; + + static int id_file; + static int id_type; + static bool init = false; + + + DropEvent::DropEvent () { + + type = DROP_FILE; + + } + + + void DropEvent::Dispatch (DropEvent* event) { + + if (DropEvent::callback) + { + + if (!init) + { + id_file = val_id("file"); + id_type = val_id("type"); + init = true; + } + + value object = (DropEvent::eventObject ? DropEvent::eventObject->get() : alloc_empty_object()); + + alloc_field(object, id_file, alloc_string(event->file)); + alloc_field(object, id_type, alloc_int (event->type)); + + val_call0 (DropEvent::callback->get ()); + + } + + } + + +} \ No newline at end of file