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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"MenuElement.cs"
 * 
 *	This is the base class for all menu elements.  It should never
 *	be added itself to a menu, as it is only a container of shared data.
 * 
 */

using UnityEngine;
using AC;

#if UNITY_EDITOR
using UnityEditor;
#endif

[System.Serializable]
public class MenuElement : ScriptableObject
{
	public int ID;
	public bool isEditing = false;
	public string title = "Element";
	public Vector2 slotSize;
	public AC_SizeType sizeType;
	public AC_PositionType2 positionType;
	public int lineID = -1;

	public Font font;
	public float fontScaleFactor = 60f;
	public Color fontColor = Color.white;
	public Color fontHighlightColor = Color.white;
	public Texture2D highlightTexture;
	
	public bool isVisible;
	public bool isClickable;
	public ElementOrientation orientation = ElementOrientation.Vertical;
	public int gridWidth = 3;

	public Texture2D backgroundTexture;

	public AudioClip hoverSound;
	public AudioClip clickSound;
	
	[SerializeField] protected Rect relativeRect;
	[SerializeField] protected Vector2 relativePosition;
	[SerializeField] protected int numSlots;
	

	public virtual void Declare ()
	{
		fontScaleFactor = 2f;
		fontColor = Color.white;
		fontHighlightColor = Color.white;
		highlightTexture = null;
		orientation = ElementOrientation.Vertical;
		positionType = AC_PositionType2.Aligned;
		sizeType = AC_SizeType.Automatic;
		gridWidth = 3;
		lineID = -1;
		hoverSound = null;
		clickSound = null;
	}
	
	
	public virtual void Copy (MenuElement _element)
	{
		ID = _element.ID;
		isEditing = false;
		title = _element.title;
		slotSize = _element.slotSize;
		sizeType = _element.sizeType;
		positionType = _element.positionType;
		relativeRect = _element.relativeRect;
		numSlots = _element.numSlots;
		lineID = _element.lineID;
	
		font = _element.font;
		fontScaleFactor = _element.fontScaleFactor;
		fontColor = _element.fontColor;
		fontHighlightColor = _element.fontHighlightColor;
		highlightTexture = _element.highlightTexture;
		
		isVisible = _element.isVisible;
		isClickable = _element.isClickable;
		orientation = _element.orientation;
		gridWidth = _element.gridWidth;

		backgroundTexture = _element.backgroundTexture;

		hoverSound = _element.hoverSound;
		clickSound = _element.clickSound;

		relativePosition = _element.relativePosition;
	}


	protected string TranslateLabel (string label)
	{
		if (Options.GetLanguage () > 0 && lineID > -1)
		{
			return (SpeechManager.GetTranslation (lineID, Options.GetLanguage ()));
		}
		else
		{
			return (label);
		}
	}


	public virtual string GetLabel (int slot)
	{
		return "";
	}

	
	#if UNITY_EDITOR
	
	public void ShowGUIStart ()
	{
		EditorGUILayout.BeginVertical ("Button");
			title = EditorGUILayout.TextField ("Element name:", title);
			isVisible = EditorGUILayout.Toggle ("Is visible?", isVisible);
		EditorGUILayout.EndVertical ();
		
		ShowGUI ();
	}
	
	
	public virtual void ShowGUI ()
	{
		EditorGUILayout.BeginVertical ("Button");
			font = (Font) EditorGUILayout.ObjectField ("Font:", font, typeof (Font), false);
			fontScaleFactor = EditorGUILayout.Slider ("Text size:", fontScaleFactor, 1f, 4f);
			fontColor = EditorGUILayout.ColorField ("Text colour:", fontColor);
			if (isClickable)
			{
				fontHighlightColor = EditorGUILayout.ColorField ("Text colour (highlighted):", fontHighlightColor);
			}
		EditorGUILayout.EndVertical ();
		
		EditorGUILayout.BeginVertical ("Button");
			if (isClickable)
			{
				EditorGUILayout.BeginHorizontal ();
					EditorGUILayout.LabelField ("Highlight texture:", GUILayout.Width (145f));
					highlightTexture = (Texture2D) EditorGUILayout.ObjectField (highlightTexture, typeof (Texture2D), false, GUILayout.Width (70f), GUILayout.Height (30f));
				EditorGUILayout.EndHorizontal ();

				hoverSound = (AudioClip) EditorGUILayout.ObjectField ("Hover sound:", hoverSound, typeof (AudioClip), false);
				clickSound = (AudioClip) EditorGUILayout.ObjectField ("Click sound:", clickSound, typeof (AudioClip), false);
			}
			
			EditorGUILayout.BeginHorizontal ();
				EditorGUILayout.LabelField ("Background texture:", GUILayout.Width (145f));
				backgroundTexture = (Texture2D) EditorGUILayout.ObjectField (backgroundTexture, typeof (Texture2D), false, GUILayout.Width (70f), GUILayout.Height (30f));
			EditorGUILayout.EndHorizontal ();
		EditorGUILayout.EndVertical ();
		
		EndGUI ();
	}
	
	
	public void EndGUI ()
	{
		EditorGUILayout.BeginVertical ("Button");
			positionType = (AC_PositionType2) EditorGUILayout.EnumPopup ("Position:", positionType);
			if (positionType == AC_PositionType2.Absolute)
			{
				EditorGUILayout.BeginHorizontal ();
					EditorGUILayout.LabelField ("X:", GUILayout.Width (15f));
					relativeRect.x = EditorGUILayout.FloatField (relativeRect.x);
					EditorGUILayout.LabelField ("Y:", GUILayout.Width (15f));
					relativeRect.y = EditorGUILayout.FloatField (relativeRect.y);
				EditorGUILayout.EndHorizontal ();
			}
			else if (positionType == AC_PositionType2.RelativeToMenuSize)
			{
				EditorGUILayout.BeginHorizontal ();
					EditorGUILayout.LabelField ("X:", GUILayout.Width (15f));
					relativePosition.x = EditorGUILayout.Slider (relativePosition.x, 0f, 100f);
					EditorGUILayout.LabelField ("Y:", GUILayout.Width (15f));
					relativePosition.y = EditorGUILayout.Slider (relativePosition.y, 0f, 100f);
				EditorGUILayout.EndHorizontal ();
			}
		EditorGUILayout.EndVertical ();
		
		EditorGUILayout.BeginVertical ("Button");
			sizeType = (AC_SizeType) EditorGUILayout.EnumPopup ("Size:", sizeType);
			if (sizeType == AC_SizeType.Manual)
			{
				EditorGUILayout.BeginHorizontal ();
				EditorGUILayout.LabelField ("W:", GUILayout.Width (17f));
				slotSize.x = EditorGUILayout.Slider (slotSize.x, 0f, 100f);
				EditorGUILayout.LabelField ("H:", GUILayout.Width (15f));
				slotSize.y = EditorGUILayout.Slider (slotSize.y, 0f, 100f);
				EditorGUILayout.EndHorizontal ();
			}
		EditorGUILayout.EndVertical ();
	}
	
	
	protected void ShowClipHelp ()
	{
		EditorGUILayout.HelpBox ("MenuSystem.OnElementClick will be run when this element is clicked.", MessageType.Info);
	}
	
	#endif


	public virtual void Display (GUIStyle _style, int _slot, float zoom)
	{
		if (backgroundTexture && _slot == 0)
		{
			GUI.DrawTexture (ZoomRect (relativeRect, zoom), backgroundTexture, ScaleMode.StretchToFill, true, 0f);
		}
	}


	protected virtual Rect ZoomRect (Rect rect, float zoom)
	{
		if (zoom == 1f)
		{
			return rect;
		}

		return (new Rect (rect.x * zoom, rect.y * zoom, rect.width * zoom, rect.height * zoom));
	}
	
	
	public Vector2 GetSize ()
	{
		Vector2 size = new Vector2 (relativeRect.width, relativeRect.height);
		return (size);
	}
	
	
	public Vector2 GetSizeFromCorner ()
	{
		Vector2 size = new Vector2 (relativeRect.width + relativeRect.x, relativeRect.height + relativeRect.y);
		return (size);
	}
	
	
	public void SetSize (Vector2 _size)
	{
		slotSize = new Vector2 (_size.x, _size.y);
	}
	
	
	protected void SetAbsoluteSize (Vector2 _size)
	{
		slotSize = new Vector2 (_size.x * 100f / AdvGame.GetMainGameViewSize ().x, _size.y * 100f / AdvGame.GetMainGameViewSize ().y);
	}
	
	
	public int GetNumSlots ()
	{
		return numSlots;
	}
	
	
	public Rect GetSlotRectRelative (int _slot)
	{
		Rect positionRect = relativeRect;
		positionRect.width = slotSize.x / 100f * AdvGame.GetMainGameViewSize ().x;
		positionRect.height = slotSize.y / 100f * AdvGame.GetMainGameViewSize ().y;
		
		if (_slot > numSlots)
		{
			_slot = numSlots;
		}
		
		if (orientation == ElementOrientation.Horizontal)
		{
			positionRect.x += slotSize.x / 100f * _slot * AdvGame.GetMainGameViewSize ().x;
		}
		else if (orientation == ElementOrientation.Vertical)
		{
			positionRect.y += slotSize.y / 100f * _slot * AdvGame.GetMainGameViewSize ().y;
		}
		else if (orientation == ElementOrientation.Grid)
		{
			int xOffset = _slot + 1;
			float numRows = Mathf.CeilToInt ((float) xOffset / gridWidth) - 1;
			while (xOffset > gridWidth)
			{
				xOffset -= gridWidth;
			}
			xOffset -= 1;

			positionRect.x += slotSize.x / 100f * AdvGame.GetMainGameViewSize ().x * (float) xOffset;
			positionRect.y += slotSize.y / 100f * AdvGame.GetMainGameViewSize ().y * numRows;
		}
		
		return (positionRect);
	}
	
	
	public virtual void RecalculateSize ()
	{
		if (sizeType == AC_SizeType.Automatic)
		{
			AutoSize ();
		}
		
		if (orientation == ElementOrientation.Horizontal)
		{
			relativeRect.width = slotSize.x / 100f * AdvGame.GetMainGameViewSize ().x * numSlots;
			relativeRect.height = slotSize.y / 100f * AdvGame.GetMainGameViewSize ().y;
		}
		else if (orientation == ElementOrientation.Vertical)
		{
			relativeRect.width = slotSize.x / 100f * AdvGame.GetMainGameViewSize ().x;
			relativeRect.height = slotSize.y / 100f * AdvGame.GetMainGameViewSize ().y * numSlots;
		}
		else if (orientation == ElementOrientation.Grid)
		{
			if (numSlots < gridWidth)
			{
				relativeRect.width = slotSize.x / 100f * AdvGame.GetMainGameViewSize ().x * numSlots;
				relativeRect.height = slotSize.y / 100f * AdvGame.GetMainGameViewSize ().y;
			}
			else
			{
				float numRows = Mathf.CeilToInt ((float) numSlots / gridWidth);

				relativeRect.width = slotSize.x / 100f * AdvGame.GetMainGameViewSize ().x * gridWidth;
				relativeRect.height = slotSize.y / 100f * AdvGame.GetMainGameViewSize ().y * numRows;
			}
		}
	}
	
	
	protected void AutoSize (GUIContent content)
	{
		GUIStyle normalStyle = new GUIStyle();
		normalStyle.font = font;
		normalStyle.fontSize = (int) (AdvGame.GetMainGameViewSize ().x * fontScaleFactor / 100);
	
		Vector2 size = GetSize ();
		size = normalStyle.CalcSize (content);
		
		SetAbsoluteSize (size);
	}
	
	
	protected virtual void AutoSize ()
	{
		GUIContent content = new GUIContent (backgroundTexture);
		AutoSize (content);
	}
	
	
	public void SetPosition (Vector2 _position)
	{
		relativeRect.x = _position.x;
		relativeRect.y = _position.y;
	}


	public void SetRelativePosition (Vector2 _size)
	{
		relativeRect.x = relativePosition.x * _size.x;
		relativeRect.y = relativePosition.y * _size.y;
	}
	
	
	public void AutoSetVisibility ()
	{
		if (numSlots == 0)
		{
			isVisible = false;
		}
		else
		{
			isVisible = true;
		}
	}

}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Menu/Menu classes/MenuElement.cs

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