JSUI.cpp 10 KB

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