UIDragDropMac.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Cocoa/Cocoa.h>
  23. #include <stdio.h>
  24. #include <ThirdParty/SDL/include/SDL.h>
  25. #include <ThirdParty/SDL/include/SDL_syswm.h>
  26. #include <Atomic/IO/Log.h>
  27. #include <Atomic/Input/InputEvents.h>
  28. #include <Atomic/Graphics/Graphics.h>
  29. #include "UIDragDrop.h"
  30. #include "UIDragDropMac.h"
  31. static Atomic::WeakPtr<Atomic::UIDragDrop> dragAndDrop_;
  32. @interface NSWindow (NSWindowWithDragAndDrop)
  33. -(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender;
  34. -(NSDragOperation)draggingUpdated:(id)sender;
  35. -(BOOL)prepareForDragOperation:(id)sender;
  36. -(BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
  37. -(void)concludeDragOperation:(id <NSDraggingInfo>)sender;
  38. @end
  39. @implementation NSWindow (NSWindowWithDragAndDrop)
  40. -(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
  41. {
  42. NSPasteboard *pboard = [sender draggingPasteboard];
  43. NSString *type = [pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]];
  44. if (type)
  45. {
  46. if ([type isEqualToString:NSFilenamesPboardType])
  47. {
  48. NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
  49. int i = (int) [filenames count];
  50. if (i)
  51. dragAndDrop_->FileDragEntered();
  52. while (i-- > 0)
  53. {
  54. NSString *filename = [filenames objectAtIndex:i];
  55. dragAndDrop_->FileDragAddFile([filename UTF8String]);
  56. }
  57. }
  58. }
  59. return NSDragOperationCopy;
  60. }
  61. -(NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
  62. {
  63. using namespace Atomic::MouseMove;
  64. Atomic::Graphics* graphics = dragAndDrop_->GetSubsystem<Atomic::Graphics>();
  65. NSPoint dragPoint = [sender draggingLocation];
  66. Atomic::VariantMap eventData;
  67. eventData[P_X] = (int) dragPoint.x;
  68. eventData[P_Y] = graphics->GetHeight() - (int) dragPoint.y;
  69. eventData[P_DX] = 0;
  70. eventData[P_DY] = 0;
  71. eventData[P_BUTTONS] = 0;
  72. eventData[P_QUALIFIERS] = 0;
  73. dragAndDrop_->SendEvent(Atomic::E_MOUSEMOVE, eventData);
  74. return NSDragOperationCopy;
  75. }
  76. -(BOOL)prepareForDragOperation:(id)sender
  77. {
  78. return YES;
  79. }
  80. -(BOOL)performDragOperation:(id <NSDraggingInfo>)sender
  81. {
  82. return YES;
  83. }
  84. -(void)concludeDragOperation:(id <NSDraggingInfo>)sender
  85. {
  86. dragAndDrop_->FileDragConclude();
  87. }
  88. @end
  89. namespace Atomic
  90. {
  91. static NSWindow* GetNSWindow()
  92. {
  93. SDL_Window* window = (SDL_Window*) dragAndDrop_->GetSubsystem<Graphics>()->GetSDLWindow();
  94. SDL_SysWMinfo info;
  95. SDL_VERSION(&info.version);
  96. if(SDL_GetWindowWMInfo(window, &info)) {
  97. return info.info.cocoa.window;
  98. }
  99. return NULL;
  100. }
  101. /*
  102. void StartDragAndDrop()
  103. {
  104. Object* o = dragAndDrop_->GetDragObject();
  105. if (!o)
  106. return;
  107. ToolCore::Asset* asset = o->GetType() == ToolCore::Asset::GetTypeStatic() ? (ToolCore::Asset*) o : NULL;
  108. if (asset)
  109. {
  110. NSWindow* window = GetNSWindow();
  111. if (window)
  112. {
  113. NSImage *dragImage = nil;
  114. NSPoint dragPosition;
  115. NSString* filePath = [NSString stringWithUTF8String:asset->GetPath().CString()];
  116. // Write data to the pasteboard
  117. NSArray *fileList = [NSArray arrayWithObjects:filePath, nil];
  118. NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
  119. [pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
  120. owner:nil];
  121. [pboard setPropertyList:fileList forType:NSFilenamesPboardType];
  122. // blocks
  123. [window dragImage:dragImage
  124. at:dragPosition
  125. offset:NSZeroSize
  126. event:nil
  127. pasteboard:pboard
  128. source:window
  129. slideBack:YES];
  130. }
  131. }
  132. }
  133. */
  134. void InitDragAndDrop(UIDragDrop *dragAndDrop)
  135. {
  136. dragAndDrop_ = dragAndDrop;
  137. NSWindow* window = GetNSWindow();
  138. if (window)
  139. {
  140. [window registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
  141. }
  142. }
  143. }