UIWidget.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 "UIEvents.h"
  4. #include "UI.h"
  5. #include "UIWidget.h"
  6. #include "UILayout.h"
  7. #include "UIFontDescription.h"
  8. #include "UIView.h"
  9. using namespace tb;
  10. namespace Atomic
  11. {
  12. UIWidget::UIWidget(Context* context, bool createWidget) : Object(context),
  13. widget_(0),
  14. preferredSize_(new UIPreferredSize())
  15. {
  16. AddRef();
  17. if (createWidget)
  18. {
  19. widget_ = new TBWidget();
  20. widget_->SetDelegate(this);
  21. GetSubsystem<UI>()->WrapWidget(this, widget_);
  22. }
  23. }
  24. UIWidget::~UIWidget()
  25. {
  26. }
  27. void UIWidget::SetIsFocusable(bool value)
  28. {
  29. if (!widget_)
  30. return;
  31. widget_->SetIsFocusable(value);
  32. }
  33. bool UIWidget::Load(const String& filename)
  34. {
  35. UI* ui = GetSubsystem<UI>();
  36. if (!ui->LoadResourceFile(widget_ , filename))
  37. return false;
  38. VariantMap eventData;
  39. eventData[WidgetLoaded::P_WIDGET] = this;
  40. SendEvent(E_WIDGETLOADED, eventData);
  41. return true;
  42. }
  43. UIPreferredSize* UIWidget::GetPreferredSize()
  44. {
  45. // error
  46. if (!widget_)
  47. return preferredSize_;
  48. preferredSize_->SetFromTBPreferredSize(widget_->GetPreferredSize());
  49. return preferredSize_;
  50. }
  51. UIWidget* UIWidget::GetWidget(const String& id)
  52. {
  53. if (!widget_)
  54. return 0;
  55. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  56. if (!child)
  57. return 0;
  58. UI* ui = GetSubsystem<UI>();
  59. return ui->WrapWidget(child);
  60. }
  61. void UIWidget::SetWidget(tb::TBWidget* widget)
  62. {
  63. widget_ = widget;
  64. widget_->SetDelegate(this);
  65. }
  66. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  67. {
  68. UI* ui = GetSubsystem<UI>();
  69. String refid;
  70. ui->GetTBIDString(ev.ref_id, refid);
  71. using namespace WidgetEvent;
  72. data[P_HANDLER] = handler;
  73. data[P_TARGET] = target;
  74. data[P_TYPE] = (unsigned) ev.type;
  75. data[P_X] = ev.target_x;
  76. data[P_Y] = ev.target_y;
  77. data[P_DELTAX] = ev.delta_x;
  78. data[P_DELTAY] = ev.delta_y;
  79. data[P_COUNT] = ev.count;
  80. data[P_KEY] = ev.key;
  81. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  82. data[P_MODIFIERKEYS] = (unsigned) ev.modifierkeys;
  83. data[P_REFID] = refid;
  84. data[P_TOUCH] = (unsigned) ev.touch;
  85. }
  86. void UIWidget::OnDelete()
  87. {
  88. if (widget_)
  89. {
  90. // if we don't have a UI subsystem, we are exiting
  91. UI* ui = GetSubsystem<UI>();
  92. if (ui)
  93. ui->UnwrapWidget(widget_);
  94. }
  95. widget_ = 0;
  96. VariantMap eventData;
  97. eventData[WidgetDeleted::P_WIDGET] = this;
  98. SendEvent(E_WIDGETDELETED, eventData);
  99. ReleaseRef();
  100. }
  101. void UIWidget::AddChild(UIWidget* child)
  102. {
  103. if (!widget_ || !child->widget_)
  104. return;
  105. widget_->AddChild(child->widget_);
  106. }
  107. String UIWidget::GetText()
  108. {
  109. if (!widget_)
  110. return "";
  111. return widget_->GetText().CStr();
  112. }
  113. void UIWidget::SetText(const String& text)
  114. {
  115. if (!widget_)
  116. return;
  117. widget_->SetText(text.CString());
  118. }
  119. void UIWidget::SetGravity(UI_GRAVITY gravity)
  120. {
  121. if (!widget_)
  122. return;
  123. widget_->SetGravity((WIDGET_GRAVITY) gravity);
  124. }
  125. bool UIWidget::IsAncestorOf(UIWidget* widget)
  126. {
  127. if (!widget_ || !widget || !widget->widget_)
  128. return false;
  129. return widget_->IsAncestorOf(widget->widget_);
  130. }
  131. void UIWidget::SetPosition(int x, int y)
  132. {
  133. if (!widget_)
  134. return;
  135. widget_->SetPosition(TBPoint(x, y));
  136. }
  137. IntRect UIWidget::GetRect()
  138. {
  139. IntRect rect(0, 0, 0, 0);
  140. if (!widget_)
  141. return rect;
  142. tb::TBRect tbrect = widget_->GetRect();
  143. rect.top_ = tbrect.y;
  144. rect.left_ = tbrect.x;
  145. rect.right_ = tbrect.x + tbrect.w;
  146. rect.bottom_ = tbrect.y + tbrect.h;
  147. return rect;
  148. }
  149. void UIWidget::SetRect(IntRect rect)
  150. {
  151. if (!widget_)
  152. return;
  153. tb::TBRect tbrect;
  154. tbrect.y = rect.top_;
  155. tbrect.x = rect.left_;
  156. tbrect.w = rect.right_ - rect.left_;
  157. tbrect.h = rect.bottom_ - rect.top_;
  158. widget_->SetRect(tbrect);
  159. }
  160. void UIWidget::SetSize(int width, int height)
  161. {
  162. if (!widget_)
  163. return;
  164. widget_->SetSize(width, height);
  165. }
  166. void UIWidget::Invalidate()
  167. {
  168. if (!widget_)
  169. return;
  170. widget_->Invalidate();
  171. }
  172. void UIWidget::Center()
  173. {
  174. if (!widget_)
  175. return;
  176. // this should center on parent widget, not root
  177. UI* ui = GetSubsystem<UI>();
  178. TBRect rect = widget_->GetRect();
  179. TBWidget* root = ui->GetRootWidget();
  180. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  181. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  182. }
  183. UIWidget* UIWidget::GetParent()
  184. {
  185. if (!widget_)
  186. return 0;
  187. TBWidget* parent = widget_->GetParent();
  188. if (!parent)
  189. return 0;
  190. UI* ui = GetSubsystem<UI>();
  191. return ui->WrapWidget(parent);
  192. }
  193. UIWidget* UIWidget::GetContentRoot()
  194. {
  195. if (!widget_)
  196. return 0;
  197. TBWidget* root = widget_->GetContentRoot();
  198. if (!root)
  199. return 0;
  200. UI* ui = GetSubsystem<UI>();
  201. return ui->WrapWidget(root);
  202. }
  203. void UIWidget::Die()
  204. {
  205. if (!widget_)
  206. return;
  207. // clear delegate
  208. widget_->SetDelegate(NULL);
  209. // explictly die (can trigger an animation)
  210. widget_->Die();
  211. // call OnDelete, which unwraps the widget and does some bookkeeping
  212. OnDelete();
  213. }
  214. void UIWidget::SetLayoutParams(UILayoutParams* params)
  215. {
  216. if (!widget_)
  217. return;
  218. widget_->SetLayoutParams(*(params->GetTBLayoutParams()));
  219. }
  220. void UIWidget::SetFontDescription(UIFontDescription* fd)
  221. {
  222. if (!widget_)
  223. return;
  224. widget_->SetFontDescription(*(fd->GetTBFontDescription()));
  225. }
  226. void UIWidget::DeleteAllChildren()
  227. {
  228. if (!widget_)
  229. return;
  230. widget_->DeleteAllChildren();
  231. }
  232. void UIWidget::SetSkinBg(const String& id)
  233. {
  234. if (!widget_)
  235. return;
  236. widget_->SetSkinBg(TBIDC(id.CString()));
  237. }
  238. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  239. {
  240. if (!widget_ || !child)
  241. return;
  242. TBWidget* childw = child->GetInternalWidget();
  243. if (!childw)
  244. return;
  245. widget_->RemoveChild(childw);
  246. if (cleanup)
  247. delete childw;
  248. }
  249. const String& UIWidget::GetId()
  250. {
  251. if (!widget_ || !widget_->GetID())
  252. {
  253. if (id_.Length())
  254. id_.Clear();
  255. return id_;
  256. }
  257. if (id_.Length())
  258. return id_;
  259. UI* ui = GetSubsystem<UI>();
  260. ui->GetTBIDString(widget_->GetID(), id_);
  261. return id_;
  262. }
  263. void UIWidget::SetId(const String& id)
  264. {
  265. if (!widget_)
  266. {
  267. if (id_.Length())
  268. id_.Clear();
  269. return;
  270. }
  271. id_ = id;
  272. widget_->SetID(TBIDC(id.CString()));
  273. }
  274. void UIWidget::SetState(/*WIDGET_STATE*/ unsigned state, bool on)
  275. {
  276. if (!widget_)
  277. return;
  278. widget_->SetState((WIDGET_STATE) state, on);
  279. }
  280. void UIWidget::SetFocus()
  281. {
  282. if (!widget_)
  283. return;
  284. widget_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  285. }
  286. void UIWidget::SetVisibility(UI_WIDGET_VISIBILITY visibility)
  287. {
  288. if (!widget_)
  289. return;
  290. widget_->SetVisibilility((WIDGET_VISIBILITY) visibility);
  291. }
  292. UI_WIDGET_VISIBILITY UIWidget::GetVisibility()
  293. {
  294. if (!widget_)
  295. return UI_WIDGET_VISIBILITY_GONE;
  296. return (UI_WIDGET_VISIBILITY) widget_->GetVisibility();
  297. }
  298. UIWidget* UIWidget::GetFirstChild()
  299. {
  300. if (!widget_)
  301. return NULL;
  302. return GetSubsystem<UI>()->WrapWidget(widget_->GetFirstChild());
  303. }
  304. UIWidget* UIWidget::GetNext()
  305. {
  306. if (!widget_)
  307. return NULL;
  308. return GetSubsystem<UI>()->WrapWidget(widget_->GetNext());
  309. }
  310. void UIWidget::SetValue(double value)
  311. {
  312. if (!widget_)
  313. return;
  314. widget_->SetValueDouble(value);
  315. }
  316. double UIWidget::GetValue()
  317. {
  318. if (!widget_)
  319. return 0.0;
  320. return widget_->GetValueDouble();
  321. }
  322. bool UIWidget::GetState(/*WIDGET_STATE*/ unsigned state)
  323. {
  324. if (!widget_)
  325. return false;
  326. return widget_->GetState((WIDGET_STATE) state);
  327. }
  328. void UIWidget::SetStateRaw(/*WIDGET_STATE*/ unsigned state)
  329. {
  330. if (!widget_)
  331. return;
  332. widget_->SetStateRaw((WIDGET_STATE) state);
  333. }
  334. /*WIDGET_STATE*/ unsigned UIWidget::GetStateRaw()
  335. {
  336. if (!widget_)
  337. return false;
  338. return (unsigned) widget_->GetStateRaw();
  339. }
  340. UIView* UIWidget::GetView()
  341. {
  342. if (!widget_)
  343. return 0;
  344. if (GetType() == UIView::GetTypeStatic())
  345. return (UIView*) this;
  346. TBWidget* tbw = widget_->GetParent();
  347. while(tbw)
  348. {
  349. TBWidgetDelegate* delegate = tbw->GetDelegate();
  350. if (delegate)
  351. {
  352. UIWidget* d = (UIWidget*) delegate;
  353. if (d->GetType() == UIView::GetTypeStatic())
  354. return (UIView*) d;
  355. }
  356. tbw = tbw->GetParent();
  357. }
  358. return 0;
  359. }
  360. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  361. {
  362. UI* ui = GetSubsystem<UI>();
  363. if (ev.type == EVENT_TYPE_CHANGED)
  364. {
  365. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  366. {
  367. VariantMap eventData;
  368. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  369. SendEvent(E_WIDGETEVENT, eventData);
  370. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  371. return true;
  372. }
  373. }
  374. else if (ev.type == EVENT_TYPE_TAB_CHANGED)
  375. {
  376. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  377. {
  378. VariantMap eventData;
  379. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  380. SendEvent(E_WIDGETEVENT, eventData);
  381. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  382. return true;
  383. }
  384. }
  385. else if (ev.type == EVENT_TYPE_CLICK)
  386. {
  387. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  388. {
  389. // popup menu
  390. if (JSGetHeapPtr())
  391. {
  392. VariantMap eventData;
  393. eventData[PopupMenuSelect::P_BUTTON] = this;
  394. String id;
  395. ui->GetTBIDString(ev.ref_id, id);
  396. eventData[PopupMenuSelect::P_REFID] = id;
  397. SendEvent(E_POPUPMENUSELECT, eventData);
  398. }
  399. return true;
  400. }
  401. else
  402. {
  403. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  404. {
  405. VariantMap eventData;
  406. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  407. SendEvent(E_WIDGETEVENT, eventData);
  408. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  409. return true;
  410. }
  411. }
  412. }
  413. return false;
  414. }
  415. }