CSComponent.cpp 6.3 KB

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