aboutsummaryrefslogtreecommitdiff
path: root/Assets/Packages/Lean/Common/Examples/Scripts/LeanDocumentation.cs
blob: 5818f48a33f3ac234fee1bff36d5f07d8ce56ca1 (plain) (blame)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Collections.Generic;

namespace Lean.Common.Examples
{
	[CustomEditor(typeof(TextAsset))]
	public class LeanDocumentation_Inspector : Editor
	{
		private static GUIStyle titleStyle;

		private static GUIStyle headerStyle;

		private static GUIStyle bodyStyle;

		private static GUIStyle rateStyle;

		private Dictionary<string, string> infos = new Dictionary<string, string>();

		private Texture2D icon;

		private Texture2D thumb;

		public static void UpdateStyles()
		{
			if (bodyStyle == null)
			{
				bodyStyle = new GUIStyle(EditorStyles.label);
				bodyStyle.wordWrap = true;
				bodyStyle.fontSize = 14;

				titleStyle = new GUIStyle(bodyStyle);
				titleStyle.fontSize = 26;
				titleStyle.alignment = TextAnchor.MiddleCenter;

				headerStyle = new GUIStyle(bodyStyle);
				headerStyle.fontSize = 18 ;

				rateStyle = new GUIStyle(EditorStyles.toolbarButton);

				rateStyle.fontSize = 20;
			}
		}

		public override void OnInspectorGUI()
		{
			var path = AssetDatabase.GetAssetPath(target);

			if (path.Contains("Lean") == true && path.EndsWith("DOCUMENTATION.html") == true)
			{
				UpdateStyles();

				EditorGUI.EndDisabledGroup();

				EditorGUILayout.LabelField("Thank You For Using " + Info("Title", "this asset") + "!", headerStyle);
				EditorGUILayout.LabelField("The documentation is in HTML format. You can open it by double clicking on this file, or by clicking below.", bodyStyle);

				if (GUILayout.Button(new GUIContent("Open Documentation", "Open In Web Browser")) == true)
				{
					System.Diagnostics.Process.Start(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), path));
				}

				EditorGUILayout.Separator();
				EditorGUILayout.Separator();

				EditorGUILayout.LabelField("Need Help?", headerStyle);
				EditorGUILayout.LabelField("If you read the documentation and still have questions, feel free to ask!", bodyStyle);

				if (infos.ContainsKey("Forum") == true)
				{
					if (GUILayout.Button("Forum Thread") == true)
					{
						Application.OpenURL(Info("Forum"));
					}
				}

				if (infos.ContainsKey("YouTube") == true)
				{
					if (GUILayout.Button("YouTube Channel") == true)
					{
						Application.OpenURL(Info("YouTube"));
					}
				}
				
				if (GUILayout.Button(new GUIContent("E-Mail Me", "carlos.wilkes@gmail.com")) == true)
				{
					Application.OpenURL("mailto:carlos.wilkes@gmail.com");
				}

				if (GUILayout.Button(new GUIContent("Private Message", "Unity Forum Profile")) == true)
				{
					Application.OpenURL("http://forum.unity.com/members/41960");
				}

				EditorGUILayout.Separator();
				EditorGUILayout.Separator();

				EditorGUILayout.LabelField("You're Awesome!", headerStyle);
				EditorGUILayout.LabelField("If you haven't already, please consider rating this asset. It really helps me out!", bodyStyle);

				if (GUILayout.Button(new GUIContent("Rate This Asset", Info("Title") + " Asset Page")) == true)
				{
					Application.OpenURL("http://CarlosWilkes.com/Get/" + Info("Link"));
				}

				EditorGUILayout.Separator();
				EditorGUILayout.Separator();

				EditorGUILayout.LabelField("Made Something Cool?", headerStyle);
				EditorGUILayout.LabelField("If you've finished a project using " + Info("Title") + " then let me know! I can shout you out, link to you from my website, and much more!", bodyStyle);

				if (GUILayout.Button(new GUIContent("E-Mail Me", "carlos.wilkes@gmail.com")) == true)
				{
					Application.OpenURL("mailto:carlos.wilkes@gmail.com");
				}

				EditorGUILayout.Separator();
				EditorGUILayout.Separator();

				EditorGUILayout.LabelField("Want More?", headerStyle);
				EditorGUILayout.LabelField("Check out all my other great assets, I'm sure there's something there that can help you!", bodyStyle);

				if (GUILayout.Button(new GUIContent("My Website", "CarlosWilkes.com")) == true)
				{
					Application.OpenURL("http://CarlosWilkes.com" + Info("Link"));
				}
			}
			else
			{
				base.OnInspectorGUI();
			}
		}

		protected override void OnHeaderGUI()
		{
			UpdateStyles();

			GUILayout.BeginHorizontal("In BigTitle");
			{
				var iconWidth = Mathf.Min(EditorGUIUtility.currentViewWidth/3f - 20f, 128f);
				var content   = new GUIContent(Info("Title", "Documentation").Replace(' ', '\n'));

				var height = Mathf.Max(titleStyle.CalcHeight(content, EditorGUIUtility.currentViewWidth - iconWidth), iconWidth);

				if (icon != null)
				{
					GUILayout.Label(icon, EditorStyles.centeredGreyMiniLabel, GUILayout.Width(iconWidth), GUILayout.Height(iconWidth));
				}

				GUILayout.Label(content, titleStyle, GUILayout.Height(height));
			}
			GUILayout.EndHorizontal();
		}

		protected virtual void OnEnable()
		{
			var textAsset = (TextAsset)target;
			var text      = textAsset.text;
			var blockA    = text.IndexOf("<!--");
			var blockB    = text.IndexOf("-->");

			if (blockA >= 0 && blockB >= 0)
			{
				var section = text.Substring(blockA, blockB - blockA);
				var lines   = section.Split('\r', '\n');

				foreach (var line in lines)
				{
					var tokens = line.Split(':');

					if (tokens.Length == 2)
					{
						var k = tokens[0];
						var v = tokens[1];

						if (k == "Icon")
						{
							icon = new Texture2D(1, 1);

							icon.LoadImage(System.Convert.FromBase64String(v));
						}
						else if (k == "Thumb")
						{
							thumb = new Texture2D(1, 1);

							thumb.LoadImage(System.Convert.FromBase64String(v));
						}
						else
						{
							infos.Add(k, v);
						}
					}
				}
			}
		}

		private string Info(string key, string fallback = null)
		{
			var value = default(string);

			if (infos.TryGetValue(key, out value) == false)
			{
				value = fallback;
			}

			return value;
		}
	}
}

namespace Lean.Common.Examples
{
	/// <summary>Unity hijacks html file opening and passes it to the default text editor. For documentation files we want to use an actual browser for this, so hijack it back!</summary>
	public static class LeanDocumentation
	{
		[OnOpenAsset(1)]
		public static bool step1(int instanceID, int line)
		{
			var path = AssetDatabase.GetAssetPath(instanceID);

			if (path.Contains("Lean") == true && path.EndsWith("DOCUMENTATION.html") == true)
			{
				System.Diagnostics.Process.Start(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), path));

				return true;
			}

			return false;
		}
	}
}
#endif