summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Nodes/Master/DependenciesHelper.cs
blob: 915f173ce742db5615313048c687caa85c42c2f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace AmplifyShaderEditor
{
    [Serializable]
    public class DependenciesData
    {
        private string DependencyFormat = "Dependency \"{0}\"=\"{1}\"\n";
        public string DependencyName;
        public string DependencyValue;
        public bool DependencyFoldout = true;

        public DependenciesData()
        {
            DependencyName = string.Empty;
            DependencyValue = string.Empty;
        }

        public DependenciesData( string data )
        {
            string[] arr = data.Split( IOUtils.VALUE_SEPARATOR );
            if( arr.Length > 1 )
            {
                DependencyName = arr[ 0 ];
                DependencyValue = arr[ 1 ];
            }
        }

        public override string ToString()
        {
            return DependencyName + IOUtils.VALUE_SEPARATOR + DependencyValue;
        }

        public string GenerateDependency()
        {
            return string.Format( DependencyFormat, DependencyName, DependencyValue );
        }

        public bool IsValid { get { return ( !string.IsNullOrEmpty( DependencyValue ) && !string.IsNullOrEmpty( DependencyName ) ); } }
    }

    [Serializable]
    public class DependenciesHelper
    {
        private const string CustomDependencysStr = " Dependencies";
        private const string DependencyNameStr = "Name";
        private const string DependencyValueStr = "Value";

        private const float ShaderKeywordButtonLayoutWidth = 15;
        private ParentNode m_currentOwner;

        [SerializeField]
        private List<DependenciesData> m_availableDependencies = new List<DependenciesData>();

        public void Draw( ParentNode owner, bool isNested = false )
        {
            m_currentOwner = owner;
            bool value = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDependencies;
			if( isNested )
			{
				NodeUtils.DrawNestedPropertyGroup( ref value, CustomDependencysStr, DrawMainBody, DrawButtons );
			}
			else
			{
				NodeUtils.DrawPropertyGroup( ref value, CustomDependencysStr, DrawMainBody, DrawButtons );
			}
			owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedDependencies = value;
        }

        void DrawButtons()
        {
            EditorGUILayout.Separator();

            // Add Dependency
            if( GUILayout.Button( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
            {
                m_availableDependencies.Add( new DependenciesData() );
                EditorGUI.FocusTextInControl( null );
            }

            //Remove Dependency
            if( GUILayout.Button( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
            {
                if( m_availableDependencies.Count > 0 )
                {
                    m_availableDependencies.RemoveAt( m_availableDependencies.Count - 1 );
                    EditorGUI.FocusTextInControl( null );
                }
            }
        }

        void DrawMainBody()
        {
            EditorGUILayout.Separator();
            int itemCount = m_availableDependencies.Count;

            if( itemCount == 0 )
            {
                EditorGUILayout.HelpBox( "Your list is Empty!\nUse the plus button to add one.", MessageType.Info );
            }

            int markedToDelete = -1;
            float originalLabelWidth = EditorGUIUtility.labelWidth;
            for( int i = 0; i < itemCount; i++ )
            {
                m_availableDependencies[ i ].DependencyFoldout = m_currentOwner.EditorGUILayoutFoldout( m_availableDependencies[ i ].DependencyFoldout, string.Format( "[{0}] - {1}", i, m_availableDependencies[ i ].DependencyName ) );
                if( m_availableDependencies[ i ].DependencyFoldout )
                {
                    EditorGUI.indentLevel += 1;
                    EditorGUIUtility.labelWidth = 70;
                    //Dependency Name
                    EditorGUI.BeginChangeCheck();
                    m_availableDependencies[ i ].DependencyName = EditorGUILayout.TextField( DependencyNameStr, m_availableDependencies[ i ].DependencyName );
                    if( EditorGUI.EndChangeCheck() )
                    {
                        m_availableDependencies[ i ].DependencyName = UIUtils.RemoveShaderInvalidCharacters( m_availableDependencies[ i ].DependencyName );
                    }

                    //Dependency Value
                    EditorGUI.BeginChangeCheck();
                    m_availableDependencies[ i ].DependencyValue = EditorGUILayout.TextField( DependencyValueStr, m_availableDependencies[ i ].DependencyValue );
                    if( EditorGUI.EndChangeCheck() )
                    {
                        m_availableDependencies[ i ].DependencyValue = UIUtils.RemoveShaderInvalidCharacters( m_availableDependencies[ i ].DependencyValue );
                    }

                    EditorGUIUtility.labelWidth = originalLabelWidth;

                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Label( " " );
                        // Add new port
                        if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.PlusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
                        {
                            m_availableDependencies.Insert( i + 1, new DependenciesData() );
                            EditorGUI.FocusTextInControl( null );
                        }

                        //Remove port
                        if( m_currentOwner.GUILayoutButton( string.Empty, UIUtils.MinusStyle, GUILayout.Width( ShaderKeywordButtonLayoutWidth ) ) )
                        {
                            markedToDelete = i;
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    EditorGUI.indentLevel -= 1;
                }

            }
            if( markedToDelete > -1 )
            {
                if( m_availableDependencies.Count > markedToDelete )
                {
                    m_availableDependencies.RemoveAt( markedToDelete );
                    EditorGUI.FocusTextInControl( null );
                }
            }
            EditorGUILayout.Separator();
        }


        public void ReadFromString( ref uint index, ref string[] nodeParams )
        {
            int count = Convert.ToInt32( nodeParams[ index++ ] );
            for( int i = 0; i < count; i++ )
            {
                m_availableDependencies.Add( new DependenciesData( nodeParams[ index++ ] ) );
            }
        }

        public void WriteToString( ref string nodeInfo )
        {
            int dependencyCount = m_availableDependencies.Count;
            IOUtils.AddFieldValueToString( ref nodeInfo, dependencyCount );
            for( int i = 0; i < dependencyCount; i++ )
            {
                IOUtils.AddFieldValueToString( ref nodeInfo, m_availableDependencies[ i ].ToString() );
            }
        }

        public string GenerateDependencies()
        {
            int dependencyCount = m_availableDependencies.Count;
            string result = dependencyCount == 0 ? string.Empty : "\n";
            UIUtils.ShaderIndentLevel++;
            for( int i = 0; i < dependencyCount; i++ )
            {
                if( m_availableDependencies[ i ].IsValid )
                {
                    result += UIUtils.ShaderIndentTabs + m_availableDependencies[ i ].GenerateDependency();
                }
            }
            UIUtils.ShaderIndentLevel--;
            return result;
        }

		public void Destroy()
        {
            m_availableDependencies.Clear();
            m_availableDependencies = null;
            m_currentOwner = null;
        }

        public bool HasDependencies { get { return m_availableDependencies.Count > 0; } }
    }
}