UIWidget.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //--player --editor-resource-paths "/Users/josh/Dev/atomic/AtomicGameEngine/Data/AtomicPlayer/Resources/CoreData!/Users/josh/Dev/atomic/AtomicGameEngine/Data/AtomicPlayer/Resources/PlayerData!/Users/josh/Dev/atomic/AtomicExamples/UIExample/Resources"
  2. #include "../IO/Log.h"
  3. #include "UIEvents.h"
  4. #include "UI.h"
  5. #include "UIWidget.h"
  6. using namespace tb;
  7. namespace Atomic
  8. {
  9. UIWidget::UIWidget(Context* context, bool createWidget) : Object(context),
  10. widget_(0)
  11. {
  12. AddRef();
  13. if (createWidget)
  14. {
  15. widget_ = new TBWidget();
  16. widget_->SetDelegate(this);
  17. }
  18. }
  19. UIWidget::~UIWidget()
  20. {
  21. }
  22. bool UIWidget::Load(const String& filename)
  23. {
  24. UI* ui = GetSubsystem<UI>();
  25. if (!ui->LoadResourceFile(widget_ , filename))
  26. return false;
  27. VariantMap eventData;
  28. eventData[WidgetLoaded::P_WIDGET] = this;
  29. SendEvent(E_WIDGETLOADED, eventData);
  30. return true;
  31. }
  32. UIWidget* UIWidget::GetWidget(const String& id)
  33. {
  34. if (!widget_)
  35. return 0;
  36. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  37. if (!child)
  38. return 0;
  39. UI* ui = GetSubsystem<UI>();
  40. return ui->WrapWidget(child);
  41. }
  42. void UIWidget::SetWidget(tb::TBWidget* widget)
  43. {
  44. widget_ = widget;
  45. widget_->SetDelegate(this);
  46. }
  47. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  48. {
  49. using namespace WidgetEvent;
  50. data[P_HANDLER] = handler;
  51. data[P_TARGET] = target;
  52. data[P_TYPE] = (unsigned) ev.type;
  53. data[P_X] = ev.target_x;
  54. data[P_Y] = ev.target_y;
  55. data[P_DELTAX] = ev.delta_x;
  56. data[P_DELTAY] = ev.delta_y;
  57. data[P_COUNT] = ev.count;
  58. data[P_KEY] = ev.key;
  59. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  60. data[P_MODIFIERKEYS] = (unsigned) ev.modifierkeys;
  61. data[P_REFID] = (unsigned) ev.ref_id;
  62. data[P_TOUCH] = (unsigned) ev.touch;
  63. }
  64. void UIWidget::OnDelete()
  65. {
  66. widget_ = 0;
  67. ReleaseRef();
  68. }
  69. void UIWidget::AddChild(UIWidget* child)
  70. {
  71. if (!widget_ || !child->widget_)
  72. return;
  73. widget_->AddChild(child->widget_);
  74. }
  75. void UIWidget::SetText(const String& text)
  76. {
  77. if (!widget_)
  78. return;
  79. widget_->SetText(text.CString());
  80. }
  81. void UIWidget::SetPosition(int x, int y)
  82. {
  83. if (!widget_)
  84. return;
  85. widget_->SetPosition(TBPoint(x, y));
  86. }
  87. void UIWidget::SetSize(int width, int height)
  88. {
  89. if (!widget_)
  90. return;
  91. widget_->SetSize(width, height);
  92. }
  93. void UIWidget::Center()
  94. {
  95. if (!widget_)
  96. return;
  97. UI* ui = GetSubsystem<UI>();
  98. TBRect rect = widget_->GetRect();
  99. TBWidget* root = ui->GetRootWidget();
  100. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  101. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  102. }
  103. UIWidget* UIWidget::GetParent()
  104. {
  105. if (!widget_)
  106. return 0;
  107. TBWidget* parent = widget_->GetParent();
  108. if (!parent)
  109. return 0;
  110. UI* ui = GetSubsystem<UI>();
  111. return ui->WrapWidget(parent);
  112. }
  113. void UIWidget::RemoveChild(UIWidget* child)
  114. {
  115. if (!widget_ || !child)
  116. return;
  117. TBWidget* childw = child->GetInternalWidget();
  118. if (!childw)
  119. return;
  120. widget_->RemoveChild(childw);
  121. }
  122. const String& UIWidget::GetId()
  123. {
  124. if (!widget_ || !widget_->GetID())
  125. {
  126. if (id_.Length())
  127. id_.Clear();
  128. return id_;
  129. }
  130. if (id_.Length())
  131. return id_;
  132. UI* ui = GetSubsystem<UI>();
  133. ui->GetTBIDString(widget_->GetID(), id_);
  134. return id_;
  135. }
  136. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  137. {
  138. UI* ui = GetSubsystem<UI>();
  139. if (ev.type == EVENT_TYPE_CLICK)
  140. {
  141. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  142. {
  143. // popup menu
  144. if (JSGetHeapPtr())
  145. {
  146. VariantMap eventData;
  147. eventData[PopupMenuSelect::P_BUTTON] = this;
  148. eventData[PopupMenuSelect::P_REFID] = (unsigned) ev.ref_id;
  149. SendEvent(E_POPUPMENUSELECT, eventData);
  150. }
  151. return true;
  152. }
  153. else
  154. {
  155. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  156. {
  157. VariantMap eventData;
  158. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  159. SendEvent(E_WIDGETEVENT, eventData);
  160. }
  161. }
  162. }
  163. return false;
  164. }
  165. }