JSEventHelper.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. JSVM* vm = JSVM::GetJSVM(NULL);
  16. if (!vm)
  17. return;
  18. if (!jsEvents_.Contains(eventType))
  19. return;
  20. duk_context* ctx = vm->GetJSContext();
  21. duk_push_global_stash(ctx);
  22. duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_VARIANTMAP_CACHE);
  23. duk_push_pointer(ctx, (void*) &eventData);
  24. js_push_variantmap(ctx, eventData);
  25. duk_put_prop(ctx, -3);
  26. duk_pop_2(ctx);
  27. }
  28. void JSEventDispatcher::EndSendEvent(Context* context, Object* sender, StringHash eventType, VariantMap& eventData)
  29. {
  30. JSVM* vm = JSVM::GetJSVM(NULL);
  31. if (!vm)
  32. return;
  33. if (!jsEvents_.Contains(eventType))
  34. return;
  35. duk_context* ctx = vm->GetJSContext();
  36. duk_push_global_stash(ctx);
  37. duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_VARIANTMAP_CACHE);
  38. duk_push_pointer(ctx, (void*) &eventData);
  39. duk_push_undefined(ctx);
  40. duk_put_prop(ctx, -3);
  41. duk_pop_2(ctx);
  42. }
  43. JSEventHelper::JSEventHelper(Context* context) :
  44. Object(context)
  45. {
  46. }
  47. JSEventHelper::~JSEventHelper()
  48. {
  49. }
  50. void JSEventHelper::AddEventHandler(StringHash eventType)
  51. {
  52. GetSubsystem<JSEventDispatcher>()->RegisterJSEvent(eventType);
  53. SubscribeToEvent(eventType, HANDLER(JSEventHelper, HandleEvent));
  54. }
  55. void JSEventHelper::AddEventHandler(Object* sender, StringHash eventType)
  56. {
  57. GetSubsystem<JSEventDispatcher>()->RegisterJSEvent(eventType);
  58. SubscribeToEvent(sender, eventType, HANDLER(JSEventHelper, HandleEvent));
  59. }
  60. void JSEventHelper::HandleEvent(StringHash eventType, VariantMap& eventData)
  61. {
  62. JSVM* vm = JSVM::GetJSVM(0);
  63. duk_context* ctx = vm->GetJSContext();
  64. duk_idx_t top = duk_get_top(ctx);
  65. js_push_class_object_instance(ctx, this);
  66. duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
  67. assert(duk_is_object(ctx, -1));
  68. duk_get_prop_string(ctx, -1, eventType.ToString().CString());
  69. if (duk_is_function(ctx, -1))
  70. {
  71. assert(duk_is_object(ctx, -1));
  72. duk_push_global_stash(ctx);
  73. duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_VARIANTMAP_CACHE);
  74. duk_push_pointer(ctx, (void*) &eventData);
  75. duk_get_prop(ctx, -2);
  76. duk_remove(ctx, -2); // vmap cache
  77. duk_remove(ctx, -2); // global stash
  78. if (!duk_is_object(ctx, -1))
  79. {
  80. duk_set_top(ctx, top);
  81. return;
  82. }
  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. }