Skip to content

Commit 68ef373

Browse files
committed
We can register our own gesture
1 parent 5c498f0 commit 68ef373

3 files changed

Lines changed: 78 additions & 14 deletions

File tree

Assets/Scripts/CapturePoints.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using UnityEngine;
2+
using System;
23
using System.Collections;
34
using System.Collections.Generic;
5+
using System.IO;
46

57
using PDollarGestureRecognizer;
68
using PDollarDemo;
@@ -26,16 +28,22 @@ public class CapturePoints : MonoBehaviour {
2628
//GUI
2729
private string message;
2830
private bool recognized;
31+
private string newGestureName = "";
2932

3033
void Start () {
3134

3235
platform = Application.platform;
3336
drawArea = new Rect(0, 0, Screen.width - Screen.width / 3, Screen.height);
3437

38+
//Load pre-made gestures
3539
TextAsset[] gesturesXml = Resources.LoadAll<TextAsset>("GestureSet/10-stylus-MEDIUM/");
36-
3740
foreach (TextAsset gestureXml in gesturesXml)
38-
trainingSet.Add(GestureIO.ReadGesture(gestureXml.text));
41+
trainingSet.Add(GestureIO.ReadGestureFromXML(gestureXml.text));
42+
43+
//Load user custom gestures
44+
string[] filePaths = Directory.GetFiles(Application.persistentDataPath, "*.xml");
45+
foreach (string filePath in filePaths)
46+
trainingSet.Add(GestureIO.ReadGestureFromFile(filePath));
3947
}
4048

4149
void Update () {
@@ -57,6 +65,7 @@ void Update () {
5765
if (recognized) {
5866

5967
recognized = false;
68+
strokeId = -1;
6069

6170
points.Clear();
6271

@@ -100,15 +109,25 @@ void OnGUI() {
100109
recognized = true;
101110

102111
Gesture candidate = new Gesture(points.ToArray());
103-
104112
Result gestureResult = PointCloudRecognizer.Classify(candidate, trainingSet.ToArray());
105113

106114
message = gestureResult.GestureClass + " " + gestureResult.Score;
107-
108-
strokeId = -1;
109115
}
110-
}
111116

117+
GUI.Label(new Rect(Screen.width - 200, 150, 70, 30), "Add as: ");
118+
newGestureName = GUI.TextField(new Rect(Screen.width - 150, 150, 100, 30), newGestureName);
119+
120+
if (GUI.Button(new Rect(Screen.width - 50, 150, 50, 30), "Add") && points.Count > 0 && newGestureName != "") {
121+
122+
string fileName = String.Format("{0}/{1}-{2}.xml", Application.persistentDataPath, newGestureName, DateTime.Now.ToFileTime());
123+
GestureIO.WriteGesture(points.ToArray(), newGestureName, fileName);
124+
125+
trainingSet.Add(new Gesture(points.ToArray(), newGestureName));
126+
127+
newGestureName = "";
128+
}
129+
}
130+
112131
private Vector3 WorldCoordinateForGesturePoint(Vector3 gesturePoint) {
113132

114133
Vector3 worldCoordinate = new Vector3(gesturePoint.x, gesturePoint.y, 10);

Assets/Scripts/PDollarDemo/GestureIO.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,56 @@ public class GestureIO
1414
/// </summary>
1515
/// <param name="xml"></param>
1616
/// <returns></returns>
17-
public static Gesture ReadGesture(string xml)
17+
public static Gesture ReadGestureFromXML(string xml) {
18+
19+
XmlTextReader xmlReader = null;
20+
Gesture gesture = null;
21+
22+
try {
23+
24+
xmlReader = new XmlTextReader(new StringReader(xml));
25+
gesture = ReadGesture(xmlReader);
26+
27+
} finally {
28+
29+
if (xmlReader != null)
30+
xmlReader.Close();
31+
}
32+
33+
return gesture;
34+
}
35+
36+
/// <summary>
37+
/// Reads a multistroke gesture from an XML file
38+
/// </summary>
39+
/// <param name="fileName"></param>
40+
/// <returns></returns>
41+
public static Gesture ReadGestureFromFile(string fileName) {
42+
43+
XmlTextReader xmlReader = null;
44+
Gesture gesture = null;
45+
46+
try {
47+
48+
xmlReader = new XmlTextReader(File.OpenText(fileName));
49+
gesture = ReadGesture(xmlReader);
50+
51+
} finally {
52+
53+
if (xmlReader != null)
54+
xmlReader.Close();
55+
}
56+
57+
return gesture;
58+
}
59+
60+
private static Gesture ReadGesture(XmlTextReader xmlReader)
1861
{
1962
List<Point> points = new List<Point>();
20-
XmlTextReader xmlReader = null;
2163
int currentStrokeIndex = -1;
2264
string gestureName = "";
2365
try
2466
{
25-
xmlReader = new XmlTextReader(new StringReader(xml));
26-
2767
while (xmlReader.Read())
2868
{
2969
if (xmlReader.NodeType != XmlNodeType.Element) continue;

ProjectSettings/ProjectSettings.asset

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ PlayerSettings:
1010
targetGlesGraphics: 1
1111
targetResolution: 0
1212
accelerometerFrequency: 60
13-
companyName: DefaultCompany
13+
companyName: Da Viking Code
1414
productName: PDollar
1515
defaultCursor: {fileID: 0}
1616
cursorHotspot: {x: 0, y: 0}
@@ -64,7 +64,7 @@ PlayerSettings:
6464
16:10: 1
6565
16:9: 1
6666
Others: 1
67-
iPhoneBundleIdentifier: com.Company.ProductName
67+
iPhoneBundleIdentifier: com.davikingcode.PDollar-Unity
6868
metroEnableIndependentInputSource: 0
6969
metroEnableLowLatencyPresentationAPI: 0
7070
productGUID: 5418926cf37a34f559f56e2b1384210e
@@ -100,7 +100,11 @@ PlayerSettings:
100100
AndroidKeystoreName:
101101
AndroidKeyaliasName:
102102
resolutionDialogBanner: {fileID: 0}
103-
m_BuildTargetIcons: []
103+
m_BuildTargetIcons:
104+
- m_BuildTarget:
105+
m_Icons:
106+
- m_Icon: {fileID: 0}
107+
m_Size: 128
104108
m_BuildTargetBatching: []
105109
webPlayerTemplate: APPLICATION:Default
106110
m_TemplateCustomTags: {}
@@ -144,7 +148,8 @@ PlayerSettings:
144148
psp2PackagedResourcesPath:
145149
flashStrippingLevel: 2
146150
spritePackerPolicy:
147-
scriptingDefineSymbols: {}
151+
scriptingDefineSymbols:
152+
2: 1
148153
metroPackageName: PDollar
149154
metroPackageLogo:
150155
metroPackageLogo140:

0 commit comments

Comments
 (0)