UIWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. // this should center on parent widget, not root
  215. UI* ui = GetSubsystem<UI>();
  216. TBRect rect = widget_->GetRect();
  217. TBWidget* root = ui->GetRootWidget();
  218. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  219. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  220. }
  221. UIWidget* UIWidget::GetParent()
  222. {
  223. if (!widget_)
  224. return 0;
  225. TBWidget* parent = widget_->GetParent();
  226. if (!parent)
  227. return 0;
  228. UI* ui = GetSubsystem<UI>();
  229. return ui->WrapWidget(parent);
  230. }
  231. UIWidget* UIWidget::GetContentRoot()
  232. {
  233. if (!widget_)
  234. return 0;
  235. TBWidget* root = widget_->GetContentRoot();
  236. if (!root)
  237. return 0;
  238. UI* ui = GetSubsystem<UI>();
  239. return ui->WrapWidget(root);
  240. }
  241. void UIWidget::Die()
  242. {
  243. if (!widget_)
  244. return;
  245. // clear delegate
  246. widget_->SetDelegate(NULL);
  247. // explictly die (can trigger an animation)
  248. widget_->Die();
  249. // call OnDelete, which unwraps the widget and does some bookkeeping
  250. OnDelete();
  251. }
  252. void UIWidget::SetLayoutParams(UILayoutParams* params)
  253. {
  254. if (!widget_)
  255. return;
  256. widget_->SetLayoutParams(*(params->GetTBLayoutParams()));
  257. }
  258. void UIWidget::SetFontDescription(UIFontDescription* fd)
  259. {
  260. if (!widget_)
  261. return;
  262. widget_->SetFontDescription(*(fd->GetTBFontDescription()));
  263. }
  264. void UIWidget::DeleteAllChildren()
  265. {
  266. if (!widget_)
  267. return;
  268. widget_->DeleteAllChildren();
  269. }
  270. void UIWidget::SetSkinBg(const String& id)
  271. {
  272. if (!widget_)
  273. return;
  274. widget_->SetSkinBg(TBIDC(id.CString()));
  275. }
  276. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  277. {
  278. if (!widget_ || !child)
  279. return;
  280. TBWidget* childw = child->GetInternalWidget();
  281. if (!childw)
  282. return;
  283. widget_->RemoveChild(childw);
  284. if (cleanup)
  285. delete childw;
  286. }
  287. const String& UIWidget::GetId()
  288. {
  289. if (!widget_ || !widget_->GetID())
  290. {
  291. if (id_.Length())
  292. id_.Clear();
  293. return id_;
  294. }
  295. if (id_.Length())
  296. return id_;
  297. UI* ui = GetSubsystem<UI>();
  298. ui->GetTBIDString(widget_->GetID(), id_);
  299. return id_;
  300. }
  301. void UIWidget::SetId(const String& id)
  302. {
  303. if (!widget_)
  304. {
  305. if (id_.Length())
  306. id_.Clear();
  307. return;
  308. }
  309. id_ = id;
  310. widget_->SetID(TBIDC(id.CString()));
  311. }
  312. void UIWidget::SetState(/*WIDGET_STATE*/ unsigned state, bool on)
  313. {
  314. if (!widget_)
  315. return;
  316. widget_->SetState((WIDGET_STATE) state, on);
  317. }
  318. void UIWidget::SetFocus()
  319. {
  320. if (!widget_)
  321. return;
  322. widget_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  323. }
  324. bool UIWidget::GetFocus()
  325. {
  326. if (!widget_)
  327. return false;
  328. return widget_->GetIsFocused();
  329. }
  330. void UIWidget::SetFocusRecursive()
  331. {
  332. if (!widget_)
  333. return;
  334. widget_->SetFocusRecursive(WIDGET_FOCUS_REASON_UNKNOWN);
  335. }
  336. void UIWidget::SetVisibility(UI_WIDGET_VISIBILITY visibility)
  337. {
  338. if (!widget_)
  339. return;
  340. widget_->SetVisibilility((WIDGET_VISIBILITY) visibility);
  341. }
  342. UI_WIDGET_VISIBILITY UIWidget::GetVisibility()
  343. {
  344. if (!widget_)
  345. return UI_WIDGET_VISIBILITY_GONE;
  346. return (UI_WIDGET_VISIBILITY) widget_->GetVisibility();
  347. }
  348. UIWidget* UIWidget::GetFirstChild()
  349. {
  350. if (!widget_)
  351. return NULL;
  352. return GetSubsystem<UI>()->WrapWidget(widget_->GetFirstChild());
  353. }
  354. UIWidget* UIWidget::GetNext()
  355. {
  356. if (!widget_)
  357. return NULL;
  358. return GetSubsystem<UI>()->WrapWidget(widget_->GetNext());
  359. }
  360. void UIWidget::SetValue(double value)
  361. {
  362. if (!widget_)
  363. return;
  364. widget_->SetValueDouble(value);
  365. }
  366. double UIWidget::GetValue()
  367. {
  368. if (!widget_)
  369. return 0.0;
  370. return widget_->GetValueDouble();
  371. }
  372. bool UIWidget::GetState(/*WIDGET_STATE*/ unsigned state)
  373. {
  374. if (!widget_)
  375. return false;
  376. return widget_->GetState((WIDGET_STATE) state);
  377. }
  378. void UIWidget::SetStateRaw(/*WIDGET_STATE*/ unsigned state)
  379. {
  380. if (!widget_)
  381. return;
  382. widget_->SetStateRaw((WIDGET_STATE) state);
  383. }
  384. /*WIDGET_STATE*/ unsigned UIWidget::GetStateRaw()
  385. {
  386. if (!widget_)
  387. return false;
  388. return (unsigned) widget_->GetStateRaw();
  389. }
  390. UIView* UIWidget::GetView()
  391. {
  392. if (!widget_)
  393. return 0;
  394. if (GetType() == UIView::GetTypeStatic())
  395. return (UIView*) this;
  396. TBWidget* tbw = widget_->GetParent();
  397. while(tbw)
  398. {
  399. TBWidgetDelegate* delegate = tbw->GetDelegate();
  400. if (delegate)
  401. {
  402. UIWidget* d = (UIWidget*) delegate;
  403. if (d->GetType() == UIView::GetTypeStatic())
  404. return (UIView*) d;
  405. }
  406. tbw = tbw->GetParent();
  407. }
  408. return 0;
  409. }
  410. void UIWidget::OnFocusChanged(bool focused)
  411. {
  412. using namespace WidgetFocusChanged;
  413. VariantMap eventData;
  414. eventData[P_WIDGET] = this;
  415. eventData[P_FOCUSED] = focused;
  416. SendEvent(E_WIDGETFOCUSCHANGED, eventData);
  417. }
  418. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  419. {
  420. UI* ui = GetSubsystem<UI>();
  421. if (ev.type == EVENT_TYPE_CHANGED || ev.type == EVENT_TYPE_KEY_UP)
  422. {
  423. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  424. {
  425. VariantMap eventData;
  426. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  427. SendEvent(E_WIDGETEVENT, eventData);
  428. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  429. return true;
  430. }
  431. }
  432. else if (ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
  433. {
  434. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  435. {
  436. VariantMap eventData;
  437. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  438. SendEvent(E_WIDGETEVENT, eventData);
  439. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  440. return true;
  441. }
  442. }
  443. else if (ev.type == EVENT_TYPE_POINTER_DOWN)
  444. {
  445. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  446. {
  447. VariantMap eventData;
  448. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  449. SendEvent(E_WIDGETEVENT, eventData);
  450. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  451. return true;
  452. }
  453. }
  454. else if (ev.type == EVENT_TYPE_SHORTCUT)
  455. {
  456. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  457. {
  458. VariantMap eventData;
  459. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  460. SendEvent(E_WIDGETEVENT, eventData);
  461. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  462. return true;
  463. }
  464. }
  465. else if (ev.type == EVENT_TYPE_TAB_CHANGED)
  466. {
  467. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  468. {
  469. VariantMap eventData;
  470. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  471. SendEvent(E_WIDGETEVENT, eventData);
  472. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  473. return true;
  474. }
  475. }
  476. else if (ev.type == EVENT_TYPE_CLICK)
  477. {
  478. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  479. {
  480. // popup menu
  481. if (JSGetHeapPtr())
  482. {
  483. VariantMap eventData;
  484. eventData[PopupMenuSelect::P_BUTTON] = this;
  485. String id;
  486. ui->GetTBIDString(ev.ref_id, id);
  487. eventData[PopupMenuSelect::P_REFID] = id;
  488. SendEvent(E_POPUPMENUSELECT, eventData);
  489. }
  490. return true;
  491. }
  492. else
  493. {
  494. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  495. {
  496. VariantMap eventData;
  497. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  498. SendEvent(E_WIDGETEVENT, eventData);
  499. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  500. return true;
  501. }
  502. }
  503. }
  504. return false;
  505. }
  506. }