SFML community forums

Help => Graphics => Topic started by: Lolilolight on January 21, 2016, 11:27:49 am

Title: Problem with character size.
Post by: Lolilolight on January 21, 2016, 11:27:49 am
Hi!

I've a problem with the character size :

I want to make a text area, so, each time I enter text, I advance the cursor like this :

cursorPos.x += text.getCharacterSize();
 

The problem is that the cursor advance faster than the glyph.

So I don't think that the getCharacterSize function member is returning the right character size.

I've also difficulties to adapt text size to the size of my labels...

Is that method returning the size of one character ?
Title: Re: Problem with character size.
Post by: Laurent on January 21, 2016, 11:51:30 am
It's the character height. Character width is of course specific to each character (except with monospace fonts) and you must use Text::getCharacterPos to position the cursor accurately.
Title: Re: Problem with character size.
Post by: SpeCter on January 21, 2016, 11:56:12 am
Isn't it Text::findCharacterPos though? ;)
Title: Re: Problem with character size.
Post by: Lolilolight on January 21, 2016, 01:52:50 pm
Yes but I would like to be able to insert text in the text field cause I want to edit scripts into my editor.

So I've to set the same width for each character and when I click on text I want to set the cursor at the right position to insert text.

How can I do that ?

Isn't there any way to force character's width ?

Here's what I want to do :

unsigned int pos = text.findCharacterPos(mousePos);
text.insert(pos, caracter);
 

Thanks.

Title: Re: Problem with character size.
Post by: Laurent on January 21, 2016, 02:23:06 pm
So you want to do the reverse process (finding the character given a pixel position). This can be done easily:

// pseudocode
for (i in [0 .. size])
    if (text.findCharacterPos(i).x > mouse.x)
    {
        text.insert(i, ...)
        break
    }
Title: Re: Problem with character size.
Post by: Lolilolight on January 21, 2016, 02:38:32 pm
Ok, thanks.