JSUI.cpp 11 KB

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