UIWidget.cpp 9.9 KB

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