JSEventHelper.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <Atomic/UI/UIEvents.h>
  2. #include "JSVM.h"
  3. #include "JSEventHelper.h"
  4. namespace Atomic
  5. {
  6. JSEventHelper::JSEventHelper(Context* context) :
  7. Object(context),
  8. currentData_((VariantMap&) Variant::emptyVariantMap)
  9. {
  10. }
  11. JSEventHelper::~JSEventHelper()
  12. {
  13. LOGINFO("Boom");
  14. }
  15. void JSEventHelper::AddEventHandler(StringHash eventType)
  16. {
  17. SubscribeToEvent(eventType, HANDLER(JSEventHelper, HandleEvent));
  18. }
  19. void JSEventHelper::AddEventHandler(Object* sender, StringHash eventType)
  20. {
  21. SubscribeToEvent(sender, eventType, HANDLER(JSEventHelper, HandleEvent));
  22. }
  23. void JSEventHelper::HandleEvent(StringHash eventType, VariantMap& eventData)
  24. {
  25. JSVM* vm = JSVM::GetJSVM(context_);
  26. duk_context* ctx = vm->GetJSContext();
  27. duk_idx_t top = duk_get_top(ctx);
  28. js_push_class_object_instance(ctx, this);
  29. duk_get_prop_string(ctx, -1, "__eventHelperFunctions");
  30. assert(duk_is_object(ctx, -1));
  31. duk_get_prop_string(ctx, -1, eventType.ToString().CString());
  32. if (duk_is_function(ctx, -1))
  33. {
  34. currentData_ = (const VariantMap&) eventData;
  35. // pass in event helper proxy
  36. duk_get_prop_string(ctx, -3, "__eventHelperProxy");
  37. assert(duk_is_object(ctx, -1));
  38. if (duk_pcall(ctx, 1) != 0)
  39. {
  40. vm->SendJSErrorEvent();
  41. }
  42. else
  43. {
  44. // For widget events, need to check return value
  45. // and set whether handled
  46. if (eventType == E_WIDGETEVENT)
  47. {
  48. if (duk_is_boolean(ctx, -1))
  49. {
  50. if (duk_to_boolean(ctx, -1))
  51. {
  52. eventData[WidgetEvent::P_HANDLED] = true;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. duk_set_top(ctx, top);
  59. }
  60. }