Skip to content

Commit 54f1377

Browse files
author
Mike Barkmin
committed
improve ScratchText rendering
1 parent ba3f48e commit 54f1377

File tree

4 files changed

+77
-55
lines changed

4 files changed

+77
-55
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.14.1
2+
3+
* Improve ScratchText rendering
4+
15
## 1.14.0
26

37
* add think and say to ScratchSprite

resources/build.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ source.repository=https://github.com/mikebarkmin/processing-library-scratch.git
131131
# This is used to compare different versions of the same Library, and check if
132132
# an update is available.
133133

134-
library.version=17
134+
library.version=18
135135

136136

137137
# The version as the user will see it.
138138

139-
library.prettyVersion=1.14.0
139+
library.prettyVersion=1.14.1
140140

141141

142142
# The min and max revision of Processing compatible with your Library.

src/eu/barkmin/processing/scratch/ScratchStage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private ScratchStage(PApplet parent, boolean debug) {
4545
this.penBuffer = parent.createGraphics(parent.width, parent.height, parent.sketchRenderer());
4646
this.timer = new HashMap<>();
4747
this.timer.put("default", new Timer());
48-
this.display = new ScratchText(null,0,parent.height, parent.width , ScratchText.BOX);
48+
this.display = new ScratchText(null,0,parent.height, true , ScratchText.BOX);
4949
this.debug = debug;
5050
this.sprites = new CopyOnWriteArrayList<>();
5151
}

src/eu/barkmin/processing/scratch/ScratchText.java

Lines changed: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import processing.core.PApplet;
44
import processing.core.PFont;
5-
import processing.core.PGraphics;
65

76
import java.util.regex.Matcher;
87
import 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

Comments
 (0)