UIWidget.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. GetSubsystem<UI>()->WrapWidget(this, widget_);
  18. }
  19. UI* ui = GetSubsystem<UI>();
  20. }
  21. UIWidget::~UIWidget()
  22. {
  23. }
  24. bool UIWidget::Load(const String& filename)
  25. {
  26. UI* ui = GetSubsystem<UI>();
  27. if (!ui->LoadResourceFile(widget_ , filename))
  28. return false;
  29. VariantMap eventData;
  30. eventData[WidgetLoaded::P_WIDGET] = this;
  31. SendEvent(E_WIDGETLOADED, eventData);
  32. return true;
  33. }
  34. UIWidget* UIWidget::GetWidget(const String& id)
  35. {
  36. if (!widget_)
  37. return 0;
  38. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  39. if (!child)
  40. return 0;
  41. UI* ui = GetSubsystem<UI>();
  42. return ui->WrapWidget(child);
  43. }
  44. void UIWidget::SetWidget(tb::TBWidget* widget)
  45. {
  46. widget_ = widget;
  47. widget_->SetDelegate(this);
  48. }
  49. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  50. {
  51. using namespace WidgetEvent;
  52. data[P_HANDLER] = handler;
  53. data[P_TARGET] = target;
  54. data[P_TYPE] = (unsigned) ev.type;
  55. data[P_X] = ev.target_x;
  56. data[P_Y] = ev.target_y;
  57. data[P_DELTAX] = ev.delta_x;
  58. data[P_DELTAY] = ev.delta_y;
  59. data[P_COUNT] = ev.count;
  60. data[P_KEY] = ev.key;
  61. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  62. data[P_MODIFIERKEYS] = (unsigned) ev.modifierkeys;
  63. data[P_REFID] = (unsigned) ev.ref_id;
  64. data[P_TOUCH] = (unsigned) ev.touch;
  65. }
  66. void UIWidget::OnDelete()
  67. {
  68. if (widget_)
  69. {
  70. // if we don't have a UI subsystem, we are exiting
  71. UI* ui = GetSubsystem<UI>();
  72. if (ui)
  73. ui->UnwrapWidget(widget_);
  74. }
  75. widget_ = 0;
  76. VariantMap eventData;
  77. eventData[WidgetDeleted::P_WIDGET] = this;
  78. SendEvent(E_WIDGETDELETED, eventData);
  79. ReleaseRef();
  80. }
  81. void UIWidget::AddChild(UIWidget* child)
  82. {
  83. if (!widget_ || !child->widget_)
  84. return;
  85. widget_->AddChild(child->widget_);
  86. }
  87. void UIWidget::SetText(const String& text)
  88. {
  89. if (!widget_)
  90. return;
  91. widget_->SetText(text.CString());
  92. }
  93. void UIWidget::SetPosition(int x, int y)
  94. {
  95. if (!widget_)
  96. return;
  97. widget_->SetPosition(TBPoint(x, y));
  98. }
  99. void UIWidget::SetSize(int width, int height)
  100. {
  101. if (!widget_)
  102. return;
  103. widget_->SetSize(width, height);
  104. }
  105. void UIWidget::Center()
  106. {
  107. if (!widget_)
  108. return;
  109. UI* ui = GetSubsystem<UI>();
  110. TBRect rect = widget_->GetRect();
  111. TBWidget* root = ui->GetRootWidget();
  112. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  113. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  114. }
  115. UIWidget* UIWidget::GetParent()
  116. {
  117. if (!widget_)
  118. return 0;
  119. TBWidget* parent = widget_->GetParent();
  120. if (!parent)
  121. return 0;
  122. UI* ui = GetSubsystem<UI>();
  123. return ui->WrapWidget(parent);
  124. }
  125. void UIWidget::Destroy()
  126. {
  127. if (!widget_)
  128. return;
  129. if (widget_->GetParent())
  130. widget_->GetParent()->RemoveChild(widget_);
  131. delete widget_;
  132. }
  133. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  134. {
  135. if (!widget_ || !child)
  136. return;
  137. TBWidget* childw = child->GetInternalWidget();
  138. if (!childw)
  139. return;
  140. widget_->RemoveChild(childw);
  141. if (cleanup)
  142. delete childw;
  143. }
  144. const String& UIWidget::GetId()
  145. {
  146. if (!widget_ || !widget_->GetID())
  147. {
  148. if (id_.Length())
  149. id_.Clear();
  150. return id_;
  151. }
  152. if (id_.Length())
  153. return id_;
  154. UI* ui = GetSubsystem<UI>();
  155. ui->GetTBIDString(widget_->GetID(), id_);
  156. return id_;
  157. }
  158. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  159. {
  160. UI* ui = GetSubsystem<UI>();
  161. if (ev.type == EVENT_TYPE_CLICK)
  162. {
  163. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  164. {
  165. // popup menu
  166. if (JSGetHeapPtr())
  167. {
  168. VariantMap eventData;
  169. eventData[PopupMenuSelect::P_BUTTON] = this;
  170. eventData[PopupMenuSelect::P_REFID] = (unsigned) ev.ref_id;
  171. SendEvent(E_POPUPMENUSELECT, eventData);
  172. }
  173. return true;
  174. }
  175. else
  176. {
  177. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  178. {
  179. VariantMap eventData;
  180. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  181. SendEvent(E_WIDGETEVENT, eventData);
  182. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  183. return true;
  184. }
  185. }
  186. }
  187. return false;
  188. }
  189. }