PageRenderTime 82ms CodeModel.GetById 53ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Scenes/Tests/Tutorials.cs

#
C# | 538 lines | 348 code | 43 blank | 147 comment | 6 complexity | 926655880f68a2bb93330114da31cd55 MD5 | raw file
  1using System;
  2using System.IO;
  3using Delta.Engine;
  4using Delta.InputSystem;
  5using Delta.Rendering.Basics.Fonts;
  6using Delta.Rendering.Basics.Materials;
  7using Delta.Scenes.Enums;
  8using Delta.Scenes.Tests.SimpleMainMenu;
  9using Delta.Scenes.UserInterfaces;
 10using Delta.Scenes.UserInterfaces.Controls;
 11using Delta.Scenes.UserInterfaces.Designs;
 12using Delta.Utilities.Datatypes;
 13using Delta.Utilities.Helpers;
 14using NUnit.Framework;
 15using Delta.Utilities.Datatypes.Advanced;
 16
 17namespace Delta.Scenes.Tests
 18{
 19	/// <summary>
 20	/// Tutorials for Scenes and UI controls
 21	/// </summary>
 22	public class Tutorials
 23	{
 24		#region Constants
 25		/// <summary>
 26		/// The name of the scene we will save in the 'SaveContentScene' test.
 27		/// </summary>
 28		private const string ContentSceneName = "TutorialScene";
 29		#endregion
 30
 31		#region TestSimpleMainMenu (Static)
 32		/// <summary>
 33		/// Tutorial: Scenes 1: Create Simple Main Menu
 34		/// Difficulty: Hard
 35		/// Description: Shows how to create a screen which contains several
 36		/// UI controls (a slider, a button, a text box, a label, etc.) to imitate
 37		/// a simple UI screen.
 38		/// Image: http://DeltaEngine.net/Icons/ScenesTutorial1.png
 39		/// </summary>
 40		[Test, Category("Visual")]
 41		public static void TestSimpleMainMenu()
 42		{
 43			// Create Main Menu Screen and open it, which automatically creates a 
 44			// new scene.
 45			new MainMenuScreen().Open();
 46			// And also start the application itself to show all this to the user.
 47			Application.Start();
 48		}
 49		#endregion
 50
 51		#region CreateBackgroundScreen (Static)
 52		/// <summary>
 53		/// Tutorial: Scenes 2: Create Background Screen
 54		/// Difficulty: Easy
 55		/// Description: Shows how to create a simple screen that displays an image
 56		/// in the background.
 57		/// Image: http://DeltaEngine.net/Icons/ScenesTutorial2.png
 58		/// </summary>
 59		[Test, Category("Visual")]
 60		public static void CreateBackgroundScreen()
 61		{
 62			// Create a screen
 63			Screen backgroundScreen = new Screen("<SampleBackgroundScreen>")
 64			{
 65				// and set the background we want to see
 66				Background = new Material2DColored("SpaceBackground"),
 67			};
 68
 69			// Now open it (with starting a new scene)
 70			backgroundScreen.Open();
 71			// and start the engine
 72			Application.Start();
 73		}
 74		#endregion
 75
 76		#region CreateScreenWithButton (Static)
 77		/// <summary>
 78		/// Tutorial: Scenes 3: Create Screen With Button
 79		/// Difficulty: Easy
 80		/// Description: Shows how to create a simple screen that contains a single
 81		/// UI button.
 82		/// Image: http://DeltaEngine.net/Icons/ScenesTutorial3.png
 83		/// </summary>
 84		[Test, Category("Visual")]
 85		public static void CreateScreenWithButton()
 86		{
 87			// Create a screen
 88			Screen buttonScreen = new Screen("<SampleButtonScreen>")
 89			{
 90				// and set the background we want to see
 91				Background = new Material2DColored(Color.DarkGrey),
 92			};
 93
 94			// Next create now our button
 95			Button exitButton = new Button
 96			{
 97				// centered around the middle of the screen (in Quadratic Space)
 98				LocalArea = Rectangle.FromCenter(0.5f, 0.5f, 0.2f, 0.05f),
 99				// and some text
100				Text = "Exit",
101			};
102			// Also tell the button what should be done if it get clicked
103			exitButton.Clicked += delegate(BaseControl sender, CommandTrigger input)
104			{
105				// Notify the application that we want to stop it
106				Application.Quit();
107				// and "mark" this (input) event as handled (like always)
108				input.IsHandled = true;
109			};
110
111			// Last we still need to add to the our UI screen
112			buttonScreen.Add(exitButton);
113
114			// before we can see it finally in our application
115			buttonScreen.Open();
116			Application.Start(delegate
117			{
118				Font.Default.Draw(
119					"Just click on the 'Exit' button to close the screen inclusive " +
120					"the application.",
121					new Rectangle(0.0f, 0.3f, 1.0f, 0.05f));
122			});
123		}
124		#endregion
125
126		#region CreateSimpleUI (Static)
127		/// <summary>
128		/// Tutorial: Scenes 4: Create Simple UI
129		/// Difficulty: Medium
130		/// Description: Shows how to create a screen which contains several
131		/// UI controls (a slider, a button, a text box, a label, etc.) to imitate
132		/// a simple UI screen.
133		/// Image: http://DeltaEngine.net/Icons/ScenesTutorial4.png
134		/// </summary>
135		[Test, Category("Visual")]
136		public static void CreateSimpleUI()
137		{
138			// Create a screen for all wished UI controls
139			Screen uiScreen = new Screen("<SampleUIScreen>")
140			{
141				Background = new Material2DColored(Color.DarkGrey),
142			};
143
144			// Let's unify the sizes of all controls
145			float width = 0.45f;
146			float height = 0.05f;
147
148			// Create a slider at the top
149			Slider slider = new Slider
150			{
151				LocalArea = Rectangle.FromCenter(0.5f, 0.35f, width, height),
152				MinValue = -1.0f,
153				MaxValue = 1.0f,
154				Value = 0.0f,
155			};
156			uiScreen.Add(slider);
157
158			// a text box for input in the upper middle
159			TextBox textBox = new TextBox
160			{
161				LocalArea = Rectangle.FromCenter(0.5f, 0.45f, width, height),
162				Text = "Click in to edit and press 'Enter' to confirm",
163			};
164			uiScreen.Add(textBox);
165
166			// a label in the lower middle to inform about control changes
167			Label infoLabel = new Label
168			{
169				LocalArea = Rectangle.FromCenter(0.5f, 0.55f, width, height),
170				Text = "<Still no control used>",
171			};
172			uiScreen.Add(infoLabel);
173
174			// and a button to exit this tutorial
175			Button exitButton = new Button
176			{
177				LocalArea = Rectangle.FromCenter(0.5f, 0.65f, width, height),
178				Text = "Exit",
179			};
180			exitButton.Clicked += delegate(BaseControl sender, CommandTrigger input)
181			{
182				Application.Quit();
183				input.IsHandled = true;
184			};
185			uiScreen.Add(exitButton);
186
187			// Still link up the controls by its event
188			slider.ValueChanged += delegate(Slider sender)
189			{
190				Slider changedSlider = sender;
191				if (changedSlider != null)
192				{
193					infoLabel.Text = "Slider value changed to '" +
194					                 changedSlider.Value.ToString("0.0000f") + "'";
195				} // if
196			};
197			textBox.TextChanged += delegate(Label sender)
198			{
199				TextBox changedTextBox = sender as TextBox;
200				if (changedTextBox != null)
201				{
202					infoLabel.Text = "TextBox value changed to '" + changedTextBox.Text +
203					                 "'";
204				} // if
205			};
206
207			// Finally open the screen, which automatically creates a new scene.
208			uiScreen.Open();
209			// And also start the application itself to show all this to the user.
210			Application.Start();
211		}
212		#endregion
213
214		#region UsingAnOwnUITheme (Static)
215		/// <summary>
216		/// Tutorial: Scenes 5: Using An Own UI Theme
217		/// Difficulty: Easy
218		/// Description: Shows how to create and set an own theme for the UI
219		/// controls in a scene.
220		/// Image: http://DeltaEngine.net/Icons/ScenesTutorial5.png
221		/// </summary>
222		[Test, Category("Visual")]
223		public static void UsingAnOwnUITheme()
224		{
225			// First create our new theme for our UI controls
226			Theme redTheme = new Theme("<RedTheme>")
227			{
228				#region Button design
229				ButtonDesign = new TextControlDesign
230				{
231					Background = BaseTheme.GetUIMaterial("ButtonBackground"),
232					Hover = BaseTheme.GetUIMaterial("ButtonHover",
233						BaseTheme.DefaultHoverColor),
234					TextFont = new Font(Font.Default, BaseTheme.DefaultTextColor),
235					Pressed = BaseTheme.GetUIMaterial("ButtonPressed",
236						BaseTheme.DefaultHoverColor),
237				},
238				#endregion
239
240				#region Slider design
241				SliderDesign = new SliderDesign
242				{
243					// Finally we normally don't need a background, because we have
244					// the "TrackerbarDesign" instead for it (for the moment)
245					Trackerbar = BaseTheme.GetUIMaterial("SliderBackground"),
246					Tracker = BaseTheme.GetUIMaterial("Slider"),
247					Hover = BaseTheme.GetUIMaterial("SliderHover"),
248					TextFont = new Font(Font.Default, BaseTheme.DefaultTextColor),
249				},
250				#endregion
251
252				#region Checkbox design
253				CheckboxDesign = new CheckboxDesign
254				{
255					TextBackground = BaseTheme.GetUIMaterial("Panel"),
256					UncheckedSymbol = BaseTheme.GetUIMaterial("CheckboxOff"),
257					CheckedSymbol = BaseTheme.GetUIMaterial("CheckboxOn"),
258					UncheckedSymbolHover = BaseTheme.GetUIMaterial("CheckboxOffHover"),
259					CheckedSymbolHover = BaseTheme.GetUIMaterial("CheckboxOnHover"),
260					TextFont = new Font(Font.Default, BaseTheme.DefaultTextColor),
261				},
262				#endregion
263
264				// Add more design types for more controls
265				// ...
266			};
267			// and set (can be done at every time, even after opening the screen)
268			//UserTheme.Current = redTheme;
269
270			// Now create a little UI screen
271			Screen uiScreen = new Screen("<SampleBackgroundScreen>")
272			{
273				Background = new Material2DColored(Color.DarkGrey),
274			};
275
276			// that shows some controls which use our new UI theme
277
278			#region Slider at the top
279			Slider slider = new Slider
280			{
281				LocalArea = Rectangle.FromCenter(0.5f, 0.375f, 0.3f, 0.05f),
282			};
283			uiScreen.Add(slider);
284			#endregion
285
286			#region Checkbox in the middle
287			Checkbox checkbox = new Checkbox
288			{
289				LocalArea = Rectangle.FromCenter(0.5f, 0.475f, 0.3f, 0.05f),
290				Text = "Uncheck me",
291				IsChecked = true,
292			};
293			checkbox.CheckedChanged += delegate(Checkbox sender)
294			{
295				Checkbox changedCheckbox = sender;
296				if (changedCheckbox != null)
297				{
298					changedCheckbox.Text = (changedCheckbox.IsChecked)
299					                       	? "Uncheck me"
300					                       	: "Check me";
301				} // if
302			};
303			uiScreen.Add(checkbox);
304			#endregion
305
306			#region Exit button at the bottom
307			Button exitButton = new Button
308			{
309				LocalArea = Rectangle.FromCenter(0.5f, 0.575f, 0.2f, 0.05f),
310				Text = "Exit",
311			};
312			exitButton.Clicked += delegate(BaseControl sender, CommandTrigger input)
313			{
314				Application.Quit();
315				input.IsHandled = true;
316			};
317			uiScreen.Add(exitButton);
318			#endregion
319
320			// Last open the screen (with starting a new scene) and start the app
321			uiScreen.Open();
322			Application.Start();
323		}
324		#endregion
325
326		#region UsingAnCustomDesign (Static)
327		/// <summary>
328		/// Tutorial: Scenes 6: Using A Custom Design
329		/// Difficulty: Easy
330		/// Description: Shows how to setup a custom design for an UI control (in
331		/// this case an UI button).
332		/// Image: http://DeltaEngine.net/Icons/ScenesTutorial6.png
333		/// </summary>
334		[Test, Category("Visual")]
335		public static void UsingAnCustomDesign()
336		{
337			/*TODO: we need extra content first!
338			// Create a screen
339			Screen buttonScreen = new Screen("<SampleButtonScreen>")
340			{
341				Background = new Material2DColored(Color.DarkGrey),
342			};
343
344			// Next create now our button
345			Button exitButton = new Button
346			{
347				LocalArea = Rectangle.FromCenter(0.5f, 0.5f, 0.2f, 0.1f),
348				Text = "Exit",
349				// and set now a custom design especially for it
350				CustomDesign = new TextControlDesign
351				{
352					// Enabled design
353					Background = BaseTheme.GetUIMaterial("ButtonBackground"),
354					Hover = BaseTheme.GetUIMaterial("ButtonHover",
355						Color.Yellow),
356					TextFont = new Font(Font.Default, Color.White),
357					Pressed = BaseTheme.GetUIMaterial("ButtonPressed",
358						Color.Yellow),
359					// Disabled design
360					DisabledBackground = BaseTheme.GetUIMaterial("ButtonBackground",
361						Color.Grey),
362					DisabledTextFont = new Font(Font.Default, Color.Grey),
363				},
364			};
365			// Also tell the button what should be done if it get clicked
366			exitButton.Clicked += delegate(BaseControl sender, CommandTrigger input)
367			{
368				// Notify the application that we want to stop it
369				Application.Quit();
370				// and "mark" this (input) event as handled (like always)
371				input.IsHandled = true;
372			};
373			buttonScreen.Add(exitButton);
374
375			// Last open the screen (with starting a new scene) and start the app
376			buttonScreen.Open();
377			Application.Start(delegate
378			{
379				Theme.DefaultFont.Draw(
380					"Just click on the 'Exit' button to close the screen inclusive " +
381					"the application.", new Rectangle(0.0f, 0.3f, 1.0f, 0.05f));
382			});
383			 */
384		}
385		#endregion
386
387		#region LoadContentScene (Static)
388		/// <summary>
389		/// Tutorial: Scenes 7: Load Content Scene
390		/// Difficulty: Easy
391		/// Description: Shows how to load a saved scene.
392		/// NOTE: Does not work because content is not 
393		/// available. 
394		/// Image: http://DeltaEngine.net/Icons/ScenesTutorial7.png
395		/// </summary>
396		[Test, Category("Visual")]
397		public static void LoadContentScene()
398		{
399			//sorry, this is not supported yet, there is no Tutorial.DeltaScene yet!
400			//Scene tutorialScene = Scene.Open(ContentSceneName);
401			//Application.Start(tutorialScene);
402		}
403		#endregion
404
405		#region Methods (Private)
406
407		#region SaveContentScene
408		/// <summary>
409		/// Save content scene
410		/// </summary>
411		private static void SaveContentScene()
412		{
413			Screen controlsScreen = new Screen("ControlsScreen")
414			{
415				Background = new Material2DColored("SpaceBackground"),
416			};
417
418			const float yPosOffset = 0.075f;
419			float yPos = 0.275f;
420
421			#region Adding an Image control
422			Image image = new Image
423			{
424				Name = "Image control",
425				LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.075f),
426				CustomDesign = new ControlDesign
427				{
428					Background = BaseTheme.GetUIMaterial("Deltaenginelogo")
429				},
430				HorizontalAlignment = HorizontalAlignment.Left,
431				VerticalAlignment = VerticalAlignment.Top,
432			};
433			controlsScreen.Add(image);
434			yPos += yPosOffset;
435			#endregion
436
437			#region Adding a Label control
438			Label label = new Label
439			{
440				Name = "Label control",
441				LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
442				Text = "Label text",
443				HorizontalAlignment = HorizontalAlignment.Centered,
444				VerticalAlignment = VerticalAlignment.Top,
445			};
446			controlsScreen.Add(label);
447			yPos += yPosOffset;
448			#endregion
449
450			#region Adding a Button control
451			Button button = new Button
452			{
453				Name = "Button control",
454				LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
455				Text = "Button text",
456				HorizontalAlignment = HorizontalAlignment.Right,
457				VerticalAlignment = VerticalAlignment.Top,
458			};
459			controlsScreen.Add(button);
460			yPos += yPosOffset;
461			#endregion
462
463			#region Adding a Slider control
464			Slider slider = new Slider
465			{
466				Name = "Slider control",
467				LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
468				Text = "Slider text",
469				MinValue = 1,
470				MaxValue = 3,
471				Value = 1.5f,
472				HorizontalAlignment = HorizontalAlignment.Left,
473				VerticalAlignment = VerticalAlignment.Centered,
474			};
475			controlsScreen.Add(slider);
476			yPos += yPosOffset;
477			#endregion
478
479			#region Adding a Checkbox control
480			Checkbox checkbox = new Checkbox
481			{
482				Name = "Checkbox control",
483				LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
484				Text = "Checkbox text",
485				IsChecked = true,
486				HorizontalAlignment = HorizontalAlignment.Centered,
487				VerticalAlignment = VerticalAlignment.Centered,
488			};
489			controlsScreen.Add(checkbox);
490			yPos += yPosOffset;
491			#endregion
492
493			#region Adding a RadioButton control
494			RadioButton radioButton = new RadioButton
495			{
496				Name = "RadioButton control",
497				LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
498				Text = "RadioButton text",
499				IsChecked = true,
500				HorizontalAlignment = HorizontalAlignment.Right,
501				VerticalAlignment = VerticalAlignment.Centered,
502			};
503			controlsScreen.Add(radioButton);
504			yPos += yPosOffset;
505			#endregion
506
507			#region Adding a TextBox control
508			TextBox textBox = new TextBox
509			{
510				Name = "TextBox control",
511				LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
512				Text = "TextBox text",
513				HorizontalAlignment = HorizontalAlignment.Centered,
514				VerticalAlignment = VerticalAlignment.Bottom,
515			};
516			controlsScreen.Add(textBox);
517			yPos += yPosOffset;
518			#endregion
519
520			Scene contentScene;
521
522			contentScene = Scene.Open(controlsScreen);
523
524			string savingPath = Path.Combine(Environment.CurrentDirectory,
525				"Content", ContentSceneName + ".SceneData");
526			using (Stream file = FileHelper.Open(savingPath, FileMode.OpenOrCreate,
527				FileAccess.Write, FileShare.Read))
528			{
529				contentScene.Save(new BinaryWriter(file));
530			}
531
532			Application.Start(contentScene);
533		}
534		#endregion
535
536		#endregion
537	}
538}