UnknownComponent.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Scene/Component.h"
  5. namespace Urho3D
  6. {
  7. /// Placeholder for allowing unregistered components to be loaded & saved along with scenes.
  8. class URHO3D_API UnknownComponent : public Component
  9. {
  10. public:
  11. /// Construct.
  12. explicit UnknownComponent(Context* context);
  13. /// Register object factory.
  14. /// @nobind
  15. static void RegisterObject(Context* context);
  16. /// Return type of the stored component.
  17. StringHash GetType() const override { return typeHash_; }
  18. /// Return type name of the stored component.
  19. const String& GetTypeName() const override { return typeName_; }
  20. /// Return attribute descriptions, or null if none defined.
  21. const Vector<AttributeInfo>* GetAttributes() const override { return &xmlAttributeInfos_; }
  22. /// Load from binary data. Return true if successful.
  23. bool Load(Deserializer& source) override;
  24. /// Load from XML data. Return true if successful.
  25. bool LoadXML(const XMLElement& source) override;
  26. /// Load from JSON data. Return true if successful.
  27. bool LoadJSON(const JSONValue& source) override;
  28. /// Save as binary data. Return true if successful.
  29. bool Save(Serializer& dest) const override;
  30. /// Save as XML data. Return true if successful.
  31. bool SaveXML(XMLElement& dest) const override;
  32. /// Save as JSON data. Return true if successful.
  33. bool SaveJSON(JSONValue& dest) const override;
  34. /// Initialize the type name. Called by Node when loading.
  35. void SetTypeName(const String& typeName);
  36. /// Initialize the type hash only when type name not known. Called by Node when loading.
  37. void SetType(StringHash typeHash);
  38. /// Return the XML format attributes. Empty when loaded with binary serialization.
  39. const Vector<String>& GetXMLAttributes() const { return xmlAttributes_; }
  40. /// Return the binary attributes. Empty when loaded with XML serialization.
  41. const Vector<unsigned char>& GetBinaryAttributes() const { return binaryAttributes_; }
  42. /// Return whether was loaded using XML data.
  43. bool GetUseXML() const { return useXML_; }
  44. /// Return static type.
  45. static Urho3D::StringHash GetTypeStatic()
  46. {
  47. static const StringHash typeStatic("UnknownComponent");
  48. return typeStatic;
  49. }
  50. /// Return static type name.
  51. static const Urho3D::String& GetTypeNameStatic()
  52. {
  53. static const String typeNameStatic("UnknownComponent");
  54. return typeNameStatic;
  55. }
  56. private:
  57. /// Type of stored component.
  58. StringHash typeHash_;
  59. /// Type name of the stored component.
  60. String typeName_;
  61. /// XML format attribute infos.
  62. Vector<AttributeInfo> xmlAttributeInfos_;
  63. /// XML format attribute data (as strings).
  64. Vector<String> xmlAttributes_;
  65. /// Binary attributes.
  66. Vector<unsigned char> binaryAttributes_;
  67. /// Flag of whether was loaded using XML/JSON data.
  68. bool useXML_;
  69. };
  70. }