UIDragDrop.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <ThirdParty/TurboBadger/tb_widgets.h>
  23. #include "../IO/Log.h"
  24. #include "../Input/Input.h"
  25. #include "../Input/InputEvents.h"
  26. #include "UI.h"
  27. #include "UIEvents.h"
  28. #include "UIWidget.h"
  29. #include "UILayout.h"
  30. #include "UIFontDescription.h"
  31. #include "UITextField.h"
  32. #include "UIImageWidget.h"
  33. #include "UISelectList.h"
  34. #include "UIDragDrop.h"
  35. #include "UIDragObject.h"
  36. #ifdef ATOMIC_PLATFORM_OSX
  37. #include "UIDragDropMac.h"
  38. #elif ATOMIC_PLATFORM_WINDOWS
  39. #include "UIDragDropWindows.h"
  40. #else
  41. namespace Atomic { void InitDragAndDrop(UIDragDrop *dragAndDrop) {} }
  42. #endif
  43. using namespace tb;
  44. namespace Atomic
  45. {
  46. UIDragDrop::UIDragDrop(Context* context) : Object(context)
  47. {
  48. SubscribeToEvent(E_UIUPDATE, ATOMIC_HANDLER(UIDragDrop, HandleUIUpdate));
  49. SubscribeToEvent(E_MOUSEMOVE, ATOMIC_HANDLER(UIDragDrop,HandleMouseMove));
  50. SubscribeToEvent(E_MOUSEBUTTONUP, ATOMIC_HANDLER(UIDragDrop,HandleMouseUp));
  51. SharedPtr<UIFontDescription> fd(new UIFontDescription(context));
  52. fd->SetId("Vera");
  53. fd->SetSize(12);
  54. dragText_ = new UITextField(context);
  55. dragText_->SetFontDescription(fd);
  56. dragText_->SetGravity(UI_GRAVITY_TOP);
  57. dragLayout_ = new UILayout(context);
  58. dragLayout_->AddChild(dragText_);
  59. dragLayout_->SetLayoutSize(UI_LAYOUT_SIZE_PREFERRED);
  60. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_GONE);
  61. // put into hierarchy so we aren't pruned
  62. TBWidget* root = GetSubsystem<UI>()->GetRootWidget();
  63. root->AddChild(dragLayout_->GetInternalWidget());
  64. InitDragAndDrop(this);
  65. }
  66. UIDragDrop::~UIDragDrop()
  67. {
  68. }
  69. void UIDragDrop::DragEnd()
  70. {
  71. SharedPtr<UIDragObject> dragObject = dragObject_;
  72. SharedPtr<UIWidget> currentTargetWidget(currentTargetWidget_);
  73. SharedPtr<UIWidget> dragSourceWidget(dragSourceWidget_);
  74. // clean up
  75. currentTargetWidget_ = 0;
  76. dragObject_ = 0;
  77. dragSourceWidget_ = 0;
  78. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_GONE);
  79. if (currentTargetWidget.Null())
  80. {
  81. return;
  82. }
  83. // Filter dragging buttons on top of themselves
  84. if ((dragSourceWidget == currentTargetWidget) && dragSourceWidget->IsInstanceOf<UIButton>())
  85. {
  86. return;
  87. }
  88. VariantMap dropData;
  89. dropData[DragEnded::P_TARGET] = currentTargetWidget;
  90. dropData[DragEnded::P_DRAGOBJECT] = dragObject;
  91. currentTargetWidget->SendEvent(E_DRAGENDED, dropData);
  92. }
  93. void UIDragDrop::HandleUIUpdate(StringHash eventType, VariantMap& eventData)
  94. {
  95. Input* input = GetSubsystem<Input>();
  96. if (dragSourceWidget_.NotNull() || !input->IsMouseVisible() || !input->GetMouseButtonDown(MOUSEB_LEFT))
  97. {
  98. return;
  99. }
  100. if (TBWidget::hovered_widget)
  101. {
  102. // see if we have a widget with a drag object
  103. TBWidget* tbw = TBWidget::hovered_widget;
  104. UIWidget* widget = nullptr;
  105. while(tbw)
  106. {
  107. if (tbw->GetDelegate())
  108. {
  109. widget = (UIWidget*) tbw->GetDelegate();
  110. if (widget->GetDragObject())
  111. {
  112. // TODO: check if we're in widget bounds
  113. // this is going to need to be updated for drag/drop multiselect
  114. break;
  115. }
  116. widget = nullptr;
  117. }
  118. tbw = tbw->GetParent();
  119. }
  120. if (!widget)
  121. return;
  122. currentTargetWidget_ = widget;
  123. dragSourceWidget_ = widget;
  124. mouseDownPosition_ = input->GetMousePosition();
  125. }
  126. }
  127. void UIDragDrop::HandleMouseUp(StringHash eventType, VariantMap& eventData)
  128. {
  129. using namespace MouseButtonUp;
  130. if (dragObject_.Null())
  131. {
  132. dragSourceWidget_ = 0;
  133. currentTargetWidget_ = 0;
  134. return;
  135. }
  136. if (!(eventData[P_BUTTON].GetInt() == MOUSEB_LEFT))
  137. return;
  138. DragEnd();
  139. }
  140. void UIDragDrop::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  141. {
  142. if (dragObject_.Null() && dragSourceWidget_.Null())
  143. return;
  144. if (dragObject_.Null())
  145. {
  146. dragObject_ = dragSourceWidget_->GetDragObject();
  147. if (dragObject_.Null())
  148. {
  149. dragSourceWidget_ = 0;
  150. return;
  151. }
  152. }
  153. using namespace MouseMove;
  154. int x = eventData[P_X].GetInt();
  155. int y = eventData[P_Y].GetInt();
  156. // tolerance to 8 pixels to start drag/drop operation
  157. IntVector2 mousePos(x, y);
  158. mousePos -= mouseDownPosition_;
  159. if (Abs(mousePos.x_) < 8 && Abs(mousePos.y_) < 8)
  160. return;
  161. // initialize if necessary
  162. if (dragLayout_->GetVisibility() == UI_WIDGET_VISIBILITY_GONE)
  163. {
  164. dragLayout_->GetInternalWidget()->SetZ(WIDGET_Z_TOP);
  165. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_VISIBLE);
  166. dragText_->SetText(dragObject_->GetText());
  167. UIPreferredSize* sz = dragLayout_->GetPreferredSize();
  168. dragLayout_->SetRect(IntRect(0, 0, sz->GetMinWidth(), sz->GetMinHeight()));
  169. }
  170. // see if we have a widget
  171. TBWidget* tbw = TBWidget::hovered_widget;
  172. while(tbw && (!tbw->GetDelegate() || tbw->IsOfType<TBLayout>()))
  173. {
  174. tbw = tbw->GetParent();
  175. }
  176. if (!tbw || !tbw->GetParent())
  177. return;
  178. UIWidget* hoverWidget = (UIWidget*) tbw->GetDelegate();
  179. if (!hoverWidget->GetInternalWidget())
  180. return;
  181. if (hoverWidget != currentTargetWidget_)
  182. {
  183. if (currentTargetWidget_)
  184. {
  185. VariantMap exitData;
  186. exitData[DragExitWidget::P_WIDGET] = currentTargetWidget_;
  187. exitData[DragExitWidget::P_DRAGOBJECT] = dragObject_;
  188. currentTargetWidget_->SendEvent(E_DRAGEXITWIDGET, exitData);
  189. }
  190. currentTargetWidget_ = hoverWidget;
  191. VariantMap enterData;
  192. enterData[DragEnterWidget::P_WIDGET] = currentTargetWidget_;
  193. enterData[DragEnterWidget::P_DRAGOBJECT] = dragObject_;
  194. currentTargetWidget_->SendEvent(E_DRAGENTERWIDGET, enterData);
  195. }
  196. dragLayout_->SetPosition(x, y - 20);
  197. }
  198. void UIDragDrop::FileDragEntered()
  199. {
  200. dragObject_ = new UIDragObject(context_);
  201. }
  202. void UIDragDrop::FileDragAddFile(const String& filename)
  203. {
  204. dragObject_->AddFilename(filename);
  205. }
  206. void UIDragDrop::FileDragConclude()
  207. {
  208. DragEnd();
  209. }
  210. }