UIWidget.cpp 5.7 KB

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