Skip to content

Commit

Permalink
Add textbook functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
austinmhorn authored Jul 2, 2023
1 parent f025435 commit 11d8cc6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/UI/Interface/Elements/Textbox/Textbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ Textbox::Textbox()
m_text.setFillColor(sf::Color::Green);

positionText();
}

m_description.text.setFont(m_font);
m_description.text.setCharacterSize(30);
};

void Textbox::setSize(const sf::Vector2f& size)
{
Expand All @@ -47,6 +50,29 @@ void Textbox::setOutlineColor(sf::Color color)
{
m_rect.setOutlineColor(color);
}
void Textbox::setTextFillColor(sf::Color color)
{
m_text.setFillColor(color);
}
void Textbox::setDescriptionString(const std::string& string)
{
m_description.text.setString(string);
positionDescription();
}
void Textbox::setDescriptionSide(Textbox::Side side)
{
m_description.side = side;
positionDescription();
}
void Textbox::setDescriptionCharacterSize(unsigned int size)
{
m_description.text.setCharacterSize(size);
positionDescription();
}
void Textbox::setDescriptionFillColor(sf::Color color)
{
m_description.text.setFillColor(color);
}
const sf::Vector2f& Textbox::getSize() const
{
return m_rect.getSize();
Expand All @@ -67,6 +93,10 @@ const sf::Color& Textbox::getOutlineColor() const
{
return m_rect.getOutlineColor();
}
const std::string& Textbox::getInputString() const
{
return m_input;
}
const bool Textbox::contains(const sf::Vector2f point) const
{
return m_rect.getGlobalBounds().contains(point);
Expand Down Expand Up @@ -151,9 +181,32 @@ void Textbox::positionText()
m_text.setPosition(center_point.x - (text_bounds.x/2.f),
center_point.y - (m_text.getCharacterSize()/2.f));
}

void Textbox::positionDescription()
{
const sf::FloatRect textRect = m_description.text.getGlobalBounds();
const sf::Vector2f boundsCenter = {m_rect.getGlobalBounds().left + m_rect.getGlobalBounds().width/2.f,
m_rect.getGlobalBounds().top + m_rect.getGlobalBounds().height/2.f};

switch ( m_description.side )
{
default:
case Side::Top:
m_description.text.setPosition({ boundsCenter.x - textRect.width/2.f, boundsCenter.y - m_rect.getGlobalBounds().height - textRect.height });
break;
case Side::Right:

break;
case Side::Bottom:

break;
case Side::Left:

break;
}
}
void Textbox::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(m_description.text);
target.draw(m_rect);
target.draw(m_text);
}
20 changes: 20 additions & 0 deletions src/UI/Interface/Elements/Textbox/Textbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
class Textbox : public UIElement
{
public:

enum class Side : unsigned {
Top = 'T',
Right = 'R',
Bottom = 'B',
Left = 'L'
};

struct Description {
sf::Text text;
Side side;
};

Textbox();

Expand All @@ -24,12 +36,18 @@ class Textbox : public UIElement
void setFocus(bool focus);
void setFillColor(sf::Color color);
void setOutlineColor(sf::Color color);
void setTextFillColor(sf::Color color);
void setDescriptionString(const std::string& string);
void setDescriptionSide(Side side);
void setDescriptionCharacterSize(unsigned int size);
void setDescriptionFillColor(sf::Color color);

const sf::Vector2f& getSize() const;
const sf::Vector2f& getPosition() const;
const bool hasFocus() const;
const sf::Color& getFillColor() const;
const sf::Color& getOutlineColor() const;
const std::string& getInputString() const;

const bool contains(const sf::Vector2f point) const;
void processKey(sf::Uint32 unicode);
Expand All @@ -45,6 +63,7 @@ class Textbox : public UIElement

void updateText(sf::Time elapsed);
void positionText();
void positionDescription();

virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;

Expand All @@ -56,6 +75,7 @@ class Textbox : public UIElement
sf::Font m_font;
bool m_focus;
sf::Time m_timer;
Description m_description;
};

#endif /* Textbox_hpp */

0 comments on commit 11d8cc6

Please sign in to comment.