JSEventHelper.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <Atomic/UI/UIEvents.h>
  2. #include "JSVM.h"
  3. #include "JSEventHelper.h"
  4. namespace Atomic
  5. {
  6. JSEventDispatcher::JSEventDispatcher(Context* context) :
  7. Object(context)
  8. {
  9. }
  10. JSEventDispatcher::~JSEventDispatcher()
  11. {
  12. }
  13. void JSEventDispatcher::BeginSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData)
  14. {
  15. }
  16. void JSEventDispatcher::EndSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData)
  17. {
  18. if (!jsEvents_.Contains(eventType))
  19. return;
  20. JSVM* vm = JSVM::GetJSVM(NULL);
  21. if (!vm)
  22. return;
  23. duk_context* ctx = vm->GetJSContext();
  24. duk_push_global_stash(ctx);
  25. duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_VARIANTMAP_CACHE);
  26. duk_push_pointer(ctx, (void*) &eventData);
  27. duk_push_undefined(ctx);
  28. duk_put_prop(ctx, -3);
  29. duk_pop_2(ctx);
  30. }
  31. JSEventHelper::JSEventHelper(Context* context, Object* object) :
  32. Object(context),
  33. object_(object)
  34. {
  35. }
  36. JSEventHelper::~JSEventHelper()
  37. {
  38. }
  39. void JSEventHelper::AddEventHandler(StringHash eventType)
  40. {
  41. GetSubsystem<JSEventDispatcher>()->RegisterJSEvent(eventType);
  42. // subscribe using object, so unsubscribing from object and not the event helper works
  43. object_->SubscribeToEvent(eventType, HANDLER(JSEventHelper, HandleEvent));
  44. }
  45. void JSEventHelper::AddEventHandler(Object* sender, StringHash eventType)
  46. {
  47. GetSubsystem<JSEventDispatcher>()->RegisterJSEvent(eventType);
  48. // subscribe using object, so unsubscribing from object and not the event helper works
  49. object_->SubscribeToEvent(sender, eventType, HANDLER(JSEventHelper, HandleEvent));
  50. }
  51. void JSEventHelper::HandleEvent(StringHash eventType, VariantMap& eventData)
  52. {
  53. if (object_.Null())
  54. return;
  55. JSVM* vm = JSVM::GetJSVM(0);
  56. duk_context* ctx = vm->GetJSContext();
  57. duk_idx_t top = duk_get_top(ctx);
  58. js_push_class_object_instance(ctx, this);
  59. duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
  60. assert(duk_is_object(ctx, -1));
  61. duk_get_prop_string(ctx, -1, eventType.ToString().CString());
  62. if (duk_is_function(ctx, -1))
  63. {
  64. // look in the variant map cache
  65. duk_push_global_stash(ctx);
  66. duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_VARIANTMAP_CACHE);
  67. duk_push_pointer(ctx, (void*) &eventData);
  68. duk_get_prop(ctx, -2);
  69. if (!duk_is_object(ctx, -1))
  70. {
  71. // pop result
  72. duk_pop(ctx);
  73. // we need to push a new variant map and store to cache
  74. // the cache object will be cleared at the send end in the
  75. // global listener above
  76. js_push_variantmap(ctx, eventData);
  77. duk_push_pointer(ctx, (void*) &eventData);
  78. duk_dup(ctx, -2);
  79. duk_put_prop(ctx, -4);
  80. }
  81. duk_remove(ctx, -2); // vmap cache
  82. duk_remove(ctx, -2); // global stash
  83. if (duk_pcall(ctx, 1) != 0)
  84. {
  85. vm->SendJSErrorEvent();
  86. }
  87. else
  88. {
  89. // For widget events, need to check return value
  90. // and set whether handled
  91. if (eventType == E_WIDGETEVENT)
  92. {
  93. if (duk_is_boolean(ctx, -1))
  94. {
  95. if (duk_to_boolean(ctx, -1))
  96. {
  97. eventData[WidgetEvent::P_HANDLED] = true;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. duk_set_top(ctx, top);
  104. }
  105. }