Prevent NPE

This commit is contained in:
Anthony Calosa
2019-09-12 20:28:48 +08:00
parent 7a28295585
commit 61f400feac

View File

@@ -558,39 +558,37 @@ public class CardRenderer {
}
//if (counterBoxBaseWidth + font.getBounds(String.valueOf(maxCounters)).width > w) {
layout.setText(font, String.valueOf(maxCounters));
if (counterBoxBaseWidth + layout.width > w) {
drawCounterImage(card, g, x, y, w, h);
return;
if(font != null && !String.valueOf(maxCounters).isEmpty()){
layout.setText(font, String.valueOf(maxCounters));
if (counterBoxBaseWidth + layout.width > w) {
drawCounterImage(card, g, x, y, w, h);
return;
}
}
}
for (Map.Entry<CounterType, Integer> counterEntry : card.getCounters().entrySet()) {
final CounterType counter = counterEntry.getKey();
final int numberOfCounters = counterEntry.getValue();
//final float counterBoxRealWidth = counterBoxBaseWidth + font.getBounds(String.valueOf(numberOfCounters)).width + 4;
layout.setText(font, String.valueOf(numberOfCounters));
final float counterBoxRealWidth = counterBoxBaseWidth + layout.width + 4;
if(font != null && !String.valueOf(numberOfCounters).isEmpty()){
layout.setText(font, String.valueOf(numberOfCounters));
final float counterBoxRealWidth = counterBoxBaseWidth + layout.width + 4;
final float counterYOffset = spaceFromTopOfCard - (currentCounter++ * (counterBoxHeight + counterBoxSpacing));
final float counterYOffset = spaceFromTopOfCard - (currentCounter++ * (counterBoxHeight + counterBoxSpacing));
g.fillRect(counterBackgroundColor, x - 3, counterYOffset, counterBoxRealWidth, counterBoxHeight);
g.fillRect(counterBackgroundColor, x - 3, counterYOffset, counterBoxRealWidth, counterBoxHeight);
if (!counterColorCache.containsKey(counter)) {
counterColorCache.put(counter, new Color(counter.getRed() / 255.0f, counter.getGreen() / 255.0f, counter.getBlue() / 255.0f, 1.0f));
if (!counterColorCache.containsKey(counter)) {
counterColorCache.put(counter, new Color(counter.getRed() / 255.0f, counter.getGreen() / 255.0f, counter.getBlue() / 255.0f, 1.0f));
}
Color counterColor = counterColorCache.get(counter);
drawText(g, counter.getCounterOnCardDisplayName(), font, counterColor, x + 2 + additionalXOffset, counterYOffset, counterBoxRealWidth, counterBoxHeight, Align.left);
drawText(g, String.valueOf(numberOfCounters), font, counterColor, x + counterBoxBaseWidth - 4f - additionalXOffset, counterYOffset, counterBoxRealWidth, counterBoxHeight, Align.left);
}
Color counterColor = counterColorCache.get(counter);
drawText(g, counter.getCounterOnCardDisplayName(), font, counterColor, x + 2 + additionalXOffset, counterYOffset, counterBoxRealWidth, counterBoxHeight, Align.left);
drawText(g, String.valueOf(numberOfCounters), font, counterColor, x + counterBoxBaseWidth - 4f - additionalXOffset, counterYOffset, counterBoxRealWidth, counterBoxHeight, Align.left);
}
}
private static final int GL_BLEND = GL20.GL_BLEND;
@@ -600,22 +598,22 @@ public class CardRenderer {
if (color.a < 1) { //enable blending so alpha colored shapes work properly
Gdx.gl.glEnable(GL_BLEND);
}
if(font != null && !text.isEmpty()) {
layout.setText(font, text);
TextBounds textBounds = new TextBounds(layout.width, layout.height);
layout.setText(font, text);
TextBounds textBounds = new TextBounds(layout.width, layout.height);
float textHeight = textBounds.height;
if (h > textHeight) {
y += (h - textHeight) / 2;
}
float textHeight = textBounds.height;
if (h > textHeight) {
y += (h - textHeight) / 2;
font.setColor(color);
font.draw(g.getBatch(), text, g.adjustX(x), g.adjustY(y, 0), w, horizontalAlignment, true);
if (color.a < 1) {
Gdx.gl.glDisable(GL_BLEND);
}
}
font.setColor(color);
font.draw(g.getBatch(), text, g.adjustX(x), g.adjustY(y, 0), w, horizontalAlignment, true);
if (color.a < 1) {
Gdx.gl.glDisable(GL_BLEND);
}
}
private static void drawCounterImage(final CardView card, final Graphics g, final float x, final float y, final float w, final float h) {