BitMapText To Geom

Hi i wanted to know if there is some way to make your text BitmapText to be allways at a center of a geometry , lets say a cube(1,1,1) .
When i try to do some thing similar its allways moving to the right or to the left , im not posting my code couse its totally failed , but idea would be like

BitmapText = bT
bT.setText(string)
Box box = new Box(1,1,1)
box.attachChild(bT)
bt.setLocalTranslation(0,0,0)

but this way its never centered at center of box

Node n = new Node();
Geometry boxGeom = ...
BitmapText text = ...
boxGeom.center();
text.center();
n.attachChild(boxGeom);
n.attachChild(text);

this way only first letter is in the center all others are going to right, what i would like to obtain is to put a center of text at center of box
your way i just make string start from center

I’m not on my pc so I can’t get all fancy, but get the bounding volume of the label and convert it into a BoundingBox. That will give you the half-length on the x axis, so shift the label left that amount.

If center is not doing that already then the bounding volume is not correct.

BitmapText is super weird… so it may be delaying calculating that stuff or something.

You can also set the text alignment to center. Which may or may not require setting a text rectangle.

Here is some thing i got to work for me , that might help some one :

   public TemporallyLabel (int lifeTime,ArrayList<String> strings,AssetManager am,AppSettings 
      settings,NoblesOriginsExe exe) {
    
    this.node = exe.getUniversalKeyNodeToClone().clone(true);

     
    node.setLocalTranslation(settings.getWidth() / 2,
    settings.getHeight() / 2, 0);//middle of screen
    node.setLocalScale(settings.getWidth() / 8,
    settings.getHeight() / 4,    0.5f);
    
    this.lifeTime = lifeTime;
    
    //BitmapFont myFont = am.loadFont("Interface/Fonts/Default.fnt");
    int line=1;
    
    float y=0;
    for (String s : strings) {
    BitmapText myText= exe.getTextToClone().clone();
    
    myText.setText(s);  
    
            //new BitmapText(myFont,false);
    myText.setSize(1.f);// lets say max rows are 15
    
    
    float divX=1;
    if(s.length()>1){
        divX=((float)s.length())/2;
    }
    float divY=1;
    if(strings.size()>1){
        divY=((float)strings.size())/2+0.5f;
    }
    
    //max total scale   max scale is scale / all strings
    float maxScale=0.7f /strings.size();
    
    if(0.7f/divX>maxScale){
        myText.setLocalScale(maxScale);
    }else{
        myText.setLocalScale(0.7f/divX);
    }
    
    //scale 0.7f
     //        -0.24f   for letter to X ,  0.44f   for row to Y,0.5f


    if(line==1){
        y=myText.getLocalScale().getY()/0.7f*0.44f*(divY);
    }
    
    
    myText.setLocalTranslation(-(myText.getLocalScale().getY()/0.7f*0.24f*s.length()),
                    y ,// *(line-1) -((myText.getLocalScale().getY()/0.7f*0.44f))
                     0.5f);
    
    y-=(myText.getLocalScale().getY()/0.7f*0.44f);
    
    
    line++;
    node.attachChild(myText);
    
    node.addLight(new AmbientLight(ColorRGBA.White.mult(0.5f)));
    
    }
    }

myText.setLocalTranslation(-(myText.getLocalScale().getY()/0.7f0.24fs.length()),
y ,// (line-1) -((myText.getLocalScale().getY()/0.7f0.44f))
0.5f);

this line is crazy, unexpected, random value one that anyway dont solve anything trully, it just work for single situation.

idk why you dont even use “aligment”(txt.setAlignment() and txt.setVerticalAlignment()) that do center for you. Here is some code that you can see use aligment for BitmapText class. But to use it you have to set proper Box for your BitmapText where it will be centered. If you were doing anything in HTML, its similar to aligment in there.

 txt.setBox(new Rectangle(0, 0, getTransform().getWidth(), getTransform().getHeight())); 
    txt.setLocalTranslation(0, getTransform().getHeight(), 0);
    if (containsAttribute("align")) {
        aligment = getAttributePrimitiveCasted("align", String.class);
    }
    if (containsAttribute("valign")) {
        valigment = getAttributePrimitiveCasted("valign", String.class);
    }
    if (aligment.equals("center")) {
        txt.setAlignment(BitmapFont.Align.Center);
    } else if(aligment.equals("right")) {
        txt.setAlignment(BitmapFont.Align.Right);
    } else {
        txt.setAlignment(BitmapFont.Align.Left);
    }
    if (valigment.equals("center")) {
        txt.setVerticalAlignment(BitmapFont.VAlign.Center);
    } else if(valigment.equals("top")) {
        txt.setVerticalAlignment(BitmapFont.VAlign.Top);
    } else {
        txt.setVerticalAlignment(BitmapFont.VAlign.Bottom);
    }

for me its just:

"<text align="center" valign="center">something</text>"

done, similar to Nifty GUI.

Also should work:

import com.simsilica.lemur.Label;

Label myText = new Label("Some text");
myText.center();

Well it did not , it go all to the right

If you were replying to me, there is some slight more things you have to do for the label to display properly… but should essentially be that.

Else you would have to center() it after you attached it to the scene.

Its not crazy , all you need to know is scale of a letter and an X and Y values you need to give it to put it at center , than you just apply proportion, i have allready tested it with different sizes and lenghts , soo its good for multiple lines as well , while i have not tested your code yet, all i’ve found and read here(exept what you gave) was tested and failed .

well thats is to try , may be the fact i added it after center ,was the thing to breack every thing,
Oh and i did not tested lemurs label , i’ve seen only answer on BitmapText , but will try to add it first as well .
Tried to center BitmapText after attaching it , did not worked as well for lenght :frowning: sad
Will give a look to lemurs label

If you want Lemur’s label to center before you add it then you can call
label.setSize(label.getPreferredSize());

…for components not in a layout, it will do that automatically when attached to the real rendered scene. Which is why center() after adding it would work, too.

1 Like