Open liquid8d opened 8 years ago
Hey there, I think problem you are running into stems from using a surface, due to what goes on when a surface gets created and resized.
What is happening when you create a surface is that the frontend creates an offscreen texture of the specified size. Think of it as a blank image sitting on your system, with the image having the dimensions used when the surface was created. Subsequently resizing the surface doesn't actually change the size of that image, instead it simply resizes the sprite that is showing the image, in much the same way as the frontend stretches videos and artworks when you resize them (the actual original video/artwork image isn't changed).
I think what you need to try doing instead of resizing the surface is to change it's subimg_width and subimg_height properties so that only part of the part of the surface's texture that you have measured is being shown. There is also subimg_x and subimg_y that you can use to control what portion of the surface texture gets displayed.
Does that help at all?
I see what you are saying, but I'm not sure my confusion is related to the surfaces - just getting the length of the message. The value I get for msg_width appears to be based on the initial width provided to the text object and not the amount of pixels necessary to show the entire message would be - which is what I am looking for.
My thought process in requesting "the length of the message" was to create a scrollable text object. I planned on creating a surface of the desired width (the scrollable area), then create a text object (the scrolling text) on that surface whose width is as long as MSG_WIDTH (which would likely exceed the width of the surface). From there, you could just change the text objects X/Y on the surface to scroll it. Perhaps I'm confused as to what msg_width is actually telling me or how it should work.
If I just add a text object and want the width of the message:
fe.layout.width = 640;
fe.layout.height = 480;
local text = fe.add_text( "[Title]", 0, 0, fe.layout.width, 30 );
fe.add_transition_callback("on_transition");
function on_transition( ttype, var, ttime )
{
switch ( ttype )
{
case Transition.StartLayout:
case Transition.ToNewSelection:
print( "msg_width is: " + text.msg_width + ", title is: " + fe.game_info( Info.Title ) + "\n" );
//text.width = text.msg_width;
break;
}
return false;
}
outputs: msg_width is: 239, title is: The Legend of Zelda
However, if I were to resize the text object to that, it doesn't fit the whole title. In fact, when using ToNewSelection, the text object width decreases each time.
If I change the initial width provided for add_text, 'msg_width' is different.
As a side note, it appears msg_width is not available until after certain Transitions ( similar to texture_width / texture_height) - you just get 0. That should be specified in Layouts.md
hmmm sorry for the delay in responding liquid8d. I'll have to do some figuring out to understand what is going on with this feature.
Basically all that is happening when you call msg_width is the frontend is getting the dimensions of the text from SFML (the width specifically) and applying a scaling factor to to account for the layout coordinate system.
When you try to put too long of a message in a text object it will automatically truncate it, so this function will only return the truncated length (which may be messed up in other ways).
To work around the automatic truncation that Attract-Mode does you could initially create the text object with a massive width, put the text in it, and then measure. But it sounds like that isn't working currently with your note about how the msg_width isn't available until after certain transition... again I'm going to have to look into this when I get a chance
Just checked into this one again...
Trying what you said by creating a super long text object, then set text to msg_width - it seems to work the first time. Subsequent sets cause the msg_width value to decrease each time, so it seems like it is measuring based on the current text width, not how long the text should be.
So here is what I did - instead of using measure on the text objects whose width changed, I kept a hidden super long text and set the visible text based on the msg_width of that. This works, except it is still cutting off some of the title (the rom info/version, sometimes just the ending parenthesis)
local hidden_text = fe.add_text( "[Title]", 0, 0, 3000, 30 );
hidden_text.visible = false;
local text = fe.add_text( "[Title]", 0, 0, 2000, 30 );
fe.add_transition_callback("on_transition");
function on_transition( ttype, var, ttime )
{
switch ( ttype )
{
case Transition.StartLayout:
case Transition.FromOldSelection:
print( "msg_width is: " + hidden_text.msg_width + ", title is: " + fe.game_info( Info.Title ) + "\n" );
if ( hidden_text.msg_width != 0 ) text.width = hidden_text.msg_width;
break;
}
return false;
}
I can't quite make it out from the code, but does the getLocalBounds here change if the width of the text object changes?
Relevant code from tp.cpp:
int FeTextPrimative::getActualWidth()
{
float w = 0;
for ( unsigned int i=0; i < m_texts.size(); i++ )
{
sf::FloatRect textSize = m_texts[i].getLocalBounds();
textSize.width *= m_texts[i].getScale().x;
if ( textSize.width > w )
w = textSize.width;
}
return (int)w;
}
I struggled with this two, experiencing the same thing. I ended up getting accurate results setting the margin to zero. I think this needs to be investigated further.
http://forum.attractmode.org/index.php?topic=2719.msg20073#msg20073
I'm trying to use msg_width to adjust the size of a surface based on the width of the text but it doesn't seem to be measuring the text width properly. Here's example code I am using:
I added pixel.png so you can see the size of the surface. I've tried a number of changes to see what might be causing the issue - I've tried adjusting the text object width to match msg_width but that makes it smaller on each transition. I've tried setting charsize so it isn't trying to autosize, used different magic tokens, etc..
Does msg_width take into account the magic token text, the font, the charsize, text align, etc? Could you give an example of how msg_width should work?