From 2f246b25bbd5648d9d343f75a98847695a3a3226 Mon Sep 17 00:00:00 2001 From: Paul Speed Date: Sat, 26 Mar 2016 04:08:51 -0400 Subject: [PATCH] Added cloneFields() method to BitmapText thought it's probably fruitless since BitmapText isn't even properly saveable and couldn't possibly have worked for any dynamic text with the old clone() method. Also a bunch of white space changes removing spaces at the ends of lines. --- .../main/java/com/jme3/font/BitmapText.java | 48 +++++++++----- .../src/main/java/com/jme3/font/Letters.java | 62 +++++++++---------- 2 files changed, 65 insertions(+), 45 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/font/BitmapText.java b/jme3-core/src/main/java/com/jme3/font/BitmapText.java index 913bfe13a..20f5cec3b 100644 --- a/jme3-core/src/main/java/com/jme3/font/BitmapText.java +++ b/jme3-core/src/main/java/com/jme3/font/BitmapText.java @@ -38,6 +38,7 @@ import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.renderer.RenderManager; import com.jme3.scene.Node; +import com.jme3.util.clone.Cloner; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -84,6 +85,25 @@ public class BitmapText extends Node { return clone; } + /** + * Called internally by com.jme3.util.clone.Cloner. Do not call directly. + */ + @Override + public void cloneFields( Cloner cloner, Object original ) { + for( int i = 0; i < textPages.length; i++ ) { + textPages[i] = cloner.clone(textPages[i]); + } + this.block = cloner.clone(block); + + // Change in behavior: The 'letters' field was not cloned or recreated + // before. I'm not sure how this worked and suspect BitmapText was just + // not cloneable if you planned to change the text later. -pspeed + this.letters = new Letters(font, block, letters.getQuad().isRightToLeft()); + + // Just noticed BitmapText is not even writable/readable really... + // so I guess cloning doesn't come up that often. + } + public BitmapFont getFont() { return font; } @@ -115,10 +135,10 @@ public class BitmapText extends Node { * * @param text String to change text to */ - public void setText(String text) { + public void setText(String text) { text = text == null ? "" : text; - if (text == block.getText() || block.getText().equals(text)) { + if (text == block.getText() || block.getText().equals(text)) { return; } @@ -126,24 +146,24 @@ public class BitmapText extends Node { The problem with the below block is that StringBlock carries pretty much all of the text-related state of the BitmapText such as size, text box, alignment, etc. - + I'm not sure why this change was needed and the commit message was - not entirely helpful because it purports to fix a problem that I've + not entirely helpful because it purports to fix a problem that I've never encountered. - + If block.setText("") doesn't do the right thing then that's where the fix should go because StringBlock carries too much information to be blown away every time. -pspeed - + Change was made: http://code.google.com/p/jmonkeyengine/source/detail?spec=svn9389&r=9389 Diff: http://code.google.com/p/jmonkeyengine/source/diff?path=/trunk/engine/src/core/com/jme3/font/BitmapText.java&format=side&r=9389&old_path=/trunk/engine/src/core/com/jme3/font/BitmapText.java&old=8843 - + // If the text is empty, reset if (text.isEmpty()) { detachAllChildren(); - + for (int page = 0; page < textPages.length; page++) { textPages[page] = new BitmapTextPage(font, true, page); attachChild(textPages[page]); @@ -153,7 +173,7 @@ public class BitmapText extends Node { letters = new Letters(font, block, letters.getQuad().isRightToLeft()); } */ - + // Update the text content block.setText(text); letters.setText(text); @@ -185,7 +205,7 @@ public class BitmapText extends Node { letters.invalidate(); // TODO: Don't have to align. needRefresh = true; } - + /** * Sets an overall alpha that will be applied to all * letters. If the alpha passed is -1 then alpha reverts @@ -196,7 +216,7 @@ public class BitmapText extends Node { public void setAlpha(float alpha) { letters.setBaseAlpha(alpha); needRefresh = true; - } + } public float getAlpha() { return letters.getBaseAlpha(); @@ -414,17 +434,17 @@ public class BitmapText extends Node { if( mp == null ) { return null; } - return (ColorRGBA)mp.getValue(); + return (ColorRGBA)mp.getValue(); } public void render(RenderManager rm, ColorRGBA color) { for (BitmapTextPage page : textPages) { Material mat = page.getMaterial(); mat.setTexture("ColorMap", page.getTexture()); - //ColorRGBA original = getColor(mat, "Color"); + //ColorRGBA original = getColor(mat, "Color"); //mat.setColor("Color", color); mat.render(page, rm); - + //if( original == null ) { // mat.clearParam("Color"); //} else { diff --git a/jme3-core/src/main/java/com/jme3/font/Letters.java b/jme3-core/src/main/java/com/jme3/font/Letters.java index e8b8c8270..604f68785 100644 --- a/jme3-core/src/main/java/com/jme3/font/Letters.java +++ b/jme3-core/src/main/java/com/jme3/font/Letters.java @@ -53,7 +53,7 @@ class Letters { private ColorRGBA baseColor = null; private float baseAlpha = -1; private String plainText; - + Letters(BitmapFont font, StringBlock bound, boolean rightToLeft) { final String text = bound.getText(); this.block = bound; @@ -78,10 +78,10 @@ class Letters { // Give the letter a default color if // one has been provided. l.setColor( baseColor ); - } + } } } - + LinkedList ranges = colorTags.getTags(); if (!ranges.isEmpty()) { for (int i = 0; i < ranges.size()-1; i++) { @@ -92,7 +92,7 @@ class Letters { Range end = ranges.getLast(); setColor(end.start, plainText.length(), end.color); } - + invalidate(); } @@ -103,17 +103,17 @@ class Letters { LetterQuad getTail() { return tail; } - + void update() { LetterQuad l = head; int lineCount = 1; BitmapCharacter ellipsis = font.getCharSet().getCharacter(block.getEllipsisChar()); float ellipsisWidth = ellipsis!=null? ellipsis.getWidth()*getScale(): 0; - + while (!l.isTail()) { if (l.isInvalid()) { l.update(block); - + if (l.isInvalid(block)) { switch (block.getLineWrapMode()) { case Character: @@ -144,7 +144,7 @@ class Letters { } } break; - case NoWrap: + case NoWrap: LetterQuad cursor = l.getPrevious(); while (cursor.isInvalid(block, ellipsisWidth) && !cursor.isLineStart()) { cursor = cursor.getPrevious(); @@ -158,10 +158,10 @@ class Letters { cursor = cursor.getNext(); } break; - case Clip: + case Clip: // Clip the character that falls out of bounds l.clip(block); - + // Clear the rest up to the next line feed. for( LetterQuad q = l.getNext(); !q.isTail() && !q.isLineFeed(); q = q.getNext() ) { q.setBitmapChar(null); @@ -178,12 +178,12 @@ class Letters { } l = l.getNext(); } - + align(); block.setLineCount(lineCount); rewind(); } - + private void align() { final Align alignment = block.getAlignment(); final VAlign valignment = block.getVerticalAlignment(); @@ -233,7 +233,7 @@ class Letters { l.invalidate(); l.update(block); // TODO: update from l } - + float getCharacterX0() { return current.getX0(); } @@ -241,54 +241,54 @@ class Letters { float getCharacterY0() { return current.getY0(); } - + float getCharacterX1() { return current.getX1(); } - + float getCharacterY1() { return current.getY1(); } - + float getCharacterAlignX() { return current.getAlignX(); } - + float getCharacterAlignY() { return current.getAlignY(); } - + float getCharacterWidth() { return current.getWidth(); } - + float getCharacterHeight() { return current.getHeight(); } - + public boolean nextCharacter() { if (current.isTail()) return false; current = current.getNext(); return true; } - + public int getCharacterSetPage() { return current.getBitmapChar().getPage(); } - + public LetterQuad getQuad() { return current; } - + public void rewind() { current = head; } - + public void invalidate() { invalidate(head); } - + public void invalidate(LetterQuad cursor) { totalWidth = -1; totalHeight = -1; @@ -298,7 +298,7 @@ class Letters { cursor = cursor.getNext(); } } - + float getScale() { return block.getSize() / font.getCharSet().getRenderedSize(); } @@ -306,7 +306,7 @@ class Letters { public boolean isPrintable() { return current.getBitmapChar() != null; } - + float getTotalWidth() { validateSize(); return totalWidth; @@ -316,7 +316,7 @@ class Letters { validateSize(); return totalHeight; } - + void validateSize() { if (totalWidth < 0) { LetterQuad l = head; @@ -371,11 +371,11 @@ class Letters { cursor = cursor.getNext(); } } - + float getBaseAlpha() { return baseAlpha; } - + void setBaseAlpha( float alpha ) { this.baseAlpha = alpha; colorTags.setBaseAlpha(alpha); @@ -409,7 +409,7 @@ class Letters { setColor(end.start, plainText.length(), end.color); } } - + invalidate(); }