UIWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. #include "UILayout.h"
  7. #include "UIFontDescription.h"
  8. #include "UIView.h"
  9. using namespace tb;
  10. namespace Atomic
  11. {
  12. UIWidget::UIWidget(Context* context, bool createWidget) : Object(context),
  13. widget_(0),
  14. preferredSize_(new UIPreferredSize())
  15. {
  16. AddRef();
  17. if (createWidget)
  18. {
  19. widget_ = new TBWidget();
  20. widget_->SetDelegate(this);
  21. GetSubsystem<UI>()->WrapWidget(this, widget_);
  22. }
  23. }
  24. UIWidget::~UIWidget()
  25. {
  26. }
  27. void UIWidget::SetIsFocusable(bool value)
  28. {
  29. if (!widget_)
  30. return;
  31. widget_->SetIsFocusable(value);
  32. }
  33. bool UIWidget::Load(const String& filename)
  34. {
  35. UI* ui = GetSubsystem<UI>();
  36. if (!ui->LoadResourceFile(widget_ , filename))
  37. return false;
  38. VariantMap eventData;
  39. eventData[WidgetLoaded::P_WIDGET] = this;
  40. SendEvent(E_WIDGETLOADED, eventData);
  41. return true;
  42. }
  43. UIPreferredSize* UIWidget::GetPreferredSize()
  44. {
  45. // error
  46. if (!widget_)
  47. return preferredSize_;
  48. preferredSize_->SetFromTBPreferredSize(widget_->GetPreferredSize());
  49. return preferredSize_;
  50. }
  51. UIWidget* UIWidget::GetWidget(const String& id)
  52. {
  53. if (!widget_)
  54. return 0;
  55. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  56. if (!child)
  57. return 0;
  58. UI* ui = GetSubsystem<UI>();
  59. return ui->WrapWidget(child);
  60. }
  61. void UIWidget::SetWidget(tb::TBWidget* widget)
  62. {
  63. widget_ = widget;
  64. widget_->SetDelegate(this);
  65. }
  66. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  67. {
  68. UI* ui = GetSubsystem<UI>();
  69. String refid;
  70. ui->GetTBIDString(ev.ref_id, refid);
  71. using namespace WidgetEvent;
  72. data[P_HANDLER] = handler;
  73. data[P_TARGET] = target;
  74. data[P_TYPE] = (unsigned) ev.type;
  75. data[P_X] = ev.target_x;
  76. data[P_Y] = ev.target_y;
  77. data[P_DELTAX] = ev.delta_x;
  78. data[P_DELTAY] = ev.delta_y;
  79. data[P_COUNT] = ev.count;
  80. data[P_KEY] = ev.key;
  81. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  82. data[P_MODIFIERKEYS] = (unsigned) ev.modifierkeys;
  83. data[P_REFID] = refid;
  84. data[P_TOUCH] = (unsigned) ev.touch;
  85. }
  86. void UIWidget::OnDelete()
  87. {
  88. if (widget_)
  89. {
  90. // if we don't have a UI subsystem, we are exiting
  91. UI* ui = GetSubsystem<UI>();
  92. if (ui)
  93. ui->UnwrapWidget(widget_);
  94. }
  95. widget_ = 0;
  96. VariantMap eventData;
  97. eventData[WidgetDeleted::P_WIDGET] = this;
  98. SendEvent(E_WIDGETDELETED, eventData);
  99. ReleaseRef();
  100. }
  101. void UIWidget::AddChild(UIWidget* child)
  102. {
  103. if (!widget_ || !child || !child->widget_)
  104. return;
  105. widget_->AddChild(child->widget_);
  106. }
  107. void UIWidget::AddChildRelative(UIWidget* child, UI_WIDGET_Z_REL z, UIWidget* reference)
  108. {
  109. if (!widget_ || !child || !child->widget_ || !reference || !reference->widget_)
  110. return;
  111. widget_->AddChildRelative(child->widget_, (WIDGET_Z_REL) z, reference->widget_);
  112. }
  113. String UIWidget::GetText()
  114. {
  115. if (!widget_)
  116. return "";
  117. return widget_->GetText().CStr();
  118. }
  119. void UIWidget::SetText(const String& text)
  120. {
  121. if (!widget_)
  122. return;
  123. widget_->SetText(text.CString());
  124. }
  125. void UIWidget::SetGravity(UI_GRAVITY gravity)
  126. {
  127. if (!widget_)
  128. return;
  129. widget_->SetGravity((WIDGET_GRAVITY) gravity);
  130. }
  131. bool UIWidget::IsAncestorOf(UIWidget* widget)
  132. {
  133. if (!widget_ || !widget || !widget->widget_)
  134. return false;
  135. return widget_->IsAncestorOf(widget->widget_);
  136. }
  137. void UIWidget::SetPosition(int x, int y)
  138. {
  139. if (!widget_)
  140. return;
  141. widget_->SetPosition(TBPoint(x, y));
  142. }
  143. IntRect UIWidget::GetRect()
  144. {
  145. IntRect rect(0, 0, 0, 0);
  146. if (!widget_)
  147. return rect;
  148. tb::TBRect tbrect = widget_->GetRect();
  149. rect.top_ = tbrect.y;
  150. rect.left_ = tbrect.x;
  151. rect.right_ = tbrect.x + tbrect.w;
  152. rect.bottom_ = tbrect.y + tbrect.h;
  153. return rect;
  154. }
  155. void UIWidget::SetRect(IntRect rect)
  156. {
  157. if (!widget_)
  158. return;
  159. tb::TBRect tbrect;
  160. tbrect.y = rect.top_;
  161. tbrect.x = rect.left_;
  162. tbrect.w = rect.right_ - rect.left_;
  163. tbrect.h = rect.bottom_ - rect.top_;
  164. widget_->SetRect(tbrect);
  165. }
  166. void UIWidget::SetSize(int width, int height)
  167. {
  168. if (!widget_)
  169. return;
  170. widget_->SetSize(width, height);
  171. }
  172. void UIWidget::Invalidate()
  173. {
  174. if (!widget_)
  175. return;
  176. widget_->Invalidate();
  177. }
  178. void UIWidget::Center()
  179. {
  180. if (!widget_)
  181. return;
  182. // this should center on parent widget, not root
  183. UI* ui = GetSubsystem<UI>();
  184. TBRect rect = widget_->GetRect();
  185. TBWidget* root = ui->GetRootWidget();
  186. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  187. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  188. }
  189. UIWidget* UIWidget::GetParent()
  190. {
  191. if (!widget_)
  192. return 0;
  193. TBWidget* parent = widget_->GetParent();
  194. if (!parent)
  195. return 0;
  196. UI* ui = GetSubsystem<UI>();
  197. return ui->WrapWidget(parent);
  198. }
  199. UIWidget* UIWidget::GetContentRoot()
  200. {
  201. if (!widget_)
  202. return 0;
  203. TBWidget* root = widget_->GetContentRoot();
  204. if (!root)
  205. return 0;
  206. UI* ui = GetSubsystem<UI>();
  207. return ui->WrapWidget(root);
  208. }
  209. void UIWidget::Die()
  210. {
  211. if (!widget_)
  212. return;
  213. // clear delegate
  214. widget_->SetDelegate(NULL);
  215. // explictly die (can trigger an animation)
  216. widget_->Die();
  217. // call OnDelete, which unwraps the widget and does some bookkeeping
  218. OnDelete();
  219. }
  220. void UIWidget::SetLayoutParams(UILayoutParams* params)
  221. {
  222. if (!widget_)
  223. return;
  224. widget_->SetLayoutParams(*(params->GetTBLayoutParams()));
  225. }
  226. void UIWidget::SetFontDescription(UIFontDescription* fd)
  227. {
  228. if (!widget_)
  229. return;
  230. widget_->SetFontDescription(*(fd->GetTBFontDescription()));
  231. }
  232. void UIWidget::DeleteAllChildren()
  233. {
  234. if (!widget_)
  235. return;
  236. widget_->DeleteAllChildren();
  237. }
  238. void UIWidget::SetSkinBg(const String& id)
  239. {
  240. if (!widget_)
  241. return;
  242. widget_->SetSkinBg(TBIDC(id.CString()));
  243. }
  244. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  245. {
  246. if (!widget_ || !child)
  247. return;
  248. TBWidget* childw = child->GetInternalWidget();
  249. if (!childw)
  250. return;
  251. widget_->RemoveChild(childw);
  252. if (cleanup)
  253. delete childw;
  254. }
  255. const String& UIWidget::GetId()
  256. {
  257. if (!widget_ || !widget_->GetID())
  258. {
  259. if (id_.Length())
  260. id_.Clear();
  261. return id_;
  262. }
  263. if (id_.Length())
  264. return id_;
  265. UI* ui = GetSubsystem<UI>();
  266. ui->GetTBIDString(widget_->GetID(), id_);
  267. return id_;
  268. }
  269. void UIWidget::SetId(const String& id)
  270. {
  271. if (!widget_)
  272. {
  273. if (id_.Length())
  274. id_.Clear();
  275. return;
  276. }
  277. id_ = id;
  278. widget_->SetID(TBIDC(id.CString()));
  279. }
  280. void UIWidget::SetState(/*WIDGET_STATE*/ unsigned state, bool on)
  281. {
  282. if (!widget_)
  283. return;
  284. widget_->SetState((WIDGET_STATE) state, on);
  285. }
  286. void UIWidget::SetFocus()
  287. {
  288. if (!widget_)
  289. return;
  290. widget_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  291. }
  292. void UIWidget::SetVisibility(UI_WIDGET_VISIBILITY visibility)
  293. {
  294. if (!widget_)
  295. return;
  296. widget_->SetVisibilility((WIDGET_VISIBILITY) visibility);
  297. }
  298. UI_WIDGET_VISIBILITY UIWidget::GetVisibility()
  299. {
  300. if (!widget_)
  301. return UI_WIDGET_VISIBILITY_GONE;
  302. return (UI_WIDGET_VISIBILITY) widget_->GetVisibility();
  303. }
  304. UIWidget* UIWidget::GetFirstChild()
  305. {
  306. if (!widget_)
  307. return NULL;
  308. return GetSubsystem<UI>()->WrapWidget(widget_->GetFirstChild());
  309. }
  310. UIWidget* UIWidget::GetNext()
  311. {
  312. if (!widget_)
  313. return NULL;
  314. return GetSubsystem<UI>()->WrapWidget(widget_->GetNext());
  315. }
  316. void UIWidget::SetValue(double value)
  317. {
  318. if (!widget_)
  319. return;
  320. widget_->SetValueDouble(value);
  321. }
  322. double UIWidget::GetValue()
  323. {
  324. if (!widget_)
  325. return 0.0;
  326. return widget_->GetValueDouble();
  327. }
  328. bool UIWidget::GetState(/*WIDGET_STATE*/ unsigned state)
  329. {
  330. if (!widget_)
  331. return false;
  332. return widget_->GetState((WIDGET_STATE) state);
  333. }
  334. void UIWidget::SetStateRaw(/*WIDGET_STATE*/ unsigned state)
  335. {
  336. if (!widget_)
  337. return;
  338. widget_->SetStateRaw((WIDGET_STATE) state);
  339. }
  340. /*WIDGET_STATE*/ unsigned UIWidget::GetStateRaw()
  341. {
  342. if (!widget_)
  343. return false;
  344. return (unsigned) widget_->GetStateRaw();
  345. }
  346. UIView* UIWidget::GetView()
  347. {
  348. if (!widget_)
  349. return 0;
  350. if (GetType() == UIView::GetTypeStatic())
  351. return (UIView*) this;
  352. TBWidget* tbw = widget_->GetParent();
  353. while(tbw)
  354. {
  355. TBWidgetDelegate* delegate = tbw->GetDelegate();
  356. if (delegate)
  357. {
  358. UIWidget* d = (UIWidget*) delegate;
  359. if (d->GetType() == UIView::GetTypeStatic())
  360. return (UIView*) d;
  361. }
  362. tbw = tbw->GetParent();
  363. }
  364. return 0;
  365. }
  366. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  367. {
  368. UI* ui = GetSubsystem<UI>();
  369. if (ev.type == EVENT_TYPE_CHANGED)
  370. {
  371. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  372. {
  373. VariantMap eventData;
  374. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  375. SendEvent(E_WIDGETEVENT, eventData);
  376. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  377. return true;
  378. }
  379. }
  380. else if (ev.type == EVENT_TYPE_TAB_CHANGED)
  381. {
  382. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  383. {
  384. VariantMap eventData;
  385. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  386. SendEvent(E_WIDGETEVENT, eventData);
  387. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  388. return true;
  389. }
  390. }
  391. else if (ev.type == EVENT_TYPE_CLICK)
  392. {
  393. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  394. {
  395. // popup menu
  396. if (JSGetHeapPtr())
  397. {
  398. VariantMap eventData;
  399. eventData[PopupMenuSelect::P_BUTTON] = this;
  400. String id;
  401. ui->GetTBIDString(ev.ref_id, id);
  402. eventData[PopupMenuSelect::P_REFID] = id;
  403. SendEvent(E_POPUPMENUSELECT, eventData);
  404. }
  405. return true;
  406. }
  407. else
  408. {
  409. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  410. {
  411. VariantMap eventData;
  412. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  413. SendEvent(E_WIDGETEVENT, eventData);
  414. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  415. return true;
  416. }
  417. }
  418. }
  419. return false;
  420. }
  421. }