Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / activity / ActivityObject_EditorTool.cs
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Godot;
  6. using UI.EditorTools;
  7.  
  8. public partial class ActivityObject
  9. {
  10. /// <summary>
  11. /// 该函数只会在编辑器中调用, 用于处理被 [ExportFill] 标记属性节点, 当在编辑器中切换页签或者创建 ActivityObject 时会调用该函数
  12. /// </summary>
  13. /// <param name="propertyName">属性名称</param>
  14. /// <param name="node">节点实例</param>
  15. /// <param name="isJustCreated">是否刚刚创建</param>
  16. protected virtual void OnExamineExportFillNode(string propertyName, Node node, bool isJustCreated)
  17. {
  18. switch (propertyName)
  19. {
  20. case "ShadowSprite":
  21. {
  22. var sprite = (Sprite2D)node;
  23. if (isJustCreated)
  24. {
  25. sprite.ZIndex = -1;
  26. }
  27.  
  28. if (sprite.Material == null)
  29. {
  30. var material =
  31. ResourceManager.Load<ShaderMaterial>(ResourcePath.resource_material_Blend_tres, false);
  32. material.ResourceLocalToScene = true;
  33. material.SetShaderParameter("blend", new Color(0, 0, 0, 0.47058824F));
  34. material.SetShaderParameter("schedule", 1);
  35. material.SetShaderParameter("modulate", new Color(1, 1, 1, 1));
  36. sprite.Material = material;
  37. }
  38. }
  39. break;
  40. case "AnimatedSprite":
  41. {
  42. var animatedSprite = (AnimatedSprite2D)node;
  43. if (animatedSprite.Material == null)
  44. {
  45. var material =
  46. ResourceManager.Load<ShaderMaterial>(ResourcePath.resource_material_Blend_tres, false);
  47. material.ResourceLocalToScene = true;
  48. material.SetShaderParameter("blend", new Color(1, 1, 1, 1));
  49. material.SetShaderParameter("schedule", 0);
  50. material.SetShaderParameter("modulate", new Color(1, 1, 1, 1));
  51. animatedSprite.Material = material;
  52. }
  53. }
  54. break;
  55. case "Collision":
  56. {
  57.  
  58. }
  59. break;
  60. }
  61. }
  62.  
  63. #if TOOLS
  64. private void _InitNodeInEditor()
  65. {
  66. var parent = GetParent();
  67. if (parent != null)
  68. {
  69. //寻找 owner
  70. Node owner;
  71. if (parent.Owner != null)
  72. {
  73. owner = parent.Owner;
  74. }
  75. else if (Plugin.Plugin.Instance.GetEditorInterface().GetEditedSceneRoot() == this)
  76. {
  77. owner = this;
  78. }
  79. else
  80. {
  81. owner = parent;
  82. }
  83.  
  84. var type = GetType();
  85. var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  86. var propertyInfoList = new List<PropertyInfo>();
  87. foreach (var propertyInfo in propertyInfos)
  88. {
  89. if (propertyInfo.GetCustomAttributes(typeof(ExportFillNodeAttribute), false).Length > 0)
  90. {
  91. if (propertyInfo.GetCustomAttributes(typeof(ExportAttribute), false).Length == 0)
  92. {
  93. EditorToolsPanel.ShowConfirmInEditor("警告", $"'{type.FullName}'中字段'{propertyInfo.Name}'使用了[ExportAutoFill],\n但是并没有加上[Export], 请补上!");
  94. return;
  95. }
  96.  
  97. if (propertyInfo.PropertyType.IsAssignableTo(typeof(Node)))
  98. {
  99. if (propertyInfo.SetMethod == null)
  100. {
  101. EditorToolsPanel.ShowConfirmInEditor("警告", $"请为'{type.FullName}'中的'{propertyInfo.Name}'属性设置set访问器, 或者将set服务器设置成public!");
  102. return;
  103. }
  104. propertyInfoList.Add(propertyInfo);
  105. }
  106. }
  107. }
  108.  
  109. var tempList = new List<PropertyInfo>();
  110. Type tempType = null;
  111. var index = -1;
  112. for (int i = propertyInfoList.Count - 1; i >= 0; i--)
  113. {
  114. var item = propertyInfoList[i];
  115. if (tempType != item.DeclaringType || i == 0)
  116. {
  117. if (tempType == null)
  118. {
  119. index = i;
  120. }
  121. else
  122. {
  123. int j;
  124. if (i == 0)
  125. {
  126. j = i;
  127. }
  128. else
  129. {
  130. j = i + 1;
  131. }
  132. for (; j <= index; j++)
  133. {
  134. tempList.Add(propertyInfoList[j]);
  135. }
  136.  
  137. index = i;
  138. }
  139. tempType = item.DeclaringType;
  140. }
  141. }
  142. foreach (var propertyInfo in tempList)
  143. {
  144. var value = propertyInfo.GetValue(this);
  145. if (value == null || ((Node)value).GetParent() == null)
  146. {
  147. var node = _FindNodeInChild(this, propertyInfo.Name, propertyInfo.PropertyType);
  148. if (node == null)
  149. {
  150. node = (Node)Activator.CreateInstance(propertyInfo.PropertyType);
  151. AddChild(node);
  152. node.Name = propertyInfo.Name;
  153. node.Owner = owner;
  154. OnExamineExportFillNode(propertyInfo.Name, node, true);
  155. }
  156. else
  157. {
  158. OnExamineExportFillNode(propertyInfo.Name, node, false);
  159. }
  160. propertyInfo.SetValue(this, node);
  161. }
  162. else
  163. {
  164. OnExamineExportFillNode(propertyInfo.Name, (Node)value, false);
  165. }
  166. }
  167. }
  168. }
  169.  
  170. private Node _FindNodeInChild(Node node, string name, Type type)
  171. {
  172. var childCount = node.GetChildCount();
  173. for (int i = 0; i < childCount; i++)
  174. {
  175. var child = node.GetChild(i);
  176. if (child.Name == name && child.GetType().IsAssignableTo(type))
  177. {
  178. return child;
  179. }
  180. else
  181. {
  182. var result = _FindNodeInChild(child, name, type);
  183. if (result != null)
  184. {
  185. return result;
  186. }
  187. }
  188. }
  189.  
  190. return null;
  191. }
  192. #endif
  193. }