UIDragDrop.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <ThirdParty/TurboBadger/tb_widgets.h>
  2. #include "../IO/Log.h"
  3. #include "../Input/Input.h"
  4. #include "../Input/InputEvents.h"
  5. #include "UI.h"
  6. #include "UIEvents.h"
  7. #include "UIWidget.h"
  8. #include "UILayout.h"
  9. #include "UIFontDescription.h"
  10. #include "UITextField.h"
  11. #include "UIImageWidget.h"
  12. #include "UISelectList.h"
  13. #include "UIDragDrop.h"
  14. #include "UIDragObject.h"
  15. #ifdef ATOMIC_PLATFORM_OSX
  16. #include "UIDragDropMac.h"
  17. #endif
  18. using namespace tb;
  19. namespace Atomic
  20. {
  21. UIDragDrop::UIDragDrop(Context* context) : Object(context)
  22. {
  23. SubscribeToEvent(E_MOUSEMOVE, HANDLER(UIDragDrop,HandleMouseMove));
  24. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UIDragDrop,HandleMouseUp));
  25. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UIDragDrop,HandleMouseDown));
  26. SharedPtr<UIFontDescription> fd(new UIFontDescription(context));
  27. fd->SetId("Vera");
  28. fd->SetSize(12);
  29. dragText_ = new UITextField(context);
  30. dragText_->SetFontDescription(fd);
  31. dragText_->SetGravity(UI_GRAVITY_TOP);
  32. dragLayout_ = new UILayout(context);
  33. dragLayout_->AddChild(dragText_);
  34. dragLayout_->SetLayoutSize(UI_LAYOUT_SIZE_PREFERRED);
  35. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_GONE);
  36. // put into hierarchy so we aren't pruned
  37. TBWidget* root = GetSubsystem<UI>()->GetRootWidget();
  38. root->AddChild(dragLayout_->GetInternalWidget());
  39. InitDragAndDrop(this);
  40. }
  41. UIDragDrop::~UIDragDrop()
  42. {
  43. }
  44. void UIDragDrop::DragEnd()
  45. {
  46. Input* input = GetSubsystem<Input>();
  47. if (!input->IsMouseVisible())
  48. return;
  49. // see if we have a widget
  50. TBWidget* tbw = TBWidget::hovered_widget;
  51. while(tbw && !tbw->GetDelegate())
  52. {
  53. tbw = tbw->GetParent();
  54. }
  55. if (!tbw)
  56. return;
  57. UIWidget* widget = (UIWidget*) tbw->GetDelegate();
  58. VariantMap dropData;
  59. dropData[DragEnded::P_TARGET] = widget;
  60. dropData[DragEnded::P_DRAGOBJECT] = dragObject_;
  61. SendEvent(E_DRAGENDED, dropData);
  62. // clean up
  63. dragObject_ = 0;
  64. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_GONE);
  65. }
  66. void UIDragDrop::HandleMouseDown(StringHash eventType, VariantMap& eventData)
  67. {
  68. using namespace MouseButtonDown;
  69. Input* input = GetSubsystem<Input>();
  70. if (!input->IsMouseVisible())
  71. return;
  72. if ((eventData[P_BUTTONS].GetUInt() & MOUSEB_LEFT) && TBWidget::hovered_widget)
  73. {
  74. // see if we have a widget
  75. TBWidget* tbw = TBWidget::hovered_widget;
  76. while(tbw && !tbw->GetDelegate())
  77. {
  78. tbw = tbw->GetParent();
  79. }
  80. if (!tbw)
  81. return;
  82. UIWidget* widget = (UIWidget*) tbw->GetDelegate();
  83. if (widget->GetType() == UISelectList::GetTypeStatic())
  84. {
  85. // handle select drag
  86. LOGINFOF("DRAG Select: %s", widget->GetTypeName().CString());
  87. }
  88. else
  89. {
  90. dragObject_ = widget->GetDragObject();
  91. }
  92. }
  93. }
  94. void UIDragDrop::HandleMouseUp(StringHash eventType, VariantMap& eventData)
  95. {
  96. using namespace MouseButtonUp;
  97. if (dragObject_.Null())
  98. return;
  99. if (!(eventData[P_BUTTON].GetInt() == MOUSEB_LEFT))
  100. return;
  101. DragEnd();
  102. }
  103. void UIDragDrop::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  104. {
  105. if (dragObject_.Null())
  106. return;
  107. Input* input = GetSubsystem<Input>();
  108. if (!input->IsMouseVisible())
  109. return;
  110. // initialize if necessary
  111. if (dragLayout_->GetVisibility() == UI_WIDGET_VISIBILITY_GONE)
  112. {
  113. dragLayout_->GetInternalWidget()->SetZ(WIDGET_Z_TOP);
  114. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_VISIBLE);
  115. dragText_->SetText(dragObject_->GetText());
  116. UIPreferredSize* sz = dragLayout_->GetPreferredSize();
  117. dragLayout_->SetRect(IntRect(0, 0, sz->GetMinWidth(), sz->GetMinHeight()));
  118. }
  119. using namespace MouseMove;
  120. int x = eventData[P_X].GetInt();
  121. int y = eventData[P_Y].GetInt();
  122. dragLayout_->SetPosition(x, y - 20);
  123. }
  124. void UIDragDrop::FileDragEntered()
  125. {
  126. dragObject_ = new UIDragObject(context_);
  127. //dragObject_->SetText("Files...");
  128. }
  129. void UIDragDrop::FileDragAddFile(const String& filename)
  130. {
  131. dragObject_->AddFilename(filename);
  132. }
  133. void UIDragDrop::FileDragConclude()
  134. {
  135. DragEnd();
  136. }
  137. }