UIWidget.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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::Invalidate()
  106. {
  107. if (!widget_)
  108. return;
  109. widget_->Invalidate();
  110. }
  111. void UIWidget::Center()
  112. {
  113. if (!widget_)
  114. return;
  115. // this should center on parent widget, not root
  116. UI* ui = GetSubsystem<UI>();
  117. TBRect rect = widget_->GetRect();
  118. TBWidget* root = ui->GetRootWidget();
  119. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  120. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  121. }
  122. UIWidget* UIWidget::GetParent()
  123. {
  124. if (!widget_)
  125. return 0;
  126. TBWidget* parent = widget_->GetParent();
  127. if (!parent)
  128. return 0;
  129. UI* ui = GetSubsystem<UI>();
  130. return ui->WrapWidget(parent);
  131. }
  132. UIWidget* UIWidget::GetContentRoot()
  133. {
  134. if (!widget_)
  135. return 0;
  136. TBWidget* root = widget_->GetContentRoot();
  137. if (!root)
  138. return 0;
  139. UI* ui = GetSubsystem<UI>();
  140. return ui->WrapWidget(root);
  141. }
  142. void UIWidget::Destroy()
  143. {
  144. if (!widget_)
  145. return;
  146. if (widget_->GetParent())
  147. widget_->GetParent()->RemoveChild(widget_);
  148. delete widget_;
  149. }
  150. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  151. {
  152. if (!widget_ || !child)
  153. return;
  154. TBWidget* childw = child->GetInternalWidget();
  155. if (!childw)
  156. return;
  157. widget_->RemoveChild(childw);
  158. if (cleanup)
  159. delete childw;
  160. }
  161. const String& UIWidget::GetId()
  162. {
  163. if (!widget_ || !widget_->GetID())
  164. {
  165. if (id_.Length())
  166. id_.Clear();
  167. return id_;
  168. }
  169. if (id_.Length())
  170. return id_;
  171. UI* ui = GetSubsystem<UI>();
  172. ui->GetTBIDString(widget_->GetID(), id_);
  173. return id_;
  174. }
  175. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  176. {
  177. UI* ui = GetSubsystem<UI>();
  178. if (ev.type == EVENT_TYPE_CLICK)
  179. {
  180. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  181. {
  182. // popup menu
  183. if (JSGetHeapPtr())
  184. {
  185. VariantMap eventData;
  186. eventData[PopupMenuSelect::P_BUTTON] = this;
  187. eventData[PopupMenuSelect::P_REFID] = (unsigned) ev.ref_id;
  188. SendEvent(E_POPUPMENUSELECT, eventData);
  189. }
  190. return true;
  191. }
  192. else
  193. {
  194. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  195. {
  196. VariantMap eventData;
  197. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  198. SendEvent(E_WIDGETEVENT, eventData);
  199. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  200. return true;
  201. }
  202. }
  203. }
  204. return false;
  205. }
  206. }