UIWidget.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. if (widget_)
  67. {
  68. // if we don't have a UI subsystem, we are exiting
  69. UI* ui = GetSubsystem<UI>();
  70. if (ui)
  71. ui->UnwrapWidget(widget_);
  72. }
  73. widget_ = 0;
  74. ReleaseRef();
  75. }
  76. void UIWidget::AddChild(UIWidget* child)
  77. {
  78. if (!widget_ || !child->widget_)
  79. return;
  80. widget_->AddChild(child->widget_);
  81. }
  82. void UIWidget::SetText(const String& text)
  83. {
  84. if (!widget_)
  85. return;
  86. widget_->SetText(text.CString());
  87. }
  88. void UIWidget::SetPosition(int x, int y)
  89. {
  90. if (!widget_)
  91. return;
  92. widget_->SetPosition(TBPoint(x, y));
  93. }
  94. void UIWidget::SetSize(int width, int height)
  95. {
  96. if (!widget_)
  97. return;
  98. widget_->SetSize(width, height);
  99. }
  100. void UIWidget::Center()
  101. {
  102. if (!widget_)
  103. return;
  104. UI* ui = GetSubsystem<UI>();
  105. TBRect rect = widget_->GetRect();
  106. TBWidget* root = ui->GetRootWidget();
  107. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  108. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  109. }
  110. UIWidget* UIWidget::GetParent()
  111. {
  112. if (!widget_)
  113. return 0;
  114. TBWidget* parent = widget_->GetParent();
  115. if (!parent)
  116. return 0;
  117. UI* ui = GetSubsystem<UI>();
  118. return ui->WrapWidget(parent);
  119. }
  120. void UIWidget::RemoveChild(UIWidget* child)
  121. {
  122. if (!widget_ || !child)
  123. return;
  124. TBWidget* childw = child->GetInternalWidget();
  125. if (!childw)
  126. return;
  127. widget_->RemoveChild(childw);
  128. }
  129. const String& UIWidget::GetId()
  130. {
  131. if (!widget_ || !widget_->GetID())
  132. {
  133. if (id_.Length())
  134. id_.Clear();
  135. return id_;
  136. }
  137. if (id_.Length())
  138. return id_;
  139. UI* ui = GetSubsystem<UI>();
  140. ui->GetTBIDString(widget_->GetID(), id_);
  141. return id_;
  142. }
  143. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  144. {
  145. UI* ui = GetSubsystem<UI>();
  146. if (ev.type == EVENT_TYPE_CLICK)
  147. {
  148. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  149. {
  150. // popup menu
  151. if (JSGetHeapPtr())
  152. {
  153. VariantMap eventData;
  154. eventData[PopupMenuSelect::P_BUTTON] = this;
  155. eventData[PopupMenuSelect::P_REFID] = (unsigned) ev.ref_id;
  156. SendEvent(E_POPUPMENUSELECT, eventData);
  157. }
  158. return true;
  159. }
  160. else
  161. {
  162. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  163. {
  164. VariantMap eventData;
  165. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  166. SendEvent(E_WIDGETEVENT, eventData);
  167. }
  168. }
  169. }
  170. return false;
  171. }
  172. }