UIWidget.cpp 16 KB

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