Skip to content

Commit adcf8df

Browse files
committed
support varargs coloring of send/drawText strings
1 parent f9f275b commit adcf8df

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/main/java/bwapi/Game.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,10 @@ public List<TilePosition> getStartLocations() {
14991499
return startLocations;
15001500
}
15011501

1502+
private String formatString(final String string, final Text... colors) {
1503+
return colors.length > 0 ? String.format(string, Arrays.stream(colors).map(c -> c.id).toArray()) : string;
1504+
}
1505+
15021506
/**
15031507
* Prints text to the screen as a notification. This function allows text
15041508
* formatting using {@link Text#formatText}.
@@ -1507,8 +1511,9 @@ public List<TilePosition> getStartLocations() {
15071511
*
15081512
* @param string String to print.
15091513
*/
1510-
public void printf(final String string) {
1511-
addCommand(Printf, client.addString(string), 0);
1514+
public void printf(final String string, final Text... colors) {
1515+
final String formatted = formatString(string, colors);
1516+
addCommand(Printf, client.addString(formatted), 0);
15121517
}
15131518

15141519
/**
@@ -1739,34 +1744,35 @@ public List<Player> observers() {
17391744
return observers;
17401745
}
17411746

1742-
public void drawText(final CoordinateType ctype, final int x, final int y, final String string) {
1743-
final int stringId = client.addString(string);
1747+
public void drawText(final CoordinateType ctype, final int x, final int y, final String string, final Text... colors) {
1748+
final String formatted = formatString(string, colors);
1749+
final int stringId = client.addString(formatted);
17441750
addShape(ShapeType.Text, ctype, x, y, 0, 0, stringId, textSize.id, 0, false);
17451751
}
17461752

1747-
public void drawTextMap(final int x, final int y, final String string) {
1748-
drawText(CoordinateType.Map, x, y, string);
1753+
public void drawTextMap(final int x, final int y, final String string, final Text... colors) {
1754+
drawText(CoordinateType.Map, x, y, string, colors);
17491755
}
17501756

1751-
public void drawTextMap(final Position p, final String string) {
1752-
drawTextMap(p.x, p.y, string);
1757+
public void drawTextMap(final Position p, final String string, final Text... colors) {
1758+
drawTextMap(p.x, p.y, string, colors);
17531759
}
17541760

1755-
public void drawTextMouse(final int x, final int y, final String string) {
1756-
drawText(CoordinateType.Mouse, x, y, string);
1761+
public void drawTextMouse(final int x, final int y, final String string, final Text... colors) {
1762+
drawText(CoordinateType.Mouse, x, y, string, colors);
17571763
}
17581764

1759-
public void drawTextMouse(final Position p, final String string) {
1760-
drawTextMouse(p.x, p.y, string);
1765+
public void drawTextMouse(final Position p, final String string, final Text... colors) {
1766+
drawTextMouse(p.x, p.y, string, colors);
17611767

17621768
}
17631769

1764-
public void drawTextScreen(final int x, final int y, final String string) {
1765-
drawText(CoordinateType.Screen, x, y, string);
1770+
public void drawTextScreen(final int x, final int y, final String string, final Text... colors) {
1771+
drawText(CoordinateType.Screen, x, y, string, colors);
17661772
}
17671773

1768-
public void drawTextScreen(final Position p, final String string) {
1769-
drawTextScreen(p.x, p.y, string);
1774+
public void drawTextScreen(final Position p, final String string, final Text... colors) {
1775+
drawTextScreen(p.x, p.y, string, colors);
17701776
}
17711777

17721778
public void drawBox(final CoordinateType ctype, final int left, final int top, final int right, final int bottom, final Color color) {

src/test/java/game/DrawTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package game;
22

3-
import bwapi.BWClient;
4-
import bwapi.DefaultBWListener;
5-
import bwapi.Game;
6-
import bwapi.Color;
3+
import bwapi.*;
74

85
class DrawTest extends DefaultBWListener {
96
final BWClient bwClient;
@@ -30,6 +27,7 @@ public void onFrame() {
3027
game.drawTriangleScreen(143, 167,407, 167, 275, 350, Color.Purple, true);
3128

3229
game.drawBoxScreen(200, 110, 350, 210, Color.Purple, true);
30+
game.drawTextScreen(50, 50, "%cHello %cWorld!", Text.Red, Text.Green);
3331

3432
}
3533

0 commit comments

Comments
 (0)