CSComponent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. ATTRIBUTE("FieldValues", VariantMap, fieldValues_, Variant::emptyVariantMap, AM_FILE);
  104. MIXED_ACCESSOR_ATTRIBUTE("Assembly", GetAssemblyFileAttr, SetAssemblyFileAttr, ResourceRef, ResourceRef(NETAssemblyFile::GetTypeStatic()), AM_DEFAULT);
  105. ACCESSOR_ATTRIBUTE("Class", GetComponentClassName, SetComponentClassName, String, String::EMPTY, AM_DEFAULT);
  106. }
  107. void CSComponent::OnSetEnabled()
  108. {
  109. UpdateEventSubscription();
  110. }
  111. void CSComponent::SetUpdateEventMask(unsigned char mask)
  112. {
  113. if (updateEventMask_ != mask)
  114. {
  115. updateEventMask_ = mask;
  116. UpdateEventSubscription();
  117. }
  118. }
  119. void CSComponent::ApplyAttributes()
  120. {
  121. }
  122. void CSComponent::CallScriptMethod(CSComponentMethod method, float value)
  123. {
  124. if (destroyed_ || !node_ || !node_->GetScene())
  125. return;
  126. // Change to callback
  127. //CSComponentCallMethod(GetRefID(), method, value);
  128. }
  129. void CSComponent::Start()
  130. {
  131. CallScriptMethod(CSComponentMethod_Start);
  132. }
  133. void CSComponent::DelayedStart()
  134. {
  135. CallScriptMethod(CSComponentMethod_DelayedStart);
  136. }
  137. void CSComponent::Update(float timeStep)
  138. {
  139. //if (!instanceInitialized_)
  140. // InitInstance();
  141. if (!started_)
  142. {
  143. started_ = true;
  144. Start();
  145. }
  146. CallScriptMethod(CSComponentMethod_Update, timeStep);
  147. }
  148. void CSComponent::PostUpdate(float timeStep)
  149. {
  150. CallScriptMethod(CSComponentMethod_PostUpdate, timeStep);
  151. }
  152. void CSComponent::FixedUpdate(float timeStep)
  153. {
  154. CallScriptMethod(CSComponentMethod_FixedUpdate, timeStep);
  155. }
  156. void CSComponent::FixedPostUpdate(float timeStep)
  157. {
  158. CallScriptMethod(CSComponentMethod_PostFixedUpdate, timeStep);
  159. }
  160. void CSComponent::OnNodeSet(Node* node)
  161. {
  162. if (node)
  163. {
  164. //UpdateReferences();
  165. }
  166. else
  167. {
  168. // We are being detached from a node: execute user-defined stop function and prepare for destruction
  169. //UpdateReferences(true);
  170. Stop();
  171. }
  172. }
  173. void CSComponent::OnSceneSet(Scene* scene)
  174. {
  175. if (scene)
  176. UpdateEventSubscription();
  177. else
  178. {
  179. UnsubscribeFromEvent(E_SCENEUPDATE);
  180. UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
  181. #ifdef ATOMIC_PHYSICS
  182. UnsubscribeFromEvent(E_PHYSICSPRESTEP);
  183. UnsubscribeFromEvent(E_PHYSICSPOSTSTEP);
  184. #endif
  185. currentEventMask_ = 0;
  186. }
  187. }
  188. void CSComponent::UpdateEventSubscription()
  189. {
  190. Scene* scene = GetScene();
  191. if (!scene)
  192. return;
  193. bool enabled = IsEnabledEffective();
  194. bool needUpdate = enabled && ((updateEventMask_ & USE_UPDATE) || !delayedStartCalled_);
  195. if (needUpdate && !(currentEventMask_ & USE_UPDATE))
  196. {
  197. SubscribeToEvent(scene, E_SCENEUPDATE, HANDLER(CSComponent, HandleSceneUpdate));
  198. currentEventMask_ |= USE_UPDATE;
  199. }
  200. else if (!needUpdate && (currentEventMask_ & USE_UPDATE))
  201. {
  202. UnsubscribeFromEvent(scene, E_SCENEUPDATE);
  203. currentEventMask_ &= ~USE_UPDATE;
  204. }
  205. bool needPostUpdate = enabled && (updateEventMask_ & USE_POSTUPDATE);
  206. if (needPostUpdate && !(currentEventMask_ & USE_POSTUPDATE))
  207. {
  208. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(CSComponent, HandleScenePostUpdate));
  209. currentEventMask_ |= USE_POSTUPDATE;
  210. }
  211. else if (!needUpdate && (currentEventMask_ & USE_POSTUPDATE))
  212. {
  213. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  214. currentEventMask_ &= ~USE_POSTUPDATE;
  215. }
  216. #ifdef ATOMIC_PHYSICS
  217. PhysicsWorld* world = scene->GetComponent<PhysicsWorld>();
  218. if (!world)
  219. return;
  220. bool needFixedUpdate = enabled && (updateEventMask_ & USE_FIXEDUPDATE);
  221. if (needFixedUpdate && !(currentEventMask_ & USE_FIXEDUPDATE))
  222. {
  223. SubscribeToEvent(world, E_PHYSICSPRESTEP, HANDLER(CSComponent, HandlePhysicsPreStep));
  224. currentEventMask_ |= USE_FIXEDUPDATE;
  225. }
  226. else if (!needFixedUpdate && (currentEventMask_ & USE_FIXEDUPDATE))
  227. {
  228. UnsubscribeFromEvent(world, E_PHYSICSPRESTEP);
  229. currentEventMask_ &= ~USE_FIXEDUPDATE;
  230. }
  231. bool needFixedPostUpdate = enabled && (updateEventMask_ & USE_FIXEDPOSTUPDATE);
  232. if (needFixedPostUpdate && !(currentEventMask_ & USE_FIXEDPOSTUPDATE))
  233. {
  234. SubscribeToEvent(world, E_PHYSICSPOSTSTEP, HANDLER(CSComponent, HandlePhysicsPostStep));
  235. currentEventMask_ |= USE_FIXEDPOSTUPDATE;
  236. }
  237. else if (!needFixedPostUpdate && (currentEventMask_ & USE_FIXEDPOSTUPDATE))
  238. {
  239. UnsubscribeFromEvent(world, E_PHYSICSPOSTSTEP);
  240. currentEventMask_ &= ~USE_FIXEDPOSTUPDATE;
  241. }
  242. #endif
  243. }
  244. void CSComponent::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  245. {
  246. using namespace SceneUpdate;
  247. assert(!destroyed_);
  248. // Execute user-defined delayed start function before first update
  249. if (!delayedStartCalled_)
  250. {
  251. DelayedStart();
  252. delayedStartCalled_ = true;
  253. // If did not need actual update events, unsubscribe now
  254. if (!(updateEventMask_ & USE_UPDATE))
  255. {
  256. UnsubscribeFromEvent(GetScene(), E_SCENEUPDATE);
  257. currentEventMask_ &= ~USE_UPDATE;
  258. return;
  259. }
  260. }
  261. // Then execute user-defined update function
  262. Update(eventData[P_TIMESTEP].GetFloat());
  263. }
  264. void CSComponent::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  265. {
  266. using namespace ScenePostUpdate;
  267. // Execute user-defined post-update function
  268. PostUpdate(eventData[P_TIMESTEP].GetFloat());
  269. }
  270. #ifdef ATOMIC_PHYSICS
  271. void CSComponent::HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
  272. {
  273. using namespace PhysicsPreStep;
  274. // Execute user-defined fixed update function
  275. FixedUpdate(eventData[P_TIMESTEP].GetFloat());
  276. }
  277. void CSComponent::HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData)
  278. {
  279. using namespace PhysicsPostStep;
  280. // Execute user-defined fixed post-update function
  281. FixedPostUpdate(eventData[P_TIMESTEP].GetFloat());
  282. }
  283. #endif
  284. bool CSComponent::Load(Deserializer& source, bool setInstanceDefault)
  285. {
  286. loading_ = true;
  287. bool success = Component::Load(source, setInstanceDefault);
  288. loading_ = false;
  289. return success;
  290. }
  291. bool CSComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  292. {
  293. loading_ = true;
  294. bool success = Component::LoadXML(source, setInstanceDefault);
  295. loading_ = false;
  296. return success;
  297. }
  298. void CSComponent::SetAssemblyFile(NETAssemblyFile* assemblyFile)
  299. {
  300. assemblyFile_ = assemblyFile;
  301. }
  302. ResourceRef CSComponent::GetAssemblyFileAttr() const
  303. {
  304. return GetResourceRef(assemblyFile_, NETAssemblyFile::GetTypeStatic());
  305. }
  306. void CSComponent::SetAssemblyFileAttr(const ResourceRef& value)
  307. {
  308. ResourceCache* cache = GetSubsystem<ResourceCache>();
  309. SetAssemblyFile(cache->GetResource<NETAssemblyFile>(value.name_));
  310. }
  311. }