UIDragDropMac.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <stdio.h>
  23. #include <ThirdParty/SDL/include/SDL.h>
  24. #include <ThirdParty/SDL/include/SDL_syswm.h>
  25. #include <Atomic/IO/Log.h>
  26. #include <Atomic/Input/InputEvents.h>
  27. #include <Atomic/Graphics/Graphics.h>
  28. #include "UIDragDrop.h"
  29. #include "UIDragDropMac.h"
  30. static Atomic::WeakPtr<Atomic::UIDragDrop> dragAndDrop_;
  31. @interface NSWindow (NSWindowWithDragAndDrop)
  32. -(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender;
  33. -(NSDragOperation)draggingUpdated:(id)sender;
  34. -(BOOL)prepareForDragOperation:(id)sender;
  35. -(BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
  36. -(void)concludeDragOperation:(id <NSDraggingInfo>)sender;
  37. @end
  38. @implementation NSWindow (NSWindowWithDragAndDrop)
  39. -(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
  40. {
  41. NSPasteboard *pboard = [sender draggingPasteboard];
  42. NSString *type = [pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]];
  43. if (type)
  44. {
  45. if ([type isEqualToString:NSFilenamesPboardType])
  46. {
  47. NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
  48. int i = (int) [filenames count];
  49. if (i)
  50. dragAndDrop_->FileDragEntered();
  51. while (i-- > 0)
  52. {
  53. NSString *filename = [filenames objectAtIndex:i];
  54. dragAndDrop_->FileDragAddFile([filename UTF8String]);
  55. }
  56. }
  57. }
  58. return NSDragOperationCopy;
  59. }
  60. -(NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
  61. {
  62. using namespace Atomic::MouseMove;
  63. Atomic::Graphics* graphics = dragAndDrop_->GetSubsystem<Atomic::Graphics>();
  64. NSPoint dragPoint = [sender draggingLocation];
  65. Atomic::VariantMap eventData;
  66. eventData[P_X] = (int) dragPoint.x;
  67. eventData[P_Y] = graphics->GetHeight() - (int) dragPoint.y;
  68. eventData[P_DX] = 0;
  69. eventData[P_DY] = 0;
  70. eventData[P_BUTTONS] = 0;
  71. eventData[P_QUALIFIERS] = 0;
  72. dragAndDrop_->SendEvent(Atomic::E_MOUSEMOVE, eventData);
  73. return NSDragOperationCopy;
  74. }
  75. -(BOOL)prepareForDragOperation:(id)sender
  76. {
  77. return YES;
  78. }
  79. -(BOOL)performDragOperation:(id <NSDraggingInfo>)sender
  80. {
  81. return YES;
  82. }
  83. -(void)concludeDragOperation:(id <NSDraggingInfo>)sender
  84. {
  85. dragAndDrop_->FileDragConclude();
  86. }
  87. @end
  88. namespace Atomic
  89. {
  90. static NSWindow* GetNSWindow()
  91. {
  92. SDL_Window* window = (SDL_Window*) dragAndDrop_->GetSubsystem<Graphics>()->GetSDLWindow();
  93. SDL_SysWMinfo info;
  94. SDL_VERSION(&info.version);
  95. if(SDL_GetWindowWMInfo(window, &info)) {
  96. return info.info.cocoa.window;
  97. }
  98. return NULL;
  99. }
  100. /*
  101. void StartDragAndDrop()
  102. {
  103. Object* o = dragAndDrop_->GetDragObject();
  104. if (!o)
  105. return;
  106. ToolCore::Asset* asset = o->GetType() == ToolCore::Asset::GetTypeStatic() ? (ToolCore::Asset*) o : NULL;
  107. if (asset)
  108. {
  109. NSWindow* window = GetNSWindow();
  110. if (window)
  111. {
  112. NSImage *dragImage = nil;
  113. NSPoint dragPosition;
  114. NSString* filePath = [NSString stringWithUTF8String:asset->GetPath().CString()];
  115. // Write data to the pasteboard
  116. NSArray *fileList = [NSArray arrayWithObjects:filePath, nil];
  117. NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
  118. [pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
  119. owner:nil];
  120. [pboard setPropertyList:fileList forType:NSFilenamesPboardType];
  121. // blocks
  122. [window dragImage:dragImage
  123. at:dragPosition
  124. offset:NSZeroSize
  125. event:nil
  126. pasteboard:pboard
  127. source:window
  128. slideBack:YES];
  129. }
  130. }
  131. }
  132. */
  133. void InitDragAndDrop(UIDragDrop *dragAndDrop)
  134. {
  135. dragAndDrop_ = dragAndDrop;
  136. NSWindow* window = GetNSWindow();
  137. if (window)
  138. {
  139. [window registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
  140. }
  141. }
  142. }