JSComponent.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include <Atomic/Script/ScriptComponent.h>
  25. #include "JSComponentFile.h"
  26. namespace Atomic
  27. {
  28. class JSVM;
  29. /// JavaScript component
  30. class JSComponent : public ScriptComponent
  31. {
  32. friend class JSComponentFactory;
  33. friend class JSComponentFile;
  34. ATOMIC_OBJECT(JSComponent, ScriptComponent);
  35. enum EventFlags
  36. {
  37. USE_UPDATE = 0x1,
  38. USE_POSTUPDATE = 0x2,
  39. USE_FIXEDUPDATE = 0x4,
  40. USE_FIXEDPOSTUPDATE = 0x8
  41. };
  42. public:
  43. /// Construct.
  44. JSComponent(Context* context);
  45. /// Destruct.
  46. virtual ~JSComponent();
  47. /// Register object factory.
  48. static void RegisterObject(Context* context);
  49. /// Match script name
  50. bool MatchScriptName(const String& path);
  51. /// Handle enabled/disabled state change. Changes update event subscription.
  52. virtual void OnSetEnabled();
  53. /// Set what update events should be subscribed to. Use this for optimization: by default all are in use. Note that this is not an attribute and is not saved or network-serialized, therefore it should always be called eg. in the subclass constructor.
  54. void SetUpdateEventMask(unsigned char mask);
  55. /// Return what update events are subscribed to.
  56. unsigned char GetUpdateEventMask() const { return updateEventMask_; }
  57. /// Return whether the DelayedStart() function has been called.
  58. bool IsDelayedStartCalled() const { return delayedStartCalled_; }
  59. void SetDestroyed() { destroyed_ = true; }
  60. bool IsInstanceInitialized();
  61. void InitInstance(bool hasArgs = false, int argIdx = 0);
  62. /// Get script attribute
  63. ResourceRef GetComponentFileAttr() const;
  64. ScriptComponentFile* GetComponentFile() const { return componentFile_; }
  65. /// Set script attribute.
  66. void SetComponentFile(JSComponentFile* cfile) { componentFile_ = cfile; }
  67. void SetComponentFileAttr(const ResourceRef& value);
  68. // a JSComponentFile only holds one class, so no classname to look up in it
  69. const String& GetComponentClassName() const { return String::EMPTY; }
  70. protected:
  71. /// Handle scene node being assigned at creation.
  72. virtual void OnNodeSet(Node* node);
  73. /// Handle scene being assigned.
  74. virtual void OnSceneSet(Scene* scene);
  75. private:
  76. /// Subscribe/unsubscribe to update events based on current enabled state and update event mask.
  77. void UpdateEventSubscription();
  78. /// Handle scene update event.
  79. void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
  80. /// Handle scene post-update event.
  81. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  82. #ifdef ATOMIC_PHYSICS
  83. /// Handle physics pre-step event.
  84. void HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData);
  85. /// Handle physics post-step event.
  86. void HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData);
  87. #endif
  88. void CallScriptMethod(const String& name, bool passValue = false, float value = 0.0f);
  89. /// Called when the component is added to a scene node. Other components may not yet exist.
  90. virtual void Start();
  91. /// Called before the first update. At this point all other components of the node should exist. Will also be called if update events are not wanted; in that case the event is immediately unsubscribed afterward.
  92. virtual void DelayedStart();
  93. /// Called when the component is detached from a scene node, usually on destruction. Note that you will no longer have access to the node and scene at that point.
  94. virtual void Stop() {}
  95. /// Called on scene update, variable timestep.
  96. virtual void Update(float timeStep);
  97. /// Called on scene post-update, variable timestep.
  98. virtual void PostUpdate(float timeStep);
  99. /// Called on physics update, fixed timestep.
  100. virtual void FixedUpdate(float timeStep);
  101. /// Called on physics post-update, fixed timestep.
  102. virtual void FixedPostUpdate(float timeStep);
  103. /// Requested event subscription mask.
  104. unsigned char updateEventMask_;
  105. /// Current event subscription mask.
  106. unsigned char currentEventMask_;
  107. bool instanceInitialized_;
  108. bool started_;
  109. bool destroyed_;
  110. bool scriptClassInstance_;
  111. /// Flag for delayed start.
  112. bool delayedStartCalled_;
  113. bool loading_;
  114. WeakPtr<JSVM> vm_;
  115. SharedPtr<JSComponentFile> componentFile_;
  116. };
  117. }