UIWidget.cpp 15 KB

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