JSUI.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #include <TurboBadger/tb_widgets.h>
  2. #include <Atomic/Core/CoreEvents.h>
  3. #include <Atomic/UI/UIEvents.h>
  4. #include <Atomic/UI/UIWidget.h>
  5. #include <Atomic/UI/UIButton.h>
  6. #include <Atomic/UI/UI.h>
  7. #include "JSVM.h"
  8. #include "JSUI.h"
  9. using namespace tb;
  10. namespace Atomic
  11. {
  12. JSUI::JSUI(Context* context) : Object(context),
  13. updateTime_(0.0f)
  14. {
  15. ctx_ = JSVM::GetJSVM(nullptr)->GetJSContext();
  16. SubscribeToEvent(E_UPDATE, HANDLER(JSUI, HandleUpdate));
  17. SubscribeToEvent(E_JSOBJECTADDED, HANDLER(JSUI, HandleObjectAdded));
  18. // for debugging only, commented out otherwise
  19. //SubscribeToEvent(E_JSOBJECTREMOVED, HANDLER(JSUI, HandleObjectRemoved));
  20. SubscribeToEvent(E_WIDGETDELETED, HANDLER(JSUI, HandleWidgetDeleted));
  21. SubscribeToEvent(E_WIDGETEVENT, HANDLER(JSUI, HandleWidgetEvent));
  22. SubscribeToEvent(E_WIDGETLOADED, HANDLER(JSUI, HandleWidgetLoaded));
  23. SubscribeToEvent(E_POPUPMENUSELECT, HANDLER(JSUI, HandlePopupMenuSelect));
  24. duk_push_global_stash(ctx_);
  25. duk_push_object(ctx_);
  26. duk_put_prop_string(ctx_, -2, "__jsui_widgetkeepalive");
  27. duk_pop(ctx_);
  28. uiTypes_["UIWidget"] = true;
  29. uiTypes_["UIButton"] = true;
  30. uiTypes_["UIView"] = true;
  31. uiTypes_["UIEditField"] = true;
  32. uiTypes_["UITextField"] = true;
  33. uiTypes_["UIImageWidget"] = true;
  34. uiTypes_["UILayout"] = true;
  35. uiTypes_["UIMenuWindow"] = true;
  36. uiTypes_["UIWindow"] = true;
  37. uiTypes_["UIClickLabel"] = true;
  38. uiTypes_["UICheckBox"] = true;
  39. uiTypes_["UISelectLost"] = true;
  40. uiTypes_["UIListView"] = true;
  41. uiTypes_["UIMessageWindow"] = true;
  42. uiTypes_["UISkinImage"] = true;
  43. uiTypes_["UITabContainer"] = true;
  44. uiTypes_["UISceneView"] = true;
  45. uiTypes_["UIContainer"] = true;
  46. uiTypes_["UISection"] = true;
  47. uiTypes_["UIInlineSelect"] = true;
  48. uiTypes_["UITextureWidget"] = true;
  49. uiTypes_["UIScrollContainer"] = true;
  50. uiTypes_["UISeparator"] = true;
  51. uiTypes_["UIDimmer"] = true;
  52. }
  53. JSUI::~JSUI()
  54. {
  55. }
  56. void JSUI::GatherWidgets(tb::TBWidget* widget, PODVector<tb::TBWidget*>& widgets)
  57. {
  58. if (widget->GetID() != TBID())
  59. widgets.Push(widget);
  60. for (TBWidget *n = widget->GetFirstChild(); n; n = n->GetNext())
  61. {
  62. GatherWidgets(n, widgets);
  63. }
  64. }
  65. void JSUI::HandleObjectAdded(StringHash eventType, VariantMap& eventData)
  66. {
  67. RefCounted* ref = static_cast<RefCounted*>(eventData[ObjectAdded::P_OBJECT].GetPtr());
  68. assert(ref->IsObject());
  69. Object* o = (Object*) ref;
  70. // for any UI type, we make sure it is kept alive
  71. if (uiTypes_.Contains(o->GetType()))
  72. {
  73. assert(o->JSGetHeapPtr());
  74. duk_push_global_stash(ctx_);
  75. duk_get_prop_string(ctx_, -1, "__jsui_widgetkeepalive");
  76. // can't use instance as key, as this coerces to [Object] for
  77. // string property, pointer will be string representation of
  78. // address, so, unique key
  79. duk_push_pointer(ctx_, o);
  80. duk_push_heapptr(ctx_, o->JSGetHeapPtr());
  81. duk_put_prop(ctx_, -3);
  82. duk_pop_2(ctx_);
  83. }
  84. }
  85. void JSUI::HandleObjectRemoved(StringHash eventType, VariantMap& eventData)
  86. {
  87. Object* o = static_cast<Object*>(eventData[ObjectAdded::P_OBJECT].GetPtr());
  88. LOGINFOF("Removing %s", o->GetTypeName().CString());
  89. }
  90. void JSUI::HandleWidgetDeleted(StringHash eventType, VariantMap& eventData)
  91. {
  92. UIWidget* widget = static_cast<UIWidget*>(eventData[WidgetDeleted::P_WIDGET].GetPtr());
  93. if (!widget->JSGetHeapPtr())
  94. return;
  95. duk_push_global_stash(ctx_);
  96. duk_get_prop_string(ctx_, -1, "__jsui_widgetkeepalive");
  97. // can't use instance as key, as this coerces to [Object] for
  98. // string property, pointer will be string representation of
  99. // address, so, unique key
  100. duk_push_pointer(ctx_, widget);
  101. duk_del_prop(ctx_, -2);
  102. duk_pop_2(ctx_);
  103. }
  104. void JSUI::HandleUpdate(StringHash eventType, VariantMap& eventData)
  105. {
  106. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  107. updateTime_ += timeStep;
  108. if (updateTime_ > 1.0f)
  109. {
  110. UI* ui = GetSubsystem<UI>();
  111. ui->PruneUnreachableWidgets();
  112. updateTime_ = 0.0f;
  113. }
  114. }
  115. void JSUI::HandleWidgetLoaded(StringHash eventType, VariantMap& eventData)
  116. {
  117. using namespace WidgetLoaded;
  118. UIWidget* widget = static_cast<UIWidget*>(eventData[P_WIDGET].GetPtr());
  119. if (!widget)
  120. return;
  121. void* heapptr = widget->JSGetHeapPtr();
  122. if (!heapptr)
  123. return;
  124. TBWidget* tbwidget = widget->GetInternalWidget();
  125. assert(tbwidget);
  126. // all widgets with id's are wrapped, so that event handlers are bubbled up properly
  127. // note that all js widget object representations are kept alive in HandleObjectAdded
  128. // when pushed into the VM
  129. PODVector<TBWidget*> widgets;
  130. GatherWidgets(tbwidget, widgets);
  131. UI* ui = GetSubsystem<UI>();
  132. for (unsigned i = 0; i < widgets.Size(); i++)
  133. {
  134. UIWidget* o = ui->WrapWidget(widgets.At(i));
  135. if (!o)
  136. continue;
  137. js_push_class_object_instance(ctx_, o);
  138. }
  139. }
  140. void JSUI::HandlePopupMenuSelect(StringHash eventType, VariantMap& eventData)
  141. {
  142. using namespace PopupMenuSelect;
  143. UIButton* button = static_cast<UIButton*>(eventData[P_BUTTON].GetPtr());
  144. if (!button)
  145. return;
  146. void* heapptr = button->JSGetHeapPtr();
  147. if (!heapptr) // gc'd
  148. return;
  149. int top = duk_get_top(ctx_);
  150. duk_push_heapptr(ctx_, heapptr);
  151. duk_get_prop_string(ctx_, -1, "__popup_menu_callback");
  152. if (!duk_is_callable(ctx_, -1))
  153. {
  154. duk_pop_n(ctx_, 2);
  155. assert(top == duk_get_top(ctx_));
  156. return;
  157. }
  158. String id;
  159. id = eventData[P_REFID].GetString();
  160. duk_push_string(ctx_, id.CString());
  161. duk_insert(ctx_, -1);
  162. if (duk_pcall(ctx_, 1) != 0)
  163. {
  164. JSVM::GetJSVM(nullptr)->SendJSErrorEvent();
  165. }
  166. duk_pop_n(ctx_, 2);
  167. assert(top == duk_get_top(ctx_));
  168. }
  169. void JSUI::PushWidgetEventObject(VariantMap& eventData)
  170. {
  171. using namespace WidgetEvent;
  172. // create the event object
  173. duk_push_object(ctx_);
  174. // target
  175. UIWidget* target = static_cast<UIWidget*>(eventData[P_TARGET].GetPtr());
  176. if (target)
  177. {
  178. assert(target->JSGetHeapPtr());
  179. duk_push_heapptr(ctx_, target->JSGetHeapPtr());
  180. }
  181. else
  182. {
  183. duk_push_null(ctx_);
  184. }
  185. duk_put_prop_string(ctx_, -2, "target");
  186. duk_push_number(ctx_, (duk_double_t) eventData[P_TYPE].GetUInt());
  187. duk_put_prop_string(ctx_, -2, "type");
  188. duk_push_number(ctx_, (duk_double_t) eventData[P_X].GetInt());
  189. duk_put_prop_string(ctx_, -2, "x");
  190. duk_push_number(ctx_, (duk_double_t) eventData[P_Y].GetInt());
  191. duk_put_prop_string(ctx_, -2, "y");
  192. duk_push_number(ctx_, (duk_double_t) eventData[P_DELTAX].GetInt());
  193. duk_put_prop_string(ctx_, -2, "deltaX");
  194. duk_push_number(ctx_, (duk_double_t) eventData[P_DELTAX].GetInt());
  195. duk_put_prop_string(ctx_, -2, "deltaY");
  196. duk_push_number(ctx_, (duk_double_t) eventData[P_COUNT].GetInt());
  197. duk_put_prop_string(ctx_, -2, "count");
  198. duk_push_number(ctx_, (duk_double_t) eventData[P_KEY].GetInt());
  199. duk_put_prop_string(ctx_, -2, "key");
  200. duk_push_number(ctx_, (duk_double_t) eventData[P_SPECIALKEY].GetInt());
  201. duk_put_prop_string(ctx_, -2, "specialKey");
  202. duk_push_number(ctx_, (duk_double_t) eventData[P_MODIFIERKEYS].GetInt());
  203. duk_put_prop_string(ctx_, -2, "modifierKeys");
  204. duk_push_number(ctx_, (duk_double_t) eventData[P_MODIFIERKEYS].GetInt());
  205. duk_put_prop_string(ctx_, -2, "modifierKeys");
  206. String id = eventData[P_REFID].GetString();
  207. duk_push_string(ctx_, id.CString() );
  208. duk_put_prop_string(ctx_, -2, "refID");
  209. duk_push_boolean(ctx_,eventData[P_TOUCH].GetBool() ? 1 : 0);
  210. duk_put_prop_string(ctx_, -2, "touch");
  211. }
  212. void JSUI::HandleWidgetEvent(StringHash eventType, VariantMap& eventData)
  213. {
  214. using namespace WidgetEvent;
  215. UIWidget* handler = static_cast<UIWidget*>(eventData[P_HANDLER].GetPtr());
  216. UIWidget* target = static_cast<UIWidget*>(eventData[P_TARGET].GetPtr());
  217. if (!handler)
  218. return;
  219. void* handlerHeapPtr = handler->JSGetHeapPtr();
  220. if (!handlerHeapPtr)
  221. return;
  222. // if we have a target, however no corresponding JS object return
  223. if (target && !target->JSGetHeapPtr())
  224. return;
  225. tb::EVENT_TYPE type = (tb::EVENT_TYPE) eventData[P_TYPE].GetUInt();
  226. if (type == tb::EVENT_TYPE_CHANGED)
  227. {
  228. int top = duk_get_top(ctx_);
  229. duk_push_heapptr(ctx_, handlerHeapPtr);
  230. duk_get_prop_string(ctx_, -1, "onChanged");
  231. if (duk_is_callable(ctx_, -1)) {
  232. if (duk_pcall(ctx_, 0) != 0)
  233. {
  234. JSVM::GetJSVM(nullptr)->SendJSErrorEvent();
  235. }
  236. else
  237. {
  238. if (duk_is_boolean(ctx_, -1) && duk_to_boolean(ctx_, -1))
  239. eventData[P_HANDLED] = true;
  240. }
  241. }
  242. duk_pop_n(ctx_, 2);
  243. assert(top == duk_get_top(ctx_));
  244. return;
  245. }
  246. // general event handler, bubbles to (wrapped) parent widgets unless handled (returns true)
  247. if (type == tb::EVENT_TYPE_CLICK)
  248. {
  249. int top = duk_get_top(ctx_);
  250. duk_push_heapptr(ctx_, handlerHeapPtr);
  251. duk_get_prop_string(ctx_, -1, "onEvent");
  252. if (duk_is_callable(ctx_, -1)) {
  253. PushWidgetEventObject(eventData);
  254. duk_call(ctx_, 1);
  255. if (duk_is_boolean(ctx_, -1) && duk_to_boolean(ctx_, -1))
  256. eventData[P_HANDLED] = true;
  257. }
  258. duk_pop_n(ctx_, 2);
  259. assert(top == duk_get_top(ctx_));
  260. }
  261. // specific event handlers
  262. if (type == tb::EVENT_TYPE_CLICK && handler == target)
  263. {
  264. int top = duk_get_top(ctx_);
  265. duk_push_heapptr(ctx_, handlerHeapPtr);
  266. duk_get_prop_string(ctx_, -1, "onClick");
  267. if (duk_is_callable(ctx_, -1)) {
  268. if (duk_pcall(ctx_, 0) != 0)
  269. {
  270. JSVM::GetJSVM(nullptr)->SendJSErrorEvent();
  271. }
  272. else
  273. {
  274. if (duk_is_boolean(ctx_, -1) && duk_to_boolean(ctx_, -1))
  275. eventData[P_HANDLED] = true;
  276. }
  277. }
  278. duk_pop_n(ctx_, 2);
  279. assert(top == duk_get_top(ctx_));
  280. return;
  281. }
  282. }
  283. }