UIWidget.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. //--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"
  23. #include "../IO/Log.h"
  24. #include "../Input/InputEvents.h"
  25. #include "UIEvents.h"
  26. #include "UI.h"
  27. #include "UIWidget.h"
  28. #include "UILayout.h"
  29. #include "UIFontDescription.h"
  30. #include "UIView.h"
  31. using namespace tb;
  32. namespace Atomic
  33. {
  34. UIWidget::UIWidget(Context* context, bool createWidget) : Object(context),
  35. widget_(0),
  36. preferredSize_(new UIPreferredSize()),
  37. multiTouch_(false)
  38. {
  39. AddRef();
  40. if (createWidget)
  41. {
  42. widget_ = new TBWidget();
  43. widget_->SetDelegate(this);
  44. GetSubsystem<UI>()->WrapWidget(this, widget_);
  45. }
  46. }
  47. UIWidget::~UIWidget()
  48. {
  49. }
  50. void UIWidget::SetIsFocusable(bool value)
  51. {
  52. if (!widget_)
  53. return;
  54. widget_->SetIsFocusable(value);
  55. }
  56. bool UIWidget::Load(const String& filename)
  57. {
  58. UI* ui = GetSubsystem<UI>();
  59. if (!ui->LoadResourceFile(widget_ , filename))
  60. return false;
  61. VariantMap eventData;
  62. eventData[WidgetLoaded::P_WIDGET] = this;
  63. SendEvent(E_WIDGETLOADED, eventData);
  64. return true;
  65. }
  66. UIPreferredSize* UIWidget::GetPreferredSize()
  67. {
  68. // error
  69. if (!widget_)
  70. return preferredSize_;
  71. preferredSize_->SetFromTBPreferredSize(widget_->GetPreferredSize());
  72. return preferredSize_;
  73. }
  74. UIWidget* UIWidget::GetWidget(const String& id)
  75. {
  76. if (!widget_)
  77. return 0;
  78. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  79. if (!child)
  80. return 0;
  81. UI* ui = GetSubsystem<UI>();
  82. return ui->WrapWidget(child);
  83. }
  84. void UIWidget::SetWidget(tb::TBWidget* widget)
  85. {
  86. widget_ = widget;
  87. widget_->SetDelegate(this);
  88. }
  89. /*
  90. enum SPECIAL_KEY
  91. {
  92. TB_KEY_UNDEFINED = 0,
  93. TB_KEY_UP, TB_KEY_DOWN, TB_KEY_LEFT, TB_KEY_RIGHT,
  94. TB_KEY_PAGE_UP, TB_KEY_PAGE_DOWN, TB_KEY_HOME, TB_KEY_END,
  95. TB_KEY_TAB, TB_KEY_BACKSPACE, TB_KEY_INSERT, TB_KEY_DELETE,
  96. TB_KEY_ENTER, TB_KEY_ESC,
  97. TB_KEY_F1, TB_KEY_F2, TB_KEY_F3, TB_KEY_F4, TB_KEY_F5, TB_KEY_F6,
  98. TB_KEY_F7, TB_KEY_F8, TB_KEY_F9, TB_KEY_F10, TB_KEY_F11, TB_KEY_F12
  99. };
  100. */
  101. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  102. {
  103. UI* ui = GetSubsystem<UI>();
  104. String refid;
  105. ui->GetTBIDString(ev.ref_id, refid);
  106. int key = ev.key;
  107. if (ev.special_key)
  108. {
  109. switch (ev.special_key)
  110. {
  111. case TB_KEY_ENTER:
  112. key = KEY_RETURN;
  113. break;
  114. case TB_KEY_BACKSPACE:
  115. key = KEY_BACKSPACE;
  116. break;
  117. case TB_KEY_DELETE:
  118. key = KEY_DELETE;
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. using namespace WidgetEvent;
  125. data[P_HANDLER] = handler;
  126. data[P_TARGET] = target;
  127. data[P_TYPE] = (unsigned) ev.type;
  128. data[P_X] = ev.target_x;
  129. data[P_Y] = ev.target_y;
  130. data[P_DELTAX] = ev.delta_x;
  131. data[P_DELTAY] = ev.delta_y;
  132. data[P_COUNT] = ev.count;
  133. data[P_KEY] = key;
  134. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  135. data[P_MODIFIERKEYS] = (unsigned) ev.modifierkeys;
  136. data[P_REFID] = refid;
  137. data[P_TOUCH] = (unsigned) ev.touch;
  138. }
  139. void UIWidget::OnDelete()
  140. {
  141. if (widget_)
  142. {
  143. // if we don't have a UI subsystem, we are exiting
  144. UI* ui = GetSubsystem<UI>();
  145. if (ui)
  146. ui->UnwrapWidget(widget_);
  147. }
  148. widget_ = 0;
  149. VariantMap eventData;
  150. eventData[WidgetDeleted::P_WIDGET] = this;
  151. SendEvent(E_WIDGETDELETED, eventData);
  152. UnsubscribeFromAllEvents();
  153. ReleaseRef();
  154. }
  155. void UIWidget::AddChildAfter(UIWidget* child, UIWidget* otherChild)
  156. {
  157. if (!widget_ || !child || !child->widget_ || !otherChild || !otherChild->widget_)
  158. return;
  159. widget_->AddChildRelative(child->widget_, tb::WIDGET_Z_REL_AFTER, otherChild->widget_);
  160. }
  161. void UIWidget::AddChildBefore(UIWidget* child, UIWidget* otherChild)
  162. {
  163. if (!widget_ || !child || !child->widget_ || !otherChild || !otherChild->widget_)
  164. return;
  165. widget_->AddChildRelative(child->widget_, tb::WIDGET_Z_REL_BEFORE, otherChild->widget_);
  166. }
  167. void UIWidget::AddChild(UIWidget* child)
  168. {
  169. if (!widget_ || !child || !child->widget_)
  170. return;
  171. widget_->AddChild(child->widget_);
  172. }
  173. void UIWidget::AddChildRelative(UIWidget* child, UI_WIDGET_Z_REL z, UIWidget* reference)
  174. {
  175. if (!widget_ || !child || !child->widget_ || !reference || !reference->widget_)
  176. return;
  177. widget_->AddChildRelative(child->widget_, (WIDGET_Z_REL) z, reference->widget_);
  178. }
  179. String UIWidget::GetText()
  180. {
  181. if (!widget_)
  182. return "";
  183. return widget_->GetText().CStr();
  184. }
  185. void UIWidget::SetText(const String& text)
  186. {
  187. if (!widget_)
  188. return;
  189. widget_->SetText(text.CString());
  190. }
  191. void UIWidget::SetGravity(UI_GRAVITY gravity)
  192. {
  193. if (!widget_)
  194. return;
  195. widget_->SetGravity((WIDGET_GRAVITY) gravity);
  196. }
  197. bool UIWidget::IsAncestorOf(UIWidget* widget)
  198. {
  199. if (!widget_ || !widget || !widget->widget_)
  200. return false;
  201. return widget_->IsAncestorOf(widget->widget_);
  202. }
  203. void UIWidget::SetPosition(int x, int y)
  204. {
  205. if (!widget_)
  206. return;
  207. widget_->SetPosition(TBPoint(x, y));
  208. }
  209. IntRect UIWidget::GetRect()
  210. {
  211. IntRect rect(0, 0, 0, 0);
  212. if (!widget_)
  213. return rect;
  214. tb::TBRect tbrect = widget_->GetRect();
  215. rect.top_ = tbrect.y;
  216. rect.left_ = tbrect.x;
  217. rect.right_ = tbrect.x + tbrect.w;
  218. rect.bottom_ = tbrect.y + tbrect.h;
  219. return rect;
  220. }
  221. void UIWidget::SetRect(IntRect rect)
  222. {
  223. if (!widget_)
  224. return;
  225. tb::TBRect tbrect;
  226. tbrect.y = rect.top_;
  227. tbrect.x = rect.left_;
  228. tbrect.w = rect.right_ - rect.left_;
  229. tbrect.h = rect.bottom_ - rect.top_;
  230. widget_->SetRect(tbrect);
  231. }
  232. void UIWidget::SetSize(int width, int height)
  233. {
  234. if (!widget_)
  235. return;
  236. widget_->SetSize(width, height);
  237. }
  238. void UIWidget::Invalidate()
  239. {
  240. if (!widget_)
  241. return;
  242. widget_->Invalidate();
  243. }
  244. void UIWidget::Center()
  245. {
  246. if (!widget_)
  247. return;
  248. TBRect rect = widget_->GetRect();
  249. TBWidget* root = widget_->GetParent();
  250. if (!root)
  251. {
  252. UI* ui = GetSubsystem<UI>();
  253. root = ui->GetRootWidget();
  254. }
  255. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  256. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  257. }
  258. UIWidget* UIWidget::GetParent()
  259. {
  260. if (!widget_)
  261. return 0;
  262. TBWidget* parent = widget_->GetParent();
  263. if (!parent)
  264. return 0;
  265. UI* ui = GetSubsystem<UI>();
  266. return ui->WrapWidget(parent);
  267. }
  268. UIWidget* UIWidget::GetContentRoot()
  269. {
  270. if (!widget_)
  271. return 0;
  272. TBWidget* root = widget_->GetContentRoot();
  273. if (!root)
  274. return 0;
  275. UI* ui = GetSubsystem<UI>();
  276. return ui->WrapWidget(root);
  277. }
  278. void UIWidget::Die()
  279. {
  280. if (!widget_)
  281. return;
  282. // clear delegate
  283. widget_->SetDelegate(NULL);
  284. // explictly die (can trigger an animation)
  285. widget_->Die();
  286. // call OnDelete, which unwraps the widget and does some bookkeeping
  287. OnDelete();
  288. }
  289. void UIWidget::SetLayoutParams(UILayoutParams* params)
  290. {
  291. if (!widget_)
  292. return;
  293. widget_->SetLayoutParams(*(params->GetTBLayoutParams()));
  294. }
  295. void UIWidget::SetFontDescription(UIFontDescription* fd)
  296. {
  297. if (!widget_)
  298. return;
  299. widget_->SetFontDescription(*(fd->GetTBFontDescription()));
  300. }
  301. void UIWidget::DeleteAllChildren()
  302. {
  303. if (!widget_)
  304. return;
  305. widget_->DeleteAllChildren();
  306. }
  307. void UIWidget::SetSkinBg(const String& id)
  308. {
  309. if (!widget_)
  310. return;
  311. widget_->SetSkinBg(TBIDC(id.CString()));
  312. }
  313. void UIWidget::Remove()
  314. {
  315. if (!widget_ || !widget_->GetParent())
  316. return;
  317. widget_->GetParent()->RemoveChild(widget_);
  318. }
  319. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  320. {
  321. if (!widget_ || !child)
  322. return;
  323. TBWidget* childw = child->GetInternalWidget();
  324. if (!childw)
  325. return;
  326. widget_->RemoveChild(childw);
  327. if (cleanup)
  328. delete childw;
  329. }
  330. const String& UIWidget::GetId()
  331. {
  332. if (!widget_ || !widget_->GetID())
  333. {
  334. if (id_.Length())
  335. id_.Clear();
  336. return id_;
  337. }
  338. if (id_.Length())
  339. return id_;
  340. UI* ui = GetSubsystem<UI>();
  341. ui->GetTBIDString(widget_->GetID(), id_);
  342. return id_;
  343. }
  344. void UIWidget::SetId(const String& id)
  345. {
  346. if (!widget_)
  347. {
  348. if (id_.Length())
  349. id_.Clear();
  350. return;
  351. }
  352. id_ = id;
  353. widget_->SetID(TBIDC(id.CString()));
  354. }
  355. void UIWidget::SetState(UI_WIDGET_STATE state, bool on)
  356. {
  357. if (!widget_)
  358. return;
  359. widget_->SetState((WIDGET_STATE) state, on);
  360. }
  361. void UIWidget::SetFocus()
  362. {
  363. if (!widget_)
  364. return;
  365. widget_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  366. }
  367. bool UIWidget::GetFocus()
  368. {
  369. if (!widget_)
  370. return false;
  371. return widget_->GetIsFocused();
  372. }
  373. void UIWidget::SetFocusRecursive()
  374. {
  375. if (!widget_)
  376. return;
  377. widget_->SetFocusRecursive(WIDGET_FOCUS_REASON_UNKNOWN);
  378. }
  379. void UIWidget::SetVisibility(UI_WIDGET_VISIBILITY visibility)
  380. {
  381. if (!widget_)
  382. return;
  383. widget_->SetVisibilility((WIDGET_VISIBILITY) visibility);
  384. }
  385. UI_WIDGET_VISIBILITY UIWidget::GetVisibility()
  386. {
  387. if (!widget_)
  388. return UI_WIDGET_VISIBILITY_GONE;
  389. return (UI_WIDGET_VISIBILITY) widget_->GetVisibility();
  390. }
  391. UIWidget* UIWidget::GetFirstChild()
  392. {
  393. if (!widget_)
  394. return NULL;
  395. return GetSubsystem<UI>()->WrapWidget(widget_->GetFirstChild());
  396. }
  397. UIWidget* UIWidget::GetNext()
  398. {
  399. if (!widget_)
  400. return NULL;
  401. return GetSubsystem<UI>()->WrapWidget(widget_->GetNext());
  402. }
  403. void UIWidget::SetValue(double value)
  404. {
  405. if (!widget_)
  406. return;
  407. widget_->SetValueDouble(value);
  408. }
  409. double UIWidget::GetValue()
  410. {
  411. if (!widget_)
  412. return 0.0;
  413. return widget_->GetValueDouble();
  414. }
  415. bool UIWidget::GetState(UI_WIDGET_STATE state)
  416. {
  417. if (!widget_)
  418. return false;
  419. return widget_->GetState((WIDGET_STATE) state);
  420. }
  421. void UIWidget::SetStateRaw(UI_WIDGET_STATE state)
  422. {
  423. if (!widget_)
  424. return;
  425. widget_->SetStateRaw((WIDGET_STATE) state);
  426. }
  427. UI_WIDGET_STATE UIWidget::GetStateRaw()
  428. {
  429. if (!widget_)
  430. return UI_WIDGET_STATE_NONE;
  431. return (UI_WIDGET_STATE) widget_->GetStateRaw();
  432. }
  433. UIView* UIWidget::GetView()
  434. {
  435. if (!widget_)
  436. return 0;
  437. if (GetType() == UIView::GetTypeStatic())
  438. return (UIView*) this;
  439. TBWidget* tbw = widget_->GetParent();
  440. while(tbw)
  441. {
  442. TBWidgetDelegate* delegate = tbw->GetDelegate();
  443. if (delegate)
  444. {
  445. UIWidget* d = (UIWidget*) delegate;
  446. if (d->GetType() == UIView::GetTypeStatic())
  447. return (UIView*) d;
  448. }
  449. tbw = tbw->GetParent();
  450. }
  451. return 0;
  452. }
  453. void UIWidget::OnFocusChanged(bool focused)
  454. {
  455. using namespace WidgetFocusChanged;
  456. VariantMap eventData;
  457. eventData[P_WIDGET] = this;
  458. eventData[P_FOCUSED] = focused;
  459. SendEvent(E_WIDGETFOCUSCHANGED, eventData);
  460. }
  461. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  462. {
  463. UI* ui = GetSubsystem<UI>();
  464. if (ev.type == EVENT_TYPE_CHANGED || ev.type == EVENT_TYPE_KEY_UP)
  465. {
  466. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  467. {
  468. VariantMap eventData;
  469. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  470. SendEvent(E_WIDGETEVENT, eventData);
  471. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  472. return true;
  473. }
  474. }
  475. else if (ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  476. {
  477. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  478. {
  479. VariantMap eventData;
  480. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  481. SendEvent(E_WIDGETEVENT, eventData);
  482. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  483. return true;
  484. }
  485. }
  486. else if (ev.type == EVENT_TYPE_POINTER_DOWN)
  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. else if (ev.type == EVENT_TYPE_SHORTCUT)
  498. {
  499. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  500. {
  501. VariantMap eventData;
  502. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  503. SendEvent(E_WIDGETEVENT, eventData);
  504. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  505. return true;
  506. }
  507. }
  508. else if (ev.type == EVENT_TYPE_TAB_CHANGED)
  509. {
  510. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  511. {
  512. VariantMap eventData;
  513. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  514. SendEvent(E_WIDGETEVENT, eventData);
  515. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  516. return true;
  517. }
  518. }
  519. else if (ev.type == EVENT_TYPE_CLICK)
  520. {
  521. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  522. {
  523. // popup menu
  524. if (JSGetHeapPtr())
  525. {
  526. VariantMap eventData;
  527. eventData[PopupMenuSelect::P_BUTTON] = this;
  528. String id;
  529. ui->GetTBIDString(ev.ref_id, id);
  530. eventData[PopupMenuSelect::P_REFID] = id;
  531. SendEvent(E_POPUPMENUSELECT, eventData);
  532. }
  533. return true;
  534. }
  535. else
  536. {
  537. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  538. {
  539. VariantMap eventData;
  540. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  541. SendEvent(E_WIDGETEVENT, eventData);
  542. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  543. return true;
  544. }
  545. }
  546. }
  547. return false;
  548. }
  549. bool UIWidget::GetCaptured()
  550. {
  551. if (!widget_)
  552. return false;
  553. return widget_->IsCaptured();
  554. }
  555. void UIWidget::SetCapturing(bool capturing)
  556. {
  557. if (!widget_)
  558. return;
  559. widget_->SetCapturing(capturing);
  560. }
  561. void UIWidget::InvalidateLayout()
  562. {
  563. if (!widget_)
  564. return;
  565. widget_->InvalidateLayout(tb::TBWidget::INVALIDATE_LAYOUT_TARGET_ONLY);
  566. }
  567. void UIWidget::InvokeShortcut(const String& shortcut)
  568. {
  569. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  570. ev.ref_id = TBIDC(shortcut.CString());
  571. widget_->OnEvent(ev);
  572. }
  573. }