CSComponent.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include <Atomic/Scene/Scene.h>
  28. #include <Atomic/Scene/SceneEvents.h>
  29. #include "NETScriptEvents.h"
  30. #include "CSComponent.h"
  31. namespace Atomic
  32. {
  33. extern const char* LOGIC_CATEGORY;
  34. CSComponent::CSComponent(Context* context) :
  35. ScriptComponent(context)
  36. {
  37. }
  38. CSComponent::~CSComponent()
  39. {
  40. }
  41. void CSComponent::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<CSComponent>(LOGIC_CATEGORY);
  44. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  45. ATOMIC_ATTRIBUTE("FieldValues", VariantMap, fieldValues_, Variant::emptyVariantMap, AM_FILE);
  46. ATOMIC_ACCESSOR_ATTRIBUTE("Class", GetComponentClassName, SetComponentClassName, String, String::EMPTY, AM_DEFAULT);
  47. }
  48. void CSComponent::OnSetEnabled()
  49. {
  50. }
  51. void CSComponent::ApplyAttributes()
  52. {
  53. }
  54. void CSComponent::ApplyFieldValues()
  55. {
  56. if (!fieldValues_.Size())
  57. return;
  58. }
  59. void CSComponent::SetComponentClassName(const String& name)
  60. {
  61. if (componentClassName_ == name)
  62. return;
  63. componentClassName_ = name;
  64. if (context_->GetEditorContext())
  65. {
  66. using namespace CSComponentClassChanged;
  67. VariantMap eventData;
  68. eventData[P_CSCOMPONENT] = this;
  69. eventData[P_CLASSNAME] = name;
  70. SendEvent(E_CSCOMPONENTCLASSCHANGED, eventData);
  71. }
  72. }
  73. void CSComponent::OnNodeSet(Node* node)
  74. {
  75. }
  76. void CSComponent::OnSceneSet(Scene* scene)
  77. {
  78. }
  79. void CSComponent::SendLoadEvent()
  80. {
  81. if (!componentClassName_.Length())
  82. return;
  83. // TODO: We need to factor out runtime loading of CSComponent assemblies
  84. CSComponentAssembly* assemblyFile = 0;
  85. #ifdef ATOMIC_PLATFORM_DESKTOP
  86. assemblyFile = CSComponentAssembly::ResolveClassAssembly(componentClassName_);
  87. #endif
  88. using namespace CSComponentLoad;
  89. VariantMap eventData;
  90. eventData[P_ASSEMBLYPATH] = assemblyFile ? GetFileName(assemblyFile->GetFullPath()) : String::EMPTY;
  91. eventData[P_CLASSNAME] = componentClassName_;
  92. eventData[P_NATIVEINSTANCE] = (void*) this;
  93. if (!fieldValues_.Empty())
  94. eventData[P_FIELDVALUES] = (void*) &fieldValues_;
  95. SendEvent(E_CSCOMPONENTLOAD, eventData);
  96. }
  97. bool CSComponent::Load(Deserializer& source, bool setInstanceDefault)
  98. {
  99. bool success = Component::Load(source, setInstanceDefault);
  100. if (success)
  101. SendLoadEvent();
  102. return success;
  103. }
  104. bool CSComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  105. {
  106. bool success = Component::LoadXML(source, setInstanceDefault);
  107. if (success)
  108. SendLoadEvent();
  109. return success;
  110. }
  111. ScriptComponentFile* CSComponent::GetComponentFile()
  112. {
  113. if (!componentClassName_.Length())
  114. return 0;
  115. return CSComponentAssembly::ResolveClassAssembly(componentClassName_);
  116. }
  117. }