v0.4.2: separated initialization and rendering of rendered text
This commit is contained in:
committed by
Evan Debenham
parent
0fc51654be
commit
a54bb5fdc8
@@ -87,6 +87,8 @@ public class RenderedText extends Image {
|
|||||||
private String text;
|
private String text;
|
||||||
private CachedText cache;
|
private CachedText cache;
|
||||||
|
|
||||||
|
private boolean needsRender = false;
|
||||||
|
|
||||||
public RenderedText( ){
|
public RenderedText( ){
|
||||||
text = null;
|
text = null;
|
||||||
}
|
}
|
||||||
@@ -100,13 +102,15 @@ public class RenderedText extends Image {
|
|||||||
this.text = text;
|
this.text = text;
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
|
||||||
render();
|
needsRender = true;
|
||||||
|
measure();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void text( String text ){
|
public void text( String text ){
|
||||||
this.text = text;
|
this.text = text;
|
||||||
|
|
||||||
render();
|
needsRender = true;
|
||||||
|
measure();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String text(){
|
public String text(){
|
||||||
@@ -115,14 +119,16 @@ public class RenderedText extends Image {
|
|||||||
|
|
||||||
public void size( int size ){
|
public void size( int size ){
|
||||||
this.size = size;
|
this.size = size;
|
||||||
render();
|
needsRender = true;
|
||||||
|
measure();
|
||||||
}
|
}
|
||||||
|
|
||||||
public float baseLine(){
|
public float baseLine(){
|
||||||
return size * scale.y;
|
return size * scale.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void render(){
|
private void measure(){
|
||||||
|
|
||||||
if ( text == null || text.equals("") ) {
|
if ( text == null || text.equals("") ) {
|
||||||
text = "";
|
text = "";
|
||||||
width=height=0;
|
width=height=0;
|
||||||
@@ -132,6 +138,27 @@ public class RenderedText extends Image {
|
|||||||
visible = true;
|
visible = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
painter.setTextSize(size);
|
||||||
|
painter.setAntiAlias(true);
|
||||||
|
|
||||||
|
if (font != null) {
|
||||||
|
painter.setTypeface(font);
|
||||||
|
} else {
|
||||||
|
painter.setTypeface(Typeface.DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
//paint outer strokes
|
||||||
|
painter.setARGB(0xff, 0, 0, 0);
|
||||||
|
painter.setStyle(Paint.Style.STROKE);
|
||||||
|
painter.setStrokeWidth(size / 5f);
|
||||||
|
|
||||||
|
width = (painter.measureText(text)+ (size/5f));
|
||||||
|
height = (-painter.ascent() + painter.descent()+ (size/5f));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void render(){
|
||||||
|
needsRender = false;
|
||||||
|
|
||||||
if (cache != null)
|
if (cache != null)
|
||||||
cache.activeTexts.remove(this);
|
cache.activeTexts.remove(this);
|
||||||
|
|
||||||
@@ -143,25 +170,13 @@ public class RenderedText extends Image {
|
|||||||
cache.activeTexts.add(this);
|
cache.activeTexts.add(this);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
painter.setTextSize(size);
|
measure();
|
||||||
painter.setAntiAlias(true);
|
|
||||||
|
|
||||||
if (font != null) {
|
if (width == 0 || height == 0)
|
||||||
painter.setTypeface(font);
|
return;
|
||||||
} else {
|
|
||||||
painter.setTypeface(Typeface.DEFAULT);
|
|
||||||
}
|
|
||||||
|
|
||||||
//paint outer strokes
|
|
||||||
painter.setARGB(0xff, 0, 0, 0);
|
|
||||||
painter.setStyle(Paint.Style.STROKE);
|
|
||||||
painter.setStrokeWidth(size / 5f);
|
|
||||||
|
|
||||||
int right = (int)(painter.measureText(text)+ (size/5f));
|
|
||||||
int bottom = (int)(-painter.ascent() + painter.descent()+ (size/5f));
|
|
||||||
|
|
||||||
//bitmap has to be in a power of 2 for some devices (as we're using openGL methods to render to texture)
|
//bitmap has to be in a power of 2 for some devices (as we're using openGL methods to render to texture)
|
||||||
Bitmap bitmap = Bitmap.createBitmap(Integer.highestOneBit(right)*2, Integer.highestOneBit(bottom)*2, Bitmap.Config.ARGB_4444);
|
Bitmap bitmap = Bitmap.createBitmap(Integer.highestOneBit((int)width)*2, Integer.highestOneBit((int)height)*2, Bitmap.Config.ARGB_4444);
|
||||||
bitmap.eraseColor(0x00000000);
|
bitmap.eraseColor(0x00000000);
|
||||||
|
|
||||||
canvas.setBitmap(bitmap);
|
canvas.setBitmap(bitmap);
|
||||||
@@ -175,7 +190,7 @@ public class RenderedText extends Image {
|
|||||||
|
|
||||||
texture = new SmartTexture(bitmap, Texture.NEAREST, Texture.CLAMP, true);
|
texture = new SmartTexture(bitmap, Texture.NEAREST, Texture.CLAMP, true);
|
||||||
|
|
||||||
RectF rect = texture.uvRect(0, 0, right, bottom);
|
RectF rect = texture.uvRect(0, 0, (int)width, (int)height);
|
||||||
frame(rect);
|
frame(rect);
|
||||||
|
|
||||||
cache = new CachedText();
|
cache = new CachedText();
|
||||||
@@ -195,6 +210,13 @@ public class RenderedText extends Image {
|
|||||||
Matrix.translate( matrix, 0, -Math.round((baseLine()*0.15f)/scale.y) );
|
Matrix.translate( matrix, 0, -Math.round((baseLine()*0.15f)/scale.y) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw() {
|
||||||
|
if (needsRender)
|
||||||
|
render();
|
||||||
|
super.draw();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
if (cache != null)
|
if (cache != null)
|
||||||
|
|||||||
Reference in New Issue
Block a user