public bool AddMultiStroke(MultiStroke multiStroke) { // Create the xml node to add to the xml file XmlElement rootElement = multiStrokeLibrary.DocumentElement; XmlElement multiStrokeNode = multiStrokeLibrary.CreateElement("multistroke"); multiStrokeNode.SetAttribute("name", multiStroke.Name); foreach (MultiStrokePoint m in multiStroke.Points) { XmlElement multiStrokePoint = multiStrokeLibrary.CreateElement("point"); multiStrokePoint.SetAttribute("x", m.Point.x.ToString()); multiStrokePoint.SetAttribute("y", m.Point.y.ToString()); multiStrokePoint.SetAttribute("id", m.StrokeID.ToString()); multiStrokeNode.AppendChild(multiStrokePoint); } // Append the node to xml file contents rootElement.AppendChild(multiStrokeNode); try { // Add the new gesture to the list of gestures this.Library.Add(multiStroke); // Save the file if it is not the web player, because // web player cannot have write permissions. #if !UNITY_WEBPLAYER && !UNITY_EDITOR FileTools.Write(persistentLibraryPath, multiStrokeLibrary.OuterXml); #elif !UNITY_WEBPLAYER && UNITY_EDITOR FileTools.Write(resourcesPath, multiStrokeLibrary.OuterXml); #endif return(true); } catch (Exception e) { Debug.Log(e.Message); return(false); } }
public bool AddMultiStroke(MultiStroke multiStroke) { // Create the xml node to add to the xml file XmlElement rootElement = multiStrokeLibrary.DocumentElement; XmlElement multiStrokeNode = multiStrokeLibrary.CreateElement("multistroke"); multiStrokeNode.SetAttribute("name", multiStroke.Name); foreach (MultiStrokePoint m in multiStroke.Points) { XmlElement multiStrokePoint = multiStrokeLibrary.CreateElement("point"); multiStrokePoint.SetAttribute("x", m.Point.x.ToString()); multiStrokePoint.SetAttribute("y", m.Point.y.ToString()); multiStrokePoint.SetAttribute("id", m.StrokeID.ToString()); multiStrokeNode.AppendChild(multiStrokePoint); } // Append the node to xml file contents rootElement.AppendChild(multiStrokeNode); try { // Add the new gesture to the list of gestures this.Library.Add(multiStroke); // Save the file if it is not the web player, because // web player cannot have write permissions. #if !UNITY_WEBPLAYER && !UNITY_EDITOR FileTools.Write(persistentLibraryPath, multiStrokeLibrary.OuterXml); #elif !UNITY_WEBPLAYER && UNITY_EDITOR FileTools.Write(resourcesPath, multiStrokeLibrary.OuterXml); #endif return true; } catch (Exception e) { Debug.Log(e.Message); return false; } }
public void LoadLibrary() { // Uses the XML file in resources folder if it is webplayer or the editor. string xmlContents = ""; string floatSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; #if !UNITY_WEBPLAYER && !UNITY_EDITOR xmlContents = FileTools.Read(persistentLibraryPath); #else xmlContents = Resources.Load <TextAsset>(libraryName).text; #endif multiStrokeLibrary.LoadXml(xmlContents); // Get "gesture" elements XmlNodeList xmlMultiStrokeList = multiStrokeLibrary.GetElementsByTagName("multistroke"); // Parse "gesture" elements and add them to library foreach (XmlNode xmlMultiStrokeNode in xmlMultiStrokeList) { string multiStrokeName = xmlMultiStrokeNode.Attributes.GetNamedItem("name").Value; XmlNodeList xmlPoints = xmlMultiStrokeNode.ChildNodes; List <MultiStrokePoint> multiStrokePoints = new List <MultiStrokePoint>(); foreach (XmlNode point in xmlPoints) { float x = (float)System.Convert.ToDouble(point.Attributes.GetNamedItem("x").Value.Replace(",", floatSeparator).Replace(".", floatSeparator)); float y = (float)System.Convert.ToDouble(point.Attributes.GetNamedItem("y").Value.Replace(",", floatSeparator).Replace(".", floatSeparator)); int multiStrokeID = (int)System.Convert.ToDouble(point.Attributes.GetNamedItem("id").Value); multiStrokePoints.Add(new MultiStrokePoint(x, y, multiStrokeID)); } MultiStroke multiStroke = new MultiStroke(multiStrokePoints.ToArray(), multiStrokeName); library.Add(multiStroke); } }
/// <summary> /// Add multistroke to the library /// </summary> public void AddMultiStroke() { multiStroke = new MultiStroke(multiStrokePoints.ToArray(), newMultiStrokeName.text); ml.AddMultiStroke(multiStroke); SetMessage(newMultiStrokeName.text + " has been added to the library"); }
/// <summary> /// Recognize drawn gesture /// </summary> void Recognize() { if (multiStrokePoints.Count > minimumPointsToRecognize) { multiStroke = new MultiStroke(multiStrokePoints.ToArray()); result = multiStroke.Recognize(ml); isRecognized = true; SetMessage("MultiStroke is recognized as <color=#ff0000>'" + result.Name + "'</color> with a score of " + result.Score); } }
// Track user input and fire OnRecognition event when necessary. void Update() { // Track user input if GestureRecognition is enabled. if (isEnabled) { // If it is a touch device, get the touch position // if it is not, get the mouse position if (Utility.IsTouchDevice()) { if (Input.touchCount > 0) { virtualKeyPosition = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y); } } else { if (Input.GetMouseButton(0)) { virtualKeyPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y); } } // It is not necessary to track the touch from this point on, // because it is already registered, and GetMouseButton event // also fires on touch devices if (Input.GetMouseButtonDown(0)) { point = Vector2.zero; lastPoint = Vector2.zero; AddStroke(); } // It is not necessary to track the touch from this point on, // because it is already registered, and GetMouseButton event // also fires on touch devices if (Input.GetMouseButton(0)) { switch (gestureLimitType) { case GestureLimitType.None: RegisterPoint(); break; case GestureLimitType.RectBoundsIgnore: if (RectTransformUtility.RectangleContainsScreenPoint(gestureLimitRectBounds, virtualKeyPosition, null)) { RegisterPoint(); } break; case GestureLimitType.RectBoundsClamp: virtualKeyPosition = Utility.ClampPointToRect(virtualKeyPosition, gestureLimitRect); RegisterPoint(); break; } } // Capture the multi stroke, recognize it, fire the recognition event, // and clear the multi stroke from the screen. if (Input.GetMouseButtonDown(1)) { if (multiStrokePoints.Count > minimumPointsToRecognize) { multiStroke = new MultiStroke(multiStrokePoints.ToArray()); result = multiStroke.Recognize(ml); if (OnRecognition != null) { OnRecognition(result); } } ClearGesture(); } } }
// Track user input and fire OnRecognition event when necessary. void Update() { // Track user input if GestureRecognition is enabled. if (isEnabled) { // If it is a touch device, get the touch position // if it is not, get the mouse position if (Utility.IsTouchDevice()) { if (Input.touchCount > 0) { virtualKeyPosition = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y); } } else { if (Input.GetMouseButton(0)) { virtualKeyPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y); } } // It is not necessary to track the touch from this point on, // because it is already registered, and GetMouseButton event // also fires on touch devices if (Input.GetMouseButtonDown(0)) { point = Vector2.zero; lastPoint = Vector2.zero; AddStroke(); } // It is not necessary to track the touch from this point on, // because it is already registered, and GetMouseButton event // also fires on touch devices if (Input.GetMouseButton(0)) { point = new Vector2(virtualKeyPosition.x, -virtualKeyPosition.y); // Register this point only if the point list is empty or current point // is far enough than the last point. This ensures that the multi stroke looks // good on the screen. Moreover, it is good to not overpopulate the screen // with so much points. if (Vector2.Distance(point, lastPoint) > distanceBetweenPoints) { multiStrokePoints.Add(new MultiStrokePoint(point.x, point.y, lastStrokeID)); lastPoint = point; currentStrokeRenderer.SetVertexCount(++vertexCount); currentStrokeRenderer.SetPosition(vertexCount - 1, Utility.WorldCoordinateForGesturePoint(virtualKeyPosition)); } } // Capture the multi stroke, recognize it, fire the recognition event, // and clear the multi stroke from the screen. if (Input.GetMouseButtonDown(1)) { if (multiStrokePoints.Count > minimumPointsToRecognize) { multiStroke = new MultiStroke(multiStrokePoints.ToArray()); result = multiStroke.Recognize(ml); if (OnRecognition != null) { OnRecognition(result); } } ClearGesture(); } } }
public void LoadLibrary() { // Uses the XML file in resources folder if it is webplayer or the editor. string xmlContents = ""; string floatSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; #if !UNITY_WEBPLAYER && !UNITY_EDITOR xmlContents = FileTools.Read(persistentLibraryPath); #else xmlContents = Resources.Load<TextAsset>(libraryName).text; #endif multiStrokeLibrary.LoadXml(xmlContents); // Get "gesture" elements XmlNodeList xmlMultiStrokeList = multiStrokeLibrary.GetElementsByTagName("multistroke"); // Parse "gesture" elements and add them to library foreach (XmlNode xmlMultiStrokeNode in xmlMultiStrokeList) { string multiStrokeName = xmlMultiStrokeNode.Attributes.GetNamedItem("name").Value; XmlNodeList xmlPoints = xmlMultiStrokeNode.ChildNodes; List<MultiStrokePoint> multiStrokePoints = new List<MultiStrokePoint>(); foreach (XmlNode point in xmlPoints) { float x = (float)System.Convert.ToDouble(point.Attributes.GetNamedItem("x").Value.Replace(",", floatSeparator).Replace(".", floatSeparator)); float y = (float)System.Convert.ToDouble(point.Attributes.GetNamedItem("y").Value.Replace(",", floatSeparator).Replace(".", floatSeparator)); int multiStrokeID = (int)System.Convert.ToDouble(point.Attributes.GetNamedItem("id").Value); multiStrokePoints.Add(new MultiStrokePoint(x, y, multiStrokeID)); } MultiStroke multiStroke = new MultiStroke(multiStrokePoints.ToArray(), multiStrokeName); library.Add(multiStroke); } }
void Recognize() { if (multiStrokePoints.Count > minimumPointsToRecognize) { multiStroke = new MultiStroke(multiStrokePoints.ToArray()); result = multiStroke.Recognize(ml); isRecognized = true; message = result.Name + "; " + result.Score; } }
void OnGUI() { GUI.Box(drawArea, "Draw Area\nLeft click and drag to draw, right click or \"Recognize\" to recognize"); GUI.skin.label.fontSize = 20; GUI.Label(new Rect(10, Screen.height - 40, 500, 50), message); GUI.Label(new Rect(Screen.width - 340, 10, 70, 30), "Add as: "); newMultiStrokeName = GUI.TextField(new Rect(Screen.width - 270, 10, 200, 30), newMultiStrokeName); if (GUI.Button(new Rect(Screen.width - 60, 10, 50, 30), "Add")) { multiStroke = new MultiStroke(multiStrokePoints.ToArray(), newMultiStrokeName); ml.AddMultiStroke(multiStroke); } if (GUI.Button(new Rect(Screen.width - 260, 90, 250, 30), "Recognize")) { Recognize(); } if (GUI.Button(new Rect(Screen.width - 260, 50, 250, 30), "Clear")) { ClearGesture(); } }