Subversion Repository Public Repository

Nextrek

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
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"ActionsManager.cs"
 * 
 *	This script handles the "Actions" tab of the main wizard.
 *	Custom actions can be added and removed by selecting them with this.
 * 
 */

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

#if UNITY_EDITOR
using UnityEditor;
#endif

[System.Serializable]
public class ActionsManager : ScriptableObject
{

	public string customFolderPath = "AdventureCreator/Scripts/Actions";
	public string folderPath = "AdventureCreator/Scripts/Actions";
	
	public int defaultClass;

	public List<ActionType> AllActions;
	public List<ActionType> EnabledActions;

	private Vector2 scrollPos;
	
	
	public string GetDefaultAction ()
	{
		if (EnabledActions.Count > 0 && EnabledActions.Count > defaultClass)
		{
			return EnabledActions[defaultClass].fileName;
		}

		return "";
	}
	
	
	#if UNITY_EDITOR
	
	private static GUILayoutOption
		titleWidth = GUILayout.MaxWidth (160),
		nameWidth = GUILayout.MaxWidth (130);
	
	
	public void ShowGUI ()
	{
		#if UNITY_WEBPLAYER
		EditorGUILayout.HelpBox ("Locating Actions requires a non-WebPlayer platform.  Please switch platform and try again.", MessageType.Warning);
		GUILayout.Space (10);
		#endif


		EditorGUILayout.BeginVertical ("Button");
		GUILayout.Label ("Custom Action scripts", EditorStyles.boldLabel);
		GUILayout.BeginHorizontal ();
			GUILayout.Label ("Folder to search:", GUILayout.Width (110f));
			GUILayout.Label (customFolderPath, EditorStyles.textField);
		GUILayout.EndHorizontal ();
		GUILayout.BeginHorizontal ();
			if (GUILayout.Button ("Set directory"))
			{
				string path = EditorUtility.OpenFolderPanel("Set custom Actions directory", "Assets", "");
				string dataPath = Application.dataPath;
				if (path.Contains (dataPath))
				{
					if (path == dataPath)
					{
						customFolderPath = "";
					}
					else
					{
						customFolderPath = path.Replace (dataPath + "/", "");
					}
				}
				else
				{
					Debug.LogError ("Cannot set new directory - be sure to select within the Assets directory.");
				}
			}
			if (GUILayout.Button ("Refresh list"))
			{
				RefreshList ();
			}
		GUILayout.EndHorizontal ();
		EditorGUILayout.EndVertical ();

		if (AllActions.Count > 0)
		{
			GUILayout.Space (10);
			
			defaultClass = EditorGUILayout.Popup ("Default action:", defaultClass, GetDefaultPopUp ());
			GUILayout.Space (10);
			
			GUILayout.BeginHorizontal ();
			
				GUILayout.Label ("Title", titleWidth);
				GUILayout.Label ("Filename", nameWidth);
				GUILayout.Label ("Enabled?", GUILayout.MaxWidth (50));
			
			GUILayout.EndHorizontal ();
			
			foreach (ActionType subclass in AllActions)
			{
				GUILayout.BeginVertical ("Button");
					GUILayout.BeginHorizontal ();
		
						GUILayout.Label (subclass.title, titleWidth);
						GUILayout.Label (subclass.fileName, nameWidth);
						
						subclass.isEnabled = GUILayout.Toggle (subclass.isEnabled, "");
					
					GUILayout.EndHorizontal();
				GUILayout.EndVertical ();
			}
			
			SetEnabled ();
			
			if (defaultClass > EnabledActions.Count - 1)
			{
				defaultClass = EnabledActions.Count - 1;
			}

		}
		else
		{
			EditorGUILayout.HelpBox ("No Action subclass files found.", MessageType.Warning);
		}
		
		EditorUtility.SetDirty (this);
	}
	

	public void RefreshList ()
	{
		#if !UNITY_WEBPLAYER
		
		Undo.RecordObject (this, "Refresh list");

		// Load default Actions
		DirectoryInfo dir = new DirectoryInfo ("Assets/" + folderPath);
		FileInfo[] info = dir.GetFiles ("*.cs");

		AllActions.Clear ();
		
		foreach (FileInfo f in info) 
		{
			int extentionPosition = f.Name.IndexOf (".cs");
			string className = f.Name.Substring (0, extentionPosition);
			AC.Action tempAction = (AC.Action) CreateInstance (className);
			string title = tempAction.title;
			AllActions.Add (new ActionType (className, title));
		}

		// Load custom Actions
		if (customFolderPath != folderPath)
		{
			dir = new DirectoryInfo ("Assets/" + customFolderPath);
			info = dir.GetFiles ("*.cs");
			
			foreach (FileInfo f in info) 
			{
				try
				{
					int extentionPosition = f.Name.IndexOf (".cs");
					string className = f.Name.Substring (0, extentionPosition);
					AC.Action tempAction = (AC.Action) CreateInstance (className);
					if (tempAction is AC.Action)
					{
						string title = tempAction.title;
						AllActions.Add (new ActionType (className, title));
					}
				}
				catch {}
			}
		}
			
		AllActions.Sort (delegate(ActionType i1, ActionType i2) { return i1.title.CompareTo(i2.title); });

		#endif
	}
	
	
	#endif
	
	private void SetEnabled ()
	{
		EnabledActions.Clear ();
		
		foreach (ActionType subclass in AllActions)
		{
			if (subclass.isEnabled)
			{
				EnabledActions.Add (subclass);
			}
		}
	}
	
	
	private string[] GetDefaultPopUp ()
	{
		List<string> defaultPopUp = new List<string> ();

		foreach (ActionType subclass in EnabledActions)
		{
			defaultPopUp.Add (subclass.title);
		}
	
		return (defaultPopUp.ToArray ());
	}
	
	
	public string GetActionName (int i)
	{
		return (EnabledActions [i].fileName);
	}
	
	
	public int GetActionsSize ()
	{
		return (EnabledActions.Count);
	}
	
	
	public string[] GetActionTitles ()
	{
		List<string> titles = new List<string>();
	
		foreach (ActionType type in EnabledActions)
		{
			titles.Add (type.title);
		}
		
		return (titles.ToArray ());
	}
	

}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Managers/ActionsManager.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000