2
2
3
3
import processing .core .PApplet ;
4
4
import processing .core .PFont ;
5
- import processing .core .PGraphics ;
6
5
7
6
import java .util .regex .Matcher ;
8
7
import java .util .regex .Pattern ;
@@ -14,6 +13,7 @@ public class ScratchText {
14
13
private float x ;
15
14
private float y ;
16
15
private final float width ;
16
+ private boolean fullWidth ;
17
17
private float height ;
18
18
private long lifetime ;
19
19
private boolean hasLifetime ;
@@ -25,31 +25,6 @@ public class ScratchText {
25
25
private boolean show ;
26
26
private int mode ;
27
27
28
- public ScratchText (String text , float x , float y ) {
29
- this (text , x , y , 242 );
30
- this .mode = ScratchText .BOX ;
31
- }
32
-
33
- public ScratchText (String text , float x , float y , float width , int mode ) {
34
- this (text , x , y , width );
35
- this .mode = mode ;
36
- }
37
-
38
- public ScratchText (ScratchText t ) {
39
- this .x = t .x ;
40
- this .y = t .y ;
41
- this .lifetime = t .lifetime ;
42
- this .hasLifetime = t .hasLifetime ;
43
- this .textSize = 16 ;
44
- this .originalText = t .originalText ;
45
- this .width = t .width ;
46
- this .height = t .height ;
47
- this .text = null ;
48
- this .show = false ;
49
- this .mono = t .mono ;
50
- this .mode = t .mode ;
51
- }
52
-
53
28
public ScratchText (String text , float x , float y , float width ) {
54
29
this .x = x ;
55
30
this .y = y ;
@@ -58,7 +33,32 @@ public ScratchText(String text, float x, float y, float width) {
58
33
this .width = width ;
59
34
this .show = false ;
60
35
this .mono = ScratchStage .parent .createFont ("UbuntuMono-Regular.ttf" , this .textSize );
36
+ }
61
37
38
+ public ScratchText (String text , float x , float y , float width , int mode ) {
39
+ this (text , x , y , width );
40
+ this .mode = mode ;
41
+ }
42
+
43
+ public ScratchText (String text , float x , float y , boolean fullWidth , int mode ) {
44
+ this (text , x , y , ScratchStage .parent .width , mode );
45
+ this .fullWidth = fullWidth ;
46
+ }
47
+
48
+ public ScratchText (ScratchText t ) {
49
+ this .fullWidth = t .fullWidth ;
50
+ this .width = t .width ;
51
+ this .originalText = t .originalText ;
52
+ this .show = t .show ;
53
+ this .lifetime = t .lifetime ;
54
+ this .hasLifetime = t .hasLifetime ;
55
+ this .height = t .height ;
56
+ this .mono = t .mono ;
57
+ this .textSize = t .textSize ;
58
+ this .text = t .text ;
59
+ this .x = t .x ;
60
+ this .y = t .y ;
61
+ this .mode = t .mode ;
62
62
}
63
63
64
64
/**
@@ -191,7 +191,7 @@ public static String wrap(String src, int lineLength, String newLineStr, boolean
191
191
// Breakup long word
192
192
while (wrapLongWords && word .length () > lineLength ) {
193
193
cache
194
- .append (word . substring ( 0 , remaining - breakLength ) )
194
+ .append (word , 0 , remaining - breakLength )
195
195
.append (longWordBreak )
196
196
.append (newLineStr );
197
197
word = longWordLinePrefix + word .substring (remaining - breakLength );
@@ -236,37 +236,55 @@ public void draw() {
236
236
237
237
if (this .text == null ) {
238
238
float cw = textBuffer .textWidth ("w" );
239
- int lineLength = Math .round (width / cw );
239
+ int lineLength = Math .round (( this . width - 16 ) / cw );
240
240
this .text = wrap (originalText , lineLength , "\n " , true , "-" , " " );
241
- this .height = 21 * this .text .split ("\n " ).length + 8 ;
242
- }
243
-
244
- textBuffer .translate (this .x , this .y - height );
245
- if (this .mode == ScratchText .BOX ) {
246
- textBuffer .rect (0 , 0 , this .width , this .height , 8 , 8 , 0 , 0 );
247
241
} else {
248
- textBuffer .rect (0 , 0 , this .width , this .height , 8 , 8 , 8 , 8 );
249
- if (this .mode == ScratchText .SPEAK ) {
250
- textBuffer .push ();
251
- textBuffer .fill (255 , 255 , 255 );
252
- textBuffer .translate (10 , height );
253
- textBuffer .triangle (0 , 20 , 0 , 0 , 20 , 0 );
254
- textBuffer .stroke (255 );
255
- textBuffer .strokeWeight (3 );
256
- textBuffer .line (2 , 0 , 16 , 0 );
257
- textBuffer .pop ();
258
- } else if (this .mode == ScratchText .THINK ) {
259
- textBuffer .circle (20 , this .height , 10 );
260
- textBuffer .circle (7 , this .height + 7 , 6 );
261
- textBuffer .circle (0 , this .height + 10 , 4 );
242
+ String [] lines = this .text .split ("\n " );
243
+
244
+ float width = 0 ;
245
+ if (this .fullWidth ) {
246
+ width = ScratchStage .parent .width ;
247
+ } else {
248
+ // get minimum width
249
+ for (String l : lines ) {
250
+ System .out .println (l );
251
+ width = Math .max (textBuffer .textWidth (l ), width );
252
+ System .out .println (width );
253
+ }
254
+ width = Math .min (width + 16 , this .width );
262
255
}
256
+
257
+ this .height = (this .textSize + 4 ) * lines .length + 16 ;
258
+ textBuffer .translate (this .x , this .y - height );
259
+ textBuffer .stroke (200 );
260
+ if (this .mode == ScratchText .BOX ) {
261
+ textBuffer .rect (0 , 0 , width , this .height , 16 , 16 , 0 , 0 );
262
+ } else {
263
+ textBuffer .rect (0 , 0 , width , this .height , 16 , 16 , 16 , 16 );
264
+ if (this .mode == ScratchText .SPEAK ) {
265
+ textBuffer .push ();
266
+ textBuffer .fill (255 , 255 , 255 );
267
+ textBuffer .translate (10 , height );
268
+ textBuffer .triangle (0 , 20 , 0 , 0 , 20 , 0 );
269
+ textBuffer .stroke (255 );
270
+ textBuffer .strokeWeight (3 );
271
+ textBuffer .line (2 , 0 , 16 , 0 );
272
+ textBuffer .pop ();
273
+ } else if (this .mode == ScratchText .THINK ) {
274
+ textBuffer .circle (20 , this .height , 10 );
275
+ textBuffer .circle (7 , this .height + 7 , 6 );
276
+ textBuffer .circle (0 , this .height + 10 , 4 );
277
+ }
278
+ }
279
+ textBuffer .fill (120 );
280
+ textBuffer .textLeading (this .textSize + 4 );
281
+ textBuffer .text (this .text , 8 , 8 );
282
+ textBuffer .pop ();
263
283
}
264
- textBuffer .fill (0 , 0 , 0 );
265
- textBuffer .text (this .text , 4 , 4 );
266
- textBuffer .pop ();
284
+
267
285
268
286
if (this .hasLifetime && this .lifetime < System .currentTimeMillis ()) {
269
- this .show = false ;
287
+ this .show = false ;
270
288
}
271
289
}
272
290
}
0 commit comments