22
33import processing .core .PApplet ;
44import processing .core .PFont ;
5- import processing .core .PGraphics ;
65
76import java .util .regex .Matcher ;
87import java .util .regex .Pattern ;
@@ -14,6 +13,7 @@ public class ScratchText {
1413 private float x ;
1514 private float y ;
1615 private final float width ;
16+ private boolean fullWidth ;
1717 private float height ;
1818 private long lifetime ;
1919 private boolean hasLifetime ;
@@ -25,31 +25,6 @@ public class ScratchText {
2525 private boolean show ;
2626 private int mode ;
2727
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-
5328 public ScratchText (String text , float x , float y , float width ) {
5429 this .x = x ;
5530 this .y = y ;
@@ -58,7 +33,32 @@ public ScratchText(String text, float x, float y, float width) {
5833 this .width = width ;
5934 this .show = false ;
6035 this .mono = ScratchStage .parent .createFont ("UbuntuMono-Regular.ttf" , this .textSize );
36+ }
6137
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 ;
6262 }
6363
6464 /**
@@ -191,7 +191,7 @@ public static String wrap(String src, int lineLength, String newLineStr, boolean
191191 // Breakup long word
192192 while (wrapLongWords && word .length () > lineLength ) {
193193 cache
194- .append (word . substring ( 0 , remaining - breakLength ) )
194+ .append (word , 0 , remaining - breakLength )
195195 .append (longWordBreak )
196196 .append (newLineStr );
197197 word = longWordLinePrefix + word .substring (remaining - breakLength );
@@ -236,37 +236,55 @@ public void draw() {
236236
237237 if (this .text == null ) {
238238 float cw = textBuffer .textWidth ("w" );
239- int lineLength = Math .round (width / cw );
239+ int lineLength = Math .round (( this . width - 16 ) / cw );
240240 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 );
247241 } 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 );
262255 }
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 ();
263283 }
264- textBuffer .fill (0 , 0 , 0 );
265- textBuffer .text (this .text , 4 , 4 );
266- textBuffer .pop ();
284+
267285
268286 if (this .hasLifetime && this .lifetime < System .currentTimeMillis ()) {
269- this .show = false ;
287+ this .show = false ;
270288 }
271289 }
272290}
0 commit comments