SerializableField.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 actual type of the object contained in the field.
  44. /// </summary>
  45. public Type InternalType
  46. {
  47. get { return internalType; }
  48. }
  49. /// <summary>
  50. /// Returns the name of the field.
  51. /// </summary>
  52. public string Name
  53. {
  54. get { return name; }
  55. }
  56. /// <summary>
  57. /// Returns true if the field accepts a defined range.
  58. /// </summary>
  59. public bool Range
  60. {
  61. get { return (flags & 0x04) != 0; }
  62. }
  63. /// <summary>
  64. /// Returns the upper bound of the range
  65. /// </summary>
  66. public float RangeMaximum
  67. {
  68. get { return Range? Internal_GetRangeMaximum(mCachedPtr) : 0; }
  69. }
  70. /// <summary>
  71. /// Returns the lower bound of the range
  72. /// </summary>
  73. public float RangeMinimum
  74. {
  75. get { return Range? Internal_GetRangeMinimum(mCachedPtr) : 0; }
  76. }
  77. /// <summary>
  78. /// Returns the step of the range
  79. /// </summary>
  80. public float Step
  81. {
  82. get { return Range ? Internal_GetStep(mCachedPtr) : 0; }
  83. }
  84. /// <summary>
  85. /// Returns true if the field will be visible in the default inspector.
  86. /// </summary>
  87. public bool Inspectable
  88. {
  89. get { return (flags & 0x02) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  90. }
  91. /// <summary>
  92. /// Returns true if the field will be automatically serialized.
  93. /// </summary>
  94. public bool Serializable
  95. {
  96. get { return (flags & 0x01) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  97. }
  98. /// <summary>
  99. /// Returns a serializable property for the field.
  100. /// </summary>
  101. /// <returns>Serializable property that allows you to manipulate contents of the field.</returns>
  102. public SerializableProperty GetProperty()
  103. {
  104. SerializableProperty.Getter getter = () =>
  105. {
  106. object parentObject = parent.GetReferencedObject();
  107. if (parentObject != null)
  108. return Internal_GetValue(mCachedPtr, parentObject);
  109. else
  110. return null;
  111. };
  112. SerializableProperty.Setter setter = (object value) =>
  113. {
  114. object parentObject = parent.GetReferencedObject();
  115. if (parentObject != null)
  116. {
  117. Internal_SetValue(mCachedPtr, parentObject, value);
  118. // If value type we cannot just modify the parent object because it's just a copy
  119. if (parentObject.GetType().IsValueType && parent.parentProperty != null)
  120. parent.parentProperty.SetValue(parentObject);
  121. }
  122. };
  123. SerializableProperty newProperty = Internal_CreateProperty(mCachedPtr);
  124. newProperty.Construct(type, internalType, getter, setter);
  125. return newProperty;
  126. }
  127. [MethodImpl(MethodImplOptions.InternalCall)]
  128. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  129. [MethodImpl(MethodImplOptions.InternalCall)]
  130. private static extern object Internal_GetValue(IntPtr nativeInstance, object instance);
  131. [MethodImpl(MethodImplOptions.InternalCall)]
  132. private static extern void Internal_SetValue(IntPtr nativeInstance, object instance, object value);
  133. [MethodImpl(MethodImplOptions.InternalCall)]
  134. private static extern float Internal_GetRangeMaximum(IntPtr field);
  135. [MethodImpl(MethodImplOptions.InternalCall)]
  136. private static extern float Internal_GetRangeMinimum(IntPtr field);
  137. [MethodImpl(MethodImplOptions.InternalCall)]
  138. private static extern float Internal_GetStep(IntPtr field);
  139. }
  140. /** @} */
  141. }