Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Justification on Buttons, ToggleButtons and Labels #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/SFGUI/Alignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SFGUI_API Alignment : public Bin, public Misc {

private:
void HandleSizeChange() override;
void HandleAlignmentChange( const sf::Vector2f& old_alignment ) override;
void HandleAlignmentChange( const Misc::Alignment& old_alignment ) override;

void UpdateChild();

Expand Down
3 changes: 2 additions & 1 deletion include/SFGUI/Button.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <SFGUI/Bin.hpp>
#include <SFGUI/Misc.hpp>

#include <SFML/System/String.hpp>
#include <memory>
Expand All @@ -11,7 +12,7 @@ class Image;

/** Pushbutton.
*/
class SFGUI_API Button : public Bin {
class SFGUI_API Button : public Bin, public Misc {
public:
typedef std::shared_ptr<Button> Ptr; //!< Shared pointer.
typedef std::shared_ptr<const Button> PtrConst; //!< Shared pointer.
Expand Down
36 changes: 32 additions & 4 deletions include/SFGUI/Misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,49 @@ class SFGUI_API Misc {
*/
virtual ~Misc() = default;

/** Widget Justification
*/
enum class Justify :char {
CENTRE = 0, /*!< No Justification. */
LEFT, /*!< Left justificiation. */
RIGHT /*!< RIght justification. */
};

/** Alignment Structure
* Alignment sf::Vector2f
* Justification char
*/

struct Alignment {
sf::Vector2f position;
Justify justification;
};

/** Set alignment
* @param alignment Alignment (0..1 for x and y).
*/
void SetAlignment( const sf::Vector2f& alignment );
void SetAlignment( const sf::Vector2f position, const Justify justification = Misc::Justify::CENTRE );

/** Get alignment.
* @return Alignment.
*/
const sf::Vector2f& GetAlignment() const;
const Alignment& GetAlignment() const;

/** Equal operator
*@return bool
*/
friend bool operator== ( Alignment &a1, Alignment &a2 );

/** Not equal operator
* @return bool
*/
friend bool operator!= ( Alignment &a1, Alignment &a2 );

protected:
virtual void HandleAlignmentChange( const sf::Vector2f& old_alignment );
virtual void HandleAlignmentChange( const Alignment& old_alignment );

private:
sf::Vector2f m_alignment;
Alignment m_alignment;
};

}
1 change: 1 addition & 0 deletions include/SFGUI/ToggleButton.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <SFGUI/Button.hpp>
#include <SFGUI/Misc.hpp>

#include <memory>

Expand Down
6 changes: 3 additions & 3 deletions src/SFGUI/Alignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void Alignment::HandleSizeChange() {
UpdateChild();
}

void Alignment::HandleAlignmentChange( const sf::Vector2f& /*old_alignment*/ ) {
void Alignment::HandleAlignmentChange( const Misc::Alignment& /*old_alignment*/ ) {
UpdateChild();
}

Expand All @@ -64,8 +64,8 @@ void Alignment::UpdateChild() {
#endif
}

allocation.left = spare_space.x * GetAlignment().x;
allocation.top = spare_space.y * GetAlignment().y;
allocation.left = spare_space.x * GetAlignment().position.x;
allocation.top = spare_space.y * GetAlignment().position.y;
allocation.width -= spare_space.x;
allocation.height -= spare_space.y;

Expand Down
1 change: 1 addition & 0 deletions src/SFGUI/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace sfg {
Button::Ptr Button::Create( const sf::String& label ) {
auto ptr = Ptr( new Button );
ptr->SetLabel( label );
ptr->SetAlignment( sf::Vector2f(0.5f, 0.5f), Misc::Justify::CENTRE );
return ptr;
}

Expand Down
38 changes: 28 additions & 10 deletions src/SFGUI/Engines/BREW/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,40 @@ std::unique_ptr<RenderQueue> BREW::CreateButtonDrawable( std::shared_ptr<const B
sf::Text text( button->GetLabel(), *font, font_size );
auto offset = ( button->GetState() == Button::State::ACTIVE ) ? border_width : 0.f;
sfg::Widget::PtrConst child( button->GetChild() );
Misc::Alignment nAlignment = button->GetAlignment();
sf::Vector2f nPosition;
nPosition.y = button->GetAllocation().height * nAlignment.position.y - metrics.y / 2.f + offset;

if( !child ) {
text.setPosition(
button->GetAllocation().width / 2.f - metrics.x / 2.f + offset,
button->GetAllocation().height / 2.f - metrics.y / 2.f + offset
);
nPosition.x = button->GetAllocation().width * nAlignment.position.x;
switch ( nAlignment.justification ) {
case Misc::Justify::LEFT:
nPosition.x += offset;
break;
case Misc::Justify::RIGHT:
nPosition.x -= metrics.x + offset;
break;
default:
nPosition.x -= metrics.x / 2.f + offset;
break;
}
}
else {
float width( button->GetAllocation().width - spacing - child->GetAllocation().width );

text.setPosition(
child->GetAllocation().width + spacing + (width / 2.f - metrics.x / 2.f) + offset,
button->GetAllocation().height / 2.f - metrics.y / 2.f + offset
);
nPosition.x = child->GetAllocation().width + spacing + width * nAlignment.position.x;
switch ( nAlignment.justification ) {
case Misc::Justify::LEFT:
nPosition.x += offset;
break;
case Misc::Justify::RIGHT:
nPosition.x -= metrics.x + offset;
break;
default:
nPosition.x -= metrics.x / 2.f - offset;
break;
}
}

text.setPosition( nPosition.x, nPosition.y );
text.setColor( color );
queue->Add( Renderer::Get().CreateText( text ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/SFGUI/Engines/BREW/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::unique_ptr<RenderQueue> BREW::CreateFrameDrawable( std::shared_ptr<const Fr
auto label_start_x = line_height;
auto label_end_x = line_height;

auto alignment = frame->GetAlignment().x;
auto alignment = frame->GetAlignment().position.x;

if( frame->GetLabel().getSize() > 0 ) {
auto metrics = GetTextStringMetrics( frame->GetLabel(), *font, font_size );
Expand Down
4 changes: 2 additions & 2 deletions src/SFGUI/Engines/BREW/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ std::unique_ptr<RenderQueue> BREW::CreateImageDrawable( std::shared_ptr<const Im
queue->Add(
Renderer::Get().CreateSprite(
sf::FloatRect(
( image->GetAllocation().width - image->GetRequisition().x ) * image->GetAlignment().x,
( image->GetAllocation().height - image->GetRequisition().y ) * image->GetAlignment().y,
( image->GetAllocation().width - image->GetRequisition().x ) * image->GetAlignment().position.x,
( image->GetAllocation().height - image->GetRequisition().y ) * image->GetAlignment().position.y,
static_cast<float>( image->GetImage().getSize().x ),
static_cast<float>( image->GetImage().getSize().y )
),
Expand Down
23 changes: 19 additions & 4 deletions src/SFGUI/Engines/BREW/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,25 @@ std::unique_ptr<RenderQueue> BREW::CreateLabelDrawable( std::shared_ptr<const La

if( !label->GetLineWrap() ) {
// Calculate alignment when word wrap is disabled.
sf::Vector2f avail_space( label->GetAllocation().width - label->GetRequisition().x, label->GetAllocation().height - label->GetRequisition().y );
sf::Vector2f position( avail_space.x * label->GetAlignment().x, avail_space.y * label->GetAlignment().y );

vis_label.setPosition( position.x, position.y );
auto metrics = GetTextStringMetrics( label->GetWrappedText(), *font, font_size );
metrics.y = GetFontLineHeight( *font, font_size );

Misc::Alignment nAlignment = label->GetAlignment();
sf::Vector2f nPosition;
nPosition.x = label->GetAllocation().width * nAlignment.position.x;
nPosition.y = label->GetAllocation().height * nAlignment.position.y - metrics.y / 2.f;

switch ( nAlignment.justification ) {
case Misc::Justify::LEFT:
break;
case Misc::Justify::RIGHT:
nPosition.x -= metrics.x;
break;
default:
nPosition.x -= metrics.x / 2.f;
break;
}
vis_label.setPosition( nPosition.x, nPosition.y );
}

queue->Add( Renderer::Get().CreateText( vis_label ) );
Expand Down
21 changes: 17 additions & 4 deletions src/SFGUI/Engines/BREW/ToggleButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,24 @@ std::unique_ptr<RenderQueue> BREW::CreateToggleButtonDrawable( std::shared_ptr<c
sf::Text text( button->GetLabel(), *font, font_size );
auto offset = ( ( button->GetState() == Button::State::ACTIVE ) || button->IsActive() ) ? border_width : 0.f;

text.setPosition(
button->GetAllocation().width / 2.f - metrics.x / 2.f + offset,
button->GetAllocation().height / 2.f - metrics.y / 2.f + offset
);
Misc::Alignment nAlignment = button->GetAlignment();
sf::Vector2f nPosition;
nPosition.x = button->GetAllocation().width * nAlignment.position.x;
nPosition.y = button->GetAllocation().height * nAlignment.position.y - metrics.y / 2.f + offset;

switch ( nAlignment.justification ) {
case Misc::Justify::LEFT:
nPosition.x += offset;
break;
case Misc::Justify::RIGHT:
nPosition.x -= metrics.x + offset;
break;
default:
nPosition.x -= metrics.x / 2.f + offset;
break;
}

text.setPosition( nPosition.x, nPosition.y );
text.setColor( color );
queue->Add( Renderer::Get().CreateText( text ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/SFGUI/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace sfg {

Image::Image( const sf::Image& image )
{
SetAlignment( sf::Vector2f( .5f, .5f ) );
SetAlignment( sf::Vector2f( .5f, .5f ), Misc::Justify::CENTRE );
SetImage( image );
}

Expand Down
3 changes: 2 additions & 1 deletion src/SFGUI/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ Label::Label( const sf::String& text ) :
m_text( text ),
m_wrap( false )
{
SetAlignment( sf::Vector2f( .5f, .5f ) );
SetAlignment( sf::Vector2f( .5f, .5f ), Misc::Justify::CENTRE);
Invalidate();
}

Label::Ptr Label::Create( const sf::String& text ) {
Ptr label( new Label( text ) );
label->RequestResize();
label->SetAlignment( sf::Vector2f( 0.5f, 0.5f ), Misc::Justify::CENTRE );
return label;
}

Expand Down
31 changes: 22 additions & 9 deletions src/SFGUI/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@

namespace sfg {

void Misc::SetAlignment( const sf::Vector2f& alignment ) {
sf::Vector2f old_alignment( m_alignment );
bool operator==( Misc::Alignment& a1, Misc::Alignment& a2 ) {
return ( a1.position.x == a2.position.x &&
a1.position.y == a2.position.y &&
a1.justification == a2.justification );
}

bool operator!=( Misc::Alignment& a1, Misc::Alignment& a2 ) {
return ( a1.position.x != a2.position.x &&
a1.position.y != a2.position.y &&
a1.justification != a2.justification );
}

void Misc::SetAlignment( const sf::Vector2f position, const Misc::Justify justification ) {
Misc::Alignment old_alignment( m_alignment );

m_alignment.x = std::max( 0.f, std::min( 1.f, alignment.x ) );
m_alignment.y = std::max( 0.f, std::min( 1.f, alignment.y ) );
m_alignment.position.x = std::max( 0.f, std::min( 1.f, position.x ) );
m_alignment.position.y = std::max( 0.f, std::min( 1.f, position.y ) );
m_alignment.justification = justification;

if( old_alignment != m_alignment ) {
HandleAlignmentChange( old_alignment );
if ( old_alignment != m_alignment ) {
HandleAlignmentChange( old_alignment );
}
}
}

const sf::Vector2f& Misc::GetAlignment() const {
const Misc::Alignment& Misc::GetAlignment() const {
return m_alignment;
}

void Misc::HandleAlignmentChange( const sf::Vector2f& /*old_alignment*/ ) {
void Misc::HandleAlignmentChange( const Misc::Alignment& /*old_alignment*/ ) {
}

}
1 change: 1 addition & 0 deletions src/SFGUI/ToggleButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ToggleButton::ToggleButton() :
ToggleButton::Ptr ToggleButton::Create( const sf::String& label ) {
Ptr button( new ToggleButton );
button->SetLabel( label );
button->SetAlignment( sf::Vector2f( 0.5f, 0.5f ), Misc::Justify::CENTRE );
return button;
}

Expand Down