JSUI.cpp 9.9 KB

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