SerializableField.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Serialization
  8. * @{
  9. */
  10. /// <summary>
  11. /// Allows you to access meta-data about field in an object. Similar to Reflection but simpler and faster.
  12. /// </summary>
  13. public class SerializableField : ScriptObject
  14. {
  15. private SerializableObject parent;
  16. private SerializableProperty.FieldType type;
  17. private int flags;
  18. private Type internalType;
  19. private string name;
  20. /// <summary>
  21. /// Constructor for internal use by the runtime.
  22. /// </summary>
  23. /// <param name="parent">Object that conains the field.</param>
  24. /// <param name="name">Name of the field.</param>
  25. /// <param name="flags">Flags that control whether the field is inspectable or serializable.</param>
  26. /// <param name="internalType">Internal C# type of the field.</param>
  27. private SerializableField(SerializableObject parent, string name, int flags, Type internalType)
  28. {
  29. this.parent = parent;
  30. this.name = name;
  31. this.flags = flags;
  32. this.type = SerializableProperty.DetermineFieldType(internalType);
  33. this.internalType = internalType;
  34. }
  35. /// <summary>
  36. /// Returns the type of data contained in the field.
  37. /// </summary>
  38. public SerializableProperty.FieldType Type
  39. {
  40. get { return type; }
  41. }
  42. /// <summary>
  43. /// Returns the name of the field.
  44. /// </summary>
  45. public string Name
  46. {
  47. get { return name; }
  48. }
  49. /// <summary>
  50. /// Returns true if the field accepts a defined range.
  51. /// </summary>
  52. public bool Range
  53. {
  54. get { return (flags & 0x04) != 0; }
  55. }
  56. /// <summary>
  57. /// Returns the upper bound of the range
  58. /// </summary>
  59. public float RangeMaximum
  60. {
  61. get { return Range? Internal_GetRangeMaximum(mCachedPtr) : 0; }
  62. }
  63. /// <summary>
  64. /// Returns the lower bound of the range
  65. /// </summary>
  66. public float RangeMinimum
  67. {
  68. get { return Range? Internal_GetRangeMinimum(mCachedPtr) : 0; }
  69. }
  70. /// <summary>
  71. /// Returns the step of the range
  72. /// </summary>
  73. public float Step
  74. {
  75. get { return Range ? Internal_GetStep(mCachedPtr) : 0; }
  76. }
  77. /// <summary>
  78. /// Returns true if the field will be visible in the default inspector.
  79. /// </summary>
  80. public bool Inspectable
  81. {
  82. get { return (flags & 0x02) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  83. }
  84. /// <summary>
  85. /// Returns true if the field will be automatically serialized.
  86. /// </summary>
  87. public bool Serializable
  88. {
  89. get { return (flags & 0x01) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  90. }
  91. /// <summary>
  92. /// Returns a serializable property for the field.
  93. /// </summary>
  94. /// <returns>Serializable property that allows you to manipulate contents of the field.</returns>
  95. public SerializableProperty GetProperty()
  96. {
  97. SerializableProperty.Getter getter = () =>
  98. {
  99. object parentObject = parent.GetReferencedObject();
  100. if (parentObject != null)
  101. return Internal_GetValue(mCachedPtr, parentObject);
  102. else
  103. return null;
  104. };
  105. SerializableProperty.Setter setter = (object value) =>
  106. {
  107. object parentObject = parent.GetReferencedObject();
  108. if (parentObject != null)
  109. {
  110. Internal_SetValue(mCachedPtr, parentObject, value);
  111. // If value type we cannot just modify the parent object because it's just a copy
  112. if (parentObject.GetType().IsValueType && parent.parentProperty != null)
  113. parent.parentProperty.SetValue(parentObject);
  114. }
  115. };
  116. SerializableProperty newProperty = Internal_CreateProperty(mCachedPtr);
  117. newProperty.Construct(type, internalType, getter, setter);
  118. return newProperty;
  119. }
  120. [MethodImpl(MethodImplOptions.InternalCall)]
  121. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  122. [MethodImpl(MethodImplOptions.InternalCall)]
  123. private static extern object Internal_GetValue(IntPtr nativeInstance, object instance);
  124. [MethodImpl(MethodImplOptions.InternalCall)]
  125. private static extern void Internal_SetValue(IntPtr nativeInstance, object instance, object value);
  126. [MethodImpl(MethodImplOptions.InternalCall)]
  127. private static extern float Internal_GetRangeMaximum(IntPtr field);
  128. [MethodImpl(MethodImplOptions.InternalCall)]
  129. private static extern float Internal_GetRangeMinimum(IntPtr field);
  130. [MethodImpl(MethodImplOptions.InternalCall)]
  131. private static extern float Internal_GetStep(IntPtr field);
  132. }
  133. /** @} */
  134. }