summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/AmplifyShaderEditor/Plugins/Editor/Templates/TemplateCodeSnippetBase.cs
blob: ea717e6295b3afb89b9ddaa1a35b3747fbe1555d (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
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>

using System;
using UnityEngine;
using System.Collections.Generic;

namespace AmplifyShaderEditor
{
	public enum TemplateCodeSnippetType
	{
		Toggle
	};


	public enum TemplateCodeSnippetInfoIdx
	{
		Name = 0, 
		Type
	};

	[Serializable]
	public class TemplateCodeSnippetElement
	{
		public string Id;
		public string Snippet;
		public TemplateCodeSnippetElement( string id, string snippet )
		{
			Id = id;
			Snippet = snippet;
		}
	}

	[Serializable]
	public class TemplateCodeSnippetBase : ScriptableObject
	{
		[SerializeField]
		private string m_nameId;

		[SerializeField]
		private TemplateCodeSnippetType m_type;

		[SerializeField]
		private List<TemplateCodeSnippetElement> m_elements = new List<TemplateCodeSnippetElement>();
		
		public void Init( string nameId, TemplateCodeSnippetType type )
		{
			m_nameId = nameId;
			m_type = type;
		}

		public void AddSnippet( TemplateCodeSnippetElement element )
		{
			m_elements.Add( element );
		}

		public void Destroy()
		{
			for ( int i = 0; i < m_elements.Count; i++ )
			{
				m_elements[ i ].Snippet = null;
			}
			m_elements.Clear();
			m_elements = null;
		}

		public virtual void DrawProperties( ParentNode owner ) { }
		public virtual bool CheckSnippet() { return true; }

		public void InsertSnippet( ref string shaderBody )
		{
			bool insertSnippet = CheckSnippet();
			for ( int i = 0; i < m_elements.Count; i++ )
			{
				shaderBody = shaderBody.Replace( m_elements[ i ].Id, ( insertSnippet ? m_elements[ i ].Snippet : string.Empty ) );
			}
		}
		public string NameId { get { return m_nameId; } }
		public TemplateCodeSnippetType Type { get { return m_type; } }
		public List<TemplateCodeSnippetElement> Elements { get { return m_elements; } }
	}

	[Serializable]
	public class TemplateCodeSnippetToggle : TemplateCodeSnippetBase
	{
		private const string Label = "Activate";
		[SerializeField]
		private bool m_value = false;


		public override bool CheckSnippet()
		{
			return m_value;
		}

		public override void DrawProperties( ParentNode owner )
		{
			m_value = owner.EditorGUILayoutToggle( Label, m_value );
		}
	}
	
}