Skip to content

Commit

Permalink
Keep speech bubbles on screen
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Dec 13, 2023
1 parent 47dea84 commit e4018e6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
14 changes: 9 additions & 5 deletions srcjs/stendhal/sprite/SpeechBubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,24 @@ export class SpeechBubble extends TextBubble {
}

const x = this.getX(), y = this.getY();
Speech.drawBubbleRounded(ctx, x, y - 15, this.width, this.height);
Speech.drawBubbleRounded(ctx, x, y, this.width, this.height);

ctx.fillStyle = "#000000";
ctx.fillText(this.text, x + 4, y);
ctx.fillText(this.text, x + 4, y + TextBubble.adjustY);

return this.expired();
}

override getX(): number {
return this.entity["_x"] * 32 + (32 * this.entity["width"]);
const x = this.entity["_x"] * 32 + (32 * this.entity["width"]);
// keep on screen (border is 1 pixel)
const overdraw = x + this.width - (stendhal.ui.gamewindow.offsetX + stendhal.ui.gamewindow.width) + 1;
return overdraw > 0 ? x - overdraw : x;
}

override getY(): number {
return this.entity["_y"] * 32 - 16 - (32 * (this.entity["height"]
- 1)) + this.offsetY;
const y = this.entity["_y"] * 32 - 16 - (32 * (this.entity["height"] - 1)) + this.offsetY - TextBubble.adjustY;
// keep on screen (border is 1 pixel)
return y < 1 ? 1 : y;
}
}
3 changes: 2 additions & 1 deletion srcjs/stendhal/sprite/TextBubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare var stendhal: any;
export abstract class TextBubble {

protected static readonly STANDARD_DUR = 5000;
protected static readonly adjustY = 15;

protected text: string;
protected timeStamp: number;
Expand Down Expand Up @@ -104,7 +105,7 @@ export abstract class TextBubble {
const pointX = evt.clientX - screenRect.x
+ stendhal.ui.gamewindow.offsetX;
const pointY = evt.clientY - screenRect.y
+ stendhal.ui.gamewindow.offsetY + 15;
+ stendhal.ui.gamewindow.offsetY + TextBubble.adjustY;

if (this.clipsPoint(pointX, pointY)) {
evt.stopPropagation();
Expand Down
9 changes: 8 additions & 1 deletion srcjs/stendhal/ui/ViewPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class ViewPort {
private offsetY = 0;
private timeStamp = Date.now();

// dimensions
private readonly width: number;
private readonly height: number;

private ctx: CanvasRenderingContext2D;
private readonly targetTileWidth = 32;
private readonly targetTileHeight = 32;
Expand Down Expand Up @@ -75,7 +79,10 @@ export class ViewPort {
* Hidden singleton constructor.
*/
private constructor() {
this.ctx = (document.getElementById("gamewindow")! as HTMLCanvasElement).getContext("2d")!;
const element = document.getElementById("gamewindow")! as HTMLCanvasElement;
this.ctx = element.getContext("2d")!;
this.width = element.width;
this.height = element.height;
}

draw() {
Expand Down

0 comments on commit e4018e6

Please sign in to comment.