JSComponent.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #pragma once
  5. #include <Atomic/Scene/Component.h>
  6. namespace Atomic
  7. {
  8. class Javascript;
  9. class JSFile;
  10. class JSVM;
  11. /// Inbuilt scripted component methods.
  12. enum JSScriptMethod
  13. {
  14. JSMETHOD_START = 0,
  15. JSMETHOD_STOP,
  16. JSMETHOD_DELAYEDSTART,
  17. JSMETHOD_UPDATE,
  18. JSMETHOD_POSTUPDATE,
  19. JSMETHOD_FIXEDUPDATE,
  20. JSMETHOD_FIXEDPOSTUPDATE,
  21. JSMETHOD_LOAD,
  22. JSMETHOD_SAVE,
  23. JSMETHOD_READNETWORKUPDATE,
  24. JSMETHOD_WRITENETWORKUPDATE,
  25. JSMETHOD_APPLYATTRIBUTES,
  26. JSMETHOD_TRANSFORMCHANGED,
  27. MAX_JSSCRIPT_METHODS
  28. };
  29. class ATOMIC_API JSComponent : public Component
  30. {
  31. OBJECT(JSComponent);
  32. public:
  33. /// Construct.
  34. JSComponent(Context* context);
  35. /// Destruct.
  36. virtual ~JSComponent();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Return class name.
  40. const String& GetClassName() const { return className_; }
  41. /// Return script file attribute.
  42. ResourceRef GetScriptFileAttr() const;
  43. void SetScriptFileAttr(ResourceRef value);
  44. void SetClassName(const String& className);
  45. const String& GetClassNameProperty() const { return classNameProperty_; }
  46. void SetClassNameProperty(const String& className) { classNameProperty_ = className; }
  47. /// Handle enabled/disabled state change.
  48. virtual void OnSetEnabled();
  49. void OnNodeSet(Node *node);
  50. bool GetDestroyed() { return destroyed_; }
  51. void SetDestroyed() { destroyed_ = true; }
  52. /// Create object of certain class from the script file. Return true if successful.
  53. bool CreateObject(JSFile* scriptFile, const String& className);
  54. /// Set script file only. Recreate object if necessary.
  55. void SetScriptFile(JSFile* scriptFile);
  56. void ListenToEvent(Object* sender, StringHash eventType, JS_HEAP_PTR __duk_function);
  57. void ApplyAttributes();
  58. private:
  59. /// (Re)create the script object and check for supported methods if successfully created.
  60. void CreateObject();
  61. void ReleaseObject();
  62. void ClearScriptMethods();
  63. void UpdateEventSubscription();
  64. void GetScriptMethods();
  65. /// Handle scene update event.
  66. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  67. /// Handle scene post-update event.
  68. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  69. #ifdef ATOMIC_PHYSICS
  70. /// Handle physics pre-step event.
  71. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  72. /// Handle physics post-step event.
  73. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  74. #endif
  75. /// Handle an event in script.
  76. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  77. /// Class name.
  78. String className_;
  79. String classNameProperty_;
  80. /// Script subsystem.
  81. SharedPtr<Javascript> script_;
  82. WeakPtr<JSVM> vm_;
  83. HashMap<StringHash, JS_HEAP_PTR> scriptEventFunctions_;
  84. /// Script object.
  85. void* scriptObject_;
  86. /// Pointers to supported inbuilt methods.
  87. void* methods_[MAX_JSSCRIPT_METHODS];
  88. /// Subscribed to scene update events flag.
  89. bool subscribed_;
  90. /// Subscribed to scene post and fixed update events flag.
  91. bool subscribedPostFixed_;
  92. bool started_;
  93. bool destroyed_;
  94. };
  95. }