JSUI.cpp 11 KB

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