UIWidget.cpp 15 KB

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