UIWidget.cpp 10 KB

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