/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
- using System;
- using System.IO;
- using Delta.Engine;
- using Delta.InputSystem;
- using Delta.Rendering.Basics.Fonts;
- using Delta.Rendering.Basics.Materials;
- using Delta.Scenes.Enums;
- using Delta.Scenes.Tests.SimpleMainMenu;
- using Delta.Scenes.UserInterfaces;
- using Delta.Scenes.UserInterfaces.Controls;
- using Delta.Scenes.UserInterfaces.Designs;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
- using NUnit.Framework;
- using Delta.Utilities.Datatypes.Advanced;
-
- namespace Delta.Scenes.Tests
- {
- /// <summary>
- /// Tutorials for Scenes and UI controls
- /// </summary>
- public class Tutorials
- {
- #region Constants
- /// <summary>
- /// The name of the scene we will save in the 'SaveContentScene' test.
- /// </summary>
- private const string ContentSceneName = "TutorialScene";
- #endregion
-
- #region TestSimpleMainMenu (Static)
- /// <summary>
- /// Tutorial: Scenes 1: Create Simple Main Menu
- /// Difficulty: Hard
- /// Description: Shows how to create a screen which contains several
- /// UI controls (a slider, a button, a text box, a label, etc.) to imitate
- /// a simple UI screen.
- /// Image: http://DeltaEngine.net/Icons/ScenesTutorial1.png
- /// </summary>
- [Test, Category("Visual")]
- public static void TestSimpleMainMenu()
- {
- // Create Main Menu Screen and open it, which automatically creates a
- // new scene.
- new MainMenuScreen().Open();
- // And also start the application itself to show all this to the user.
- Application.Start();
- }
- #endregion
-
- #region CreateBackgroundScreen (Static)
- /// <summary>
- /// Tutorial: Scenes 2: Create Background Screen
- /// Difficulty: Easy
- /// Description: Shows how to create a simple screen that displays an image
- /// in the background.
- /// Image: http://DeltaEngine.net/Icons/ScenesTutorial2.png
- /// </summary>
- [Test, Category("Visual")]
- public static void CreateBackgroundScreen()
- {
- // Create a screen
- Screen backgroundScreen = new Screen("<SampleBackgroundScreen>")
- {
- // and set the background we want to see
- Background = new Material2DColored("SpaceBackground"),
- };
-
- // Now open it (with starting a new scene)
- backgroundScreen.Open();
- // and start the engine
- Application.Start();
- }
- #endregion
-
- #region CreateScreenWithButton (Static)
- /// <summary>
- /// Tutorial: Scenes 3: Create Screen With Button
- /// Difficulty: Easy
- /// Description: Shows how to create a simple screen that contains a single
- /// UI button.
- /// Image: http://DeltaEngine.net/Icons/ScenesTutorial3.png
- /// </summary>
- [Test, Category("Visual")]
- public static void CreateScreenWithButton()
- {
- // Create a screen
- Screen buttonScreen = new Screen("<SampleButtonScreen>")
- {
- // and set the background we want to see
- Background = new Material2DColored(Color.DarkGrey),
- };
-
- // Next create now our button
- Button exitButton = new Button
- {
- // centered around the middle of the screen (in Quadratic Space)
- LocalArea = Rectangle.FromCenter(0.5f, 0.5f, 0.2f, 0.05f),
- // and some text
- Text = "Exit",
- };
- // Also tell the button what should be done if it get clicked
- exitButton.Clicked += delegate(BaseControl sender, CommandTrigger input)
- {
- // Notify the application that we want to stop it
- Application.Quit();
- // and "mark" this (input) event as handled (like always)
- input.IsHandled = true;
- };
-
- // Last we still need to add to the our UI screen
- buttonScreen.Add(exitButton);
-
- // before we can see it finally in our application
- buttonScreen.Open();
- Application.Start(delegate
- {
- Font.Default.Draw(
- "Just click on the 'Exit' button to close the screen inclusive " +
- "the application.",
- new Rectangle(0.0f, 0.3f, 1.0f, 0.05f));
- });
- }
- #endregion
-
- #region CreateSimpleUI (Static)
- /// <summary>
- /// Tutorial: Scenes 4: Create Simple UI
- /// Difficulty: Medium
- /// Description: Shows how to create a screen which contains several
- /// UI controls (a slider, a button, a text box, a label, etc.) to imitate
- /// a simple UI screen.
- /// Image: http://DeltaEngine.net/Icons/ScenesTutorial4.png
- /// </summary>
- [Test, Category("Visual")]
- public static void CreateSimpleUI()
- {
- // Create a screen for all wished UI controls
- Screen uiScreen = new Screen("<SampleUIScreen>")
- {
- Background = new Material2DColored(Color.DarkGrey),
- };
-
- // Let's unify the sizes of all controls
- float width = 0.45f;
- float height = 0.05f;
-
- // Create a slider at the top
- Slider slider = new Slider
- {
- LocalArea = Rectangle.FromCenter(0.5f, 0.35f, width, height),
- MinValue = -1.0f,
- MaxValue = 1.0f,
- Value = 0.0f,
- };
- uiScreen.Add(slider);
-
- // a text box for input in the upper middle
- TextBox textBox = new TextBox
- {
- LocalArea = Rectangle.FromCenter(0.5f, 0.45f, width, height),
- Text = "Click in to edit and press 'Enter' to confirm",
- };
- uiScreen.Add(textBox);
-
- // a label in the lower middle to inform about control changes
- Label infoLabel = new Label
- {
- LocalArea = Rectangle.FromCenter(0.5f, 0.55f, width, height),
- Text = "<Still no control used>",
- };
- uiScreen.Add(infoLabel);
-
- // and a button to exit this tutorial
- Button exitButton = new Button
- {
- LocalArea = Rectangle.FromCenter(0.5f, 0.65f, width, height),
- Text = "Exit",
- };
- exitButton.Clicked += delegate(BaseControl sender, CommandTrigger input)
- {
- Application.Quit();
- input.IsHandled = true;
- };
- uiScreen.Add(exitButton);
-
- // Still link up the controls by its event
- slider.ValueChanged += delegate(Slider sender)
- {
- Slider changedSlider = sender;
- if (changedSlider != null)
- {
- infoLabel.Text = "Slider value changed to '" +
- changedSlider.Value.ToString("0.0000f") + "'";
- } // if
- };
- textBox.TextChanged += delegate(Label sender)
- {
- TextBox changedTextBox = sender as TextBox;
- if (changedTextBox != null)
- {
- infoLabel.Text = "TextBox value changed to '" + changedTextBox.Text +
- "'";
- } // if
- };
-
- // Finally open the screen, which automatically creates a new scene.
- uiScreen.Open();
- // And also start the application itself to show all this to the user.
- Application.Start();
- }
- #endregion
-
- #region UsingAnOwnUITheme (Static)
- /// <summary>
- /// Tutorial: Scenes 5: Using An Own UI Theme
- /// Difficulty: Easy
- /// Description: Shows how to create and set an own theme for the UI
- /// controls in a scene.
- /// Image: http://DeltaEngine.net/Icons/ScenesTutorial5.png
- /// </summary>
- [Test, Category("Visual")]
- public static void UsingAnOwnUITheme()
- {
- // First create our new theme for our UI controls
- Theme redTheme = new Theme("<RedTheme>")
- {
- #region Button design
- ButtonDesign = new TextControlDesign
- {
- Background = BaseTheme.GetUIMaterial("ButtonBackground"),
- Hover = BaseTheme.GetUIMaterial("ButtonHover",
- BaseTheme.DefaultHoverColor),
- TextFont = new Font(Font.Default, BaseTheme.DefaultTextColor),
- Pressed = BaseTheme.GetUIMaterial("ButtonPressed",
- BaseTheme.DefaultHoverColor),
- },
- #endregion
-
- #region Slider design
- SliderDesign = new SliderDesign
- {
- // Finally we normally don't need a background, because we have
- // the "TrackerbarDesign" instead for it (for the moment)
- Trackerbar = BaseTheme.GetUIMaterial("SliderBackground"),
- Tracker = BaseTheme.GetUIMaterial("Slider"),
- Hover = BaseTheme.GetUIMaterial("SliderHover"),
- TextFont = new Font(Font.Default, BaseTheme.DefaultTextColor),
- },
- #endregion
-
- #region Checkbox design
- CheckboxDesign = new CheckboxDesign
- {
- TextBackground = BaseTheme.GetUIMaterial("Panel"),
- UncheckedSymbol = BaseTheme.GetUIMaterial("CheckboxOff"),
- CheckedSymbol = BaseTheme.GetUIMaterial("CheckboxOn"),
- UncheckedSymbolHover = BaseTheme.GetUIMaterial("CheckboxOffHover"),
- CheckedSymbolHover = BaseTheme.GetUIMaterial("CheckboxOnHover"),
- TextFont = new Font(Font.Default, BaseTheme.DefaultTextColor),
- },
- #endregion
-
- // Add more design types for more controls
- // ...
- };
- // and set (can be done at every time, even after opening the screen)
- //UserTheme.Current = redTheme;
-
- // Now create a little UI screen
- Screen uiScreen = new Screen("<SampleBackgroundScreen>")
- {
- Background = new Material2DColored(Color.DarkGrey),
- };
-
- // that shows some controls which use our new UI theme
-
- #region Slider at the top
- Slider slider = new Slider
- {
- LocalArea = Rectangle.FromCenter(0.5f, 0.375f, 0.3f, 0.05f),
- };
- uiScreen.Add(slider);
- #endregion
-
- #region Checkbox in the middle
- Checkbox checkbox = new Checkbox
- {
- LocalArea = Rectangle.FromCenter(0.5f, 0.475f, 0.3f, 0.05f),
- Text = "Uncheck me",
- IsChecked = true,
- };
- checkbox.CheckedChanged += delegate(Checkbox sender)
- {
- Checkbox changedCheckbox = sender;
- if (changedCheckbox != null)
- {
- changedCheckbox.Text = (changedCheckbox.IsChecked)
- ? "Uncheck me"
- : "Check me";
- } // if
- };
- uiScreen.Add(checkbox);
- #endregion
-
- #region Exit button at the bottom
- Button exitButton = new Button
- {
- LocalArea = Rectangle.FromCenter(0.5f, 0.575f, 0.2f, 0.05f),
- Text = "Exit",
- };
- exitButton.Clicked += delegate(BaseControl sender, CommandTrigger input)
- {
- Application.Quit();
- input.IsHandled = true;
- };
- uiScreen.Add(exitButton);
- #endregion
-
- // Last open the screen (with starting a new scene) and start the app
- uiScreen.Open();
- Application.Start();
- }
- #endregion
-
- #region UsingAnCustomDesign (Static)
- /// <summary>
- /// Tutorial: Scenes 6: Using A Custom Design
- /// Difficulty: Easy
- /// Description: Shows how to setup a custom design for an UI control (in
- /// this case an UI button).
- /// Image: http://DeltaEngine.net/Icons/ScenesTutorial6.png
- /// </summary>
- [Test, Category("Visual")]
- public static void UsingAnCustomDesign()
- {
- /*TODO: we need extra content first!
- // Create a screen
- Screen buttonScreen = new Screen("<SampleButtonScreen>")
- {
- Background = new Material2DColored(Color.DarkGrey),
- };
-
- // Next create now our button
- Button exitButton = new Button
- {
- LocalArea = Rectangle.FromCenter(0.5f, 0.5f, 0.2f, 0.1f),
- Text = "Exit",
- // and set now a custom design especially for it
- CustomDesign = new TextControlDesign
- {
- // Enabled design
- Background = BaseTheme.GetUIMaterial("ButtonBackground"),
- Hover = BaseTheme.GetUIMaterial("ButtonHover",
- Color.Yellow),
- TextFont = new Font(Font.Default, Color.White),
- Pressed = BaseTheme.GetUIMaterial("ButtonPressed",
- Color.Yellow),
- // Disabled design
- DisabledBackground = BaseTheme.GetUIMaterial("ButtonBackground",
- Color.Grey),
- DisabledTextFont = new Font(Font.Default, Color.Grey),
- },
- };
- // Also tell the button what should be done if it get clicked
- exitButton.Clicked += delegate(BaseControl sender, CommandTrigger input)
- {
- // Notify the application that we want to stop it
- Application.Quit();
- // and "mark" this (input) event as handled (like always)
- input.IsHandled = true;
- };
- buttonScreen.Add(exitButton);
-
- // Last open the screen (with starting a new scene) and start the app
- buttonScreen.Open();
- Application.Start(delegate
- {
- Theme.DefaultFont.Draw(
- "Just click on the 'Exit' button to close the screen inclusive " +
- "the application.", new Rectangle(0.0f, 0.3f, 1.0f, 0.05f));
- });
- */
- }
- #endregion
-
- #region LoadContentScene (Static)
- /// <summary>
- /// Tutorial: Scenes 7: Load Content Scene
- /// Difficulty: Easy
- /// Description: Shows how to load a saved scene.
- /// NOTE: Does not work because content is not
- /// available.
- /// Image: http://DeltaEngine.net/Icons/ScenesTutorial7.png
- /// </summary>
- [Test, Category("Visual")]
- public static void LoadContentScene()
- {
- //sorry, this is not supported yet, there is no Tutorial.DeltaScene yet!
- //Scene tutorialScene = Scene.Open(ContentSceneName);
- //Application.Start(tutorialScene);
- }
- #endregion
-
- #region Methods (Private)
-
- #region SaveContentScene
- /// <summary>
- /// Save content scene
- /// </summary>
- private static void SaveContentScene()
- {
- Screen controlsScreen = new Screen("ControlsScreen")
- {
- Background = new Material2DColored("SpaceBackground"),
- };
-
- const float yPosOffset = 0.075f;
- float yPos = 0.275f;
-
- #region Adding an Image control
- Image image = new Image
- {
- Name = "Image control",
- LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.075f),
- CustomDesign = new ControlDesign
- {
- Background = BaseTheme.GetUIMaterial("Deltaenginelogo")
- },
- HorizontalAlignment = HorizontalAlignment.Left,
- VerticalAlignment = VerticalAlignment.Top,
- };
- controlsScreen.Add(image);
- yPos += yPosOffset;
- #endregion
-
- #region Adding a Label control
- Label label = new Label
- {
- Name = "Label control",
- LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
- Text = "Label text",
- HorizontalAlignment = HorizontalAlignment.Centered,
- VerticalAlignment = VerticalAlignment.Top,
- };
- controlsScreen.Add(label);
- yPos += yPosOffset;
- #endregion
-
- #region Adding a Button control
- Button button = new Button
- {
- Name = "Button control",
- LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
- Text = "Button text",
- HorizontalAlignment = HorizontalAlignment.Right,
- VerticalAlignment = VerticalAlignment.Top,
- };
- controlsScreen.Add(button);
- yPos += yPosOffset;
- #endregion
-
- #region Adding a Slider control
- Slider slider = new Slider
- {
- Name = "Slider control",
- LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
- Text = "Slider text",
- MinValue = 1,
- MaxValue = 3,
- Value = 1.5f,
- HorizontalAlignment = HorizontalAlignment.Left,
- VerticalAlignment = VerticalAlignment.Centered,
- };
- controlsScreen.Add(slider);
- yPos += yPosOffset;
- #endregion
-
- #region Adding a Checkbox control
- Checkbox checkbox = new Checkbox
- {
- Name = "Checkbox control",
- LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
- Text = "Checkbox text",
- IsChecked = true,
- HorizontalAlignment = HorizontalAlignment.Centered,
- VerticalAlignment = VerticalAlignment.Centered,
- };
- controlsScreen.Add(checkbox);
- yPos += yPosOffset;
- #endregion
-
- #region Adding a RadioButton control
- RadioButton radioButton = new RadioButton
- {
- Name = "RadioButton control",
- LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
- Text = "RadioButton text",
- IsChecked = true,
- HorizontalAlignment = HorizontalAlignment.Right,
- VerticalAlignment = VerticalAlignment.Centered,
- };
- controlsScreen.Add(radioButton);
- yPos += yPosOffset;
- #endregion
-
- #region Adding a TextBox control
- TextBox textBox = new TextBox
- {
- Name = "TextBox control",
- LocalArea = Rectangle.FromCenter(0.5f, yPos, 0.2f, 0.05f),
- Text = "TextBox text",
- HorizontalAlignment = HorizontalAlignment.Centered,
- VerticalAlignment = VerticalAlignment.Bottom,
- };
- controlsScreen.Add(textBox);
- yPos += yPosOffset;
- #endregion
-
- Scene contentScene;
-
- contentScene = Scene.Open(controlsScreen);
-
- string savingPath = Path.Combine(Environment.CurrentDirectory,
- "Content", ContentSceneName + ".SceneData");
- using (Stream file = FileHelper.Open(savingPath, FileMode.OpenOrCreate,
- FileAccess.Write, FileShare.Read))
- {
- contentScene.Save(new BinaryWriter(file));
- }
-
- Application.Start(contentScene);
- }
- #endregion
-
- #endregion
- }
- }