CSComponent.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Core/Context.h>
  26. #include <Atomic/Resource/ResourceCache.h>
  27. #ifdef ATOMIC_PHYSICS
  28. #include <Atomic/Physics/PhysicsEvents.h>
  29. #include <Atomic/Physics/PhysicsWorld.h>
  30. #endif
  31. #include <Atomic/Scene/Scene.h>
  32. #include <Atomic/Scene/SceneEvents.h>
  33. #include "CSComponent.h"
  34. //#include "AtomicSharpAPI.h"
  35. namespace Atomic
  36. {
  37. extern const char* LOGIC_CATEGORY;
  38. class CSComponentFactory : public ObjectFactory
  39. {
  40. public:
  41. /// Construct.
  42. CSComponentFactory(Context* context) :
  43. ObjectFactory(context)
  44. {
  45. type_ = CSComponent::GetTypeStatic();
  46. baseType_ = CSComponent::GetBaseTypeStatic();
  47. typeName_ = CSComponent::GetTypeNameStatic();
  48. }
  49. /// Create an object of the specific type.
  50. SharedPtr<Object> CreateObject(const XMLElement& source = XMLElement::EMPTY)
  51. {
  52. // if in editor, just create the CSComponent
  53. if (context_->GetEditorContext())
  54. {
  55. return SharedPtr<Object>(new CSComponent(context_));
  56. }
  57. // At runtime, a XML CSComponent may refer to a managed component
  58. String managedClass;
  59. if (source != XMLElement::EMPTY)
  60. {
  61. XMLElement attrElem = source.GetChild("attribute");
  62. while (attrElem)
  63. {
  64. if (attrElem.GetAttribute("name") == "ManagedClass")
  65. {
  66. managedClass = attrElem.GetAttribute("value");
  67. break;
  68. }
  69. attrElem = attrElem.GetNext("attribute");
  70. }
  71. }
  72. SharedPtr<Object> ptr;
  73. if (managedClass.Length())
  74. {
  75. // change to callback
  76. //ptr = CSComponentCreate(managedClass);
  77. }
  78. if (ptr.Null())
  79. {
  80. ptr = new CSComponent(context_);
  81. }
  82. return ptr; }
  83. };
  84. CSComponent::CSComponent(Context* context) :
  85. Component(context),
  86. updateEventMask_(USE_UPDATE | USE_POSTUPDATE | USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE),
  87. currentEventMask_(0),
  88. instanceInitialized_(false),
  89. started_(false),
  90. destroyed_(false),
  91. scriptClassInstance_(false),
  92. delayedStartCalled_(false),
  93. loading_(false)
  94. {
  95. }
  96. CSComponent::~CSComponent()
  97. {
  98. }
  99. void CSComponent::RegisterObject(Context* context)
  100. {
  101. context->RegisterFactory(new CSComponentFactory(context), LOGIC_CATEGORY);
  102. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  103. }
  104. void CSComponent::OnSetEnabled()
  105. {
  106. UpdateEventSubscription();
  107. }
  108. void CSComponent::SetUpdateEventMask(unsigned char mask)
  109. {
  110. if (updateEventMask_ != mask)
  111. {
  112. updateEventMask_ = mask;
  113. UpdateEventSubscription();
  114. }
  115. }
  116. void CSComponent::ApplyAttributes()
  117. {
  118. }
  119. void CSComponent::CallScriptMethod(CSComponentMethod method, float value)
  120. {
  121. if (destroyed_ || !node_ || !node_->GetScene())
  122. return;
  123. // Change to callback
  124. //CSComponentCallMethod(GetRefID(), method, value);
  125. }
  126. void CSComponent::Start()
  127. {
  128. CallScriptMethod(CSComponentMethod_Start);
  129. }
  130. void CSComponent::DelayedStart()
  131. {
  132. CallScriptMethod(CSComponentMethod_DelayedStart);
  133. }
  134. void CSComponent::Update(float timeStep)
  135. {
  136. //if (!instanceInitialized_)
  137. // InitInstance();
  138. if (!started_)
  139. {
  140. started_ = true;
  141. Start();
  142. }
  143. CallScriptMethod(CSComponentMethod_Update, timeStep);
  144. }
  145. void CSComponent::PostUpdate(float timeStep)
  146. {
  147. CallScriptMethod(CSComponentMethod_PostUpdate, timeStep);
  148. }
  149. void CSComponent::FixedUpdate(float timeStep)
  150. {
  151. CallScriptMethod(CSComponentMethod_FixedUpdate, timeStep);
  152. }
  153. void CSComponent::FixedPostUpdate(float timeStep)
  154. {
  155. CallScriptMethod(CSComponentMethod_PostFixedUpdate, timeStep);
  156. }
  157. void CSComponent::OnNodeSet(Node* node)
  158. {
  159. if (node)
  160. {
  161. //UpdateReferences();
  162. }
  163. else
  164. {
  165. // We are being detached from a node: execute user-defined stop function and prepare for destruction
  166. //UpdateReferences(true);
  167. Stop();
  168. }
  169. }
  170. void CSComponent::OnSceneSet(Scene* scene)
  171. {
  172. if (scene)
  173. UpdateEventSubscription();
  174. else
  175. {
  176. UnsubscribeFromEvent(E_SCENEUPDATE);
  177. UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
  178. #ifdef ATOMIC_PHYSICS
  179. UnsubscribeFromEvent(E_PHYSICSPRESTEP);
  180. UnsubscribeFromEvent(E_PHYSICSPOSTSTEP);
  181. #endif
  182. currentEventMask_ = 0;
  183. }
  184. }
  185. void CSComponent::UpdateEventSubscription()
  186. {
  187. Scene* scene = GetScene();
  188. if (!scene)
  189. return;
  190. bool enabled = IsEnabledEffective();
  191. bool needUpdate = enabled && ((updateEventMask_ & USE_UPDATE) || !delayedStartCalled_);
  192. if (needUpdate && !(currentEventMask_ & USE_UPDATE))
  193. {
  194. SubscribeToEvent(scene, E_SCENEUPDATE, HANDLER(CSComponent, HandleSceneUpdate));
  195. currentEventMask_ |= USE_UPDATE;
  196. }
  197. else if (!needUpdate && (currentEventMask_ & USE_UPDATE))
  198. {
  199. UnsubscribeFromEvent(scene, E_SCENEUPDATE);
  200. currentEventMask_ &= ~USE_UPDATE;
  201. }
  202. bool needPostUpdate = enabled && (updateEventMask_ & USE_POSTUPDATE);
  203. if (needPostUpdate && !(currentEventMask_ & USE_POSTUPDATE))
  204. {
  205. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(CSComponent, HandleScenePostUpdate));
  206. currentEventMask_ |= USE_POSTUPDATE;
  207. }
  208. else if (!needUpdate && (currentEventMask_ & USE_POSTUPDATE))
  209. {
  210. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  211. currentEventMask_ &= ~USE_POSTUPDATE;
  212. }
  213. #ifdef ATOMIC_PHYSICS
  214. PhysicsWorld* world = scene->GetComponent<PhysicsWorld>();
  215. if (!world)
  216. return;
  217. bool needFixedUpdate = enabled && (updateEventMask_ & USE_FIXEDUPDATE);
  218. if (needFixedUpdate && !(currentEventMask_ & USE_FIXEDUPDATE))
  219. {
  220. SubscribeToEvent(world, E_PHYSICSPRESTEP, HANDLER(CSComponent, HandlePhysicsPreStep));
  221. currentEventMask_ |= USE_FIXEDUPDATE;
  222. }
  223. else if (!needFixedUpdate && (currentEventMask_ & USE_FIXEDUPDATE))
  224. {
  225. UnsubscribeFromEvent(world, E_PHYSICSPRESTEP);
  226. currentEventMask_ &= ~USE_FIXEDUPDATE;
  227. }
  228. bool needFixedPostUpdate = enabled && (updateEventMask_ & USE_FIXEDPOSTUPDATE);
  229. if (needFixedPostUpdate && !(currentEventMask_ & USE_FIXEDPOSTUPDATE))
  230. {
  231. SubscribeToEvent(world, E_PHYSICSPOSTSTEP, HANDLER(CSComponent, HandlePhysicsPostStep));
  232. currentEventMask_ |= USE_FIXEDPOSTUPDATE;
  233. }
  234. else if (!needFixedPostUpdate && (currentEventMask_ & USE_FIXEDPOSTUPDATE))
  235. {
  236. UnsubscribeFromEvent(world, E_PHYSICSPOSTSTEP);
  237. currentEventMask_ &= ~USE_FIXEDPOSTUPDATE;
  238. }
  239. #endif
  240. }
  241. void CSComponent::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  242. {
  243. using namespace SceneUpdate;
  244. assert(!destroyed_);
  245. // Execute user-defined delayed start function before first update
  246. if (!delayedStartCalled_)
  247. {
  248. DelayedStart();
  249. delayedStartCalled_ = true;
  250. // If did not need actual update events, unsubscribe now
  251. if (!(updateEventMask_ & USE_UPDATE))
  252. {
  253. UnsubscribeFromEvent(GetScene(), E_SCENEUPDATE);
  254. currentEventMask_ &= ~USE_UPDATE;
  255. return;
  256. }
  257. }
  258. // Then execute user-defined update function
  259. Update(eventData[P_TIMESTEP].GetFloat());
  260. }
  261. void CSComponent::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  262. {
  263. using namespace ScenePostUpdate;
  264. // Execute user-defined post-update function
  265. PostUpdate(eventData[P_TIMESTEP].GetFloat());
  266. }
  267. #ifdef ATOMIC_PHYSICS
  268. void CSComponent::HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
  269. {
  270. using namespace PhysicsPreStep;
  271. // Execute user-defined fixed update function
  272. FixedUpdate(eventData[P_TIMESTEP].GetFloat());
  273. }
  274. void CSComponent::HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData)
  275. {
  276. using namespace PhysicsPostStep;
  277. // Execute user-defined fixed post-update function
  278. FixedPostUpdate(eventData[P_TIMESTEP].GetFloat());
  279. }
  280. #endif
  281. bool CSComponent::Load(Deserializer& source, bool setInstanceDefault)
  282. {
  283. loading_ = true;
  284. bool success = Component::Load(source, setInstanceDefault);
  285. loading_ = false;
  286. return success;
  287. }
  288. bool CSComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  289. {
  290. loading_ = true;
  291. bool success = Component::LoadXML(source, setInstanceDefault);
  292. loading_ = false;
  293. return success;
  294. }
  295. }