Skip to content

Commit 7b61845

Browse files
author
Builder
committed
Merge branch 'adding-bitmaptext' into 'master'
[FEAT][SDK] Adding bitmaptext support See merge request codingame/game-engine!144
2 parents 0355677 + 48ededf commit 7b61845

File tree

10 files changed

+272
-102
lines changed

10 files changed

+272
-102
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codingame.gameengine.module.entities;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* Represents a label on screen, you can use any bitmap font in your asset folder as it's font.
7+
*/
8+
public class BitmapText extends TextBasedEntity<BitmapText> {
9+
10+
private String font;
11+
12+
@Override
13+
Type getType() {
14+
return Entity.Type.BITMAPTEXT;
15+
}
16+
17+
/**
18+
* Returns the name of the font of this <code>BitmapText</code> in px.
19+
* <p>
20+
* Default is null.
21+
*
22+
* @return the size of the font of this <code>BitmapText</code>.
23+
*/
24+
public String getFont() {
25+
return font;
26+
}
27+
28+
/**
29+
* Sets the name of the font of this <code>BitmapText</code>.
30+
* <p>
31+
* Only fonts available to the browser can be displayed.
32+
*
33+
*
34+
* @param fontFamily
35+
* the size for the font of this <code>BitmapText</code>.
36+
* @return this <code>BitmapText</code>.
37+
* @exception NullPointerException
38+
* if fontFamily is null.
39+
*/
40+
public BitmapText setFont(String fontFamily) {
41+
Objects.requireNonNull(fontFamily);
42+
this.font = fontFamily;
43+
set("fontFamily", fontFamily, null);
44+
return this;
45+
}
46+
47+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public abstract class Entity<T extends Entity<?>> {
2323
Mask mask;
2424

2525
static enum Type {
26-
CIRCLE, LINE, RECTANGLE, SPRITE, TEXT, GROUP, BUFFERED_GROUP, SPRITEANIMATION
26+
CIRCLE, LINE, RECTANGLE, SPRITE, TEXT, BITMAPTEXT, GROUP, BUFFERED_GROUP, SPRITEANIMATION
2727
}
2828

2929
Entity() {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,18 @@ public Text createText(String string) {
278278
return e;
279279

280280
}
281+
282+
/**
283+
* Creates a new BitmapText entity, its graphical counterpart will be created on the frame currently being computed.
284+
*
285+
* @return the entity. Modify its properties to animate the graphical counterpart.
286+
*/
287+
public BitmapText createBitmapText() {
288+
BitmapText e = new BitmapText();
289+
newEntity(e);
290+
return e;
291+
292+
}
281293

282294
/**
283295
* Creates a new Group entity, its graphical counterpart will be created on the frame currently being computed.

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
@@ -93,6 +93,7 @@ class Serializer {
9393
types.put(Type.LINE, "L");
9494
types.put(Type.SPRITE, "S");
9595
types.put(Type.TEXT, "T");
96+
types.put(Type.BITMAPTEXT, "X");
9697
types.put(Type.SPRITEANIMATION, "A");
9798

9899
if (keys.values().stream().distinct().count() != keys.values().size()) {

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

Lines changed: 30 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.codingame.gameengine.module.entities;
22

3-
import java.util.Objects;
4-
53
/**
64
* Represents a label on screen.
75
*/
8-
public class Text extends TextureBasedEntity<Text> {
6+
public class Text extends TextBasedEntity<Text> {
97

108
/**
119
* The list of supported font weights.
@@ -19,11 +17,9 @@ public String toString() {
1917
}
2018
}
2119

22-
private String text = "";
2320
private int strokeColor = 0;
2421
private double strokeThickness = 0;
2522
private int fillColor = 0;
26-
private int fontSize = 26;
2723
private String fontFamily = "Lato";
2824
private FontWeight fontWeight = FontWeight.NORMAL;
2925

@@ -36,32 +32,6 @@ Entity.Type getType() {
3632
return Entity.Type.TEXT;
3733
}
3834

39-
/**
40-
* Returns the string this <code>Text</code> displays.
41-
* <p>
42-
* Default is "" (empty string).
43-
*
44-
* @return the string of this <code>Text</code>.
45-
*/
46-
public String getText() {
47-
return text;
48-
}
49-
50-
/**
51-
* Sets the string for this <code>Text</code> to display.
52-
*
53-
* @param text
54-
* the string for this <code>Text</code> to display.
55-
* @return this <code>Text</code>.
56-
* @exception NullPointerException
57-
* if text is null.
58-
*/
59-
public Text setText(String text) {
60-
Objects.requireNonNull(text);
61-
this.text = text;
62-
set("text", text, null);
63-
return this;
64-
}
6535

6636
/**
6737
* Returns the color of the stroke of this <code>Text</code> as an RGB integer.
@@ -106,7 +76,34 @@ public Text setStrokeColor(int strokeColor, Curve curve) {
10676
}
10777

10878
/**
109-
* Returs the thickness of the stroke of this <code>Text</code> in pixels.
79+
* Returns the name of the font of this <code>Text</code> in px.
80+
* <p>
81+
* Default is "Lato".
82+
*
83+
* @return the size of the font of this <code>Text</code>.
84+
*/
85+
public String getFontFamily() {
86+
return fontFamily;
87+
}
88+
89+
/**
90+
* Sets the name of the font of this <code>Text</code>.
91+
* <p>
92+
* Only fonts available to the browser can be displayed.
93+
*
94+
*
95+
* @param fontFamily
96+
* the size for the font of this <code>Text</code>.
97+
* @return this <code>Text</code>.
98+
*/
99+
public Text setFontFamily(String fontFamily) {
100+
this.fontFamily = fontFamily;
101+
set("fontFamily", fontFamily, null);
102+
return this;
103+
}
104+
105+
/**
106+
* Returns the thickness of the stroke of this <code>Text</code> in pixels.
110107
*
111108
* @return the thickness of the stroke of this <code>Text</code>
112109
*/
@@ -148,7 +145,7 @@ public Text setStrokeThickness(double strokeThickness, Curve curve) {
148145
/**
149146
* Sets the weight of the font of this <code>Text</code>.
150147
*
151-
* @param style
148+
* @param weight
152149
* the FontWeight of the <code>Text</code>.
153150
* @return this <code>Text</code>.
154151
*/
@@ -213,71 +210,4 @@ public Text setFillColor(int fillColor, Curve curve) {
213210
set("fillColor", fillColor, curve);
214211
return this;
215212
}
216-
217-
/**
218-
* Returns the size of the font of this <code>Text</code> in px.
219-
* <p>
220-
* Default is 26.
221-
*
222-
* @return the size of the font of this <code>Text</code>.
223-
*/
224-
public int getFontSize() {
225-
return fontSize;
226-
}
227-
228-
/**
229-
* Sets the size of the font of this <code>Text</code> in px.
230-
*
231-
*
232-
* @param fontSize
233-
* the size for the font sof this <code>Text</code>.
234-
* @return this <code>Text</code>.
235-
*/
236-
public Text setFontSize(int fontSize) {
237-
return setFontSize(fontSize, null);
238-
}
239-
240-
/**
241-
* Sets the size of the font of this <code>Text</code> in px.
242-
*
243-
*
244-
* @param fontSize
245-
* the size for the font sof this <code>Text</code>.
246-
* @param curve
247-
* the transition to animate between values of this property.
248-
* @return this <code>Text</code>.
249-
*/
250-
public Text setFontSize(int fontSize, Curve curve) {
251-
this.fontSize = fontSize;
252-
set("fontSize", fontSize, curve);
253-
return this;
254-
}
255-
256-
/**
257-
* Returns the name of the font of this <code>Text</code> in px.
258-
* <p>
259-
* Default is "Lato".
260-
*
261-
* @return the size of the font of this <code>Text</code>.
262-
*/
263-
public String getFontFamily() {
264-
return fontFamily;
265-
}
266-
267-
/**
268-
* Sets the name of the font of this <code>Text</code>.
269-
* <p>
270-
* Only fonts available to the browser can be displayed.
271-
*
272-
*
273-
* @param fontFamily
274-
* the size for the font sof this <code>Text</code>.
275-
* @return this <code>Text</code>.
276-
*/
277-
public Text setFontFamily(String fontFamily) {
278-
this.fontFamily = fontFamily;
279-
set("fontFamily", fontFamily, null);
280-
return this;
281-
}
282-
283213
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.codingame.gameengine.module.entities;
2+
3+
import java.util.Objects;
4+
/**
5+
* Generic type for entities containing text.
6+
*/
7+
public abstract class TextBasedEntity<T extends TextureBasedEntity<?>> extends TextureBasedEntity<T> {
8+
protected String text = "";
9+
protected int fontSize = 26;
10+
11+
/**
12+
* Returns the string this <code>TextBasedEntity</code> displays.
13+
* <p>
14+
* Default is "" (empty string).
15+
*
16+
* @return the string of this <code>TextBasedEntity</code>.
17+
*/
18+
public String getText() {
19+
return text;
20+
}
21+
22+
23+
/**
24+
* Sets the string for this <code>TextBasedEntity</code> to display.
25+
*
26+
* @param text
27+
* the string for this <code>TextBasedEntity</code> to display.
28+
* @return this <code>Text</code>.
29+
* @exception NullPointerException
30+
* if text is null.
31+
*/
32+
public T setText(String text) {
33+
Objects.requireNonNull(text);
34+
this.text = text;
35+
set("text", text, null);
36+
return self();
37+
}
38+
39+
/**
40+
* Returns the size of the font of this <code>TextBasedEntity</code> in px.
41+
* <p>
42+
* Default is 26.
43+
*
44+
* @return the size of the font of this <code>TextBasedEntity</code>.
45+
*/
46+
public int getFontSize() {
47+
return fontSize;
48+
}
49+
50+
/**
51+
* Sets the size of the font of this <code>TextBasedEntity</code> in px.
52+
*
53+
*
54+
* @param fontSize
55+
* the size for the font of this <code>TextBasedEntity</code>.
56+
* @return this <code>Text</code>.
57+
*/
58+
public T setFontSize(int fontSize) {
59+
return setFontSize(fontSize, null);
60+
}
61+
62+
/**
63+
* Sets the size of the font of this <code>TextBasedEntity</code> in px.
64+
*
65+
*
66+
* @param fontSize
67+
* the size for the font of this <code>TextBasedEntity</code>.
68+
* @param curve
69+
* the transition to animate between values of this property.
70+
* @return this <code>Text</code>.
71+
*/
72+
public T setFontSize(int fontSize, Curve curve) {
73+
this.fontSize = fontSize;
74+
set("fontSize", fontSize, curve);
75+
return self();
76+
}
77+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Entity } from './Entity.js'
2+
import {ErrorLog} from '../core/ErrorLog.js'
3+
import {MissingBitmapFontError} from './errors/MissingBitmapFontError.js'
4+
import { TextureBasedEntity } from './TextureBasedEntity.js'
5+
6+
/* global PIXI */
7+
8+
export class BitmapText extends Entity {
9+
constructor () {
10+
super()
11+
Object.assign(this.defaultState, {
12+
text: '',
13+
fontSize: 26,
14+
fontFamily: null,
15+
anchorX: TextureBasedEntity.defaultAnchor(),
16+
anchorY: TextureBasedEntity.defaultAnchor(),
17+
blendMode: PIXI.BLEND_MODES.NORMAL,
18+
tint: 0xFFFFFF
19+
})
20+
this.missingFonts = {}
21+
}
22+
23+
initDisplay () {
24+
super.initDisplay()
25+
this.graphics = new PIXI.Container()
26+
}
27+
28+
updateDisplay (state, changed, globalData) {
29+
super.updateDisplay(state, changed, globalData)
30+
if (state.fontFamily !== null) {
31+
if (PIXI.extras.BitmapText.fonts[state.fontFamily]) {
32+
if (this.graphics.children.length === 0) {
33+
this.displayed = new PIXI.BitmapText(state.text, {
34+
font: {size: state.fontSize || 1, name: state.fontFamily}
35+
})
36+
this.graphics.addChild(this.displayed)
37+
} else {
38+
this.displayed.text = state.text
39+
this.displayed.font = { size: state.fontSize || 1, name: state.fontFamily }
40+
}
41+
this.displayed.anchor.set(state.anchorX, state.anchorY)
42+
this.displayed.blendMode = state.blendMode
43+
this.displayed.tint = state.tint
44+
} else {
45+
if (!this.missingFonts[state.fontFamily]) {
46+
this.missingFonts[state.fontFamily] = true
47+
ErrorLog.push(new MissingBitmapFontError(state.fontFamily))
48+
}
49+
this.graphics.removeChildren()
50+
}
51+
} else {
52+
this.graphics.removeChildren()
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)