Hi!
Thanks @pspeed for the rapid fixing of the issue here: Exception on BitmapText.clone() · Issue #577 · jMonkeyEngine/jmonkeyengine · GitHub
This solves the crashing and I can confirm it was no regression bug. BitmapText.clone()
always did unexpected things.
In my game I use cloning for a drag and drop system in the GUI. As you can see below, changes in the original text (greyed out icon) are reflected by the clone (item anchored to mouse cursor). This is how it always was and I thought it was kinda neat in this particular use case but it’s not what I would expect.
So I did some testing and noticed more weirdness regarding the cloning of BitmapText.
I hope the comments in the different test cases below illustrate the issues.
import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.scene.Node;
public class CloneTest3 extends SimpleApplication {
public CloneTest3() { super(null); }
private BitmapText mkText(String str) {
BitmapFont font = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText text = new BitmapText(font);
text.setText(str);
text.setLocalTranslation(300, 200, 0);
return text;
}
// Works, displays text
private void case1() {
BitmapText text = mkText("case1");
guiNode.attachChild(text);
}
// Displays nothing
private void case2() {
BitmapText orig = mkText("case2");
guiNode.attachChild(orig);
BitmapText copy = orig.clone(); // This makes the text disappear
}
// Cloning works when BitmapText ist nested inside a Node
private void case3() {
BitmapText orig = mkText("case3");
Node node = new Node();
node.attachChild(orig);
guiNode.attachChild(node);
Node nodeCopy = (Node) node.clone();
// Everything still visible
}
// Cloning Node with nested BitmapText doesn't make deep copy?
private void case4() {
BitmapText orig = mkText("case4");
Node node = new Node();
node.attachChild(orig);
guiNode.attachChild(node);
Node nodeCopy = (Node) node.clone();
nodeCopy.move(0, -30, 0);
guiNode.attachChild(nodeCopy);
// Changes displayed text of copy AND of original
BitmapText textCopy = (BitmapText) nodeCopy.getChild(0);
textCopy.setText("new text for copy (case4)");
// The values returned by getText() are as expected
System.out.println("orig text: " + orig.getText()); // Outputs "case4"
System.out.println("copy text: " + textCopy.getText()); // Outputs "new text..."
}
// Setting new text in the original after it has been cloned doesn't work
private void case5() {
BitmapText orig = mkText("case5");
Node node = new Node();
node.attachChild(orig);
guiNode.attachChild(node);
Node nodeCopy = (Node) node.clone();
nodeCopy.move(0, -30, 0);
guiNode.attachChild(nodeCopy);
// Doesn't change displayed text
// This is strange since it works similarly in my game.
// There, changes to orig however are reflected in the cloned BitmapText.
orig.setText("new text for original (case5)");
// Again, the value returned by getText() is as expected
System.out.println("orig text: " + orig.getText()); // Outputs "new text..."
}
public void simpleInitApp() {
case5();
}
public static void main(String[] args) {
CloneTest3 app = new CloneTest3();
app.setShowSettings(false);
app.start();
}
}
I guess something’s still wrong with deep copying a BitmapText. Can’t help any further though since atm I neither understand the Font things nor the Cloner.
Using latest version from master branch (157af24) and Java 1.8.0_102.