CSComponent.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "NETVariant.h"
  34. #include "NETManaged.h"
  35. #include "CSComponent.h"
  36. namespace Atomic
  37. {
  38. extern const char* LOGIC_CATEGORY;
  39. class CSComponentFactory : public ObjectFactory
  40. {
  41. public:
  42. /// Construct.
  43. CSComponentFactory(Context* context) :
  44. ObjectFactory(context)
  45. {
  46. type_ = CSComponent::GetTypeStatic();
  47. baseType_ = CSComponent::GetBaseTypeStatic();
  48. typeName_ = CSComponent::GetTypeNameStatic();
  49. }
  50. /// Create an object of the specific type.
  51. SharedPtr<Object> CreateObject(const XMLElement& source = XMLElement::EMPTY)
  52. {
  53. // if in editor, just create the CSComponent
  54. if (context_->GetEditorContext())
  55. {
  56. return SharedPtr<Object>(new CSComponent(context_));
  57. }
  58. // At runtime, a XML CSComponent may refer to a managed component
  59. String managedClass;
  60. String assemblyRef;
  61. if (source != XMLElement::EMPTY)
  62. {
  63. XMLElement attrElem = source.GetChild("attribute");
  64. while (attrElem)
  65. {
  66. if (attrElem.GetAttribute("name") == "Assembly")
  67. {
  68. assemblyRef = attrElem.GetAttribute("value");
  69. }
  70. else if (attrElem.GetAttribute("name") == "Class")
  71. {
  72. managedClass = attrElem.GetAttribute("value");
  73. }
  74. if (assemblyRef.Length() && managedClass.Length())
  75. break;
  76. attrElem = attrElem.GetNext("attribute");
  77. }
  78. }
  79. SharedPtr<Object> ptr;
  80. if (assemblyRef.Length())
  81. {
  82. Vector<String> split = assemblyRef.Split(';');
  83. if (split.Size() == 2)
  84. {
  85. ResourceCache* cache = context_->GetSubsystem<ResourceCache>();
  86. NETAssemblyFile* componentFile = cache->GetResource<NETAssemblyFile>(split[1]);
  87. if (componentFile)
  88. ptr = componentFile->CreateCSComponent(managedClass);
  89. else
  90. {
  91. LOGERRORF("Unable to load component file %s", split[1].CString());
  92. }
  93. }
  94. }
  95. if (ptr.Null())
  96. {
  97. ptr = new CSComponent(context_);
  98. }
  99. return ptr; }
  100. };
  101. CSComponent::CSComponent(Context* context) :
  102. ScriptComponent(context)
  103. {
  104. }
  105. CSComponent::~CSComponent()
  106. {
  107. }
  108. void CSComponent::RegisterObject(Context* context)
  109. {
  110. context->RegisterFactory(new CSComponentFactory(context), LOGIC_CATEGORY);
  111. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  112. ATTRIBUTE("FieldValues", VariantMap, fieldValues_, Variant::emptyVariantMap, AM_FILE);
  113. MIXED_ACCESSOR_ATTRIBUTE("Assembly", GetAssemblyFileAttr, SetAssemblyFileAttr, ResourceRef, ResourceRef(NETAssemblyFile::GetTypeStatic()), AM_DEFAULT);
  114. ACCESSOR_ATTRIBUTE("Class", GetComponentClassName, SetComponentClassName, String, String::EMPTY, AM_DEFAULT);
  115. }
  116. void CSComponent::OnSetEnabled()
  117. {
  118. }
  119. void CSComponent::ApplyAttributes()
  120. {
  121. }
  122. void CSComponent::ApplyFieldValues()
  123. {
  124. if (!fieldValues_.Size())
  125. return;
  126. SharedPtr<NETVariantMap> vmap(new NETVariantMap());
  127. vmap->CopySourceVariantMap(fieldValues_);
  128. NETManaged* managed = GetSubsystem<NETManaged>();
  129. managed->CSComponentApplyFields(this, vmap);
  130. }
  131. void CSComponent::OnNodeSet(Node* node)
  132. {
  133. if (node)
  134. {
  135. //UpdateReferences();
  136. }
  137. else
  138. {
  139. // We are being detached from a node: execute user-defined stop function and prepare for destruction
  140. //UpdateReferences(true);
  141. //Stop();
  142. }
  143. }
  144. void CSComponent::OnSceneSet(Scene* scene)
  145. {
  146. }
  147. bool CSComponent::Load(Deserializer& source, bool setInstanceDefault)
  148. {
  149. bool success = Component::Load(source, setInstanceDefault);
  150. return success;
  151. }
  152. bool CSComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  153. {
  154. bool success = Component::LoadXML(source, setInstanceDefault);
  155. return success;
  156. }
  157. void CSComponent::SetAssemblyFile(NETAssemblyFile* assemblyFile)
  158. {
  159. assemblyFile_ = assemblyFile;
  160. }
  161. ResourceRef CSComponent::GetAssemblyFileAttr() const
  162. {
  163. return GetResourceRef(assemblyFile_, NETAssemblyFile::GetTypeStatic());
  164. }
  165. void CSComponent::SetAssemblyFileAttr(const ResourceRef& value)
  166. {
  167. ResourceCache* cache = GetSubsystem<ResourceCache>();
  168. SetAssemblyFile(cache->GetResource<NETAssemblyFile>(value.name_));
  169. }
  170. }