UnknownComponent.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Scene/Component.h"
  24. namespace Atomic
  25. {
  26. /// Placeholder for allowing unregistered components to be loaded & saved along with scenes.
  27. class ATOMIC_API UnknownComponent : public Component
  28. {
  29. public:
  30. /// Construct.
  31. UnknownComponent(Context* context);
  32. /// Register object factory.
  33. static void RegisterObject(Context* context);
  34. /// Return type of the stored component.
  35. virtual StringHash GetType() const { return typeHash_; }
  36. /// Return type name of the stored component.
  37. virtual const String& GetTypeName() const { return typeName_; }
  38. /// Return attribute descriptions, or null if none defined.
  39. virtual const Vector<AttributeInfo>* GetAttributes() const { return &xmlAttributeInfos_; }
  40. /// Load from binary data. Return true if successful.
  41. virtual bool Load(Deserializer& source, bool setInstanceDefault = false);
  42. /// Load from XML data. Return true if successful.
  43. virtual bool LoadXML(const XMLElement& source, bool setInstanceDefault = false);
  44. /// Load from JSON data. Return true if successful.
  45. virtual bool LoadJSON(const JSONValue& source, bool setInstanceDefault = false);
  46. /// Save as binary data. Return true if successful.
  47. virtual bool Save(Serializer& dest) const;
  48. /// Save as XML data. Return true if successful.
  49. virtual bool SaveXML(XMLElement& dest) const;
  50. /// Save as JSON data. Return true if successful.
  51. virtual bool SaveJSON(JSONValue& dest) const;
  52. /// Initialize the type name. Called by Node when loading.
  53. void SetTypeName(const String& typeName);
  54. /// Initialize the type hash only when type name not known. Called by Node when loading.
  55. void SetType(StringHash typeHash);
  56. /// Return the XML format attributes. Empty when loaded with binary serialization.
  57. const Vector<String>& GetXMLAttributes() const { return xmlAttributes_; }
  58. /// Return the binary attributes. Empty when loaded with XML serialization.
  59. const PODVector<unsigned char>& GetBinaryAttributes() const { return binaryAttributes_; }
  60. /// Return whether was loaded using XML data.
  61. bool GetUseXML() const { return useXML_; }
  62. /// Return static type.
  63. static Atomic::StringHash GetTypeStatic()
  64. {
  65. static const StringHash typeStatic("UnknownComponent");
  66. return typeStatic;
  67. }
  68. /// Return static type name.
  69. static const Atomic::String& GetTypeNameStatic()
  70. {
  71. static const String typeNameStatic("UnknownComponent");
  72. return typeNameStatic;
  73. }
  74. private:
  75. /// Type of stored component.
  76. StringHash typeHash_;
  77. /// Type name of the stored component.
  78. String typeName_;
  79. /// XML format attribute infos.
  80. Vector<AttributeInfo> xmlAttributeInfos_;
  81. /// XML format attribute data (as strings)
  82. Vector<String> xmlAttributes_;
  83. /// Binary attributes.
  84. PODVector<unsigned char> binaryAttributes_;
  85. /// Flag of whether was loaded using XML/JSON data.
  86. bool useXML_;
  87. };
  88. }