Skip to content

Commit 2a9ae48

Browse files
committed
feat(sdk): new property to set max width of a text in px before ellipsis
1 parent 7477b9b commit 2a9ae48

File tree

6 files changed

+80
-9
lines changed

6 files changed

+80
-9
lines changed

engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/TextBasedEntity.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codingame.gameengine.module.entities;
22

33
import java.util.Objects;
4+
45
/**
56
* Generic type for entities containing text.
67
* @param <T> a subclass inheriting TextureBasedEntity, used in order to return <b>this</b> as a T instead of a <code>TextBasedEntity</code>.
@@ -33,10 +34,12 @@ private int getValue() {
3334
return value;
3435
}
3536
}
37+
3638
protected String text = "";
3739
protected int fontSize = 26;
40+
protected int maxWidth = 0;
3841
protected TextAlign textAlign;
39-
42+
4043
/**
4144
* Returns the string this <code>TextBasedEntity</code> displays.
4245
* <p>
@@ -47,7 +50,6 @@ private int getValue() {
4750
public String getText() {
4851
return text;
4952
}
50-
5153

5254
/**
5355
* Sets the string for this <code>TextBasedEntity</code> to display.
@@ -64,6 +66,7 @@ public T setText(String text) {
6466
set("text", text, null);
6567
return self();
6668
}
69+
6770
/**
6871
* Returns text alignment of this <code>TextBasedEntity</code>.
6972
* <p>
@@ -74,7 +77,6 @@ public T setText(String text) {
7477
public TextAlign getTextAlign() {
7578
return textAlign;
7679
}
77-
7880

7981
/**
8082
* Sets the text alignment of this <code>TextBasedEntity</code>.
@@ -91,7 +93,7 @@ public T setTextAlign(TextAlign align) {
9193
set("textAlign", align.getValue(), null);
9294
return self();
9395
}
94-
96+
9597
/**
9698
* Returns the size of the font of this <code>TextBasedEntity</code> in px.
9799
* <p>
@@ -130,4 +132,41 @@ public T setFontSize(int fontSize, Curve curve) {
130132
set("fontSize", fontSize, curve);
131133
return self();
132134
}
135+
136+
/**
137+
* Returns the maximum width of this <code>TextBasedEntity</code> in pixels, before it gets ellipsed.
138+
* <p>
139+
* The ellipsis is applied before the entity is scaled. A max width of 0 means there is no maximum width.
140+
* </p>
141+
* <p>
142+
* Default is 0 (no max width).
143+
* </p>
144+
* @return the maximum width in pixels of this <code>TextBasedEntity</code>.
145+
*/
146+
public int getMaxWidth() {
147+
return maxWidth;
148+
}
149+
150+
/**
151+
* Sets the maximum width of this <code>TextBasedEntity</code> in pixels before it gets ellipsed.
152+
* <p>
153+
* The ellipsis is applied before the entity is scaled. A max width of 0 means there is no maximum width.
154+
* </p>
155+
* <p>
156+
* Default is 0 (no max width).
157+
* </p>
158+
*
159+
* @param maxWidth
160+
* the maximum width in pixels of this <code>TextBasedEntity</code>.
161+
* @return this <code>Text</code>.
162+
*/
163+
public T setMaxWidth(int maxWidth) {
164+
if (maxWidth < 0) {
165+
throw new IllegalArgumentException("Invalid max width: " + maxWidth);
166+
}
167+
168+
this.maxWidth = maxWidth;
169+
set("maxWidth", maxWidth, null);
170+
return self();
171+
}
133172
}

engine/modules/entities/src/main/resources/view/entity-module/BitmapText.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Entity } from './Entity.js'
22
import { ErrorLog } from '../core/ErrorLog.js'
33
import { MissingBitmapFontError } from './errors/MissingBitmapFontError.js'
44
import { TextureBasedEntity } from './TextureBasedEntity.js'
5+
import { ellipsis } from './textUtils.js'
56

67
/* global PIXI */
78

@@ -16,7 +17,8 @@ export class BitmapText extends Entity {
1617
anchorX: TextureBasedEntity.defaultAnchor(),
1718
anchorY: TextureBasedEntity.defaultAnchor(),
1819
blendMode: PIXI.BLEND_MODES.NORMAL,
19-
tint: 0xFFFFFF
20+
tint: 0xFFFFFF,
21+
maxWidth: 0
2022
})
2123
this.missingFonts = {}
2224
}
@@ -31,18 +33,25 @@ export class BitmapText extends Entity {
3133
if (state.fontFamily !== null) {
3234
if (PIXI.extras.BitmapText.fonts[state.fontFamily]) {
3335
if (this.graphics.children.length === 0) {
34-
this.displayed = new PIXI.BitmapText(state.text, {
36+
this.displayed = new PIXI.extras.BitmapText('', {
3537
font: { size: state.fontSize || 1, name: state.fontFamily }
3638
})
3739
this.graphics.addChild(this.displayed)
3840
} else {
39-
this.displayed.text = state.text
4041
this.displayed.font = { size: state.fontSize || 1, name: state.fontFamily }
4142
}
4243
this.displayed.anchor.set(state.anchorX, state.anchorY)
4344
this.displayed.blendMode = state.blendMode
4445
this.displayed.tint = state.tint
4546
this.displayed.align = state.textAlign
47+
48+
if (changed.text || changed.maxWidth || changed.fontSize ||
49+
changed.fontFamily) {
50+
this.displayed.text = state.text
51+
if (state.maxWidth) {
52+
ellipsis(this.displayed, state.maxWidth)
53+
}
54+
}
4655
} else {
4756
if (!this.missingFonts[state.fontFamily]) {
4857
this.missingFonts[state.fontFamily] = true

engine/modules/entities/src/main/resources/view/entity-module/Text.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TextureBasedEntity } from './TextureBasedEntity.js'
2+
import { ellipsis } from './textUtils.js'
23

34
/* global PIXI */
45

@@ -13,7 +14,8 @@ export class Text extends TextureBasedEntity {
1314
fillColor: 0,
1415
fontSize: 26,
1516
fontFamily: 'Lato',
16-
fontWeight: 'normal'
17+
fontWeight: 'normal',
18+
maxWidth: 0
1719
})
1820
}
1921

@@ -30,13 +32,22 @@ export class Text extends TextureBasedEntity {
3032

3133
updateDisplay (state, changed, globalData) {
3234
super.updateDisplay(state, changed, globalData)
33-
this.graphics.text = state.text
35+
3436
this.graphics.style.align = state.textAlign
3537
this.graphics.style.stroke = state.strokeColor
3638
this.graphics.style.strokeThickness = state.strokeThickness
3739
this.graphics.style.fill = state.fillColor
3840
this.graphics.style.fontSize = state.fontSize || 1
3941
this.graphics.style.fontFamily = state.fontFamily
4042
this.graphics.style.fontWeight = state.fontWeight
43+
44+
if (changed.text || changed.strokeThickness || changed.fontSize ||
45+
changed.fontFamily || changed.fontWeight || changed.maxWidth ||
46+
changed.maxWidth) {
47+
this.graphics.text = state.text
48+
if (state.maxWidth) {
49+
ellipsis(this.graphics, state.maxWidth)
50+
}
51+
}
4152
}
4253
}

engine/modules/entities/src/main/resources/view/entity-module/properties.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const PROPERTIES = {
7171

7272
animationProgressTime: timeOpts,
7373

74+
maxWidth: constOpts,
7475
mask: constOpts,
7576
baseWidth: constOpts,
7677
baseHeight: constOpts,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function ellipsis (text, maxWidth) {
2+
if (text.width > maxWidth) {
3+
while (text.text.length > 3 && text.width > maxWidth) {
4+
text.text = text.text.slice(0, -4) + '...'
5+
}
6+
}
7+
}

playground/misc/misc-3-release-notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The CodinGame SDK is regularly updated and improved. This document lets you know
44

55
## Next Release
66

7+
### 🎁 Features
8+
9+
- It is now possible to set the maximum width of a Text or BitmapText. This will add an ellipsis to the string.
10+
711
### 🐞 Bug fix
812

913
- Fixed an issue where games with a `stepByStepAnimateSpeed` would sometimes try to animate frame 0, causing a black screen.

0 commit comments

Comments
 (0)