UIDragDrop.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "UIWidget.h"
  7. #include "UIFontDescription.h"
  8. #include "UITextField.h"
  9. #include "UIImageWidget.h"
  10. #include "UISelectList.h"
  11. #include "UIDragDrop.h"
  12. using namespace tb;
  13. namespace Atomic
  14. {
  15. UIDragDrop::UIDragDrop(Context* context) : Object(context)
  16. {
  17. SubscribeToEvent(E_MOUSEMOVE, HANDLER(UIDragDrop,HandleMouseMove));
  18. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UIDragDrop,HandleMouseUp));
  19. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UIDragDrop,HandleMouseDown));
  20. SharedPtr<UIFontDescription> fd(new UIFontDescription(context));
  21. fd->SetId("Vera");
  22. fd->SetSize(12);
  23. dragText_ = new UITextField(context);
  24. dragText_->SetFontDescription(fd);
  25. dragText_->SetText("This is a test!");
  26. dragText_->SetGravity(WIDGET_GRAVITY_TOP);
  27. UIPreferredSize* sz = dragText_->GetPreferredSize();
  28. dragText_->SetRect(IntRect(0, 0, sz->GetMinWidth(), sz->GetMinHeight()));
  29. dragWidget_ = new UIWidget(context);
  30. dragWidget_->AddChild(dragText_);
  31. sz = dragWidget_->GetPreferredSize();
  32. dragWidget_->SetRect(IntRect(0, 0, sz->GetMinWidth(), sz->GetMinHeight()));
  33. // put into hierarchy so we aren't pruned
  34. TBWidget* root = GetSubsystem<UI>()->GetRootWidget();
  35. root->AddChild(dragWidget_->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. SharedPtr<Object> dragObject(widget->GetDragObject());
  65. dragWidget_->GetInternalWidget()->SetZ(WIDGET_Z_TOP);
  66. if (dragObject.NotNull())
  67. LOGINFOF("DRAG WIDGET: %s Object: %s", widget->GetTypeName().CString(), dragObject->GetTypeName().CString());
  68. else
  69. LOGINFOF("DRAG WIDGET: %s Object: None", widget->GetTypeName().CString());
  70. }
  71. }
  72. }
  73. void UIDragDrop::HandleMouseUp(StringHash eventType, VariantMap& eventData)
  74. {
  75. }
  76. void UIDragDrop::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  77. {
  78. using namespace MouseMove;
  79. Input* input = GetSubsystem<Input>();
  80. if (!input->IsMouseVisible())
  81. return;
  82. int x = eventData[P_X].GetInt();
  83. int y = eventData[P_Y].GetInt();
  84. dragWidget_->SetPosition(x, y - 20);
  85. }
  86. }