UIDragDrop.cpp 7.1 KB

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