UIWidget.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. using namespace tb;
  7. namespace Atomic
  8. {
  9. UIWidget::UIWidget(Context* context, bool createWidget) : Object(context),
  10. widget_(0)
  11. {
  12. AddRef();
  13. if (createWidget)
  14. {
  15. widget_ = new TBWidget();
  16. widget_->SetDelegate(this);
  17. GetSubsystem<UI>()->WrapWidget(this, widget_);
  18. }
  19. UI* ui = GetSubsystem<UI>();
  20. }
  21. UIWidget::~UIWidget()
  22. {
  23. }
  24. bool UIWidget::Load(const String& filename)
  25. {
  26. UI* ui = GetSubsystem<UI>();
  27. if (!ui->LoadResourceFile(widget_ , filename))
  28. return false;
  29. VariantMap eventData;
  30. eventData[WidgetLoaded::P_WIDGET] = this;
  31. SendEvent(E_WIDGETLOADED, eventData);
  32. return true;
  33. }
  34. UIWidget* UIWidget::GetWidget(const String& id)
  35. {
  36. if (!widget_)
  37. return 0;
  38. TBWidget* child = widget_->GetWidgetByID(TBID(id.CString()));
  39. if (!child)
  40. return 0;
  41. UI* ui = GetSubsystem<UI>();
  42. return ui->WrapWidget(child);
  43. }
  44. void UIWidget::SetWidget(tb::TBWidget* widget)
  45. {
  46. widget_ = widget;
  47. widget_->SetDelegate(this);
  48. }
  49. void UIWidget::ConvertEvent(UIWidget *handler, UIWidget* target, const tb::TBWidgetEvent &ev, VariantMap& data)
  50. {
  51. using namespace WidgetEvent;
  52. data[P_HANDLER] = handler;
  53. data[P_TARGET] = target;
  54. data[P_TYPE] = (unsigned) ev.type;
  55. data[P_X] = ev.target_x;
  56. data[P_Y] = ev.target_y;
  57. data[P_DELTAX] = ev.delta_x;
  58. data[P_DELTAY] = ev.delta_y;
  59. data[P_COUNT] = ev.count;
  60. data[P_KEY] = ev.key;
  61. data[P_SPECIALKEY] = (unsigned) ev.special_key;
  62. data[P_MODIFIERKEYS] = (unsigned) ev.modifierkeys;
  63. data[P_REFID] = (unsigned) ev.ref_id;
  64. data[P_TOUCH] = (unsigned) ev.touch;
  65. }
  66. void UIWidget::OnDelete()
  67. {
  68. if (widget_)
  69. {
  70. // if we don't have a UI subsystem, we are exiting
  71. UI* ui = GetSubsystem<UI>();
  72. if (ui)
  73. ui->UnwrapWidget(widget_);
  74. }
  75. widget_ = 0;
  76. VariantMap eventData;
  77. eventData[WidgetDeleted::P_WIDGET] = this;
  78. SendEvent(E_WIDGETDELETED, eventData);
  79. ReleaseRef();
  80. }
  81. void UIWidget::AddChild(UIWidget* child)
  82. {
  83. if (!widget_ || !child->widget_)
  84. return;
  85. widget_->AddChild(child->widget_);
  86. }
  87. void UIWidget::SetText(const String& text)
  88. {
  89. if (!widget_)
  90. return;
  91. widget_->SetText(text.CString());
  92. }
  93. void UIWidget::SetGravity(/*WIDGET_GRAVITY*/ unsigned gravity)
  94. {
  95. if (!widget_)
  96. return;
  97. widget_->SetGravity((WIDGET_GRAVITY) gravity);
  98. }
  99. void UIWidget::SetPosition(int x, int y)
  100. {
  101. if (!widget_)
  102. return;
  103. widget_->SetPosition(TBPoint(x, y));
  104. }
  105. IntRect UIWidget::GetRect()
  106. {
  107. IntRect rect(0, 0, 0, 0);
  108. if (!widget_)
  109. return rect;
  110. tb::TBRect tbrect = widget_->GetRect();
  111. rect.top_ = tbrect.y;
  112. rect.left_ = tbrect.x;
  113. rect.right_ = tbrect.x + tbrect.w;
  114. rect.bottom_ = tbrect.y + tbrect.h;
  115. return rect;
  116. }
  117. void UIWidget::SetSize(int width, int height)
  118. {
  119. if (!widget_)
  120. return;
  121. widget_->SetSize(width, height);
  122. }
  123. void UIWidget::Invalidate()
  124. {
  125. if (!widget_)
  126. return;
  127. widget_->Invalidate();
  128. }
  129. void UIWidget::Center()
  130. {
  131. if (!widget_)
  132. return;
  133. // this should center on parent widget, not root
  134. UI* ui = GetSubsystem<UI>();
  135. TBRect rect = widget_->GetRect();
  136. TBWidget* root = ui->GetRootWidget();
  137. TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
  138. widget_->SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
  139. }
  140. UIWidget* UIWidget::GetParent()
  141. {
  142. if (!widget_)
  143. return 0;
  144. TBWidget* parent = widget_->GetParent();
  145. if (!parent)
  146. return 0;
  147. UI* ui = GetSubsystem<UI>();
  148. return ui->WrapWidget(parent);
  149. }
  150. UIWidget* UIWidget::GetContentRoot()
  151. {
  152. if (!widget_)
  153. return 0;
  154. TBWidget* root = widget_->GetContentRoot();
  155. if (!root)
  156. return 0;
  157. UI* ui = GetSubsystem<UI>();
  158. return ui->WrapWidget(root);
  159. }
  160. void UIWidget::Die()
  161. {
  162. if (!widget_)
  163. return;
  164. // clear delegate
  165. widget_->SetDelegate(NULL);
  166. // explictly die (can trigger an animation)
  167. widget_->Die();
  168. // call OnDelete, which unwraps the widget and does some bookkeeping
  169. OnDelete();
  170. }
  171. void UIWidget::RemoveChild(UIWidget* child, bool cleanup)
  172. {
  173. if (!widget_ || !child)
  174. return;
  175. TBWidget* childw = child->GetInternalWidget();
  176. if (!childw)
  177. return;
  178. widget_->RemoveChild(childw);
  179. if (cleanup)
  180. delete childw;
  181. }
  182. const String& UIWidget::GetId()
  183. {
  184. if (!widget_ || !widget_->GetID())
  185. {
  186. if (id_.Length())
  187. id_.Clear();
  188. return id_;
  189. }
  190. if (id_.Length())
  191. return id_;
  192. UI* ui = GetSubsystem<UI>();
  193. ui->GetTBIDString(widget_->GetID(), id_);
  194. return id_;
  195. }
  196. void UIWidget::SetId(const String& id)
  197. {
  198. if (!widget_)
  199. {
  200. if (id_.Length())
  201. id_.Clear();
  202. return;
  203. }
  204. id_ = id;
  205. widget_->SetID(TBIDC(id.CString()));
  206. }
  207. void UIWidget::SetState(/*WIDGET_STATE*/ unsigned state, bool on)
  208. {
  209. if (!widget_)
  210. return;
  211. widget_->SetState((WIDGET_STATE) state, on);
  212. }
  213. void UIWidget::SetValue(double value)
  214. {
  215. if (!widget_)
  216. return;
  217. widget_->SetValueDouble(value);
  218. }
  219. double UIWidget::GetValue()
  220. {
  221. if (!widget_)
  222. return 0.0;
  223. return widget_->GetValueDouble();
  224. }
  225. bool UIWidget::GetState(/*WIDGET_STATE*/ unsigned state)
  226. {
  227. if (!widget_)
  228. return false;
  229. return widget_->GetState((WIDGET_STATE) state);
  230. }
  231. void UIWidget::SetStateRaw(/*WIDGET_STATE*/ unsigned state)
  232. {
  233. if (!widget_)
  234. return;
  235. widget_->SetStateRaw((WIDGET_STATE) state);
  236. }
  237. /*WIDGET_STATE*/ unsigned UIWidget::GetStateRaw()
  238. {
  239. if (!widget_)
  240. return false;
  241. return (unsigned) widget_->GetStateRaw();
  242. }
  243. bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
  244. {
  245. UI* ui = GetSubsystem<UI>();
  246. if (ev.type == EVENT_TYPE_CHANGED)
  247. {
  248. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  249. {
  250. VariantMap eventData;
  251. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  252. SendEvent(E_WIDGETEVENT, eventData);
  253. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  254. return true;
  255. }
  256. }
  257. else if (ev.type == EVENT_TYPE_CLICK)
  258. {
  259. if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
  260. {
  261. // popup menu
  262. if (JSGetHeapPtr())
  263. {
  264. VariantMap eventData;
  265. eventData[PopupMenuSelect::P_BUTTON] = this;
  266. eventData[PopupMenuSelect::P_REFID] = (unsigned) ev.ref_id;
  267. SendEvent(E_POPUPMENUSELECT, eventData);
  268. }
  269. return true;
  270. }
  271. else
  272. {
  273. if (!ev.target || ui->IsWidgetWrapped(ev.target))
  274. {
  275. VariantMap eventData;
  276. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  277. SendEvent(E_WIDGETEVENT, eventData);
  278. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  279. return true;
  280. }
  281. }
  282. }
  283. return false;
  284. }
  285. }