UIDragDrop.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. VariantMap dropData;
  84. dropData[DragEnded::P_TARGET] = currentTargetWidget;
  85. dropData[DragEnded::P_DRAGOBJECT] = dragObject;
  86. currentTargetWidget->SendEvent(E_DRAGENDED, dropData);
  87. }
  88. void UIDragDrop::HandleUIUpdate(StringHash eventType, VariantMap& eventData)
  89. {
  90. Input* input = GetSubsystem<Input>();
  91. if (dragSourceWidget_.NotNull() || !input->IsMouseVisible() || !input->GetMouseButtonDown(MOUSEB_LEFT))
  92. {
  93. return;
  94. }
  95. if (TBWidget::hovered_widget)
  96. {
  97. // see if we have a widget with a drag object
  98. TBWidget* tbw = TBWidget::hovered_widget;
  99. UIWidget* widget = nullptr;
  100. while(tbw)
  101. {
  102. if (tbw->GetDelegate())
  103. {
  104. widget = (UIWidget*) tbw->GetDelegate();
  105. if (widget->GetDragObject())
  106. {
  107. // TODO: check if we're in widget bounds
  108. // this is going to need to be updated for drag/drop multiselect
  109. break;
  110. }
  111. widget = nullptr;
  112. }
  113. tbw = tbw->GetParent();
  114. }
  115. if (!widget)
  116. return;
  117. currentTargetWidget_ = widget;
  118. dragSourceWidget_ = widget;
  119. mouseDownPosition_ = input->GetMousePosition();
  120. }
  121. }
  122. void UIDragDrop::HandleMouseUp(StringHash eventType, VariantMap& eventData)
  123. {
  124. using namespace MouseButtonUp;
  125. if (dragObject_.Null())
  126. {
  127. dragSourceWidget_ = 0;
  128. currentTargetWidget_ = 0;
  129. return;
  130. }
  131. if (!(eventData[P_BUTTON].GetInt() == MOUSEB_LEFT))
  132. return;
  133. DragEnd();
  134. }
  135. void UIDragDrop::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  136. {
  137. if (dragObject_.Null() && dragSourceWidget_.Null())
  138. return;
  139. if (dragObject_.Null())
  140. {
  141. dragObject_ = dragSourceWidget_->GetDragObject();
  142. if (dragObject_.Null())
  143. {
  144. dragSourceWidget_ = 0;
  145. return;
  146. }
  147. }
  148. using namespace MouseMove;
  149. int x = eventData[P_X].GetInt();
  150. int y = eventData[P_Y].GetInt();
  151. // tolerance to 8 pixels to start drag/drop operation
  152. IntVector2 mousePos(x, y);
  153. mousePos -= mouseDownPosition_;
  154. if (Abs(mousePos.x_) < 8 && Abs(mousePos.y_) < 8)
  155. return;
  156. // initialize if necessary
  157. if (dragLayout_->GetVisibility() == UI_WIDGET_VISIBILITY_GONE)
  158. {
  159. dragLayout_->GetInternalWidget()->SetZ(WIDGET_Z_TOP);
  160. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_VISIBLE);
  161. dragText_->SetText(dragObject_->GetText());
  162. UIPreferredSize* sz = dragLayout_->GetPreferredSize();
  163. dragLayout_->SetRect(IntRect(0, 0, sz->GetMinWidth(), sz->GetMinHeight()));
  164. }
  165. // see if we have a widget
  166. TBWidget* tbw = TBWidget::hovered_widget;
  167. while(tbw && (!tbw->GetDelegate() || tbw->IsOfType<TBLayout>()))
  168. {
  169. tbw = tbw->GetParent();
  170. }
  171. if (!tbw || !tbw->GetParent())
  172. return;
  173. UIWidget* hoverWidget = (UIWidget*) tbw->GetDelegate();
  174. if (!hoverWidget->GetInternalWidget())
  175. return;
  176. if (hoverWidget != currentTargetWidget_)
  177. {
  178. if (currentTargetWidget_)
  179. {
  180. VariantMap exitData;
  181. exitData[DragExitWidget::P_WIDGET] = currentTargetWidget_;
  182. exitData[DragExitWidget::P_DRAGOBJECT] = dragObject_;
  183. currentTargetWidget_->SendEvent(E_DRAGEXITWIDGET, exitData);
  184. }
  185. currentTargetWidget_ = hoverWidget;
  186. VariantMap enterData;
  187. enterData[DragEnterWidget::P_WIDGET] = currentTargetWidget_;
  188. enterData[DragEnterWidget::P_DRAGOBJECT] = dragObject_;
  189. currentTargetWidget_->SendEvent(E_DRAGENTERWIDGET, enterData);
  190. }
  191. dragLayout_->SetPosition(x, y - 20);
  192. }
  193. void UIDragDrop::FileDragEntered()
  194. {
  195. dragObject_ = new UIDragObject(context_);
  196. }
  197. void UIDragDrop::FileDragAddFile(const String& filename)
  198. {
  199. dragObject_->AddFilename(filename);
  200. }
  201. void UIDragDrop::FileDragConclude()
  202. {
  203. DragEnd();
  204. }
  205. }