UIWidget.cpp 9.7 KB

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