-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputing_Letters.pde
561 lines (518 loc) · 14.7 KB
/
Inputing_Letters.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//Returning letters - the word processing aspect
char inputKey = 'a';
String document = "";
boolean preventRepeat = true;
int inputHeight = ((4*inputCanvasHeight)/7);
int inputHeightBelowLine = ((3*inputCanvasHeight)/7);
int xPosition = 10; //Starting x point
int yPosition = inputHeight + 50; //Starting y point
int resetX = 100; //For starting new lines
int leftMargin = resetX - 20; //For margin line
char nextLetter;
int lineNumber = 1;
boolean settingUp = true;
boolean print = true;
boolean donePrinting = false;
int wordLength;
int stringLength;
int startPreviousWord;
String previousWord;
int footerHeight = 20;
boolean showExample = true;
boolean quotationOrientation = false; //Start with open quote
//UI
PFont UIFont;
color backgroundColor = color(240, 231, 232); //Background color of output sheet
//Font size
float fontSize = 12;
float fontSizeMultiple = 1.0/18.0;
float scale = 1;
//Adjustable
int spaceBetweenLetters = 10;
int spaceValue = 20;
int defaultPeriod = 5;
void returnLetters()
{
if (doneCreating)
{
firstTimeSetup(); //Setup canvas
userInterface(); //UI
typeLetters();
printShapes();
}
}
void firstTimeSetup()
{
if (settingUp)
{
//Add functions that should only trigger once
background(backgroundColor); //Make background gray ish
referenceLines();
println("About to add buttons and sliders"); //Debugging
addButtonsAndSliders();
displayAllLetters(); //For feedback
settingUp = false;
}
}
void referenceLines()
{
strokeWeight(2); //Set stroke weight
stroke(106, 150, 208); //Set stroke color to blue
float multiplyer = fontSize * fontSizeMultiple;
for (int i = inputHeight + 50; i < height - footerHeight - 10; i = i + int ( (multiplyer * inputHeight)))
{
line(10, i, width - 10, i); //Draw lines
}
stroke(255, 102, 102); //Light red
line (leftMargin, inputHeight, leftMargin, height - footerHeight); //Left margin line
}
void userInterface()
{
fontValue();
documentStats();
}
void typeLetters() //Get string document
{
if (keyPressed & !typing)
{
inputKey = key;
specialKeys();
regularKeys();
println(document); //Debugging
}
}
void regularKeys()
{
if (Character.isLetterOrDigit(inputKey))
{
if (!showExample)
{
typing = true;
document = document + key;
} else //if showExample is true
{
background(backgroundColor);
referenceLines(); //Refresh screen
document = "";
showExample = false;
}
}
}
void specialKeys()
{
backspaceKey();
returnKey();
spacebar();
periodKey();
exclamationKey();
colonKey();
commaKey();
hyphenKey();
quotationKey();
leftParenthesisKey();
rightParenthesisKey();
apostropheKey();
}
void printShapes() //Reading and interpreting string
{
xPosition = resetX; //Reset x
stringLength = 0; //Reset string length
wordLength = 0; //Reset word length
// startPreviousWord = 0; //Reset start of previous word
lineNumber = 1; //Reset line
for (int i = 0; i < document.length (); i++)
{
nextLetter = document.charAt(i);
readSpecialChar();
for (int j = 0; j < 52; j++)
{
if (nextLetter == letters[j])
{
setScale(j); //Set font size
float deltaX = xPosition - (scale * letterSmallestX[j]);
float deltaY = yPosition - (scale * letterLargestY[j]);
int x = 0/* xPosition - letterSmallestX[j] */; //Have very most left point at position
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight))/* + (yPosition - letterLargestY[j])*/; //Take into account which line and makes all the smallest Y values on the same line
shape(letterShapes[j], deltaX, y + deltaY); //Add pshape
xPosition = xPosition + int(scale * letterWidth[j]) + int(scale * spaceBetweenLetters); //Move to new position
wordLength = wordLength + int(scale * letterWidth[j]) + int(scale * spaceBetweenLetters); //Increase width of word
resetScale(j); //Reset font size
}
}
}
wrapText();
}
void readSpecialChar()
{
readSpace();
readReturn();
readPeriod();
readExclamation();
readColon();
readComma();
readHyphen();
readQuotation();
readLeftParenthesis();
readRightParenthesis();
readApostrophe();
}
void backspaceKey() //backspace
{
if (inputKey == BACKSPACE & !preventRepeat)
{
if (document.length() > 0)
{
document = document.substring(0, document.length() - 1); //Delete last character
background(backgroundColor); //Clear/refresh screen
referenceLines(); //Add lines again
checkKeyPress();
}
}
}
void readSpace()
{
if (nextLetter == ' ')
{
xPosition = xPosition + spaceValue;
stringLength = stringLength + wordLength + int((fontSize * fontSizeMultiple) * spaceValue); //New total string length
wordLength = 0; //Reset word length
}
}
void spacebar() //spacebar
{
if (inputKey == ' ' & !preventRepeat)
{
document = document + " "; //Add space
String[] list = split(document, " "); //Split at spaces
println("There are " + (list.length-1) + " words"); //Feedback, subtract one to compensate and make correct
startPreviousWord = 0;
for (int i = 0; i < list.length-2; i++)
{
startPreviousWord = startPreviousWord + list[i].length() + 1;
}
println("Start of previous word: " + startPreviousWord); //Debuggging
checkKeyPress();
}
}
void readReturn() //interpret return key
{
if (nextLetter == '~')
{
lineNumber = lineNumber + 1; //New line
xPosition = resetX; //Reset x value
stringLength = 0; //Reset string length
}
}
void returnKey() //return key
{
if (inputKey == RETURN || inputKey == ENTER)
{
if (!preventRepeat)
{
document = document + "~"; //Add ~ to indicate space
println("NEW LINE"); //Feedback
checkKeyPress();
}
}
}
void readPeriod()
{
if (nextLetter == '.')
{
float size = ((fontSize * fontSizeMultiple) * defaultPeriod);
int x = xPosition;
// println(lineNumber-1); //Debugging
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight)) + yPosition - int(size);
fill(0);
stroke(0);
strokeWeight(1);
ellipse(x, y, size, size); //Draw period
xPosition = xPosition + int(size) + spaceBetweenLetters; //Move xPosition
}
}
void periodKey()
{
if (inputKey == '.' & !preventRepeat)
{
document = document + ".";
checkKeyPress();
}
}
void readExclamation()
{
if (nextLetter == '!')
{
float size = ((fontSize * fontSizeMultiple) * defaultPeriod);
int x = xPosition;
// println(lineNumber-1); //Debugging
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight)) + yPosition - int(size);
fill(0);
stroke(0);
strokeWeight(1);
ellipse(x, y, size, size); //Draw period
int heightOfExclamation = int((fontSize * fontSizeMultiple) * (inputHeight-20));
int yVertex = y - heightOfExclamation;
triangle(x-(size/2), yVertex, x + (size/2), yVertex, x, y - 5); //Draw top part of exclamation point
xPosition = xPosition + int(size) + spaceBetweenLetters; //Move xPosition
}
}
void exclamationKey()
{
if (inputKey == '!' & !preventRepeat)
{
document = document + "!";
checkKeyPress();
}
}
void readColon()
{
if (nextLetter == ':')
{
float size = ((fontSize * fontSizeMultiple) * defaultPeriod);
int x = xPosition;
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight)) + yPosition - int((fontSize * fontSizeMultiple) * (inputHeight-25));
int spacing = int((fontSize * fontSizeMultiple) * (inputHeight-35));
fill(0);
stroke(0);
strokeWeight(1);
ellipse(x, y, size, size); //Draw higher dot
ellipse(x, y + spacing, size, size); //Draw lower dot
xPosition = xPosition + int(size) + spaceBetweenLetters; //Move xPosition
}
}
void colonKey()
{
if (inputKey == ':' & !preventRepeat)
{
document = document + ":";
checkKeyPress();
}
}
void readComma()
{
if (nextLetter == ',')
{
float size = ((fontSize * fontSizeMultiple) * defaultPeriod);
int x = xPosition;
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight)) + yPosition - int(size);
fill(0);
stroke(0);
strokeWeight(1);
ellipse(x, y, size, size); //Draw dot
arc(x, y + (size/2), size, 3 * size, -3 * PI/4, PI/2); //Draw arc below dot
xPosition = xPosition + int(size) + spaceBetweenLetters; //Move xPosition
}
}
void commaKey()
{
if (inputKey == ',' & !preventRepeat)
{
document = document + ",";
checkKeyPress();
}
}
void readHyphen()
{
if (nextLetter == '-')
{
float size = ((fontSize * fontSizeMultiple) * defaultPeriod);
int x = xPosition;
// println((int((fontSize * fontSizeMultiple) * inputHeight)/2));
// println(yPosition);
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight)) + yPosition - (int((fontSize * fontSizeMultiple) * inputHeight)/3);
line(x, y, x + (int((fontSize * fontSizeMultiple) * inputHeight)/10), y); //Draw line for hyphen
xPosition = xPosition + (int((fontSize * fontSizeMultiple) * inputHeight)/10) + spaceBetweenLetters; //Move curser over
}
}
void hyphenKey()
{
if (inputKey == '-' & !preventRepeat)
{
document = document + "-";
checkKeyPress();
}
}
void readQuotation()
{
if (nextLetter == '"')
{
float size = ((fontSize * fontSizeMultiple) * defaultPeriod);
int x = xPosition;
int xSpacing = int((fontSize * fontSizeMultiple) * 5);
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight)) + yPosition- int((fontSize * fontSizeMultiple) * (inputHeight-20));
if (quotationOrientation = true) //If close quote
{
arc(x, y, 2 * size, 5 * size, -PI/3, -PI/6); //Left quote
arc(x + xSpacing, y, 2 * size, 5 * size, -PI/3, -PI/6); //Right quote
quotationOrientation = false;
} else //Open quote
{
arc(x, y, 2 * size, 5 * size, -PI/3, -PI/6); //Left quote
arc(x + xSpacing, y, 2 * size, 5 * size, -2*PI/3, -5 * PI/6); //Right quote
quotationOrientation = true;
}
xPosition = xPosition + xSpacing + int(4 * size); //Move curser
}
}
void quotationKey()
{
if (inputKey == '"' & !preventRepeat)
{
document = document + '"';
checkKeyPress();
}
}
void readApostrophe()
{
String apostrophe = "' ";
if (nextLetter == apostrophe.charAt(0))
{
float size = ((fontSize * fontSizeMultiple) * defaultPeriod);
int x = xPosition;
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight)) + yPosition- int((fontSize * fontSizeMultiple) * (inputHeight-20));
arc(x, y, 2 * size, 5 * size, -PI/3, -PI/6); //Left quote
xPosition = xPosition + int(4 * size); //Move position
}
}
void apostropheKey()
{
String apostrophe = "' ";
if (inputKey == apostrophe.charAt(0) & !preventRepeat)
{
document = document + "'";
checkKeyPress();
}
}
void readLeftParenthesis()
{
if (nextLetter == '(')
{
int x = xPosition;
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight)) + yPosition - (int((fontSize * fontSizeMultiple) * inputHeight)/3);
float xSize = 2 * ((fontSize * fontSizeMultiple) * defaultPeriod);
float ySize = int(((fontSize * fontSizeMultiple) * 3 *(inputHeight-10))/4);
noFill();
strokeWeight(2);
arc(x, y - 2, xSize, ySize, HALF_PI, 3 * HALF_PI);
xPosition = xPosition + int(xSize) + 10; //Move curser over
strokeWeight(1);
}
}
void leftParenthesisKey()
{
if (inputKey == '(' & !preventRepeat)
{
document = document + "(";
checkKeyPress();
}
}
void readRightParenthesis()
{
if (nextLetter == ')')
{
int x = xPosition;
int y = ((lineNumber-1) * int((fontSize * fontSizeMultiple) * inputHeight)) + yPosition - (int((fontSize * fontSizeMultiple) * inputHeight)/3);
float xSize = 2 * ((fontSize * fontSizeMultiple) * defaultPeriod);
float ySize = int(((fontSize * fontSizeMultiple) * 3 * (inputHeight-10))/4);
noFill();
strokeWeight(2);
arc(x, y - 2, xSize, ySize, -HALF_PI, HALF_PI);
xPosition = xPosition + int(xSize) + 10; //Move curser over
strokeWeight(1);
}
}
void rightParenthesisKey()
{
if (inputKey == ')' & !preventRepeat)
{
document = document + ")";
checkKeyPress();
}
}
void checkKeyPress()
{
if (keyPressed)
{
preventRepeat = true;
}
}
void wrapText()
{
if (stringLength > width)
{
if (document.charAt(document.length()-1) != '~')
{
// println("Document length exceded line"); //Feedback
// println(stringLength); //Debugging
previousWord = document.substring(startPreviousWord, document.length()); //Find previous word
document = document.substring(0, startPreviousWord) + "~" + previousWord; //Add line break after last complete word
println(document); //Feedback
background(backgroundColor); //Clear/refresh screen
referenceLines(); //Add lines again
stringLength = 0; //Reset
wordLength = 0; //Reset
startPreviousWord = 0; //Reset
}
}
}
void setScale(int i)
{
scale = fontSize * fontSizeMultiple;
letterShapes[i].scale(scale); //Scale to new font size
}
void resetScale(int i)
{
scale = 1/scale;
letterShapes[i].scale(scale); //Scale back to original
}
void fontValue()
{
fill(255); //White fill
strokeWeight(2); //Stroke
stroke(0); //Set color of stroke: black
textFont(UIFont); //Set font
rect(60 + (8*((width-20)/40)), 10, ((width-20)/40), ((width-20)/40)); //Make rectangle
fill(0);
text(int(fontSize), 63 + (8*((width-20)/40)), 5+((width-20)/40)); //Display text
}
void documentStats()
{
fill(255); //White fill
strokeWeight(2); //Stroke
stroke(0); //Set color of stroke: black
textFont(UIFont); //Set font
rect(10, height - footerHeight - 5, width - 20, footerHeight); //Make rectangle
fill(0);
String[] list = split(document, " "); //Split at spaces
String[] linesList = split(document, "~"); //Split at line breaks
int wordCount;
int letterCountNoSpaces;
if (list.length < 0)
{
wordCount = 0;
} else
{
wordCount = list.length;
wordCount = wordCount + linesList.length - 1;
}
if (document.length() - wordCount < 0)
{
letterCountNoSpaces = 0;
} else
{
letterCountNoSpaces = document.length() - wordCount;
}
int letterCountSpaces = document.length();
int numOfLines = linesList.length;
int spaceBetweenStats = width/4;
text("Letters (with spaces): " + letterCountSpaces, 20, height - 10);
text("Letters (no spaces): " + letterCountNoSpaces, 20 + spaceBetweenStats, height - 10);
text("Words: " + wordCount, 20 + (2 * spaceBetweenStats), height - 10);
text("Lines: " + numOfLines, 20 + (3 * spaceBetweenStats), height - 10);
}
void displayAllLetters()
{
document = "abcdefghijklmnopqrstuvwxyz~ABCDEFGHIJKLMNOPQRSTUVWXYZ";
println("Press any key to begin typing"); //Feedback
showExample = true;
}