Guest User

Untitled

a guest
May 20th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Reflection;
  7.  
  8. using Microsoft.Xna.Framework.Input;
  9.  
  10. using Nuclex.UserInterface.Controls;
  11. using Nuclex.UserInterface.Controls.Desktop;
  12. using Nuclex.UserInterface;
  13.  
  14. using SpeechLib;
  15.  
  16. using System.Xml.Linq;
  17.  
  18. namespace Space_Total_Conflict
  19. {
  20.     public class Dialog
  21.         : WindowControl
  22.     {
  23.         private StringBuilder textToSpeach;
  24.         private String title;
  25.         private UniRectangle rectangle;
  26.  
  27.         private Dictionary<ButtonControl, ButtonParameters> buttons;
  28.  
  29.         public Dialog(String xml)
  30.         {
  31.             buttons = new Dictionary<ButtonControl, ButtonParameters> ();
  32.             XDocument doc = XDocument.Parse(xml);
  33.             XElement tree = doc.Element("dialog");
  34.  
  35.             rectangle = new UniRectangle(
  36.                 new UniScalar(float.Parse(tree.Attribute("fx").Value), float.Parse(tree.Attribute("x").Value)),
  37.                 new UniScalar(float.Parse(tree.Attribute("fy").Value), float.Parse(tree.Attribute("y").Value)),
  38.                 float.Parse(tree.Attribute("width").Value),
  39.                 float.Parse(tree.Attribute("height").Value)
  40.             );
  41.             title = tree.Attribute("title").Value;
  42.  
  43.             this.Bounds = rectangle;
  44.             this.Title = title;
  45.  
  46.             IEnumerable<XElement> elements = tree.Elements("control");
  47.             foreach (XElement element in elements)
  48.             {
  49.                 UniRectangle bounds = new UniRectangle(
  50.                     new UniScalar(float.Parse(element.Attribute("fx").Value), float.Parse(element.Attribute("x").Value)),
  51.                     new UniScalar(float.Parse(element.Attribute("fy").Value), float.Parse(element.Attribute("y").Value)),
  52.                     float.Parse(element.Attribute("width").Value),
  53.                     float.Parse(element.Attribute("height").Value)
  54.                 );
  55.  
  56.                 switch (element.Attribute("type").Value)
  57.                 {
  58.                     case "label":
  59.                         {
  60.                             LabelControl label = new LabelControl
  61.                             {
  62.                                 Bounds = bounds,
  63.                                 Text = element.Element ("text").Value
  64.                             };
  65.  
  66.                             textToSpeach.Append (element.Element("tts").Value);
  67.                             Children.Add(label);
  68.  
  69.                             break;
  70.                         }
  71.                     case "button":
  72.                         {
  73.                             ButtonControl button = new ButtonControl
  74.                             {
  75.                                 Bounds = bounds,
  76.                                 Text = element.Element("text").Value,
  77.                             };
  78.  
  79.                             button.Pressed += delegate(object sender, EventArgs arguments)
  80.                             {
  81.                                 ButtonParameters buttonParameters = buttons[(ButtonControl)sender];
  82.  
  83.                                 Assembly MyAssembly = Assembly.Load("Space_Total_Conflict");
  84.                                 Type MyType = MyAssembly.GetType("Space_Total_Conflict." + buttonParameters.ControlObject);
  85.  
  86.                                 try
  87.                                 {
  88.                                     MyType.InvokeMember (buttonParameters.Method,
  89.                                         BindingFlags.Default | BindingFlags.InvokeMethod,
  90.                                         null, null, new object[] { }
  91.                                     );
  92.                                 }
  93.                                 catch (MissingMethodException e)
  94.                                 {
  95.                                     // You shouldn't ignore this exception, it should crash if there's something wrong so you can catch it early
  96.                                     if (Debugger.IsAttached)
  97.                                         throw e;
  98.                                 }
  99.                             };
  100.  
  101.                             buttons.Add (button, new ButtonParameters (element.Element ("object").Value, element.Element ("method").Value));
  102.                             Children.Add(button);
  103.  
  104.                             break;
  105.                         }
  106.                     case "input":
  107.                         {
  108.                             InputControl input = new InputControl
  109.                             {
  110.                                 Bounds = bounds
  111.                             };
  112.  
  113.                             Children.Add(input);
  114.  
  115.                             break;
  116.                         }
  117.                 }
  118.             }
  119.         }
  120.  
  121.         private void okClicked(object sender, EventArgs arguments)
  122.         {
  123.             Close();
  124.         }
  125.  
  126.         private void cancelClicked(object sender, EventArgs arguments)
  127.         {
  128.             Close();
  129.         }
  130.     }
  131.  
  132.     public class ButtonParameters
  133.     {
  134.         public ButtonParameters (string controlObject, string method)
  135.         {
  136.             this.ControlObject = controlObject;
  137.             this.Method = method;
  138.         }
  139.  
  140.         public string ControlObject
  141.         {
  142.             get;
  143.             private set;
  144.         }
  145.  
  146.         public string Method
  147.         {
  148.             get;
  149.             private set;
  150.         }
  151.     }
  152. }
Add Comment
Please, Sign In to add comment