JSUI.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <TurboBadger/tb_widgets.h>
  2. #include <Atomic/UI/UIEvents.h>
  3. #include <Atomic/UI/UIWidget.h>
  4. #include "JSVM.h"
  5. #include "JSUI.h"
  6. namespace Atomic
  7. {
  8. JSUI::JSUI(Context* context) : Object(context)
  9. {
  10. ctx_ = JSVM::GetJSVM(nullptr)->GetJSContext();
  11. SubscribeToEvent(E_WIDGETEVENT, HANDLER(JSUI, HandleWidgetEvent));
  12. }
  13. JSUI::~JSUI()
  14. {
  15. }
  16. void JSUI::HandleWidgetEvent(StringHash eventType, VariantMap& eventData)
  17. {
  18. using namespace WidgetEvent;
  19. UIWidget* widget = static_cast<UIWidget*>(eventData[P_TARGET].GetPtr());
  20. if (!widget)
  21. return;
  22. void* heapptr = widget->JSGetHeapPtr();
  23. if (!heapptr)
  24. return;
  25. tb::EVENT_TYPE type = (tb::EVENT_TYPE) eventData[P_TYPE].GetUInt();
  26. if (type == tb::EVENT_TYPE_CLICK)
  27. {
  28. int top = duk_get_top(ctx_);
  29. duk_push_heapptr(ctx_, heapptr);
  30. duk_get_prop_string(ctx_, -1, "onClick");
  31. if (duk_is_callable(ctx_, -1)) {
  32. duk_call(ctx_, 0);
  33. }
  34. duk_pop_n(ctx_, 2);
  35. assert(top == duk_get_top(ctx_));
  36. return;
  37. }
  38. eventData[P_TYPE];
  39. eventData[P_X];
  40. eventData[P_Y];
  41. eventData[P_DELTAX];
  42. eventData[P_DELTAY];
  43. eventData[P_COUNT];
  44. eventData[P_KEY];
  45. eventData[P_SPECIALKEY];
  46. eventData[P_MODIFIERKEYS];
  47. eventData[P_ID];
  48. eventData[P_TOUCH];
  49. }
  50. }