UIDragDrop.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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, HANDLER(UIDragDrop,HandleMouseMove));
  49. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UIDragDrop,HandleMouseUp));
  50. SubscribeToEvent(E_MOUSEBUTTONDOWN, 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. WeakPtr<UIWidget> currentTargetWidget = currentTargetWidget_;
  73. // clean up
  74. currentTargetWidget_ = 0;
  75. dragObject_ = 0;
  76. dragSourceWidget_ = 0;
  77. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_GONE);
  78. if (currentTargetWidget.Null())
  79. {
  80. return;
  81. }
  82. VariantMap dropData;
  83. dropData[DragEnded::P_TARGET] = currentTargetWidget;
  84. dropData[DragEnded::P_DRAGOBJECT] = dragObject;
  85. currentTargetWidget->SendEvent(E_DRAGENDED, dropData);
  86. }
  87. void UIDragDrop::HandleMouseDown(StringHash eventType, VariantMap& eventData)
  88. {
  89. using namespace MouseButtonDown;
  90. Input* input = GetSubsystem<Input>();
  91. if (!input->IsMouseVisible())
  92. return;
  93. if ((eventData[P_BUTTONS].GetUInt() & MOUSEB_LEFT) && TBWidget::hovered_widget)
  94. {
  95. // see if we have a widget
  96. TBWidget* tbw = TBWidget::hovered_widget;
  97. while(tbw && !tbw->GetDelegate())
  98. {
  99. tbw = tbw->GetParent();
  100. }
  101. if (!tbw)
  102. return;
  103. UIWidget* widget = (UIWidget*) tbw->GetDelegate();
  104. currentTargetWidget_ = widget;
  105. dragSourceWidget_ = widget;
  106. }
  107. }
  108. void UIDragDrop::HandleMouseUp(StringHash eventType, VariantMap& eventData)
  109. {
  110. using namespace MouseButtonUp;
  111. if (dragObject_.Null())
  112. {
  113. dragSourceWidget_ = 0;
  114. currentTargetWidget_ = 0;
  115. return;
  116. }
  117. if (!(eventData[P_BUTTON].GetInt() == MOUSEB_LEFT))
  118. return;
  119. DragEnd();
  120. }
  121. void UIDragDrop::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  122. {
  123. Input* input = GetSubsystem<Input>();
  124. if (dragObject_.Null() && dragSourceWidget_.Null())
  125. return;
  126. if (dragObject_.Null())
  127. {
  128. dragObject_ = dragSourceWidget_->GetDragObject();
  129. if (dragObject_.Null())
  130. {
  131. dragSourceWidget_ = 0;
  132. return;
  133. }
  134. }
  135. // initialize if necessary
  136. if (dragLayout_->GetVisibility() == UI_WIDGET_VISIBILITY_GONE)
  137. {
  138. dragLayout_->GetInternalWidget()->SetZ(WIDGET_Z_TOP);
  139. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_VISIBLE);
  140. dragText_->SetText(dragObject_->GetText());
  141. UIPreferredSize* sz = dragLayout_->GetPreferredSize();
  142. dragLayout_->SetRect(IntRect(0, 0, sz->GetMinWidth(), sz->GetMinHeight()));
  143. }
  144. using namespace MouseMove;
  145. int x = eventData[P_X].GetInt();
  146. int y = eventData[P_Y].GetInt();
  147. // see if we have a widget
  148. TBWidget* tbw = TBWidget::hovered_widget;
  149. while(tbw && !tbw->GetDelegate())
  150. {
  151. tbw = tbw->GetParent();
  152. }
  153. if (!tbw || !tbw->GetParent())
  154. return;
  155. UIWidget* hoverWidget = (UIWidget*) tbw->GetDelegate();
  156. if (!hoverWidget->GetInternalWidget())
  157. return;
  158. if (hoverWidget != currentTargetWidget_)
  159. {
  160. if (currentTargetWidget_)
  161. {
  162. VariantMap exitData;
  163. exitData[DragExitWidget::P_WIDGET] = currentTargetWidget_;
  164. exitData[DragExitWidget::P_DRAGOBJECT] = dragObject_;
  165. currentTargetWidget_->SendEvent(E_DRAGEXITWIDGET, exitData);
  166. }
  167. currentTargetWidget_ = hoverWidget;
  168. VariantMap enterData;
  169. enterData[DragEnterWidget::P_WIDGET] = currentTargetWidget_;
  170. enterData[DragEnterWidget::P_DRAGOBJECT] = dragObject_;
  171. currentTargetWidget_->SendEvent(E_DRAGENTERWIDGET, enterData);
  172. }
  173. dragLayout_->SetPosition(x, y - 20);
  174. }
  175. void UIDragDrop::FileDragEntered()
  176. {
  177. dragObject_ = new UIDragObject(context_);
  178. //dragObject_->SetText("Files...");
  179. }
  180. void UIDragDrop::FileDragAddFile(const String& filename)
  181. {
  182. dragObject_->AddFilename(filename);
  183. }
  184. void UIDragDrop::FileDragConclude()
  185. {
  186. DragEnd();
  187. }
  188. }