Skip to content

Commit 66a3f80

Browse files
committed
fix text bubble
1 parent ad770b1 commit 66a3f80

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

examples/java/Sensing/Sensing.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public Sensing() {
1717
m = new MovableHero();
1818
this.add(h);
1919
this.add(m);
20+
21+
var uiH = new Hero();
22+
uiH.isUI(true);
23+
uiH.setPosition(300, 300);
24+
this.add(uiH);
2025
}
2126

2227
public void run() {
@@ -84,7 +89,7 @@ public void run() {
8489
if (this.isKeyPressed(82)) {
8590
this.turnRight(1);
8691
}
87-
if (this.isTouchingSprite(Sensing.h)) {
92+
if (this.isTouchingSprite(Hero.class)) {
8893
this.say("Hit");
8994
} else {
9095
this.say(null);

src/org/openpatch/scratch/Sprite.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public void setOnEdgeBounce(boolean b) {
449449
}
450450

451451
public void ifOnEdgeBounce() {
452-
if (this.hitboxDisabled) return;
452+
if (this.hitboxDisabled || this.isUI) return;
453453

454454
var h = this.getHitbox();
455455

@@ -739,6 +739,11 @@ public boolean isTouchingMousePointer() {
739739
var mx = this.getMouseX();
740740
var my = this.getMouseY();
741741

742+
if (isUI && this.stage != null) {
743+
mx = this.stage.getCamera().toGlobalX(mx);
744+
my = this.stage.getCamera().toGlobalY(my);
745+
}
746+
742747
double[] mouse = Utils.rotateXY(mx, my, this.x, this.y, this.direction - 90);
743748

744749
var relativeMouseX = (int) Math.round(mouse[0] - this.x + this.getWidth() / 2);
@@ -879,6 +884,7 @@ public boolean isTouchingSprite(Sprite sprite) {
879884
public boolean isTouchingSprite(Class<? extends Sprite> c) {
880885
if (stage == null) return false;
881886
return this.stage.sprites.stream()
887+
.filter(s -> !s.isUI())
882888
.filter(s -> c.isInstance(s) && this.isTouchingSprite(s))
883889
.findFirst()
884890
.isPresent();
@@ -887,6 +893,7 @@ public boolean isTouchingSprite(Class<? extends Sprite> c) {
887893
public <T extends Sprite> T getTouchingSprite(Class<T> c) {
888894
if (stage == null) return null;
889895
return this.stage.sprites.stream()
896+
.filter(s -> !s.isUI())
890897
.filter(s -> c.isInstance(s) && this.isTouchingSprite(s))
891898
.findFirst()
892899
.map(c::cast)
@@ -896,6 +903,7 @@ public <T extends Sprite> T getTouchingSprite(Class<T> c) {
896903
public <T extends Sprite> List<T> getTouchingSprites(Class<T> c) {
897904
if (stage == null) return null;
898905
return this.stage.sprites.stream()
906+
.filter(s -> !s.isUI())
899907
.filter(s -> c.isInstance(s) && this.isTouchingSprite(s))
900908
.map(c::cast)
901909
.collect(Collectors.toList());
@@ -1211,7 +1219,7 @@ public void draw() {
12111219
}
12121220

12131221
public void drawDebug() {
1214-
if (!this.hitboxDisabled) {
1222+
if (!this.hitboxDisabled && !this.isUI) {
12151223
this.getHitbox().drawDebug(this.getStage().getDebugBuffer());
12161224
}
12171225
if (this.costumes.size() > 0 && this.show) {

src/org/openpatch/scratch/Stage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,8 +1080,9 @@ public void draw() {
10801080
this.debugBuffer.scale((float) this.camera.getZoom() / 100.0f);
10811081
this.debugBuffer.translate((float) -this.camera.getX(), (float) this.camera.getY());
10821082
this.debugBuffer.clear();
1083-
this.sprites.stream().forEach(s -> s.drawDebug());
1083+
this.sprites.stream().filter(s -> !s.isUI()).forEach(s -> s.drawDebug());
10841084
this.debugBuffer.popMatrix();
1085+
this.sprites.stream().filter(s -> s.isUI()).forEach(s -> s.drawDebug());
10851086
this.debugBuffer.strokeWeight(1);
10861087
this.debugBuffer.stroke(Window.DEBUG_COLOR[0], Window.DEBUG_COLOR[1], Window.DEBUG_COLOR[2]);
10871088
this.debugBuffer.fill(Window.DEBUG_COLOR[0], Window.DEBUG_COLOR[1], Window.DEBUG_COLOR[2]);

src/org/openpatch/scratch/extensions/text/Text.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,28 +276,28 @@ private void drawBubble() {
276276
return;
277277
}
278278

279-
this.y = -this.sprite.getY() - this.sprite.getHeight() * 1.1 / 2;
280-
this.x = this.sprite.getX() + this.sprite.getWidth() * 0.9 / 2;
279+
this.y = this.sprite.getY() + this.sprite.getHeight() * 1.1 / 2;
280+
this.x = this.sprite.getX() + this.sprite.getWidth() * 0.9 / 2.0;
281281

282282
this.width = maxLineWidth + 16;
283283
this.text = String.join("\n", lines);
284284
this.height = (this.textSize + 4) * lines.length + 16;
285285
applet.rectMode(PApplet.CORNER);
286286

287287
// bound for top
288-
var y = Math.max(0, this.y - this.height);
288+
var y = Math.min(applet.getHeight() / 2.0, this.y + this.height);
289289

290290
// bound for bottom
291-
if (y + this.height > applet.getHeight()) {
292-
y = applet.getHeight() - this.height;
291+
if (y - this.height < -applet.getHeight() / 2.0) {
292+
y = -applet.getHeight() / 2.0 + this.height;
293293
}
294294

295-
// bound for left side
296-
var x = Math.max(0, this.x);
297-
298295
// bound for right side
296+
var x = Math.min(applet.getWidth() / 2.0 - this.width, this.x + this.width);
297+
298+
// bound for left side
299299
var mirror = false;
300-
if (x + this.width > applet.getWidth()) {
300+
if (x - this.width < -applet.getWidth() / 2.0) {
301301
mirror = true;
302302
}
303303

@@ -306,9 +306,9 @@ private void drawBubble() {
306306
if (x < 0) {
307307
x = 0;
308308
}
309-
applet.translate((float) x, (float) y);
309+
applet.translate((float) x, (float) -y);
310310
} else {
311-
applet.translate((float) x, (float) y);
311+
applet.translate((float) x, (float) -y);
312312
}
313313
applet.stroke(
314314
(float) this.strokeColor.getRed(),
@@ -377,8 +377,7 @@ private void drawBox() {
377377
this.text = String.join("\n", lines);
378378

379379
applet.rectMode(PApplet.CORNER);
380-
applet.translate(
381-
(float) this.x, (float) (-this.y - this.height));
380+
applet.translate((float) this.x, (float) (-this.y - this.height));
382381
applet.stroke(
383382
(float) this.strokeColor.getRed(),
384383
(float) this.strokeColor.getGreen(),

0 commit comments

Comments
 (0)