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.SetShaderParameter("blend", new Color(0, 0, 0, 0.47058824F));
  33. material.SetShaderParameter("schedule", 1);
  34. material.SetShaderParameter("modulate", new Color(1, 1, 1, 1));
  35. sprite.Material = material;
  36. }
  37. }
  38. break;
  39. case "AnimatedSprite":
  40. {
  41. var animatedSprite = (AnimatedSprite2D)node;
  42. if (animatedSprite.Material == null)
  43. {
  44. var material =
  45. ResourceManager.Load<ShaderMaterial>(ResourcePath.resource_material_Blend_tres, false);
  46. material.SetShaderParameter("blend", new Color(1, 1, 1, 1));
  47. material.SetShaderParameter("schedule", 0);
  48. material.SetShaderParameter("modulate", new Color(1, 1, 1, 1));
  49. animatedSprite.Material = material;
  50. }
  51. }
  52. break;
  53. case "Collision":
  54. {
  55.  
  56. }
  57. break;
  58. }
  59. }
  60.  
  61. #if TOOLS
  62. private void _InitNodeInEditor()
  63. {
  64. var parent = GetParent();
  65. if (parent != null)
  66. {
  67. //寻找 owner
  68. Node owner;
  69. if (parent.Owner != null)
  70. {
  71. owner = parent.Owner;
  72. }
  73. else if (Plugin.Plugin.Instance.GetEditorInterface().GetEditedSceneRoot() == this)
  74. {
  75. owner = this;
  76. }
  77. else
  78. {
  79. owner = parent;
  80. }
  81.  
  82. var type = GetType();
  83. var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  84. var propertyInfoList = new List<PropertyInfo>();
  85. foreach (var propertyInfo in propertyInfos)
  86. {
  87. if (propertyInfo.GetCustomAttributes(typeof(ExportFillNodeAttribute), false).Length > 0)
  88. {
  89. if (propertyInfo.GetCustomAttributes(typeof(ExportAttribute), false).Length == 0)
  90. {
  91. EditorToolsPanel.ShowConfirmInEditor("警告", $"'{type.FullName}'中字段'{propertyInfo.Name}'使用了[ExportAutoFill],\n但是并没有加上[Export], 请补上!");
  92. return;
  93. }
  94.  
  95. if (propertyInfo.PropertyType.IsAssignableTo(typeof(Node)))
  96. {
  97. if (propertyInfo.SetMethod == null)
  98. {
  99. EditorToolsPanel.ShowConfirmInEditor("警告", $"请为'{type.FullName}'中的'{propertyInfo.Name}'属性设置set访问器, 或者将set服务器设置成public!");
  100. return;
  101. }
  102. propertyInfoList.Add(propertyInfo);
  103. }
  104. }
  105. }
  106.  
  107. var tempList = new List<PropertyInfo>();
  108. Type tempType = null;
  109. var index = -1;
  110. for (int i = propertyInfoList.Count - 1; i >= 0; i--)
  111. {
  112. var item = propertyInfoList[i];
  113. if (tempType != item.DeclaringType || i == 0)
  114. {
  115. if (tempType == null)
  116. {
  117. index = i;
  118. }
  119. else
  120. {
  121. int j;
  122. if (i == 0)
  123. {
  124. j = i;
  125. }
  126. else
  127. {
  128. j = i + 1;
  129. }
  130. for (; j <= index; j++)
  131. {
  132. tempList.Add(propertyInfoList[j]);
  133. }
  134.  
  135. index = i;
  136. }
  137. tempType = item.DeclaringType;
  138. }
  139. }
  140. foreach (var propertyInfo in tempList)
  141. {
  142. var value = propertyInfo.GetValue(this);
  143. if (value == null || ((Node)value).GetParent() == null)
  144. {
  145. var node = _FindNodeInChild(this, propertyInfo.Name, propertyInfo.PropertyType);
  146. if (node == null)
  147. {
  148. node = (Node)Activator.CreateInstance(propertyInfo.PropertyType);
  149. AddChild(node);
  150. node.Name = propertyInfo.Name;
  151. node.Owner = owner;
  152. OnExamineExportFillNode(propertyInfo.Name, node, true);
  153. }
  154. else
  155. {
  156. OnExamineExportFillNode(propertyInfo.Name, node, false);
  157. }
  158. propertyInfo.SetValue(this, node);
  159. }
  160. else
  161. {
  162. OnExamineExportFillNode(propertyInfo.Name, (Node)value, false);
  163. }
  164. }
  165. }
  166. }
  167.  
  168. private Node _FindNodeInChild(Node node, string name, Type type)
  169. {
  170. var childCount = node.GetChildCount();
  171. for (int i = 0; i < childCount; i++)
  172. {
  173. var child = node.GetChild(i);
  174. if (child.Name == name && child.GetType().IsAssignableTo(type))
  175. {
  176. return child;
  177. }
  178. else
  179. {
  180. var result = _FindNodeInChild(child, name, type);
  181. if (result != null)
  182. {
  183. return result;
  184. }
  185. }
  186. }
  187.  
  188. return null;
  189. }
  190. #endif
  191. }