question

Mike Mayer avatar image
0 Likes"
Mike Mayer asked Ben Wilson commented

Right-justify 3D text?

I use 3D "Text" under the "Visual" panel of the Library tab for an easy way to display counters, like showing the content of a queue going up, down, etc. next to the queue itself, stuck to the floor (Z=0) like a sticker. I sometimes use that trick as an alternative to the default floating block of 2D text info below each object, since the 3D text stays where I put it.

I do something like this in the Text Display code:

int numBoxes = Model.find("Box Queue").as(Object).stats.content.value;
string numBoxesStr = string.fromNum(numBoxes);
return numBoxesStr;

My question: By default, 3D text is left-justified. Thus as the number gets larger (from 1's to 10's to 100's to 1000's) it "grows" to the right, being anchored (justified) on the left.

Can 3D text be right-justified, i.e., as the number gets bigger it grows to the left?

I could probably come up with a blank padding trick (increase/decrease the text string front padding spaces on the fly depending on how many digits in the number are being displayed), but I wondered if there was an easier way.

Thanks to all.

FlexSim 21.2.4
right justifytext justification
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Phil BoBo avatar image
1 Like"
Phil BoBo answered Ben Wilson commented

The 3D text draws one character at a time, left-to-right, advancing the position based on the size of the character. There isn't an easy way to make it right-justified right now.

You could draw each character in your string right-to-left though instead of drawing the entire string:

fglTranslate(1, 0, 0);
drawtomodelscale(current);
fglTranslate(0, 0.1, 0.2);
string text = string.fromNum(current.text);
for (int c = text.length; c >= 1; c--) {
    fglTranslate(-0.12, 0, 0);
    drawtext(view, text.charAt(c), 0, 0, 0);
}

1636062562902.png

1636062572703.png

text_right_to_left.fsm


1636062562902.png (693.0 KiB)
1636062572703.png (690.4 KiB)
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Mike Mayer avatar image Mike Mayer commented ·


Thank you Phil - that's pretty clever.

I see one character at a time gives best control over what gets printed and where. This means I could also change the angle of the text reading direction if needed, and perhaps even compound angles in 3D space.

After my original post (and before I saw yours), I came up with a brute-force hack that seems to do the trick, for up to a 5-digit number (I'm not displaying any values greater than 99999).

It's rather limited though, and likely not entirely dependable for different fonts, sizes, etc. The font size I'm using is 1000 (model is in mm), so 1m i.e., the FlexSim default.

int strlen = numBoxesStr.length;
string strPad;
if (strlen == 1) strPad = "        "; // 8 blank spaces.
if (strlen == 2) strPad = "      "; // 6 blank spaces.
if (strlen == 3) strPad = "    "; // 4 blank spaces.
if (strlen == 4) strPad = "  "; // 2 blank spaces.
if (strlen == 5) strPad = ""; // 0 blank spaces.
return strPad + numBoxesStr;

A "switch" statement based on strlen would have been another way to structure the same logic (and probably more efficient).

I also discovered string.padStart, which I played around with too.

Thanks.

Mike Mayer

0 Likes 0 ·
Ben Wilson avatar image Ben Wilson ♦♦ Mike Mayer commented ·

@Mike Mayer,

If you toggle your code with the code button <> it preserves formatting, including multiple spaces, and as a bonus gives you some syntax highlighting as well.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.