SFML community forums
Help => General => Topic started by: buggy123 on November 19, 2009, 07:32:42 am
-
I'm trying to make "selectable text" meaning that when the mouse hovers over the words, it gets highlighted and stuff. However, the text is changing in length. How do I do this?
Is a way to figure out how much pixels each letter is or something?
-
You can calculate the total size of the string with the GetRect() function.
You can also get the 2D position of a character inside the text with the GetCharacterPos() function.
-
I very recently did this same thing.
Here is my relevant code, which converts a mouse position to the caret position. Note the averaging of the last value, this is so the caret snaps to the nearest gap.
mString is a pointer to my string.
size_t TextBox::GetCharacter(float x) const
{
size_t caret = 0;
float lastPos = 0.0f;
const std::string s = mString->GetText();
while (caret < s.length())
{
const float curPos = mString->GetCharacterPos(caret + 1).x + mString->GetPosition().x;
if ((lastPos + curPos) / 2.0f > x)
break;
++caret;
lastPos = curPos;
}
return caret;
}
Knowing where to draw the caret is just a call to mString->GetCharacterPos(caret).
-
Thanks for the answers.
So anyway, I tried to create basic class that would let me display text.
class TextEngine{
public:
sf::Font Font;
std::string GetFont();
int CreateFont(std::string FileDir);
sf::String CreateText(sf::String Text, std::string String, float Size, float X, float Y);
int DisplayText(sf::RenderWindow Window, sf::String FileToDisplay);
};
std::string TextEngine::GetFont(){
std::string FileName;
lua_getglobal(Menu,"Font");
FileName = (std::string)lua_tostring(Menu, -1);
lua_pop(Menu, 1);
return FileName;
};
int TextEngine::CreateFont(std::string FileDir)
{
Font.LoadFromFile(FileDir);
return 0;
}
sf::String TextEngine::CreateText(sf::String Text, std::string String, float Size, float X, float Y){
Text.SetText(String);
Text.SetFont(Font);
Text.SetSize(Size);
Text.SetX(X);
Text.SetY(Y);
return Text;
};
int TextEngine::DisplayText(sf::RenderWindow Window, sf::String FileToDisplay){
Window.Draw(FileToDisplay);
return 0;
};
but whenever I try to use it, it gives me compiler errors that refers me to this "NonCopyable.hpp" page.
Am I accessing a private member? How do I fix this?
-
In your last function :
int TextEngine::DisplayText(sf::RenderWindow Window, sf::String FileToDisplay){
Window.Draw(FileToDisplay);
return 0;
};
You try to get a copy of sf::RenderWindow. The fact is sf::RenderWindow has sf::NonCopyable class as base Class. That means you can't copy an instance of sf::RenderWindow. You should try this:
int TextEngine::DisplayText(sf::RenderWindow& Window, sf::String FileToDisplay){
Window.Draw(FileToDisplay);
return 0;
};
-
thanks for the answer!
Another problem I've run into with fonts is that when I set the size high (like 100) the text becomes blurry. I use a .ttf font.
Is there font types that uses vector graphics rather then raster graphics? can I load/use them with SFML?
Or is there any solution to increase the quality? (I'm quite fond of the font I'm using)
-
If the size of the font is the same as the size of the string then the quality should be the best.
-
oh, I get it. What if I just set the Font(not string) size really high? will it take up more resources and slow the program down?
-
It will just take more video memory.