SerializableField.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 Ranged
  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 Ranged? 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 Ranged? Internal_GetRangeMinimum(mCachedPtr) : 0; }
  76. }
  77. /// <summary>
  78. /// Whether the field is rendered as a slider
  79. /// </summary>
  80. public bool IsSlider
  81. {
  82. get { return (Ranged && Internal_RenderAsSlider(mCachedPtr)); }
  83. }
  84. public bool Stepped
  85. {
  86. get { return (flags & 0x04) != 0; }
  87. }
  88. /// <summary>
  89. /// Returns the step of the range
  90. /// </summary>
  91. public float Step
  92. {
  93. get { return Internal_GetStep(mCachedPtr); }
  94. }
  95. /// <summary>
  96. /// Returns true if the field will be visible in the default inspector.
  97. /// </summary>
  98. public bool Inspectable
  99. {
  100. get { return (flags & 0x02) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  101. }
  102. /// <summary>
  103. /// Returns true if the field will be automatically serialized.
  104. /// </summary>
  105. public bool Serializable
  106. {
  107. get { return (flags & 0x01) != 0; } // Flags as defined in native code in BsManagedSerializableObjectInfo.h
  108. }
  109. /// <summary>
  110. /// Returns a serializable property for the field.
  111. /// </summary>
  112. /// <returns>Serializable property that allows you to manipulate contents of the field.</returns>
  113. public SerializableProperty GetProperty()
  114. {
  115. SerializableProperty.Getter getter = () =>
  116. {
  117. object parentObject = parent.GetReferencedObject();
  118. if (parentObject != null)
  119. return Internal_GetValue(mCachedPtr, parentObject);
  120. else
  121. return null;
  122. };
  123. SerializableProperty.Setter setter = (object value) =>
  124. {
  125. object parentObject = parent.GetReferencedObject();
  126. if (parentObject != null)
  127. {
  128. Internal_SetValue(mCachedPtr, parentObject, value);
  129. // If value type we cannot just modify the parent object because it's just a copy
  130. if (parentObject.GetType().IsValueType && parent.parentProperty != null)
  131. parent.parentProperty.SetValue(parentObject);
  132. }
  133. };
  134. SerializableProperty newProperty = Internal_CreateProperty(mCachedPtr);
  135. newProperty.Construct(type, internalType, getter, setter);
  136. return newProperty;
  137. }
  138. [MethodImpl(MethodImplOptions.InternalCall)]
  139. private static extern SerializableProperty Internal_CreateProperty(IntPtr nativeInstance);
  140. [MethodImpl(MethodImplOptions.InternalCall)]
  141. private static extern object Internal_GetValue(IntPtr nativeInstance, object instance);
  142. [MethodImpl(MethodImplOptions.InternalCall)]
  143. private static extern void Internal_SetValue(IntPtr nativeInstance, object instance, object value);
  144. [MethodImpl(MethodImplOptions.InternalCall)]
  145. private static extern float Internal_GetRangeMaximum(IntPtr field);
  146. [MethodImpl(MethodImplOptions.InternalCall)]
  147. private static extern float Internal_GetRangeMinimum(IntPtr field);
  148. [MethodImpl(MethodImplOptions.InternalCall)]
  149. private static extern bool Internal_RenderAsSlider(IntPtr field);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. private static extern float Internal_GetStep(IntPtr field);
  152. }
  153. /** @} */
  154. }