123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using UnityEditor.Experimental;
- using UnityEditor.IMGUI.Controls;
- using UnityEngine;
- using UnityEngine.UIElements;
- using Object = UnityEngine.Object;
-
- namespace UnityEditor.U2D.PSD
- {
- internal class PSDLayerImportSettingSerializedPropertyWrapper : IPSDLayerMappingStrategyComparable
- {
- PSDLayerData m_Layer;
- SerializedProperty m_Array;
- SerializedProperty m_Element;
- SerializedProperty m_NameProperty;
- SerializedProperty m_LayerIdProperty;
- SerializedProperty m_FlattenProperty;
- SerializedProperty m_IsGroupProperty;
- SerializedProperty m_ImportLayerProperty;
- int m_ArrayIndex;
- bool m_WasLayerImported;
-
- public string name
- {
- get => m_NameProperty.stringValue;
- set
- {
- CheckAndAddElement();
- m_NameProperty.stringValue = value;
- }
- }
-
- public bool isGroup
- {
- get => m_IsGroupProperty.boolValue;
- set
- {
- CheckAndAddElement();
- m_IsGroupProperty.boolValue = value;
- }
- }
-
- public int layerID
- {
- get => m_LayerIdProperty.intValue;
- set
- {
- CheckAndAddElement();
- m_LayerIdProperty.intValue = value;
- }
- }
-
- public bool flatten
- {
- get
- {
- CheckIfIndexChanged();
- return m_FlattenProperty == null ? false : m_FlattenProperty.boolValue;
- }
- set
- {
- CheckAndAddElement();
- if (m_FlattenProperty.boolValue != value)
- {
- m_FlattenProperty.boolValue = value;
- m_FlattenProperty.serializedObject.ApplyModifiedProperties();
- }
- }
- }
-
- public bool wasLayerImported
- {
- get => m_WasLayerImported;
- set => m_WasLayerImported = value;
- }
-
- public bool importLayer
- {
- get
- {
- CheckIfIndexChanged();
- return m_ImportLayerProperty == null ? wasLayerImported : m_ImportLayerProperty.boolValue;
- }
- set
- {
- CheckAndAddElement();
- if (m_ImportLayerProperty.boolValue != value)
- {
- m_ImportLayerProperty.boolValue = value;
- m_ImportLayerProperty.serializedObject.ApplyModifiedProperties();
- }
- }
- }
-
- void CheckIfIndexChanged()
- {
- if (m_ArrayIndex >= m_Array.arraySize)
- {
- m_ArrayIndex = m_Array.arraySize - 1;
- m_NameProperty = null;
- m_LayerIdProperty = null;
- m_FlattenProperty = null;
- m_IsGroupProperty = null;
- m_ImportLayerProperty = null;
- m_Element = null;
- }
-
- }
- void CheckAndAddElement()
- {
- if (m_Element == null)
- {
- var arraySize = m_Array.arraySize;
- m_ArrayIndex = arraySize;
- m_Array.arraySize = arraySize + 1;
- m_Element = m_Array.GetArrayElementAtIndex(arraySize);
- CacheProperty(m_Element);
- flatten = false;
- name = m_Layer.name;
- layerID = m_Layer.layerID;
- isGroup = m_Layer.isGroup;
- importLayer = wasLayerImported;
- }
- }
-
- void CacheProperty(SerializedProperty property)
- {
- m_NameProperty = property.FindPropertyRelative("name");
- m_LayerIdProperty = property.FindPropertyRelative("layerId");
- m_FlattenProperty = property.FindPropertyRelative("flatten");
- m_IsGroupProperty = property.FindPropertyRelative("isGroup");
- m_ImportLayerProperty = property.FindPropertyRelative("importLayer");
- }
-
- public PSDLayerImportSettingSerializedPropertyWrapper(SerializedProperty sp, SerializedProperty array, PSDLayerData layer, int index)
- {
- if (sp != null)
- {
- m_Element = sp;
- CacheProperty(sp);
- }
-
- m_Layer = layer;
- m_Array = array;
- m_ArrayIndex = index;
- }
- }
-
- class PSDTreeViewNode : TreeViewItem
- {
- PSDLayerData m_Layer;
- bool m_Disable = false;
- public PSDLayerData layer => m_Layer;
-
- PSDLayerImportSettingSerializedPropertyWrapper m_Property;
-
- public bool disable
- {
- get => m_Disable;
- set => m_Disable = value;
- }
-
- public PSDTreeViewNode()
- {
- id = 1;
- displayName = "";
- }
-
- public PSDTreeViewNode(PSDLayerData layer, int id, PSDLayerImportSettingSerializedPropertyWrapper importSetting)
- {
- m_Layer = layer;
- displayName = layer.name;
- this.id = id;
- m_Property = importSetting;
- }
-
- protected PSDLayerImportSettingSerializedPropertyWrapper property => m_Property;
-
-
- public virtual bool importLayer
- {
- get => property.importLayer;
- set
- {
- if (property.importLayer != value)
- {
- property.importLayer = value;
- }
- }
- }
-
- public TreeViewItemData<int> BuildTreeViewItemData()
- {
- var c =new List<TreeViewItemData<int>>();
- if (children != null)
- {
- c = children.Select(x =>
- {
- var n = (PSDTreeViewNode)x;
- return n.BuildTreeViewItemData();
- }).ToList();
- }
- return new TreeViewItemData<int>(id, id, c);
- }
- }
-
- class PSDFoldoutTreeViewNode :PSDTreeViewNode
- {
- public virtual bool flatten
- {
- get => property.flatten;
- set
- {
- if(property.flatten != value)
- property.flatten = value;
- }
- }
-
- public PSDFoldoutTreeViewNode()
- : base()
- { }
-
- public PSDFoldoutTreeViewNode(PSDLayerData layer, int id, PSDLayerImportSettingSerializedPropertyWrapper property)
- : base(layer, id, property)
- { }
-
- }
-
- class PSDFileTreeViewNode : PSDFoldoutTreeViewNode
- {
- LayerManagementTreeViewData m_PsdFileSerializedProperty;
-
- public PSDFileTreeViewNode(LayerManagementTreeViewData sp)
- {
- m_PsdFileSerializedProperty = sp;
- }
- public override bool flatten
- {
- get => !m_PsdFileSerializedProperty.mosaicLayers.boolValue;
- set
- {
- if (m_PsdFileSerializedProperty.mosaicLayers.boolValue == value)
- {
- m_PsdFileSerializedProperty.mosaicLayers.boolValue = !value;
- m_PsdFileSerializedProperty.mosaicLayers.serializedObject.ApplyModifiedProperties();
- }
- }
- }
-
- public override bool importLayer
- {
- get => m_PsdFileSerializedProperty.importFileNodeState.boolValue;
- set
- {
- if (m_PsdFileSerializedProperty.importFileNodeState.boolValue != value)
- {
- m_PsdFileSerializedProperty.importFileNodeState.boolValue = value;
- m_PsdFileSerializedProperty.importFileNodeState.serializedObject.ApplyModifiedProperties();
- }
- }
- }
- }
-
- class PSDLayerTreeViewNode : PSDTreeViewNode
- {
- public PSDLayerTreeViewNode(PSDLayerData layer, int id, PSDLayerImportSettingSerializedPropertyWrapper property):base(layer, id, property)
- { }
- }
-
- class PSDGroupTreeViewNode : PSDFoldoutTreeViewNode
- {
- public PSDGroupTreeViewNode(PSDLayerData layer, int id, PSDLayerImportSettingSerializedPropertyWrapper property)
- : base(layer, id, property)
- {
- this.icon = EditorGUIUtility.FindTexture(EditorResources.folderIconName);
- }
- }
- }
|