Skip to content

Commit 20c6bf4

Browse files
Merge branch '5210-sdk-adding-the-text-align-property-to-textbasedentity' into 'master'
feature(TextBasedEntity): add textAlign property See merge request codingame/game-engine!259
2 parents e7af657 + 7529798 commit 20c6bf4

File tree

8 files changed

+82
-6
lines changed

8 files changed

+82
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ public String getFont() {
3131
* Only fonts available to the browser can be displayed.
3232
*
3333
*
34-
* @param fontFamily
34+
* @param font
3535
* the size for the font of this <code>BitmapText</code>.
3636
* @return this <code>BitmapText</code>.
3737
* @exception NullPointerException
38-
* if fontFamily is null.
38+
* if font is null.
3939
*/
40-
public BitmapText setFont(String fontFamily) {
41-
Objects.requireNonNull(fontFamily);
42-
this.font = fontFamily;
43-
set("fontFamily", fontFamily, null);
40+
public BitmapText setFont(String font) {
41+
Objects.requireNonNull(font);
42+
this.font = font;
43+
set("fontFamily", font, null);
4444
return this;
4545
}
4646

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Serializer {
5050
keys.put("fontSize", "s");
5151
keys.put("fontWeight", "fw");
5252
keys.put("text", "T");
53+
keys.put("textAlign", "ta");
5354
keys.put("children", "ch");
5455
keys.put("scaleX", "sx");
5556
keys.put("scaleY", "sy");

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,36 @@
66
* @param <T> a subclass inheriting TextureBasedEntity, used in order to return <b>this</b> as a T instead of a <code>TextBasedEntity</code>.
77
*/
88
public abstract class TextBasedEntity<T extends TextureBasedEntity<?>> extends TextureBasedEntity<T> {
9+
/**
10+
* This is an enumeration that contains the three options for text alignment: left, center, and right.
11+
*/
12+
public static enum TextAlign {
13+
/**
14+
* Align text left
15+
*/
16+
LEFT(0),
17+
/**
18+
* Align text center
19+
*/
20+
CENTER(1),
21+
/**
22+
* Align text right
23+
*/
24+
RIGHT(2);
25+
26+
private int value;
27+
28+
private TextAlign(int value) {
29+
this.value = value;
30+
}
31+
32+
private int getValue() {
33+
return value;
34+
}
35+
}
936
protected String text = "";
1037
protected int fontSize = 26;
38+
protected TextAlign textAlign;
1139

1240
/**
1341
* Returns the string this <code>TextBasedEntity</code> displays.
@@ -36,6 +64,33 @@ public T setText(String text) {
3664
set("text", text, null);
3765
return self();
3866
}
67+
/**
68+
* Returns text alignment of this <code>TextBasedEntity</code>.
69+
* <p>
70+
* Default is TextAlign.LEFT (align left).
71+
*
72+
* @return the text alignment of this <code>TextBasedEntity</code>.
73+
*/
74+
public TextAlign getTextAlign() {
75+
return textAlign;
76+
}
77+
78+
79+
/**
80+
* Sets the text alignment of this <code>TextBasedEntity</code>.
81+
*
82+
* @param align
83+
* the text alignment of this <code>TextBasedEntity</code>.
84+
* @return this <code>Text</code>.
85+
* @exception NullPointerException
86+
* if align is null.
87+
*/
88+
public T setTextAlign(TextAlign align) {
89+
Objects.requireNonNull(align);
90+
this.textAlign = align;
91+
set("textAlign", align.getValue(), null);
92+
return self();
93+
}
3994

4095
/**
4196
* Returns the size of the font of this <code>TextBasedEntity</code> in px.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class BitmapText extends Entity {
1010
super()
1111
Object.assign(this.defaultState, {
1212
text: '',
13+
textAlign: 'left',
1314
fontSize: 26,
1415
fontFamily: null,
1516
anchorX: TextureBasedEntity.defaultAnchor(),
@@ -41,6 +42,7 @@ export class BitmapText extends Entity {
4142
this.displayed.anchor.set(state.anchorX, state.anchorY)
4243
this.displayed.blendMode = state.blendMode
4344
this.displayed.tint = state.tint
45+
this.displayed.align = state.textAlign
4446
} else {
4547
if (!this.missingFonts[state.fontFamily]) {
4648
this.missingFonts[state.fontFamily] = true

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const PROPERTY_KEY_MAP = {
2626
s: 'fontSize',
2727
fw: 'fontWeight',
2828
T: 'text',
29+
ta: 'textAlign',
2930
ch: 'children',
3031
sx: 'scaleX',
3132
sy: 'scaleY',

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export class Text extends TextureBasedEntity {
77
super()
88
Object.assign(this.defaultState, {
99
text: '',
10+
textAlign: 'left',
1011
strokeColor: 0,
1112
strokeThickness: 0,
1213
fillColor: 0,
@@ -20,6 +21,7 @@ export class Text extends TextureBasedEntity {
2021
super.initDisplay()
2122

2223
this.graphics = new PIXI.Text(this.defaultState.text, {
24+
align: this.defaultState.textAlign,
2325
fontSize: this.defaultState.fontSize + 'px',
2426
fontFamily: this.defaultState.fontFamily,
2527
fill: this.defaultState.fillColor
@@ -29,6 +31,7 @@ export class Text extends TextureBasedEntity {
2931
updateDisplay (state, changed, globalData) {
3032
super.updateDisplay(state, changed, globalData)
3133
this.graphics.text = state.text
34+
this.graphics.style.align = state.textAlign
3235
this.graphics.style.stroke = state.strokeColor
3336
this.graphics.style.strokeThickness = globalData.atLeastOnePixel(state.strokeThickness)
3437
this.graphics.style.fill = state.fillColor

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ export const PROPERTIES = {
113113
return res
114114
}
115115
},
116+
textAlign: {
117+
...stringOpts,
118+
convert: (value) => (
119+
{
120+
0: 'left',
121+
1: 'center',
122+
2: 'right'
123+
}[value]
124+
)
125+
},
116126
fontFamily: stringOpts,
117127
fontWeight: stringOpts,
118128
children: {

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+
- Added text-align property to TextBasedEntity
10+
711
### 🐞 Bug fix
812

913
- Changed CSS of statement preview to match the codingame IDE

0 commit comments

Comments
 (0)