TankOs / SFGUI

Simple and Fast Graphical User Interface
zlib License
429 stars 61 forks source link

Label alignment not centred when setting a requisition on the label #27

Open Jamie-Booth opened 8 years ago

Jamie-Booth commented 8 years ago

When setting a fixed width for a label using SetRequisition, the label will not be centered, but will be left justified.

Brew code snippet from Label.cpp:

22  if( !label->GetLineWrap() ) { 
23      // Calculate alignment when word wrap is disabled. 
24      sf::Vector2f avail_space( label->GetAllocation().width - label->GetRequisition().x, label->GetAllocation().height - label->GetRequisition().y ); 
25      sf::Vector2f position( avail_space.x * label->GetAlignment().x, avail_space.y * label->GetAlignment().y ); 
26 
27      vis_label.setPosition( position.x, position.y ); 
28  } 

This issue is I believe is line 24 where the GetRequisition.x is the same size as GetAllocation.width, as your available space will be 0. It doesn't matter what Alignment you assign, it will always be 0 if this is the case (Left Justified).

Sample code snippet, I have modified the alignment of 'File' to be the far right of the label for demonstration.

        m_boSingleFileHeader = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL, 0.0f);
        m_lSingleName = sfg::Label::Create("File");
        m_lSingleName->SetRequisition(sf::Vector2f(633.0f, 0.0f));
        m_lSingleName->SetAlignment(sf::Vector2f(1.0f, 0.5f));
        m_lSingleDate = sfg::Label::Create("Date");
        m_lSingleDate->SetRequisition(sf::Vector2f(180.0f, 0.0f));
        m_lSingleDate->SetAlignment(sf::Vector2f(0.1f, 0.5f));
        m_boSingleFileHeader->Pack(m_lSingleName, false, false);
        m_boSingleFileHeader->Pack(m_lSingleDate, false, false);

Please see the position of 'File' in the image below, it should be at the far right hand side of the 633.0f label, but instead it is at the far left. lablebug

I'll do some playing around to see if I can fix it myself.

Jamie-Booth commented 8 years ago

I have a fix for the alignment, though I've bundled it in with the ability to set text justification in the Misc class. I have also added the Misc class via inheritance to the button and togglebutton classes. This provides left, centre and right justification. If you are interested in this at all, let me know and I'll send a pull request so you can review the code.

binary1248 commented 8 years ago

Send a pull request. :wink:

I'll review it and provide feedback. We tend to like individual pull requests for each feature, but if you don't feel like splitting them up, I can edit your commits myself.