UIDragDrop.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. using namespace tb;
  16. namespace Atomic
  17. {
  18. UIDragDrop::UIDragDrop(Context* context) : Object(context)
  19. {
  20. SubscribeToEvent(E_MOUSEMOVE, HANDLER(UIDragDrop,HandleMouseMove));
  21. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UIDragDrop,HandleMouseUp));
  22. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UIDragDrop,HandleMouseDown));
  23. SharedPtr<UIFontDescription> fd(new UIFontDescription(context));
  24. fd->SetId("Vera");
  25. fd->SetSize(12);
  26. dragText_ = new UITextField(context);
  27. dragText_->SetFontDescription(fd);
  28. dragText_->SetGravity(WIDGET_GRAVITY_TOP);
  29. dragLayout_ = new UILayout(context);
  30. dragLayout_->AddChild(dragText_);
  31. dragLayout_->SetLayoutSize(UI_LAYOUT_SIZE_PREFERRED);
  32. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_GONE);
  33. // put into hierarchy so we aren't pruned
  34. TBWidget* root = GetSubsystem<UI>()->GetRootWidget();
  35. root->AddChild(dragLayout_->GetInternalWidget());
  36. }
  37. UIDragDrop::~UIDragDrop()
  38. {
  39. }
  40. void UIDragDrop::HandleMouseDown(StringHash eventType, VariantMap& eventData)
  41. {
  42. using namespace MouseButtonDown;
  43. Input* input = GetSubsystem<Input>();
  44. if (!input->IsMouseVisible())
  45. return;
  46. if ((eventData[P_BUTTONS].GetUInt() & MOUSEB_LEFT) && TBWidget::hovered_widget)
  47. {
  48. // see if we have a widget
  49. TBWidget* tbw = TBWidget::hovered_widget;
  50. while(tbw && !tbw->GetDelegate())
  51. {
  52. tbw = tbw->GetParent();
  53. }
  54. if (!tbw)
  55. return;
  56. UIWidget* widget = (UIWidget*) tbw->GetDelegate();
  57. if (widget->GetType() == UISelectList::GetTypeStatic())
  58. {
  59. // handle select drag
  60. LOGINFOF("DRAG Select: %s", widget->GetTypeName().CString());
  61. }
  62. else
  63. {
  64. dragObject_ = widget->GetDragObject();
  65. }
  66. }
  67. }
  68. void UIDragDrop::HandleMouseUp(StringHash eventType, VariantMap& eventData)
  69. {
  70. using namespace MouseButtonUp;
  71. if (dragObject_.Null())
  72. return;
  73. Input* input = GetSubsystem<Input>();
  74. if (!input->IsMouseVisible())
  75. return;
  76. if ((eventData[P_BUTTON].GetInt() == MOUSEB_LEFT) &&
  77. TBWidget::hovered_widget && TBWidget::hovered_widget->GetDelegate())
  78. {
  79. UIWidget* widget = (UIWidget*) TBWidget::hovered_widget->GetDelegate();
  80. VariantMap dropData;
  81. dropData[DragEnded::P_TARGET] = widget;
  82. dropData[DragEnded::P_DRAGOBJECT] = dragObject_;
  83. SendEvent(E_DRAGENDED, dropData);
  84. }
  85. // clean up
  86. dragObject_ = 0;
  87. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_GONE);
  88. }
  89. void UIDragDrop::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  90. {
  91. if (dragObject_.Null())
  92. return;
  93. Input* input = GetSubsystem<Input>();
  94. if (!input->IsMouseVisible())
  95. return;
  96. // initialize if necessary
  97. if (dragLayout_->GetVisibility() == UI_WIDGET_VISIBILITY_GONE)
  98. {
  99. dragLayout_->GetInternalWidget()->SetZ(WIDGET_Z_TOP);
  100. dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_VISIBLE);
  101. dragText_->SetText(dragObject_->GetText());
  102. UIPreferredSize* sz = dragLayout_->GetPreferredSize();
  103. dragLayout_->SetRect(IntRect(0, 0, sz->GetMinWidth(), sz->GetMinHeight()));
  104. }
  105. using namespace MouseMove;
  106. int x = eventData[P_X].GetInt();
  107. int y = eventData[P_Y].GetInt();
  108. dragLayout_->SetPosition(x, y - 20);
  109. }
  110. }