UIWidget.cpp 13 KB

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