[Solved] How to set relative font space width Topic is solved

Problems building or running the engine, queries about how to use features etc.
Post Reply
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

[Solved] How to set relative font space width

Post by Oogst »

Ogre Version: 1.12.11
Operating System: Windows 10
Render System: DX11

As I'm transitioning from ogre 1.9 to 1.12 I've found that TextAreaOverlayElements don't show spaces anymore unless space_width is set. However, space_width is a rather impractical setting because it's relative to the screen instead of relative to the font size. As is described in the documentation: "Sets the width of a space in relation to the screen." That means that if I use the same font at different sizes, the spaces won't scale with the font size. That's a surprisingly odd design choice and I've never seen any tool do that.

To work around this, I've now implemented a static helper function that sets the width of a space whenever I set the text, based on font size:

Code: Select all

void Tools::setCaption(Ogre::OverlayElement* overlayElement, const std::string& text)
{
	if (overlayElement->getTypeName() == "TextArea")
	{
		auto textArea = static_cast<Ogre::TextAreaOverlayElement*>(overlayElement);
		textArea->setSpaceWidth(0.3f) * textArea->getCharHeight());
	}
	overlayElement->setCaption(text);
}
That works, but that's a pretty ugly workaround and wasn't needed in Ogre 1.9.

So I was wondering: does Ogre have a feature to set the width of a space relative to fontsize instead of relative to the screen?
Last edited by Oogst on Wed Apr 14, 2021 7:58 pm, edited 1 time in total.
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: How to set relative font space width

Post by paroj »

that should be the default behaviour, see:
https://github.com/OGRECave/ogre/blob/a ... #L199-L203

if this does not work, this might be a bug
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

Re: How to set relative font space width

Post by Oogst »

I've investigated a bit further based on what you said and it's indeed a bug, with a clear repro case: it only fails when an overlay_element template is used. So it seems that in copying data from the template, Ogre fails to recognise that space_width hasn't been set, and assumes it was explicitely set to 0 instead. In this code the overlay_element "TestBroken" doesn't get spaces, while TestFine shows the spaces as expected:

Code: Select all

overlay_element template/MainText TextArea
{
    font_name MenuFont
    char_height 0.05
    colour_top 0 0 0
    colour_bottom 0 0 0
    alignment center
}

overlay CGMainMenu
{
    zorder 100
    container CGMainMenu_Panel Panel
    {
        metrics_mode relative
        left 0
        top 0
        width 1
        height 1

        overlay_element TestBroken TextArea: template/MainText
        {
            metrics_mode relative
            left 0.5
            top 0.76
            caption banana pineapple pear
        }

        overlay_element TestFine TextArea
        {
            metrics_mode relative
            left 0.5
            top 0.7
            char_height 0.05
            caption banana pineapple pear
            font_name MenuFont
            colour_top 0 0 0
            colour_bottom 0 0 0
            alignment center
        }
    }
}
Oh, by the way: I had decided I'd back your Patreon if I managed to get everything I need running in Ogre 1.12. I've reached that point now, so I've backed your Patreon. Thanks for your work on the Ogre engine and support! Image
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
paroj
OGRE Team Member
OGRE Team Member
Posts: 1993
Joined: Sun Mar 30, 2014 2:51 pm
x 1073
Contact:

Re: How to set relative font space width

Post by paroj »

Oogst wrote: Wed Apr 14, 2021 8:58 am Oh, by the way: I had decided I'd back your Patreon if I managed to get everything I need running in Ogre 1.12. I've reached that point now, so I've backed your Patreon. Thanks for your work on the Ogre engine and support! Image
greatly appreciated!

does this change fix your issue?
https://github.com/OGRECave/ogre/pull/1972
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

Re: How to set relative font space width

Post by Oogst »

Yes, that fixes it! Thanks!
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
Post Reply