JSComponent.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /// Handle enabled/disabled state change.
  46. virtual void OnSetEnabled();
  47. void OnNodeSet(Node *node);
  48. void SetDestroyed() { destroyed_ = true; }
  49. /// Create object of certain class from the script file. Return true if successful.
  50. bool CreateObject(JSFile* scriptFile, const String& className);
  51. /// Set script file only. Recreate object if necessary.
  52. void SetScriptFile(JSFile* scriptFile);
  53. void ListenToEvent(Object* sender, StringHash eventType, JS_HEAP_PTR __duk_function);
  54. private:
  55. /// (Re)create the script object and check for supported methods if successfully created.
  56. void CreateObject();
  57. void ReleaseObject();
  58. void ClearScriptMethods();
  59. void UpdateEventSubscription();
  60. void GetScriptMethods();
  61. /// Handle scene update event.
  62. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  63. /// Handle scene post-update event.
  64. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  65. #ifdef ATOMIC_PHYSICS
  66. /// Handle physics pre-step event.
  67. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  68. /// Handle physics post-step event.
  69. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  70. #endif
  71. /// Handle an event in script.
  72. void HandleScriptEvent(StringHash eventType, VariantMap& eventData);
  73. /// Class name.
  74. String className_;
  75. /// Script subsystem.
  76. SharedPtr<Javascript> script_;
  77. WeakPtr<JSVM> vm_;
  78. HashMap<StringHash, JS_HEAP_PTR> scriptEventFunctions_;
  79. /// Script object.
  80. void* scriptObject_;
  81. /// Pointers to supported inbuilt methods.
  82. void* methods_[MAX_JSSCRIPT_METHODS];
  83. /// Subscribed to scene update events flag.
  84. bool subscribed_;
  85. /// Subscribed to scene post and fixed update events flag.
  86. bool subscribedPostFixed_;
  87. bool started_;
  88. bool destroyed_;
  89. };
  90. }