UIWidget.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. }
  20. UIWidget::~UIWidget()
  21. {
  22. }
  23. bool UIWidget::Load(const String& filename)
  24. {
  25. UI* ui = GetSubsystem<UI>();
  26. if (!ui->LoadResourceFile(widget_ , filename))
  27. return false;
  28. VariantMap eventData;
  29. eventData[WidgetLoaded::P_WIDGET] = this;
  30. SendEvent(E_WIDGETLOADED, eventData);
  31. return true;
  32. }
  33. UIWidget* UIWidget::GetWidget(const String& id)
  34. {
  35. if (!widget_)
  36. return 0;
  37. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  38. if (!child)
  39. return 0;
  40. UI* ui = GetSubsystem<UI>();
  41. return ui->WrapWidget(child);
  42. }
  43. void UIWidget::SetWidget(tb::TBWidget* widget)
  44. {
  45. widget_ = widget;
  46. widget_->SetDelegate(this);
  47. }
  48. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  49. {
  50. UI* ui = GetSubsystem<UI>();
  51. String id;
  52. ui->GetTBIDString(ev.ref_id, id);
  53. using namespace WidgetEvent;
  54. data[P_HANDLER] = handler;
  55. data[P_TARGET] = target;
  56. data[P_TYPE] = (unsigned) ev.type;
  57. data[P_X] = ev.target_x;
  58. data[P_Y] = ev.target_y;
  59. data[P_DELTAX] = ev.delta_x;
  60. data[P_DELTAY] = ev.delta_y;
  61. data[P_COUNT] = ev.count;
  62. data[P_KEY] = ev.key;
  63. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  64. data[P_MODIFIERKEYS] = (unsigned) ev.modifierkeys;
  65. data[P_REFID] = id;
  66. data[P_TOUCH] = (unsigned) ev.touch;
  67. }
  68. void UIWidget::OnDelete()
  69. {
  70. if (widget_)
  71. {
  72. // if we don't have a UI subsystem, we are exiting
  73. UI* ui = GetSubsystem<UI>();
  74. if (ui)
  75. ui->UnwrapWidget(widget_);
  76. }
  77. widget_ = 0;
  78. VariantMap eventData;
  79. eventData[WidgetDeleted::P_WIDGET] = this;
  80. SendEvent(E_WIDGETDELETED, eventData);
  81. ReleaseRef();
  82. }
  83. void UIWidget::AddChild(UIWidget* child)
  84. {
  85. if (!widget_ || !child->widget_)
  86. return;
  87. widget_->AddChild(child->widget_);
  88. }
  89. void UIWidget::SetText(const String& text)
  90. {
  91. if (!widget_)
  92. return;
  93. widget_->SetText(text.CString());
  94. }
  95. void UIWidget::SetGravity(/*WIDGET_GRAVITY*/ unsigned gravity)
  96. {
  97. if (!widget_)
  98. return;
  99. widget_->SetGravity((WIDGET_GRAVITY) gravity);
  100. }
  101. void UIWidget::SetPosition(int x, int y)
  102. {
  103. if (!widget_)
  104. return;
  105. widget_->SetPosition(TBPoint(x, y));
  106. }
  107. IntRect UIWidget::GetRect()
  108. {
  109. IntRect rect(0, 0, 0, 0);
  110. if (!widget_)
  111. return rect;
  112. tb::TBRect tbrect = widget_->GetRect();
  113. rect.top_ = tbrect.y;
  114. rect.left_ = tbrect.x;
  115. rect.right_ = tbrect.x + tbrect.w;
  116. rect.bottom_ = tbrect.y + tbrect.h;
  117. return rect;
  118. }
  119. void UIWidget::SetRect(IntRect rect)
  120. {
  121. if (!widget_)
  122. return;
  123. tb::TBRect tbrect;
  124. tbrect.y = rect.top_;
  125. tbrect.x = rect.left_;
  126. tbrect.w = rect.right_ - rect.left_;
  127. tbrect.h = rect.bottom_ - rect.top_;
  128. widget_->SetRect(tbrect);
  129. }
  130. void UIWidget::SetSize(int width, int height)
  131. {
  132. if (!widget_)
  133. return;
  134. widget_->SetSize(width, height);
  135. }
  136. void UIWidget::Invalidate()
  137. {
  138. if (!widget_)
  139. return;
  140. widget_->Invalidate();
  141. }
  142. void UIWidget::Center()
  143. {
  144. if (!widget_)
  145. return;
  146. // this should center on parent widget, not root
  147. UI* ui = GetSubsystem<UI>();
  148. TBRect rect = widget_->GetRect();
  149. TBWidget* root = ui->GetRootWidget();
  150. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  151. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  152. }
  153. UIWidget* UIWidget::GetParent()
  154. {
  155. if (!widget_)
  156. return 0;
  157. TBWidget* parent = widget_->GetParent();
  158. if (!parent)
  159. return 0;
  160. UI* ui = GetSubsystem<UI>();
  161. return ui->WrapWidget(parent);
  162. }
  163. UIWidget* UIWidget::GetContentRoot()
  164. {
  165. if (!widget_)
  166. return 0;
  167. TBWidget* root = widget_->GetContentRoot();
  168. if (!root)
  169. return 0;
  170. UI* ui = GetSubsystem<UI>();
  171. return ui->WrapWidget(root);
  172. }
  173. void UIWidget::Die()
  174. {
  175. if (!widget_)
  176. return;
  177. // clear delegate
  178. widget_->SetDelegate(NULL);
  179. // explictly die (can trigger an animation)
  180. widget_->Die();
  181. // call OnDelete, which unwraps the widget and does some bookkeeping
  182. OnDelete();
  183. }
  184. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  185. {
  186. if (!widget_ || !child)
  187. return;
  188. TBWidget* childw = child->GetInternalWidget();
  189. if (!childw)
  190. return;
  191. widget_->RemoveChild(childw);
  192. if (cleanup)
  193. delete childw;
  194. }
  195. const String& UIWidget::GetId()
  196. {
  197. if (!widget_ || !widget_->GetID())
  198. {
  199. if (id_.Length())
  200. id_.Clear();
  201. return id_;
  202. }
  203. if (id_.Length())
  204. return id_;
  205. UI* ui = GetSubsystem<UI>();
  206. ui->GetTBIDString(widget_->GetID(), id_);
  207. return id_;
  208. }
  209. void UIWidget::SetId(const String& id)
  210. {
  211. if (!widget_)
  212. {
  213. if (id_.Length())
  214. id_.Clear();
  215. return;
  216. }
  217. id_ = id;
  218. widget_->SetID(TBIDC(id.CString()));
  219. }
  220. void UIWidget::SetState(/*WIDGET_STATE*/ unsigned state, bool on)
  221. {
  222. if (!widget_)
  223. return;
  224. widget_->SetState((WIDGET_STATE) state, on);
  225. }
  226. void UIWidget::SetValue(double value)
  227. {
  228. if (!widget_)
  229. return;
  230. widget_->SetValueDouble(value);
  231. }
  232. double UIWidget::GetValue()
  233. {
  234. if (!widget_)
  235. return 0.0;
  236. return widget_->GetValueDouble();
  237. }
  238. bool UIWidget::GetState(/*WIDGET_STATE*/ unsigned state)
  239. {
  240. if (!widget_)
  241. return false;
  242. return widget_->GetState((WIDGET_STATE) state);
  243. }
  244. void UIWidget::SetStateRaw(/*WIDGET_STATE*/ unsigned state)
  245. {
  246. if (!widget_)
  247. return;
  248. widget_->SetStateRaw((WIDGET_STATE) state);
  249. }
  250. /*WIDGET_STATE*/ unsigned UIWidget::GetStateRaw()
  251. {
  252. if (!widget_)
  253. return false;
  254. return (unsigned) widget_->GetStateRaw();
  255. }
  256. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  257. {
  258. UI* ui = GetSubsystem<UI>();
  259. if (ev.type == EVENT_TYPE_CHANGED)
  260. {
  261. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  262. {
  263. VariantMap eventData;
  264. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  265. SendEvent(E_WIDGETEVENT, eventData);
  266. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  267. return true;
  268. }
  269. }
  270. else if (ev.type == EVENT_TYPE_CLICK)
  271. {
  272. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  273. {
  274. // popup menu
  275. if (JSGetHeapPtr())
  276. {
  277. VariantMap eventData;
  278. eventData[PopupMenuSelect::P_BUTTON] = this;
  279. String id;
  280. ui->GetTBIDString(ev.ref_id, id);
  281. eventData[PopupMenuSelect::P_REFID] = id;
  282. SendEvent(E_POPUPMENUSELECT, eventData);
  283. }
  284. return true;
  285. }
  286. else
  287. {
  288. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  289. {
  290. VariantMap eventData;
  291. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  292. SendEvent(E_WIDGETEVENT, eventData);
  293. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  294. return true;
  295. }
  296. }
  297. }
  298. return false;
  299. }
  300. }