How to get pixel ratio of text label

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

Oam
Not too shy to talk
Not too shy to talk
Posts: 23
Joined: Thu Nov 13, 2014 07:01

How to get pixel ratio of text label

Postby Oam » Tue Oct 27, 2015 02:05

Hello,

I'm currently trying to get the correct corresponding pixel ratio of my text label for placing within my root messenger window area.

(window)
------------------------------------------------
[Text2] => h2
[Text1] => h1
[Text0] => h0 -> first text inserted
...
-----------------------------------------------

This is what I'm trying to do currently:

Code: Select all

void InGameNetMsgSheet::AddMsg(const std::string& msg)
{
  CEGUI::WindowManager& winManager = CEGUI::WindowManager::getSingleton();
  CEGUI::Window* label = winManager.createWindow("TaharezLook/Label", "");
  label->setText(msg);
  label->setProperty("HorzFormatting", "WordWrapLeftAligned");
  float hScale = (label->getRenderedString().getHorizontalExtent(sheetRoot) / sheetRoot->getPixelSize().d_height);
  label->setArea(CEGUI::UDim(0.f, 0.f), CEGUI::UDim(.035f, 0.f), CEGUI::UDim(.2f, 0.f), CEGUI::UDim(hScale, 0.f));
  label->setHorizontalAlignment(CEGUI::HorizontalAlignment::HA_LEFT);
  sheetRoot->addChild(label);

  msgs.push_front(MessageInfo(hScale, label));

  // we need to displace the following messages
  MessageInfo* prevMsg = &msgs.front();
  for (auto it = std::next(msgs.begin(), 1); it != msgs.end(); ++it)
  {
    MessageInfo& msgInfo = *it;
    msgInfo.msgLabel->setArea(CEGUI::UDim(0.f, 0.f), CEGUI::UDim(prevMsg->msgLabel->getArea().getPosition().d_y.d_scale + msgInfo.hScale, 0.f), CEGUI::UDim(.2f, 0.f), CEGUI::UDim(msgInfo.hScale, 0.f));
    prevMsg = &msgInfo;
  }
}

I'm using the label->getRenderedString().getHorizontalExtent(sheetRoot) function for trying to obtain the pixel size of my Label relative to the sheetRoot. Then I divide by the pixelSize.d_height of the sheetRoot to obtain a ratio between the two. Is there a step I'm missing to get this setup right? Right now it is displacing way too far down the screen instead of one after another. I found if I multiply the hScale by .1f I get close to the values I want, but it still isn't correct if the HorizontalExtents differ from line to line.

YaronCT
CEGUI Team
Posts: 448
Joined: Fri Jun 19, 2015 12:18
Location: Kiryat-Bialik, Israel

Re: How to get pixel ratio of text label

Postby YaronCT » Tue Oct 27, 2015 09:54

Oam: I can't figure out what u want to do. What do u mean by "pixel ratio"? is it the aspect ratio - that is, the ratio between the width and height?

Oam
Not too shy to talk
Not too shy to talk
Posts: 23
Joined: Thu Nov 13, 2014 07:01

Re: How to get pixel ratio of text label

Postby Oam » Tue Oct 27, 2015 10:16

What I'm wanting to do is to obtain either the d_scale or d_offset for the pixel height for each of my particular Labels. Then after I obtain those values I'll have to displace them a long the y-axis to allow each label to append after it's previous label.

I'm using a WordWrapLeftAligned style, I'm having trouble obtaining it's correct Label text height in pixels / units for the label. I've been trying to use: label->getRenderedString().getHorizontalExtent(sheetRoot) to obtain the horizontal extents relative to the sheetRoot, but doing so gives me a value of like 326 or so for just a single line of text, where I'm more so looking for something like 26 pixels per line. When I was using the function to get the number of lines it doesn't seem to give me more than 1 line, I'm thinking it's because I'm using a generic label instead of a text area or something else.

Are there other ways to obtain the correct pixel height of the rendered label w/ respect to word wrapping?

YaronCT
CEGUI Team
Posts: 448
Joined: Fri Jun 19, 2015 12:18
Location: Kiryat-Bialik, Israel

Re: How to get pixel ratio of text label

Postby YaronCT » Tue Oct 27, 2015 18:12

Ok. currently there isn't a way to do that. As a matter of fact I now work on a project to make it possible for widgets to adjust their size according to their content's size (in the case of a label - the text's size). The project isn't ready yet. but I've made a small patch to make it possible to do what (I think) u wanna do.

Plz take my project from here (branch "label_size") by running:

Code: Select all

hg clone https://bitbucket.org/yaronct/cegui -b label_size


(You'll have to have Mercurial installed). Then u can get the height of the label's text this way:

Code: Select all

CEGUI::WindowManager& winManager = CEGUI::WindowManager::getSingleton();
CEGUI::Window* label = winManager.createWindow("TaharezLook/Label", "");

...

label->setArea( ... )

...

cout << "Text height is " << CEGUI::WidgetLookManager::getSingleton().getWidgetLook("TaharezLook/Label").getImagerySection("Label").getTextComponentIterator().getCurrentValue().getVerticalTextExtent(*label);


Note, however, that with word wrapping, this will work only after setting the label's size. That's because with word wrapping, the height of the text depends on the width of the text! So first set the label's area (only the width matters, the position and height don't matter for now), then compute the text's height, and then set the label's area again, using your new info.

I'll also submit this patch to be included in the next 0.8.5 version.

Oam
Not too shy to talk
Not too shy to talk
Posts: 23
Joined: Thu Nov 13, 2014 07:01

Re: How to get pixel ratio of text label

Postby Oam » Wed Oct 28, 2015 05:51

Thank you yaronct, I'll make sure to take a look at your project. I appreciate it very much.


Return to “Help”

Who is online

Users browsing this forum: No registered users and 26 guests