About Monkey 2 › Forums › Monkey 2 Programming Help › Drag and drop?
This topic contains 10 replies, has 6 voices, and was last updated by
Gwenel
8 months, 1 week ago.
-
AuthorPosts
-
August 26, 2016 at 7:03 am #3463
Does mx2 support dropping of files on the app window?
If so, how?
if not, how?
August 26, 2016 at 12:34 pm #3471I know (and found in module source) that SDL has an event SDL_DropEvent but not sure if this is implemented/used in MX2 (its defined so I imagine that can be catched).
edit:
tested using the SDL example, just adding in Select..End Select
Case SDL_DROPFILE
Print “Event dropfile”edit2:
looking at the event.monkey2 source it seems that SDL_DROP is not implemented (or maybe I’ve looked in the wrong place! – there are only key, windows and mouse events)
it seems to works
August 26, 2016 at 1:48 pm #3479Looks like you need to hack Mojo to support dragging items onto the application window.
Rough “proof of concept” example:
monkey2/modules/mojo/app/app.monkey2
Add the following field, property and method to to the AppInstance class:
[/crayon]Monkey12345678910111213[crayon-5cb9ac4ade5ab755293036 inline="true" ]PrivateField _draggedFileName:StringPublicProperty DraggedFileName:String()Return _draggedFileNameEndMethod ClearDraggedFileName:Void()_draggedFileName = NullEndAdd the following function call to the bottom of the AppInstance class constructor, just before the End statement:
[/crayon]Monkey123456789[crayon-5cb9ac4ade5b0476549710 inline="true" ]Method New()...SDL_EventState(SDL_DROPFILE, SDL_ENABLE)EndAdd the following to the Select statement in the DispatchEvent method:
[/crayon]Monkey1234567891011121314151617[crayon-5cb9ac4ade5b4496054223 inline="true" ]Method DispatchEvent( event:SDL_Event Ptr )Select event->type...Case SDL_DROPFILELocal devent:=Cast<SDL_DropEvent Ptr>( event )_draggedFileName = String.FromCString(devent->file)Print _draggedFileNameSDL_free(devent->file)EndEndCompile the mojo module. Test it out with the following example.
sdl2DragTest.monkey2:
[/crayon]Monkey1234567891011121314151617181920212223242526272829[crayon-5cb9ac4ade5b9591711235 inline="true" ]#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowMethod New()Super.New("Window",640, 480)EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()If App.DraggedFileNamecanvas.DrawText(App.DraggedFileName, Width/2, Height/2, .5, .5)Elsecanvas.DrawText("Drag something onto the window...", Width/2, Height/2, .5, .5)EndifEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndAs I said, that’s just a proof of concept that works for me (on Windows).
To make DropEvent behave like the other Mojo event types requires extra work… Something for another time…
August 26, 2016 at 8:30 pm #3488Looks nice and easy, will have a hack at this today.
August 8, 2018 at 1:55 pm #15266Does drag and drop work for both windows and icons? Please create a drag and drop banana how to use it in new releases it could be emmensly useful to have sometimes
August 9, 2018 at 11:59 am #15268It’s easy. Write in your widow constructor
Monkey123App.FileDropped+=Lambda( path: String )Print pathEndWorks fine on Windows, don’t know about osx.
August 9, 2018 at 12:55 pm #15269it works in macos the same
, also with folders and multiple files is confirmed.
How about dropping files onto the actual app icon instead of the app window?
In windows this would mean reading the parameters of the exe file right? What about macOS?
August 9, 2018 at 1:08 pm #15270Ted2go is supporting drag-into-exe. You can test it on win and mac.
The code
Monkey12345678910App.Idle+=Lambda()' open docs from argsLocal args:=AppArgs()For Local i:=1 Until args.LengthLocal arg:=args[i]arg=arg.Replace( "\","/" )MainWindow.OnFileDropped( arg )NextEndAugust 9, 2018 at 1:49 pm #15271I get the error “identifier “MainWindow” Not found?
August 9, 2018 at 2:04 pm #15272Replace MainWindow.OnFileDropped( arg ) with your code .
August 9, 2018 at 2:22 pm #15273oh gotcha I thought was some clever mechanism to chain parameter/icon input to the window drag and drop input.
for macos you need to edit info.plist to allow dragging to an icon on the desktop or onto the dock
info.plist needs the added line:
Add/Modify public.data mime type
I can be misstaken as this is all new to me, but am exploring it now. Is this why you can’t drag files onto Monkey2 in the macos dock (or desk) maybe.
-
AuthorPosts
You must be logged in to reply to this topic.