JSUI.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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, ATOMIC_HANDLER(JSUI, HandleUpdate));
  38. SubscribeToEvent(E_WIDGETEVENT, ATOMIC_HANDLER(JSUI, HandleWidgetEvent));
  39. SubscribeToEvent(E_WIDGETLOADED, ATOMIC_HANDLER(JSUI, HandleWidgetLoaded));
  40. SubscribeToEvent(E_POPUPMENUSELECT, ATOMIC_HANDLER(JSUI, HandlePopupMenuSelect));
  41. }
  42. JSUI::~JSUI()
  43. {
  44. }
  45. void JSUI::GatherWidgets(tb::TBWidget* widget, PODVector<tb::TBWidget*>& widgets)
  46. {
  47. if (widget->GetID() != TBID())
  48. widgets.Push(widget);
  49. for (TBWidget *n = widget->GetFirstChild(); n; n = n->GetNext())
  50. {
  51. GatherWidgets(n, widgets);
  52. }
  53. }
  54. void JSUI::HandleUpdate(StringHash eventType, VariantMap& eventData)
  55. {
  56. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  57. updateTime_ += timeStep;
  58. if (updateTime_ > 1.0f)
  59. {
  60. UI* ui = GetSubsystem<UI>();
  61. ui->PruneUnreachableWidgets();
  62. updateTime_ = 0.0f;
  63. }
  64. }
  65. void JSUI::HandleWidgetLoaded(StringHash eventType, VariantMap& eventData)
  66. {
  67. using namespace WidgetLoaded;
  68. UIWidget* widget = static_cast<UIWidget*>(eventData[P_WIDGET].GetPtr());
  69. if (!widget)
  70. return;
  71. void* heapptr = widget->JSGetHeapPtr();
  72. if (!heapptr)
  73. return;
  74. TBWidget* tbwidget = widget->GetInternalWidget();
  75. assert(tbwidget);
  76. // all widgets with id's are wrapped, so that event handlers are bubbled up properly
  77. // note that all js widget object representations are kept alive in HandleObjectAdded
  78. // when pushed into the VM
  79. PODVector<TBWidget*> widgets;
  80. GatherWidgets(tbwidget, widgets);
  81. UI* ui = GetSubsystem<UI>();
  82. for (unsigned i = 0; i < widgets.Size(); i++)
  83. {
  84. UIWidget* o = ui->WrapWidget(widgets.At(i));
  85. if (!o)
  86. continue;
  87. js_push_class_object_instance(ctx_, o);
  88. }
  89. }
  90. void JSUI::HandlePopupMenuSelect(StringHash eventType, VariantMap& eventData)
  91. {
  92. using namespace PopupMenuSelect;
  93. UIButton* button = static_cast<UIButton*>(eventData[P_BUTTON].GetPtr());
  94. if (!button)
  95. return;
  96. void* heapptr = button->JSGetHeapPtr();
  97. if (!heapptr) // gc'd
  98. return;
  99. int top = duk_get_top(ctx_);
  100. duk_push_heapptr(ctx_, heapptr);
  101. duk_get_prop_string(ctx_, -1, "__popup_menu_callback");
  102. if (!duk_is_callable(ctx_, -1))
  103. {
  104. duk_pop_n(ctx_, 2);
  105. assert(top == duk_get_top(ctx_));
  106. return;
  107. }
  108. String id;
  109. id = eventData[P_REFID].GetString();
  110. duk_push_string(ctx_, id.CString());
  111. duk_insert(ctx_, -1);
  112. if (duk_pcall(ctx_, 1) != 0)
  113. {
  114. JSVM::GetJSVM(nullptr)->SendJSErrorEvent();
  115. }
  116. duk_pop_n(ctx_, 2);
  117. assert(top == duk_get_top(ctx_));
  118. }
  119. void JSUI::PushWidgetEventObject(VariantMap& eventData)
  120. {
  121. using namespace WidgetEvent;
  122. // create the event object
  123. duk_push_object(ctx_);
  124. // target
  125. UIWidget* target = static_cast<UIWidget*>(eventData[P_TARGET].GetPtr());
  126. if (target)
  127. {
  128. assert(target->JSGetHeapPtr());
  129. duk_push_heapptr(ctx_, target->JSGetHeapPtr());
  130. }
  131. else
  132. {
  133. duk_push_null(ctx_);
  134. }
  135. duk_put_prop_string(ctx_, -2, "target");
  136. duk_push_number(ctx_, (duk_double_t) eventData[P_TYPE].GetUInt());
  137. duk_put_prop_string(ctx_, -2, "type");
  138. duk_push_number(ctx_, (duk_double_t) eventData[P_X].GetInt());
  139. duk_put_prop_string(ctx_, -2, "x");
  140. duk_push_number(ctx_, (duk_double_t) eventData[P_Y].GetInt());
  141. duk_put_prop_string(ctx_, -2, "y");
  142. duk_push_number(ctx_, (duk_double_t) eventData[P_DELTAX].GetInt());
  143. duk_put_prop_string(ctx_, -2, "deltaX");
  144. duk_push_number(ctx_, (duk_double_t) eventData[P_DELTAX].GetInt());
  145. duk_put_prop_string(ctx_, -2, "deltaY");
  146. duk_push_number(ctx_, (duk_double_t) eventData[P_COUNT].GetInt());
  147. duk_put_prop_string(ctx_, -2, "count");
  148. duk_push_number(ctx_, (duk_double_t) eventData[P_KEY].GetInt());
  149. duk_put_prop_string(ctx_, -2, "key");
  150. duk_push_number(ctx_, (duk_double_t) eventData[P_SPECIALKEY].GetInt());
  151. duk_put_prop_string(ctx_, -2, "specialKey");
  152. duk_push_number(ctx_, (duk_double_t) eventData[P_MODIFIERKEYS].GetInt());
  153. duk_put_prop_string(ctx_, -2, "modifierKeys");
  154. duk_push_number(ctx_, (duk_double_t) eventData[P_MODIFIERKEYS].GetInt());
  155. duk_put_prop_string(ctx_, -2, "modifierKeys");
  156. String id = eventData[P_REFID].GetString();
  157. duk_push_string(ctx_, id.CString() );
  158. duk_put_prop_string(ctx_, -2, "refID");
  159. duk_push_boolean(ctx_,eventData[P_TOUCH].GetBool() ? 1 : 0);
  160. duk_put_prop_string(ctx_, -2, "touch");
  161. }
  162. void JSUI::HandleWidgetEvent(StringHash eventType, VariantMap& eventData)
  163. {
  164. using namespace WidgetEvent;
  165. UIWidget* handler = static_cast<UIWidget*>(eventData[P_HANDLER].GetPtr());
  166. UIWidget* target = static_cast<UIWidget*>(eventData[P_TARGET].GetPtr());
  167. if (!handler)
  168. return;
  169. void* handlerHeapPtr = handler->JSGetHeapPtr();
  170. if (!handlerHeapPtr)
  171. return;
  172. // if we have a target, however no corresponding JS object return
  173. if (target && !target->JSGetHeapPtr())
  174. return;
  175. tb::EVENT_TYPE type = (tb::EVENT_TYPE) eventData[P_TYPE].GetUInt();
  176. if (type == tb::EVENT_TYPE_CHANGED)
  177. {
  178. int top = duk_get_top(ctx_);
  179. duk_push_heapptr(ctx_, handlerHeapPtr);
  180. duk_get_prop_string(ctx_, -1, "onChanged");
  181. if (duk_is_callable(ctx_, -1)) {
  182. if (duk_pcall(ctx_, 0) != 0)
  183. {
  184. JSVM::GetJSVM(nullptr)->SendJSErrorEvent();
  185. }
  186. else
  187. {
  188. if (duk_is_boolean(ctx_, -1) && duk_to_boolean(ctx_, -1))
  189. eventData[P_HANDLED] = true;
  190. }
  191. }
  192. duk_pop_n(ctx_, 2);
  193. assert(top == duk_get_top(ctx_));
  194. return;
  195. }
  196. // general event handler, bubbles to (wrapped) parent widgets unless handled (returns true)
  197. if (type == tb::EVENT_TYPE_CLICK)
  198. {
  199. int top = duk_get_top(ctx_);
  200. duk_push_heapptr(ctx_, handlerHeapPtr);
  201. duk_get_prop_string(ctx_, -1, "onEvent");
  202. if (duk_is_callable(ctx_, -1)) {
  203. PushWidgetEventObject(eventData);
  204. duk_call(ctx_, 1);
  205. if (duk_is_boolean(ctx_, -1) && duk_to_boolean(ctx_, -1))
  206. eventData[P_HANDLED] = true;
  207. }
  208. duk_pop_n(ctx_, 2);
  209. assert(top == duk_get_top(ctx_));
  210. }
  211. // specific event handlers
  212. if (type == tb::EVENT_TYPE_CLICK && handler == target)
  213. {
  214. int top = duk_get_top(ctx_);
  215. duk_push_heapptr(ctx_, handlerHeapPtr);
  216. duk_get_prop_string(ctx_, -1, "onClick");
  217. if (duk_is_callable(ctx_, -1)) {
  218. if (duk_pcall(ctx_, 0) != 0)
  219. {
  220. JSVM::GetJSVM(nullptr)->SendJSErrorEvent();
  221. }
  222. else
  223. {
  224. if (duk_is_boolean(ctx_, -1) && duk_to_boolean(ctx_, -1))
  225. eventData[P_HANDLED] = true;
  226. }
  227. }
  228. duk_pop_n(ctx_, 2);
  229. assert(top == duk_get_top(ctx_));
  230. return;
  231. }
  232. }
  233. }