PageRenderTime 4877ms CodeModel.GetById 21ms RepoModel.GetById 2ms app.codeStats 0ms

/DualityEditorPlugins/EditorBase/PropertyEditors/Components/TransformPropertyEditor.cs

http://duality.googlecode.com/
C# | 284 lines | 264 code | 20 blank | 0 comment | 97 complexity | 1951e5371bc0cbbd56593ac6867a3f2d MD5 | raw file
Possible License(s): BSD-2-Clause
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Reflection;
  4. using AdamsLair.PropertyGrid;
  5. using OpenTK;
  6. using Duality;
  7. using Duality.EditorHints;
  8. using Duality.Components;
  9. using DualityEditor;
  10. namespace EditorBase.PropertyEditors
  11. {
  12. public class TransformPropertyEditor : ComponentPropertyEditor, IHelpProvider
  13. {
  14. private bool showRelative = false;
  15. private PropertyEditor editorPos = null;
  16. private PropertyEditor editorVel = null;
  17. private PropertyEditor editorScale = null;
  18. private PropertyEditor editorAngle = null;
  19. private PropertyEditor editorAngleVel = null;
  20. private PropertyEditor editorShowRelative = null;
  21. protected override bool IsAutoCreateMember(MemberInfo info)
  22. {
  23. return false;
  24. }
  25. protected override void BeforeAutoCreateEditors()
  26. {
  27. base.BeforeAutoCreateEditors();
  28. this.editorPos = this.ParentGrid.CreateEditor((typeof(Vector3)), this);
  29. if (this.editorPos != null)
  30. {
  31. this.editorPos.BeginUpdate();
  32. this.editorPos.Getter = this.PosGetter;
  33. this.editorPos.Setter = this.PosSetter;
  34. this.editorPos.PropertyName = "Pos";
  35. this.ParentGrid.ConfigureEditor(this.editorPos, new EditorHintMemberAttribute[]
  36. { new EditorHintDecimalPlacesAttribute(0), new EditorHintIncrementAttribute(1) });
  37. this.AddPropertyEditor(this.editorPos);
  38. this.editorPos.EndUpdate();
  39. }
  40. this.editorVel = this.ParentGrid.CreateEditor(typeof(Vector3), this);
  41. if (this.editorVel != null)
  42. {
  43. this.editorVel.BeginUpdate();
  44. this.editorVel.Getter = this.VelGetter;
  45. this.editorVel.PropertyName = "Vel";
  46. this.ParentGrid.ConfigureEditor(this.editorVel);
  47. this.AddPropertyEditor(this.editorVel);
  48. this.editorVel.EndUpdate();
  49. }
  50. this.editorScale = this.ParentGrid.CreateEditor(typeof(Vector3), this);
  51. if (this.editorScale != null)
  52. {
  53. this.editorScale.BeginUpdate();
  54. this.editorScale.Getter = this.ScaleGetter;
  55. this.editorScale.Setter = this.ScaleSetter;
  56. this.editorScale.PropertyName = "Scale";
  57. this.ParentGrid.ConfigureEditor(this.editorScale);
  58. this.AddPropertyEditor(this.editorScale);
  59. this.editorScale.EndUpdate();
  60. }
  61. this.editorAngle = this.ParentGrid.CreateEditor(typeof(float), this);
  62. if (this.editorAngle != null)
  63. {
  64. this.editorAngle.BeginUpdate();
  65. this.editorAngle.Getter = this.AngleGetter;
  66. this.editorAngle.Setter = this.AngleSetter;
  67. this.editorAngle.PropertyName = "Angle";
  68. this.ParentGrid.ConfigureEditor(this.editorAngle, new EditorHintMemberAttribute[]
  69. { new EditorHintDecimalPlacesAttribute(1), new EditorHintIncrementAttribute(1) });
  70. this.AddPropertyEditor(this.editorAngle);
  71. this.editorAngle.EndUpdate();
  72. }
  73. this.editorAngleVel = this.ParentGrid.CreateEditor(typeof(float), this);
  74. if (this.editorAngleVel != null)
  75. {
  76. this.editorAngleVel.BeginUpdate();
  77. this.editorAngleVel.Getter = this.AngleVelGetter;
  78. this.editorAngleVel.PropertyName = "AngleVel";
  79. this.ParentGrid.ConfigureEditor(this.editorAngleVel, new[] { new EditorHintIncrementAttribute(0.1f) });
  80. this.AddPropertyEditor(this.editorAngleVel);
  81. this.editorAngleVel.EndUpdate();
  82. }
  83. this.AddEditorForProperty(ReflectionInfo.Property_Transform_DeriveAngle);
  84. this.AddEditorForProperty(ReflectionInfo.Property_Transform_IgnoreParent);
  85. this.editorShowRelative = this.ParentGrid.CreateEditor(typeof(bool), this);
  86. if (editorShowRelative != null)
  87. {
  88. this.editorShowRelative.BeginUpdate();
  89. this.editorShowRelative.Getter = this.ShowRelativeGetter;
  90. this.editorShowRelative.Setter = this.ShowRelativeSetter;
  91. this.editorShowRelative.PropertyName = "[ Relative values ]";
  92. this.ParentGrid.ConfigureEditor(this.editorShowRelative);
  93. this.AddPropertyEditor(this.editorShowRelative);
  94. this.editorShowRelative.EndUpdate();
  95. }
  96. }
  97. protected override bool IsChildValueModified(PropertyEditor childEditor)
  98. {
  99. MemberInfo info = childEditor.EditedMember;
  100. if (childEditor == this.editorPos)
  101. info = ReflectionInfo.Property_Transform_RelativePos;
  102. else if (childEditor == this.editorVel)
  103. info = ReflectionInfo.Property_Transform_RelativeVel;
  104. else if (childEditor == this.editorScale)
  105. info = ReflectionInfo.Property_Transform_RelativeScale;
  106. else if (childEditor == this.editorAngle)
  107. info = ReflectionInfo.Property_Transform_RelativeAngle;
  108. else if (childEditor == this.editorAngleVel)
  109. info = ReflectionInfo.Property_Transform_RelativeAngleVel;
  110. if (info != null)
  111. {
  112. Component[] values = this.GetValue().Cast<Component>().NotNull().ToArray();
  113. return values.Any(delegate (Component c)
  114. {
  115. Duality.Resources.PrefabLink l = c.GameObj.AffectedByPrefabLink;
  116. return l != null && l.HasChange(c, info as PropertyInfo);
  117. });
  118. }
  119. else
  120. return base.IsChildValueModified(childEditor);
  121. }
  122. protected IEnumerable<object> ShowRelativeGetter()
  123. {
  124. return new object[] { this.showRelative };
  125. }
  126. protected void ShowRelativeSetter(IEnumerable<object> values)
  127. {
  128. this.showRelative = values.Cast<bool>().FirstOrDefault();
  129. this.PerformGetValue();
  130. }
  131. protected IEnumerable<object> PosGetter()
  132. {
  133. if (this.showRelative)
  134. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)o.RelativePos : null);
  135. else
  136. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)o.Pos : null);
  137. }
  138. protected void PosSetter(IEnumerable<object> values)
  139. {
  140. IEnumerator<Vector3> valuesEnum = values.Cast<Vector3>().GetEnumerator();
  141. Transform[] targetArray = this.GetValue().Cast<Transform>().ToArray();
  142. Vector3 curValue = Vector3.Zero;
  143. if (valuesEnum.MoveNext()) curValue = valuesEnum.Current;
  144. foreach (Transform target in targetArray)
  145. {
  146. if (target != null)
  147. {
  148. if (this.showRelative)
  149. target.RelativePos = curValue;
  150. else
  151. target.Pos = curValue;
  152. }
  153. if (valuesEnum.MoveNext()) curValue = valuesEnum.Current;
  154. }
  155. this.OnPropertySet(ReflectionInfo.Property_Transform_RelativePos, targetArray);
  156. this.OnUpdateFromObjects(this.GetValue().ToArray());
  157. }
  158. protected IEnumerable<object> VelGetter()
  159. {
  160. if (this.showRelative)
  161. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)o.RelativeVel : null);
  162. else
  163. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)o.Vel : null);
  164. }
  165. protected IEnumerable<object> ScaleGetter()
  166. {
  167. if (this.showRelative)
  168. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)o.RelativeScale : null);
  169. else
  170. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)o.Scale : null);
  171. }
  172. protected void ScaleSetter(IEnumerable<object> values)
  173. {
  174. IEnumerator<Vector3> valuesEnum = values.Cast<Vector3>().GetEnumerator();
  175. Transform[] targetArray = this.GetValue().Cast<Transform>().ToArray();
  176. Vector3 curValue = Vector3.Zero;
  177. if (valuesEnum.MoveNext()) curValue = valuesEnum.Current;
  178. foreach (Transform target in targetArray)
  179. {
  180. if (target != null)
  181. {
  182. if (this.showRelative)
  183. target.RelativeScale = curValue;
  184. else
  185. target.Scale = curValue;
  186. }
  187. if (valuesEnum.MoveNext()) curValue = valuesEnum.Current;
  188. }
  189. this.OnPropertySet(ReflectionInfo.Property_Transform_RelativeScale, targetArray);
  190. this.OnUpdateFromObjects(this.GetValue().ToArray());
  191. }
  192. protected IEnumerable<object> AngleGetter()
  193. {
  194. if (this.showRelative)
  195. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)MathF.RadToDeg(o.RelativeAngle) : null);
  196. else
  197. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)MathF.RadToDeg(o.Angle) : null);
  198. }
  199. protected void AngleSetter(IEnumerable<object> values)
  200. {
  201. IEnumerator<float> valuesEnum = values.Cast<float>().GetEnumerator();
  202. Transform[] targetArray = this.GetValue().Cast<Transform>().ToArray();
  203. float curValue = 0.0f;
  204. if (valuesEnum.MoveNext()) curValue = valuesEnum.Current;
  205. foreach (Transform target in targetArray)
  206. {
  207. if (target != null)
  208. {
  209. if (this.showRelative)
  210. target.RelativeAngle = MathF.DegToRad(curValue);
  211. else
  212. target.Angle = MathF.DegToRad(curValue);
  213. }
  214. if (valuesEnum.MoveNext()) curValue = valuesEnum.Current;
  215. }
  216. this.OnPropertySet(ReflectionInfo.Property_Transform_RelativeAngle, targetArray);
  217. this.OnUpdateFromObjects(this.GetValue().ToArray());
  218. }
  219. protected IEnumerable<object> AngleVelGetter()
  220. {
  221. if (this.showRelative)
  222. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)MathF.RadToDeg(o.RelativeAngleVel) : null);
  223. else
  224. return this.GetValue().Cast<Transform>().Select(o => o != null ? (object)MathF.RadToDeg(o.AngleVel) : null);
  225. }
  226. HelpInfo IHelpProvider.ProvideHoverHelp(System.Drawing.Point localPos, ref bool captured)
  227. {
  228. PropertyEditor pickedEditor = this.PickEditorAt(localPos.X, localPos.Y, true);
  229. if (this.showRelative)
  230. {
  231. if (pickedEditor == this.editorPos)
  232. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_RelativePos);
  233. else if (pickedEditor == this.editorVel)
  234. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_RelativeVel);
  235. else if (pickedEditor == this.editorScale)
  236. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_RelativeScale);
  237. else if (pickedEditor == this.editorAngle)
  238. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_RelativeAngle);
  239. else if (pickedEditor == this.editorAngleVel)
  240. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_RelativeAngleVel);
  241. }
  242. else
  243. {
  244. if (pickedEditor == this.editorPos)
  245. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_Pos);
  246. else if (pickedEditor == this.editorVel)
  247. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_Vel);
  248. else if (pickedEditor == this.editorScale)
  249. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_Scale);
  250. else if (pickedEditor == this.editorAngle)
  251. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_Angle);
  252. else if (pickedEditor == this.editorAngleVel)
  253. return HelpInfo.FromMember(ReflectionInfo.Property_Transform_AngleVel);
  254. }
  255. if (pickedEditor == this.editorShowRelative)
  256. return HelpInfo.FromText("Show relative values?", "If true, the relative Transform values are displayed for editing. This is an editor property that does not affect object behaviour in any way.");
  257. else if (pickedEditor.EditedMember != null)
  258. return HelpInfo.FromMember(pickedEditor.EditedMember);
  259. else if (pickedEditor.EditedType != null)
  260. return HelpInfo.FromMember(pickedEditor.EditedType);
  261. return null;
  262. }
  263. }
  264. }