UIWidget.cpp 12 KB

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