-
Notifications
You must be signed in to change notification settings - Fork 18
/
Keyboard.pde
224 lines (204 loc) · 5.32 KB
/
Keyboard.pde
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//standard library imports
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
//processing imports
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;
//smt imports
import vialab.SMT.*;
import vialab.SMT.swipekeyboard.*;
//SMT library imports
/**
* Demo program for the new SwipeKeyboard zone
* by Kalev Kalda Sikes
*/
// display properties
int window_width = 1600;
int window_height = 900;
int window_halfWidth;
int window_halfHeight;
final int fps_limit = 60;
// objects
SwipeKeyboard keyboard;
SwipeKeyboard arrows;
// other
PShape arrow_down;
PShape arrow_left;
PShape arrow_right;
PShape arrow_up;
boolean arrow_down_visible;
boolean arrow_left_visible;
boolean arrow_right_visible;
boolean arrow_up_visible;
// main functions
public void setup(){
window_halfWidth = window_width / 2;
window_halfHeight = window_height / 2;
//processing library setup
frameRate( fps_limit);
size( window_width, window_height, P3D);
textMode( SHAPE);
frame.setTitle("Swipe Keyboard Test");
//smt library setup
SMT.init( this, TouchSource.AUTOMATIC);
SMT.setTouchDraw( TouchDraw.TEXTURED);
SMT.setTrailEnabled( true);
//add keyboards
keyboard = new SwipeKeyboard( SwipeKeyboard.condensedLayout);
keyboard.translate( 40, 400);
SMT.add( keyboard);
arrows = new SwipeKeyboard( SwipeKeyboard.arrowKeysLayout);
arrows.translate(
keyboard.width + 100,
400 + keyboard.height - arrows.height);
SMT.add( arrows);
//add text zone
SwipeDisplayer texty = new SwipeDisplayer( "Texty", 300, 100, 1000, 200);
SMT.add( texty);
keyboard.addKeyListener( texty);
keyboard.addSwipeKeyboardListener( texty);
arrows.addKeyListener( texty);
arrows.addSwipeKeyboardListener( texty);
arrow_down = loadShape( "resources/arrow_down.svg");
arrow_left = loadShape( "resources/arrow_left.svg");
arrow_right = loadShape( "resources/arrow_right.svg");
arrow_up = loadShape( "resources/arrow_up.svg");
arrow_down_visible = false;
arrow_left_visible = false;
arrow_right_visible = false;
arrow_up_visible = false;
}
public void draw(){
fill( 222, 222, 222);
//draw background
rect( 0, 0, window_width, window_height);
//draw textures
draw_shapes();
}
public void draw_shapes(){
//setup drawing options
pushStyle();
noFill();
//draw pics
if( arrow_down_visible)
shape( arrow_down, 100f, 0f);
if( arrow_left_visible)
shape( arrow_left, 200f, 0f);
if( arrow_right_visible)
shape( arrow_right, 300f, 0f);
if( arrow_up_visible)
shape( arrow_up, 400f, 0f);
//clean up
popStyle();
}
public void debug_shapes(){
System.out.printf( "arrow_down: %f, %f\n",
arrow_down.getWidth(), arrow_down.getHeight());
System.out.printf( "arrow_left: %f, %f\n",
arrow_left.getWidth(), arrow_left.getHeight());
System.out.printf( "arrow_right: %f, %f\n",
arrow_right.getWidth(), arrow_right.getHeight());
System.out.printf( "arrow_up: %f, %f\n",
arrow_up.getWidth(), arrow_up.getHeight());
}
public void stop(){
super.stop();
}
// subclasses
private class SwipeDisplayer extends Zone
implements SwipeKeyboardListener, KeyListener {
String content;
public SwipeDisplayer( String name, int x, int y, int width, int height){
super( name, x, y , width, height);
content = new String();
}
public void drawImpl(){
pushStyle();
noFill();
strokeWeight( 3);
stroke( 200, 120, 120, 150);
rect( 0, 0, width, height);
drawText( content);
popStyle();
}
public void drawText( String text){
pushStyle();
fill( 255, 255, 255, 255);
textSize( Math.round( dimension.height * 0.6));
textAlign( CENTER);
float halfAscent = textAscent() / 2;
float halfDescent = textDescent() / 2;
text( text,
halfDimension.width,
halfDimension.height + halfAscent - halfDescent);
popStyle();
}
public void touchImpl(){
rst();
}
//swipe events
public void swipeCompleted( SwipeKeyboardEvent event){}
public void swipeStarted( SwipeKeyboardEvent event){}
public void swipeProgressed( SwipeKeyboardEvent event){
content = new String();
boolean shift_down = event.getShiftDown();
for( String suggestion : event.getSuggestions()){
String word = null;
if( shift_down){
word = String.valueOf(
Character.toUpperCase( suggestion.charAt(0)));
if( suggestion.length() > 1)
word += suggestion.substring( 1);
}
else
word = suggestion;
content += String.format( "%s ", word);
}
}
//key events
public void keyPressed( KeyEvent event){
switch( event.getKeyCode()){
case KeyEvent.VK_LEFT:
arrow_left_visible = true;
break;
case KeyEvent.VK_RIGHT:
arrow_right_visible = true;
break;
case KeyEvent.VK_UP:
arrow_up_visible = true;
break;
case KeyEvent.VK_DOWN:
arrow_down_visible = true;
break;
default: break;
}
}
public void keyReleased( KeyEvent event){
switch( event.getKeyCode()){
case KeyEvent.VK_LEFT:
arrow_left_visible = false;
break;
case KeyEvent.VK_RIGHT:
arrow_right_visible = false;
break;
case KeyEvent.VK_UP:
arrow_up_visible = false;
break;
case KeyEvent.VK_DOWN:
arrow_down_visible = false;
break;
default: break;
}
}
public void keyTyped( KeyEvent event){}
}