CSComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. String assemblyRef;
  60. if (source != XMLElement::EMPTY)
  61. {
  62. XMLElement attrElem = source.GetChild("attribute");
  63. while (attrElem)
  64. {
  65. if (attrElem.GetAttribute("name") == "Assembly")
  66. {
  67. assemblyRef = attrElem.GetAttribute("value");
  68. }
  69. else if (attrElem.GetAttribute("name") == "Class")
  70. {
  71. managedClass = attrElem.GetAttribute("value");
  72. }
  73. if (assemblyRef.Length() && managedClass.Length())
  74. break;
  75. attrElem = attrElem.GetNext("attribute");
  76. }
  77. }
  78. SharedPtr<Object> ptr;
  79. if (assemblyRef.Length())
  80. {
  81. Vector<String> split = assemblyRef.Split(';');
  82. if (split.Size() == 2)
  83. {
  84. ResourceCache* cache = context_->GetSubsystem<ResourceCache>();
  85. NETAssemblyFile* componentFile = cache->GetResource<NETAssemblyFile>(split[1]);
  86. if (componentFile)
  87. ptr = componentFile->CreateCSComponent(managedClass);
  88. else
  89. {
  90. LOGERRORF("Unable to load component file %s", split[1].CString());
  91. }
  92. }
  93. }
  94. if (ptr.Null())
  95. {
  96. ptr = new CSComponent(context_);
  97. }
  98. return ptr; }
  99. };
  100. CSComponent::CSComponent(Context* context) :
  101. ScriptComponent(context),
  102. updateEventMask_(USE_UPDATE | USE_POSTUPDATE | USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE),
  103. currentEventMask_(0),
  104. instanceInitialized_(false),
  105. started_(false),
  106. destroyed_(false),
  107. scriptClassInstance_(false),
  108. delayedStartCalled_(false),
  109. loading_(false)
  110. {
  111. }
  112. CSComponent::~CSComponent()
  113. {
  114. }
  115. void CSComponent::RegisterObject(Context* context)
  116. {
  117. context->RegisterFactory(new CSComponentFactory(context), LOGIC_CATEGORY);
  118. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  119. ATTRIBUTE("FieldValues", VariantMap, fieldValues_, Variant::emptyVariantMap, AM_FILE);
  120. MIXED_ACCESSOR_ATTRIBUTE("Assembly", GetAssemblyFileAttr, SetAssemblyFileAttr, ResourceRef, ResourceRef(NETAssemblyFile::GetTypeStatic()), AM_DEFAULT);
  121. ACCESSOR_ATTRIBUTE("Class", GetComponentClassName, SetComponentClassName, String, String::EMPTY, AM_DEFAULT);
  122. }
  123. void CSComponent::OnSetEnabled()
  124. {
  125. UpdateEventSubscription();
  126. }
  127. void CSComponent::SetUpdateEventMask(unsigned char mask)
  128. {
  129. if (updateEventMask_ != mask)
  130. {
  131. updateEventMask_ = mask;
  132. UpdateEventSubscription();
  133. }
  134. }
  135. void CSComponent::ApplyAttributes()
  136. {
  137. }
  138. void CSComponent::CallScriptMethod(CSComponentMethod method, float value)
  139. {
  140. if (destroyed_ || !node_ || !node_->GetScene())
  141. return;
  142. // Change to callback
  143. //CSComponentCallMethod(GetRefID(), method, value);
  144. }
  145. void CSComponent::Start()
  146. {
  147. CallScriptMethod(CSComponentMethod_Start);
  148. }
  149. void CSComponent::DelayedStart()
  150. {
  151. CallScriptMethod(CSComponentMethod_DelayedStart);
  152. }
  153. void CSComponent::Update(float timeStep)
  154. {
  155. //if (!instanceInitialized_)
  156. // InitInstance();
  157. if (!started_)
  158. {
  159. started_ = true;
  160. Start();
  161. }
  162. CallScriptMethod(CSComponentMethod_Update, timeStep);
  163. }
  164. void CSComponent::PostUpdate(float timeStep)
  165. {
  166. CallScriptMethod(CSComponentMethod_PostUpdate, timeStep);
  167. }
  168. void CSComponent::FixedUpdate(float timeStep)
  169. {
  170. CallScriptMethod(CSComponentMethod_FixedUpdate, timeStep);
  171. }
  172. void CSComponent::FixedPostUpdate(float timeStep)
  173. {
  174. CallScriptMethod(CSComponentMethod_PostFixedUpdate, timeStep);
  175. }
  176. void CSComponent::OnNodeSet(Node* node)
  177. {
  178. if (node)
  179. {
  180. //UpdateReferences();
  181. }
  182. else
  183. {
  184. // We are being detached from a node: execute user-defined stop function and prepare for destruction
  185. //UpdateReferences(true);
  186. Stop();
  187. }
  188. }
  189. void CSComponent::OnSceneSet(Scene* scene)
  190. {
  191. if (scene)
  192. UpdateEventSubscription();
  193. else
  194. {
  195. UnsubscribeFromEvent(E_SCENEUPDATE);
  196. UnsubscribeFromEvent(E_SCENEPOSTUPDATE);
  197. #ifdef ATOMIC_PHYSICS
  198. UnsubscribeFromEvent(E_PHYSICSPRESTEP);
  199. UnsubscribeFromEvent(E_PHYSICSPOSTSTEP);
  200. #endif
  201. currentEventMask_ = 0;
  202. }
  203. }
  204. void CSComponent::UpdateEventSubscription()
  205. {
  206. Scene* scene = GetScene();
  207. if (!scene)
  208. return;
  209. bool enabled = IsEnabledEffective();
  210. bool needUpdate = enabled && ((updateEventMask_ & USE_UPDATE) || !delayedStartCalled_);
  211. if (needUpdate && !(currentEventMask_ & USE_UPDATE))
  212. {
  213. SubscribeToEvent(scene, E_SCENEUPDATE, HANDLER(CSComponent, HandleSceneUpdate));
  214. currentEventMask_ |= USE_UPDATE;
  215. }
  216. else if (!needUpdate && (currentEventMask_ & USE_UPDATE))
  217. {
  218. UnsubscribeFromEvent(scene, E_SCENEUPDATE);
  219. currentEventMask_ &= ~USE_UPDATE;
  220. }
  221. bool needPostUpdate = enabled && (updateEventMask_ & USE_POSTUPDATE);
  222. if (needPostUpdate && !(currentEventMask_ & USE_POSTUPDATE))
  223. {
  224. SubscribeToEvent(scene, E_SCENEPOSTUPDATE, HANDLER(CSComponent, HandleScenePostUpdate));
  225. currentEventMask_ |= USE_POSTUPDATE;
  226. }
  227. else if (!needUpdate && (currentEventMask_ & USE_POSTUPDATE))
  228. {
  229. UnsubscribeFromEvent(scene, E_SCENEPOSTUPDATE);
  230. currentEventMask_ &= ~USE_POSTUPDATE;
  231. }
  232. #ifdef ATOMIC_PHYSICS
  233. PhysicsWorld* world = scene->GetComponent<PhysicsWorld>();
  234. if (!world)
  235. return;
  236. bool needFixedUpdate = enabled && (updateEventMask_ & USE_FIXEDUPDATE);
  237. if (needFixedUpdate && !(currentEventMask_ & USE_FIXEDUPDATE))
  238. {
  239. SubscribeToEvent(world, E_PHYSICSPRESTEP, HANDLER(CSComponent, HandlePhysicsPreStep));
  240. currentEventMask_ |= USE_FIXEDUPDATE;
  241. }
  242. else if (!needFixedUpdate && (currentEventMask_ & USE_FIXEDUPDATE))
  243. {
  244. UnsubscribeFromEvent(world, E_PHYSICSPRESTEP);
  245. currentEventMask_ &= ~USE_FIXEDUPDATE;
  246. }
  247. bool needFixedPostUpdate = enabled && (updateEventMask_ & USE_FIXEDPOSTUPDATE);
  248. if (needFixedPostUpdate && !(currentEventMask_ & USE_FIXEDPOSTUPDATE))
  249. {
  250. SubscribeToEvent(world, E_PHYSICSPOSTSTEP, HANDLER(CSComponent, HandlePhysicsPostStep));
  251. currentEventMask_ |= USE_FIXEDPOSTUPDATE;
  252. }
  253. else if (!needFixedPostUpdate && (currentEventMask_ & USE_FIXEDPOSTUPDATE))
  254. {
  255. UnsubscribeFromEvent(world, E_PHYSICSPOSTSTEP);
  256. currentEventMask_ &= ~USE_FIXEDPOSTUPDATE;
  257. }
  258. #endif
  259. }
  260. void CSComponent::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  261. {
  262. using namespace SceneUpdate;
  263. assert(!destroyed_);
  264. // Execute user-defined delayed start function before first update
  265. if (!delayedStartCalled_)
  266. {
  267. DelayedStart();
  268. delayedStartCalled_ = true;
  269. // If did not need actual update events, unsubscribe now
  270. if (!(updateEventMask_ & USE_UPDATE))
  271. {
  272. UnsubscribeFromEvent(GetScene(), E_SCENEUPDATE);
  273. currentEventMask_ &= ~USE_UPDATE;
  274. return;
  275. }
  276. }
  277. // Then execute user-defined update function
  278. Update(eventData[P_TIMESTEP].GetFloat());
  279. }
  280. void CSComponent::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  281. {
  282. using namespace ScenePostUpdate;
  283. // Execute user-defined post-update function
  284. PostUpdate(eventData[P_TIMESTEP].GetFloat());
  285. }
  286. #ifdef ATOMIC_PHYSICS
  287. void CSComponent::HandlePhysicsPreStep(StringHash eventType, VariantMap& eventData)
  288. {
  289. using namespace PhysicsPreStep;
  290. // Execute user-defined fixed update function
  291. FixedUpdate(eventData[P_TIMESTEP].GetFloat());
  292. }
  293. void CSComponent::HandlePhysicsPostStep(StringHash eventType, VariantMap& eventData)
  294. {
  295. using namespace PhysicsPostStep;
  296. // Execute user-defined fixed post-update function
  297. FixedPostUpdate(eventData[P_TIMESTEP].GetFloat());
  298. }
  299. #endif
  300. bool CSComponent::Load(Deserializer& source, bool setInstanceDefault)
  301. {
  302. loading_ = true;
  303. bool success = Component::Load(source, setInstanceDefault);
  304. loading_ = false;
  305. return success;
  306. }
  307. bool CSComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  308. {
  309. loading_ = true;
  310. bool success = Component::LoadXML(source, setInstanceDefault);
  311. loading_ = false;
  312. return success;
  313. }
  314. void CSComponent::SetAssemblyFile(NETAssemblyFile* assemblyFile)
  315. {
  316. assemblyFile_ = assemblyFile;
  317. }
  318. ResourceRef CSComponent::GetAssemblyFileAttr() const
  319. {
  320. return GetResourceRef(assemblyFile_, NETAssemblyFile::GetTypeStatic());
  321. }
  322. void CSComponent::SetAssemblyFileAttr(const ResourceRef& value)
  323. {
  324. ResourceCache* cache = GetSubsystem<ResourceCache>();
  325. SetAssemblyFile(cache->GetResource<NETAssemblyFile>(value.name_));
  326. }
  327. }