abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/KeyboardData.java
blob: c08fe8bf613aece80e42c1dc4b23cc1ba8afa50d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package juloo.keyboard2;

import android.content.res.XmlResourceParser;
import java.util.ArrayList;

class KeyboardData
{
	private ArrayList<Row>	_rows;

	public KeyboardData(XmlResourceParser parser)
	{
		ArrayList<Row> rows = new ArrayList<Row>();

		try
		{
			int status;

			while (parser.next() != XmlResourceParser.START_TAG)
				continue ;
			if (!parser.getName().equals("keyboard"))
				throw new Exception("Unknow tag: " + parser.getName());
			while ((status = parser.next()) != XmlResourceParser.END_DOCUMENT)
			{
				if (status == XmlResourceParser.START_TAG)
				{
					String tag = parser.getName();
					if (tag.equals("row"))
						rows.add(new Row(parser));
					else
						throw new Exception("Unknow keyboard tag: " + tag);
				}
			}
			_rows = rows;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public ArrayList<Row>	getRows()
	{
		return (_rows);
	}

	public class Row extends ArrayList<Key>
	{
		private float		_keysWidth;

		public Row(XmlResourceParser parser) throws Exception
		{
			super();

			int status;
			_keysWidth = 0;
			while ((status = parser.next()) != XmlResourceParser.END_TAG)
			{
				if (status == XmlResourceParser.START_TAG)
				{
					String tag = parser.getName();
					if (tag.equals("key"))
					{
						Key k = new Key(parser);
						_keysWidth += k.width;
						add(k);
					}
					else
						throw new Exception("Unknow row tag: " + tag);
				}
			}
		}

		public float		getWidth(float keyWidth, float keyMargin)
		{
			return (keyWidth * _keysWidth + ((size() - 1) * keyMargin));
		}
	}

	public class Key
	{
		/*
		** 1   2
		**   0
		** 3   4
		*/
		public KeyValue		key0;
		public KeyValue		key1;
		public KeyValue		key2;
		public KeyValue		key3;
		public KeyValue		key4;

		public float		width;

		public Key(XmlResourceParser parser) throws Exception
		{
			downPointer = -1;
			key0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
			key1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
			key2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
			key3 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key3"));
			key4 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key4"));
			try
			{
				width = parser.getAttributeFloatValue(null, "width", 1f);
			}
			catch (Exception e)
			{
				width = 1f;
			}
			while (parser.next() != XmlResourceParser.END_TAG)
				continue ;
		}

		// TODO move it in Keyboard2View
		private static final float	SUB_VALUE_DIST = 5f;

		public int				downPointer;
		public KeyValue			downValue;
		public float			downX;
		public float			downY;

		public KeyValue			getDownValue(float x, float y)
		{
			x -= downX;
			y -= downY;
			if ((Math.abs(x) + Math.abs(y)) < SUB_VALUE_DIST)
				return (key0);
			if (x < 0)
			{
				if (y < 0)
					return ((key1 == null) ? key0 : key1);
				return ((key3 == null) ? key0 : key3);
			}
			else if (y < 0)
				return ((key2 == null) ? key0 : key2);
			return ((key4 == null) ? key0 : key4);
		}
	}
}