PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Scenes/Tests/Tutorials.cs

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