UIWidget.cpp 13 KB

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