Page 1 of 1

CEGUI::TextItem problems

Posted: Fri Feb 23, 2007 18:38
by davesh
I'm creating a GUI programatically. In general I'm able to create and place widgets where I want them. I am having difficulties placing CEGUI::TextItems.

Assuming that I have just created the TextItem and have a pointer to it textItem, then I immediately set its text using setText:

Code: Select all

textItem->setText("Hi");

I subsequently try to get the size (in pixels) of this text using

Code: Select all

int width = textItem->getWidth();

The width shows up as 0, in spite of the fact that the text is not zero length. I have tried various things to force the textItem to compute its width, but this does not seem to work. What is the proper way to initialize the text item (so that it has the appropriate width/height? Do I have to call textItem->setWidth(), if so what mechanism is in place for me to compute the width given the text string "Hi"?

I'm using CEGUI with Ogre. I believe it is version 0.4.1 of CEGUI.

Posted: Sat Feb 24, 2007 19:47
by scriptkid
Hi,

TextItems are not meant to be placed directly; they should be passed to lists (combobox, listbox and multicolumnlist). The reason they don't return a correct value is probably because their parents (the aformentioned lists) calculate & mange their sizes.

If you want a bit of static text, use the StaticText widget; if you want an editable text, use the Editbox.

To check which CEGUI comes with your Ogre version, have a look at the CEGUI.log file, and see below this line: "CEGUI::System singleton created".

If it reads 0.5, there is not StaticText, but rather a DefaultWindow to use. After that everything works as you were used to.

[EDIT] Yes their should be a way to ask for the width of a string, but i forgot the method's name. But maybe my post is already useful.[/EDIT]

Good luck! :-)

Posted: Mon Feb 26, 2007 15:10
by davesh
Thanks for the response. Yup your response was indeed useful. Its good to know that TextItems are not typically created programmatically.

I do still need to know, however, how to get the width of the string. We need to be able to get width and height in order to manage the placement of the string within our control panel.

Posted: Tue Feb 27, 2007 08:20
by scriptkid
davesh wrote:I do still need to know, however, how to get the width of the string. We need to be able to get width and height in order to manage the placement of the string within our control panel.


Hi, i found it. Have a look at the Font::getFontHeight and Font::getTextExtent methods. Using values returned from those methods allows you to set a widget size using UDim values. Because both methods return an absolute (pixels) value, you should use 0 for the scale and the returned values for the offset. For example:

Code: Select all

button->setWidth(UVector2(0, textWidth));
button->setHeight(UVector2(0, fontHeight));


(not tested though, but should work)

HTH :-)