/// <summary> /// Read the config file and store everyting in a dictionnary to /// simplify the usage /// </summary> /// <param name="path"></param> public static void ReadConfigFile(string path) { using (FileStream baselayoutStream = new FileStream(Path.Combine(path, BaseLayout), FileMode.Open)) using (FileStream schemaStream = new FileStream(Path.Combine(path, LayoutSchema), FileMode.Open)) using (FileStream layoutStream = new FileStream(Path.Combine(path, LayoutDirectory, layoutName), FileMode.Open)) using (StreamReader schemaReader = new StreamReader(schemaStream)) { XmlDocument baseLayoutDoc = new XmlDocument(); XmlDocument layoutDoc = new XmlDocument(); XmlSchema schema = XmlSchema.Read(schemaStream, null); XmlReaderSettings settings = new XmlReaderSettings(); bool izOK = true; settings.Schemas.Add(schema); settings.ValidationType = ValidationType.Schema; settings.ValidationEventHandler += delegate(object sender, ValidationEventArgs e) { if (e.Severity == XmlSeverityType.Error) { izOK = false; } }; XmlReader baseLayoutReader = XmlReader.Create(baselayoutStream, settings); XmlReader layoutReader = XmlReader.Create(layoutStream, settings); baseLayoutDoc.Load(baseLayoutReader); layoutDoc.Load(layoutReader); if (izOK) { //Source XmlNode baseRoot = baseLayoutDoc["Layout"]; string keyName; KeyboardButtonType buttonType; string positionType; string axisRef = string.Empty; ; int x = -1; int y = -1; foreach (XmlNode node in baseRoot) { if (node.Name == "Key") { keyName = node["Value"].InnerText; buttonType = GetButtonType(node["ButtonType"].InnerText); positionType = node["PositionType"].InnerText; if (positionType == "Relative") { axisRef = node["PositionType"].Attributes[0].Value; } if (node["X"] != null) { int.TryParse(node["X"].InnerText, out x); } if (node["Y"] != null) { int.TryParse(node["Y"].InnerText, out y); } _Innerlist.Add(GetKey(keyName), new Rectangle[2] { GetRectangle(positionType, axisRef, buttonType, x, y), Rectangle.Empty }); lastButtonType = buttonType; } } //Destination baseRoot = layoutDoc["Layout"]; foreach (XmlNode node in baseRoot) { if (node.Name == "Key") { keyName = node["Value"].InnerText; buttonType = GetButtonType(node["ButtonType"].InnerText); positionType = node["PositionType"].InnerText; if (positionType == "Relative") { axisRef = node["PositionType"].Attributes[0].Value; } if (node["X"] != null) { int.TryParse(node["X"].InnerText, out x); } if (node["Y"] != null) { int.TryParse(node["Y"].InnerText, out y); } _Innerlist[GetKey(keyName)][1] = GetRectangle(positionType, axisRef, buttonType, x, y); lastButtonType = buttonType; } } } } }
/// <summary> /// Get a width depending of the specified button type /// </summary> /// <param name="buttonType">The button type</param> /// <returns>Width depending of the type</returns> private static int GetButtonWidth(KeyboardButtonType buttonType) { switch (buttonType) { case KeyboardButtonType.Rectangle123: return 123; case KeyboardButtonType.Rectangle24: return 24; case KeyboardButtonType.Rectangle30: return 30; case KeyboardButtonType.Rectangle36: return 36; case KeyboardButtonType.Rectangle43: return 43; case KeyboardButtonType.Rectangle48: return 48; case KeyboardButtonType.Square: case KeyboardButtonType.VerticalRectangle: default: return 20; } }
/// <summary> /// Build a rectangle using specified parameters and return it /// </summary> /// <param name="positionType">The position type (e.g. relative or absolutute)</param> /// <param name="axisRef">The axis we used for reference when using relative position type</param> /// <param name="buttonType">The type of the button (Square, rectangle, etc...)</param> /// <param name="x">X positoin</param> /// <param name="y">Y position</param> /// <returns>A pretty rectangle ready to use</returns> private static Rectangle GetRectangle(string positionType, string axisRef, KeyboardButtonType buttonType, int x, int y) { Rectangle rect = Rectangle.Empty; rect.Width = GetButtonWidth(buttonType); rect.Height = GetButtonHeight(buttonType); switch (positionType) { case "Absolute": rect.X = x; rect.Y = y; break; case "Relative": if (axisRef == "X") { rect.X = lastX + GetButtonWidth(lastButtonType); rect.Y = lastY; } else { rect.X = x; rect.Y = lastY + GetButtonHeight(lastButtonType); } break; default: throw new IndexOutOfRangeException(string.Format("The specified position type \"{0}\" is not managed")); } lastX = rect.X; lastY = rect.Y; return rect; }
/// <summary> /// Get a height depending of the specified button type /// </summary> /// <param name="buttonType">The button type</param> /// <returns>Heoght depending of the type</returns> private static int GetButtonHeight(KeyboardButtonType buttonType) { if (buttonType == KeyboardButtonType.VerticalRectangle) { return 38; } else { return 19; } }