public TextCacheKey set(String text, TextAttributes attributes) { this.text = text; if (attributes != null) { this.textColor = (this.textColor != null) ? this.textColor.set(attributes.getTextColor()) : new Color(attributes.getTextColor()); this.textSize = attributes.getTextSize(); this.typeface = attributes.getTypeface(); this.enableOutline = attributes.isEnableOutline(); this.outlineColor = (this.outlineColor != null) ? this.outlineColor.set(attributes.getOutlineColor()) : new Color(attributes.getOutlineColor()); this.outlineWidth = attributes.getOutlineWidth(); } else { this.textColor = null; this.textSize = 0; this.typeface = null; this.enableOutline = false; this.outlineColor = null; this.outlineWidth = 0; } return this; }
public Texture renderText(String text, TextAttributes attributes) { TextCacheKey key = new TextCacheKey().set(text, attributes); Texture texture = null; if (text != null && attributes != null) { this.textRenderer.setTextColor(attributes.getTextColor()); this.textRenderer.setTextSize(attributes.getTextSize()); this.textRenderer.setTypeface(attributes.getTypeface()); this.textRenderer.setEnableOutline(attributes.isEnableOutline()); this.textRenderer.setOutlineColor(attributes.getOutlineColor()); this.textRenderer.setOutlineWidth(attributes.getOutlineWidth()); texture = this.textRenderer.renderText(text); } this.renderResourceCache.put(key, texture, (texture != null) ? texture.getByteCount() : 0); return texture; }
@Test public void testModifiedAttrs() { // Common Text Attributes TextAttributes attrs = new TextAttributes(); attrs.setEnableOutline(true); attrs.setOutlineColor(new Color(0.5f, 0.2f, 0.3f, 1)); attrs.setOutlineWidth(15); attrs.setTextColor(new Color(1, 1, 0.2f, 1)); attrs.setTextSize(90); // Common Text String text = "Testing"; // Cache Key 1 RenderContext.TextCacheKey textCacheKeyOne = new RenderContext.TextCacheKey().set(text, attrs); // Modify color property Color outlineColor = attrs.getOutlineColor().set(1, 1, 0.3f, 1); attrs.setOutlineColor(outlineColor); // Cache Key 2 RenderContext.TextCacheKey textCacheKeyTwo = new RenderContext.TextCacheKey().set(text, attrs); assertFalse("TextCacheKey modified attributes equals", textCacheKeyOne.equals(textCacheKeyTwo)); assertFalse("TextCacheKey modified attributes hashcode", textCacheKeyOne.hashCode() == textCacheKeyTwo.hashCode()); } }