JSUI.cpp 9.9 KB

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