No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PSDImporterEditorLayerTreeView.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor.Experimental;
  6. using UnityEditor.IMGUI.Controls;
  7. using UnityEngine;
  8. using UnityEngine.UIElements;
  9. using Object = UnityEngine.Object;
  10. namespace UnityEditor.U2D.PSD
  11. {
  12. internal class PSDLayerImportSettingSerializedPropertyWrapper : IPSDLayerMappingStrategyComparable
  13. {
  14. PSDLayerData m_Layer;
  15. SerializedProperty m_Array;
  16. SerializedProperty m_Element;
  17. SerializedProperty m_NameProperty;
  18. SerializedProperty m_LayerIdProperty;
  19. SerializedProperty m_FlattenProperty;
  20. SerializedProperty m_IsGroupProperty;
  21. SerializedProperty m_ImportLayerProperty;
  22. int m_ArrayIndex;
  23. bool m_WasLayerImported;
  24. public string name
  25. {
  26. get => m_NameProperty.stringValue;
  27. set
  28. {
  29. CheckAndAddElement();
  30. m_NameProperty.stringValue = value;
  31. }
  32. }
  33. public bool isGroup
  34. {
  35. get => m_IsGroupProperty.boolValue;
  36. set
  37. {
  38. CheckAndAddElement();
  39. m_IsGroupProperty.boolValue = value;
  40. }
  41. }
  42. public int layerID
  43. {
  44. get => m_LayerIdProperty.intValue;
  45. set
  46. {
  47. CheckAndAddElement();
  48. m_LayerIdProperty.intValue = value;
  49. }
  50. }
  51. public bool flatten
  52. {
  53. get
  54. {
  55. CheckIfIndexChanged();
  56. return m_FlattenProperty == null ? false : m_FlattenProperty.boolValue;
  57. }
  58. set
  59. {
  60. CheckAndAddElement();
  61. if (m_FlattenProperty.boolValue != value)
  62. {
  63. m_FlattenProperty.boolValue = value;
  64. m_FlattenProperty.serializedObject.ApplyModifiedProperties();
  65. }
  66. }
  67. }
  68. public bool wasLayerImported
  69. {
  70. get => m_WasLayerImported;
  71. set => m_WasLayerImported = value;
  72. }
  73. public bool importLayer
  74. {
  75. get
  76. {
  77. CheckIfIndexChanged();
  78. return m_ImportLayerProperty == null ? wasLayerImported : m_ImportLayerProperty.boolValue;
  79. }
  80. set
  81. {
  82. CheckAndAddElement();
  83. if (m_ImportLayerProperty.boolValue != value)
  84. {
  85. m_ImportLayerProperty.boolValue = value;
  86. m_ImportLayerProperty.serializedObject.ApplyModifiedProperties();
  87. }
  88. }
  89. }
  90. void CheckIfIndexChanged()
  91. {
  92. if (m_ArrayIndex >= m_Array.arraySize)
  93. {
  94. m_ArrayIndex = m_Array.arraySize - 1;
  95. m_NameProperty = null;
  96. m_LayerIdProperty = null;
  97. m_FlattenProperty = null;
  98. m_IsGroupProperty = null;
  99. m_ImportLayerProperty = null;
  100. m_Element = null;
  101. }
  102. }
  103. void CheckAndAddElement()
  104. {
  105. if (m_Element == null)
  106. {
  107. var arraySize = m_Array.arraySize;
  108. m_ArrayIndex = arraySize;
  109. m_Array.arraySize = arraySize + 1;
  110. m_Element = m_Array.GetArrayElementAtIndex(arraySize);
  111. CacheProperty(m_Element);
  112. flatten = false;
  113. name = m_Layer.name;
  114. layerID = m_Layer.layerID;
  115. isGroup = m_Layer.isGroup;
  116. importLayer = wasLayerImported;
  117. }
  118. }
  119. void CacheProperty(SerializedProperty property)
  120. {
  121. m_NameProperty = property.FindPropertyRelative("name");
  122. m_LayerIdProperty = property.FindPropertyRelative("layerId");
  123. m_FlattenProperty = property.FindPropertyRelative("flatten");
  124. m_IsGroupProperty = property.FindPropertyRelative("isGroup");
  125. m_ImportLayerProperty = property.FindPropertyRelative("importLayer");
  126. }
  127. public PSDLayerImportSettingSerializedPropertyWrapper(SerializedProperty sp, SerializedProperty array, PSDLayerData layer, int index)
  128. {
  129. if (sp != null)
  130. {
  131. m_Element = sp;
  132. CacheProperty(sp);
  133. }
  134. m_Layer = layer;
  135. m_Array = array;
  136. m_ArrayIndex = index;
  137. }
  138. }
  139. class PSDTreeViewNode : TreeViewItem
  140. {
  141. PSDLayerData m_Layer;
  142. bool m_Disable = false;
  143. public PSDLayerData layer => m_Layer;
  144. PSDLayerImportSettingSerializedPropertyWrapper m_Property;
  145. public bool disable
  146. {
  147. get => m_Disable;
  148. set => m_Disable = value;
  149. }
  150. public PSDTreeViewNode()
  151. {
  152. id = 1;
  153. displayName = "";
  154. }
  155. public PSDTreeViewNode(PSDLayerData layer, int id, PSDLayerImportSettingSerializedPropertyWrapper importSetting)
  156. {
  157. m_Layer = layer;
  158. displayName = layer.name;
  159. this.id = id;
  160. m_Property = importSetting;
  161. }
  162. protected PSDLayerImportSettingSerializedPropertyWrapper property => m_Property;
  163. public virtual bool importLayer
  164. {
  165. get => property.importLayer;
  166. set
  167. {
  168. if (property.importLayer != value)
  169. {
  170. property.importLayer = value;
  171. }
  172. }
  173. }
  174. public TreeViewItemData<int> BuildTreeViewItemData()
  175. {
  176. var c =new List<TreeViewItemData<int>>();
  177. if (children != null)
  178. {
  179. c = children.Select(x =>
  180. {
  181. var n = (PSDTreeViewNode)x;
  182. return n.BuildTreeViewItemData();
  183. }).ToList();
  184. }
  185. return new TreeViewItemData<int>(id, id, c);
  186. }
  187. }
  188. class PSDFoldoutTreeViewNode :PSDTreeViewNode
  189. {
  190. public virtual bool flatten
  191. {
  192. get => property.flatten;
  193. set
  194. {
  195. if(property.flatten != value)
  196. property.flatten = value;
  197. }
  198. }
  199. public PSDFoldoutTreeViewNode()
  200. : base()
  201. { }
  202. public PSDFoldoutTreeViewNode(PSDLayerData layer, int id, PSDLayerImportSettingSerializedPropertyWrapper property)
  203. : base(layer, id, property)
  204. { }
  205. }
  206. class PSDFileTreeViewNode : PSDFoldoutTreeViewNode
  207. {
  208. LayerManagementTreeViewData m_PsdFileSerializedProperty;
  209. public PSDFileTreeViewNode(LayerManagementTreeViewData sp)
  210. {
  211. m_PsdFileSerializedProperty = sp;
  212. }
  213. public override bool flatten
  214. {
  215. get => !m_PsdFileSerializedProperty.mosaicLayers.boolValue;
  216. set
  217. {
  218. if (m_PsdFileSerializedProperty.mosaicLayers.boolValue == value)
  219. {
  220. m_PsdFileSerializedProperty.mosaicLayers.boolValue = !value;
  221. m_PsdFileSerializedProperty.mosaicLayers.serializedObject.ApplyModifiedProperties();
  222. }
  223. }
  224. }
  225. public override bool importLayer
  226. {
  227. get => m_PsdFileSerializedProperty.importFileNodeState.boolValue;
  228. set
  229. {
  230. if (m_PsdFileSerializedProperty.importFileNodeState.boolValue != value)
  231. {
  232. m_PsdFileSerializedProperty.importFileNodeState.boolValue = value;
  233. m_PsdFileSerializedProperty.importFileNodeState.serializedObject.ApplyModifiedProperties();
  234. }
  235. }
  236. }
  237. }
  238. class PSDLayerTreeViewNode : PSDTreeViewNode
  239. {
  240. public PSDLayerTreeViewNode(PSDLayerData layer, int id, PSDLayerImportSettingSerializedPropertyWrapper property):base(layer, id, property)
  241. { }
  242. }
  243. class PSDGroupTreeViewNode : PSDFoldoutTreeViewNode
  244. {
  245. public PSDGroupTreeViewNode(PSDLayerData layer, int id, PSDLayerImportSettingSerializedPropertyWrapper property)
  246. : base(layer, id, property)
  247. {
  248. this.icon = EditorGUIUtility.FindTexture(EditorResources.folderIconName);
  249. }
  250. }
  251. }