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