Aligning Label with Transform

With 2D Screen coordinates 0,0 bottom left (and so Transform also) I cannot get Label coordinates match with the Transform. I want to put label just above the transform.

Label1.Left:= PlayerTransform.Translation.X;
Label1.Bottom := PlayerTransform.Translation.Y + PlayerTransform.LocalBoundingBox.Max.Y/4;

Puts the label too far to the right and too high.
?

To answer about the exact problem you have, I would have to know exactly your layout, how did you put and anchor Label1 (assuming it is TCastleLabel) and PlayerTransform . As usual, it is better to attach a testcase to reproduce your problem.

But I can answer what I would do to show text above a sprite sheet.


Instead of TCastleLabel, it is easier to use TCastleText as a child of PlayerTransform, then it also automatically “sticks” to the player. This is what I would do, as this works regardless of other things – it will work regardless of how you anchor viewport, how you anchor label, regardless of what is your Camera.Orthographic.Origin etc.

I attach a trivial demo how I would do it. It’s a simple design, and in code I just do

  { Temporarily make Text1.Exists = false, otherwise the Text1 size
    would be included in Scene1.LocalBoundingBox, making a feedback loop
    (as the box size would move the text higher and higher). }
  Text1.Exists := false;
  Text1.Translation := Vector3(0, Scene1.LocalBoundingBox.Max.Y, 0);
  Text1.Exists := true;

in state Update.

Note that I set on TCastleText the VerticalAlignment = vpBottom and Alignment = hpMiddle, to make the task easier. This way the Text1.Translation needs to calculate only the bottom-middle point of the text.

Observe that I can move and scale the Scene1 freely, the code will work OK.

my-new-project-spr-size.zip (1.1 MB)

( P.S. Sorry @Carring for not yet getting attention to your other thread, “Coordinates again”. There’s just a whole lot of work happening lately, around my company Cat-astrophe Games and around Castle Game Engine :slight_smile: Lots of good things, but also lots of work. )

P.S. Note that the text is pixelated at large sizes (this is true for both TCastleLabel and TCastleText).

If this is an issue, you can solve it by creating a TCastleFont, loading there some TTF/OTF file, and increasing OptimalSize to something sufficiently large (like 20, 40, 60). Then assign this font as TCastleText.CustomFont or TCastleLabel.CustomFont. Following news on Font classes upgraded and available in CGE editor – Castle Game Engine . A manual page about it is also coming, along with a movie tutorial :slight_smile:

Thanks. It works fine. It is especially nice because it scales along with my playersprite moving back and forward. :slight_smile:

Cool. I will wait for that. I would like to add a black outline to the castletext font or use a different font instead of the standard one.
I started another topic on using labeltext in “corrupted labeltext.” I uploaded a small video to display the problem I have with it.

Sorry for the delay in replies – I went for a week of vacation, and then a backlog of tasks swamped me :slight_smile: I have the unanswered forum threads on my TODO list, I’ll get to them.

As for the font: Note that you can adjust it already. I.e. the manual page is not yet ready, but the functionality to adjust TCastleText font is fully ready. Just add a TCastleFont (using “Add Non-Visual Component”) somewhere in your design, configure it (choose a font in NewFont.URL, maybe adjust NewFont.OptimalSize), and then assign as TCastleFont.CustomFont value.

The outlines for TCastleText will come in the future. I noted you asked for them already earlier, and there’s a partial implementation now. It waits to be finished, but it’s not much work.

Thanks. I just tried this and it works.
Now if I want this font to be used in code in CastleText, to “attach” to a bunch of NPC Transforms that I create in code, do I need to add DesignedComponent (‘Font1’) as TCastleFont? And how can I then point to “Font1” in the CastleText Transforms?

Clipboard01

To customize the font used by TCastleText, you should assign the font (any TCastleFont instance) as TCastleText.CustomFont value.

This is a general statement – it is valid when setting things up in editor and in code. Both TCastleText and TCastleFont instances can be created in editor or in code, and can be linked (using TCastleText.CustomFont) in editor or in code.

So, yes, if I understand your situation right:

  • you have a Font1 instance you created in the editor

  • you create some new TCastleText instance using the code

… then yes, you can do in your state Start method something like

Font1 := DesignedComponent ('Font1') as TCastleFont;

and then if you create a TCastleText instance using code, you can assign the font there:

MyNewText := TCastleText.Create(...);
MyNewText.CustomFont := Font1;
MyNewText.Caption := 'something';
MyNewText.Translation := ... ;
Viewport.Items.Add(MyNewText);

Remember about the lifetime of objects in Pascal. The Font1 instance exists as long as the state is between Start and Stop.

  • If your MyNewText lifetime is within this (that is, you destroy MyNewText before the state stops) – then good, nothing needs to be done.

  • If you need the MyNewText, and thus Font1, to exist longer – then you need to move Font1 to something that will exist for a longer time. 1 possibility is to create it by code, another possibility is to use a non-visual design for components that should exist always, similar to a “sounds collection” mentioned on Sound | Manual | Castle Game Engine . I want to make it even easier to create such “always existing components” soon, watch our for news about new approach for CastleSettings.