1
/* This file is part of DroidPad.
3
* DroidPad is free software: you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation, either version 3 of the License, or
6
* (at your option) any later version.
8
* DroidPad is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with DroidPad. If not, see <http://www.gnu.org/licenses/>.
17
package uk.digitalsquid.droidpad.layout;
19
import java.io.IOException;
20
import java.io.Reader;
21
import java.util.Locale;
23
import org.json.JSONArray;
24
import org.json.JSONException;
25
import org.json.JSONObject;
27
import uk.digitalsquid.droidpad.LogTag;
28
import uk.digitalsquid.droidpad.buttons.Button;
29
import uk.digitalsquid.droidpad.buttons.Item;
30
import uk.digitalsquid.droidpad.buttons.Layout;
31
import uk.digitalsquid.droidpad.buttons.ModeSpec;
32
import uk.digitalsquid.droidpad.buttons.Orientation;
33
import uk.digitalsquid.droidpad.buttons.Slider;
34
import uk.digitalsquid.droidpad.buttons.ToggleButton;
35
import uk.digitalsquid.droidpad.buttons.TouchPanel;
37
public class JsonDecoder implements LogTag {
39
public static final ModeSpec decodeLayout(Reader reader) throws IOException {
40
StringBuffer contents = new StringBuffer();
41
char[] buf = new char[1024];
43
while((read = reader.read(buf)) != -1) {
44
contents.append(buf, 0, read);
47
return decodeLayout(contents.toString());
48
} catch (JSONException e) {
49
IOException e1 = new IOException("Failed to parse JSON string from reader");
55
public static final ModeSpec decodeLayout(String input) throws JSONException {
56
JSONObject root = new JSONObject(input);
57
ModeSpec result = new ModeSpec();
59
result.setMode(getModeId(root.optString("mode")));
61
String title = root.optString("name", "Custom Layout");
62
String description = root.optString("description", "A new custom layout");
63
int gridX = root.optInt("gridX", 4);
64
int gridY = root.optInt("gridY", 5);
65
// Editor size used to work out orientation
66
int editorWidth = root.optInt("width", 720);
67
int editorHeight = root.optInt("height", 405);
68
boolean horizontal = editorWidth > editorHeight;
69
Layout layout = new Layout(title, description, gridX, gridY);
70
layout.setActivityHorizontal(horizontal);
71
result.setLayout(layout);
73
JSONArray items = root.getJSONArray("items");
74
for(int i = 0; i < items.length(); i++) {
75
JSONObject item = items.getJSONObject(i);
76
String type = item.optString("type", "button");
77
boolean gridSnap = item.optBoolean("gridSnap", true);
78
// These values are integers if grid snapping, floats if not
79
float x = (float)item.optDouble("x", 0);
80
float y = (float)item.optDouble("y", 0);
81
float width = (float)item.optDouble("width", 1);
82
float height = (float)item.optDouble("height", 1);
83
int textSize = item.optInt("textSize", 20);
84
String text = item.optString("text");
85
if(text.equals("")) text = String.format(Locale.getDefault(), "%d", i+1);
87
Orientation orientation = Orientation.Both;
88
String orientationType = item.optString("orientation", "both");
89
if(orientationType.equals("both")) orientation = Orientation.Both;
90
if(orientationType.equals("x")) orientation = Orientation.X;
91
if(orientationType.equals("y")) orientation = Orientation.Y;
93
Item component = null;
94
if(type.equals("button")) {
95
component = new Button(x, y, width, height, !gridSnap, text, textSize);
96
} else if(type.equals("toggle")) {
97
component = new ToggleButton(x, y, width, height, !gridSnap, text, textSize);
98
} else if(type.equals("slider")) {
99
component = new Slider(x, y, width, height, !gridSnap, orientation);
100
} else if(type.equals("panel")) {
101
component = new TouchPanel(x, y, width, height, !gridSnap, orientation);
103
if(component != null) layout.add(component);
109
static final int getModeId(String mode) {
110
// TODO: check these against editor
111
if(mode.equalsIgnoreCase("joystick")) return ModeSpec.LAYOUTS_JS;
112
if(mode.equalsIgnoreCase("mouse")) return ModeSpec.LAYOUTS_MOUSE;
113
if(mode.equalsIgnoreCase("touch")) return ModeSpec.LAYOUTS_MOUSE_ABS;
114
if(mode.equalsIgnoreCase("slideshow")) return ModeSpec.LAYOUTS_SLIDE;
115
return ModeSpec.LAYOUTS_JS;