JSUI.cpp 11 KB

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