Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with character size.  (Read 3611 times)

0 Members and 1 Guest are viewing this topic.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Problem with character size.
« 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 ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with character size.
« Reply #1 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.
Laurent Gomila - SFML developer

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: Problem with character size.
« Reply #2 on: January 21, 2016, 11:56:12 am »
Isn't it Text::findCharacterPos though? ;)

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Problem with character size.
« Reply #3 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.

« Last Edit: January 21, 2016, 01:57:40 pm by Lolilolight »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with character size.
« Reply #4 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
    }
Laurent Gomila - SFML developer

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Problem with character size.
« Reply #5 on: January 21, 2016, 02:38:32 pm »
Ok, thanks.

 

anything