Permalink
Browse files

Upgrade the GUI::TextLine

Upgrade of the TextLine of the GUI to have a text align and a best positioning.
- Addition of the Style::Align with Right and Center for the text.
- Addition of the SetOrigin, SetMaxTextSize and SetTextAlign methods. The max text size is the limit of character can be entered in the text line. And the text is automaticaly cut if it is entered in the code by the programmer and it is longer than the max text size.
- Modification of the cursor to automaticaly positioning at the end of a text initialised in the code.
- And now, the text is positionated in function of the background of the text line (visible or not).

Signed-off-by: TimyusVegetari <ant110283@hotmail.fr>
  • Loading branch information...
TimyusVegetari committed Mar 1, 2017
1 parent 37b927d commit ca562e408c89ce937d4c0c59153b2670d4028576
Showing with 136 additions and 22 deletions.
  1. +57 −0 include/Gui/Style.hpp
  2. +30 −3 include/Gui/TextLine.hpp
  3. +49 −19 src/Gui/TextLine.cpp
View
@@ -0,0 +1,57 @@
////////////////////////////////////////////////////////////
//
// This file is part of DRIMI::Gui.
// Copyright (C) 2013-2017 Acroute Anthony (ant110283@hotmail.fr)
//
// DRIMI::Gui is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// DRIMI::Gui is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with DRIMI::Gui. If not, see <http://www.gnu.org/licenses/>.
//
// A part of the code in this file is inspired by the book
// "SFML Game Development".
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Description for Doxygen
////////////////////////////////////////////////////////////
/**
* \file Style.hpp
* \brief Enumeration of text styles.
* \author Anthony Acroute
* \version 0.1
* \date 2017
*
*/
#ifndef STYLE_HPP__
#define STYLE_HPP__
////////////////////////////////////////////////////////////
/// \brief Enumeration of identifiers for the states of the game.
///
////////////////////////////////////////////////////////////
namespace drimi {
namespace Gui {
namespace Style {
enum Align {
Left,
Center,
//Right,
};
}
}
}
#endif // STYLE_HPP__
View
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// This file is part of DRIMI::Gui.
// Copyright (C) 2013-2016 Acroute Anthony (ant110283@hotmail.fr)
// Copyright (C) 2013-2017 Acroute Anthony (ant110283@hotmail.fr)
//
// DRIMI::Gui is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@@ -28,8 +28,8 @@
* \file TextLine.hpp
* \brief Class for the text line of the gui.
* \author Anthony Acroute
* \version 0.1
* \date 2016
* \version 0.2
* \date 2016-2017
*
*/
@@ -42,6 +42,7 @@
#include <DRIMI/Gui/Component.hpp>
#include <DRIMI/BitmapFont/BmpFont.hpp>
#include <DRIMI/BitmapFont/BmpText.hpp>
#include <DRIMI/Gui/Style.hpp>
#include <SFML/Graphics/Sprite.hpp>
@@ -79,6 +80,7 @@ class TextLine : public Component {
drimi::BmpText m_oBmpText;
std::string m_szText;
GLuint m_uiMaxTextSize;
Style::Align m_eTextAlign;
// Cursor of the text line
drimi::BmpText m_oBmpCursor;
char m_cCursor;
@@ -212,6 +214,15 @@ class TextLine : public Component {
////////////////////////////////////////////////////////////
void SetPosition ( GLfloat fX, GLfloat fY );
////////////////////////////////////////////////////////////
/// \brief Set the origin of the text line.
///
/// \param uiX The X value of the origin.
/// uiY The Y value of the origin.
///
////////////////////////////////////////////////////////////
void SetOrigin ( GLfloat fX, GLfloat fY );
////////////////////////////////////////////////////////////
/// \brief Set the background texture.
///
@@ -230,6 +241,14 @@ class TextLine : public Component {
////////////////////////////////////////////////////////////
void SetColor ( sf::Color sfTextColor );
////////////////////////////////////////////////////////////
/// \brief Set the maximum characters can be entered in the text line.
///
/// \param uiSize The maximum characters.
///
////////////////////////////////////////////////////////////
void SetMaxTextSize ( GLuint uiSize );
////////////////////////////////////////////////////////////
/// \brief Set the text of the text line.
///
@@ -239,6 +258,14 @@ class TextLine : public Component {
////////////////////////////////////////////////////////////
void SetText ( const std::string& szText, drimi::BmpFont& oBmpFont );
////////////////////////////////////////////////////////////
/// \brief Set the alignement of the text.
///
/// \param eTextAlign The alignement of the text.
///
////////////////////////////////////////////////////////////
void SetTextAlign ( drimi::Gui::Style::Align eTextAlign );
////////////////////////////////////////////////////////////
/// \brief Get the text of the text line.
///
View
@@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////
//
// This file is part of DRIMI::Gui.
// Copyright (C) 2013-2016 Acroute Anthony (ant110283@hotmail.fr)
// Copyright (C) 2013-2017 Acroute Anthony (ant110283@hotmail.fr)
//
// DRIMI::Gui is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@@ -38,6 +38,7 @@ drimi::Gui::TextLine::TextLine ( void ) :
m_oBmpText (),
m_szText (""),
m_uiMaxTextSize (16),
m_eTextAlign (drimi::Gui::Style::Align::Center),
// Cursor of the text line
m_oBmpCursor (),
m_cCursor (L'_'),
@@ -94,19 +95,23 @@ GLboolean drimi::Gui::TextLine::HandleEvent ( const drimi::Event::Type eEventTyp
////////////////////////////////////////////////////////////
GLboolean drimi::Gui::TextLine::HandleTextUnicode ( const char cUnicode ) {
if (cUnicode >= 32 && m_szText.size () < m_uiMaxTextSize) {
if (cUnicode >= 32 && m_szText.size ()-1 < m_uiMaxTextSize) {
m_szText.insert (m_uiCursorIndex, 1, cUnicode);
m_oBmpText.SetString (m_szText);
m_oBmpText.SetOrigin (0.f, m_oBmpText.GetLocalBounds ().height / 2.f);
m_oBmpText.SetOrigin (m_oBmpText.GetLocalBounds ().width / 2.f, m_oBmpText.GetLocalBounds ().height / 2.f);
if (m_eTextAlign == drimi::Gui::Style::Align::Left)
m_oBmpText.SetOrigin (0.f, m_oBmpText.GetLocalBounds ().height / 2.f);
else if (m_eTextAlign == drimi::Gui::Style::Align::Center)
m_oBmpText.SetOrigin (m_oBmpText.GetLocalBounds ().width / 2.f, m_oBmpText.GetLocalBounds ().height / 2.f);
// Update the cursor
m_uiCursorIndex++;
} else if (cUnicode == 8) { // backspace
if (m_szText.size () > 0 && m_uiCursorIndex > 0) {
m_szText.erase (m_uiCursorIndex-1, 1);
m_oBmpText.SetString (m_szText);
m_oBmpText.SetOrigin (0.f, m_oBmpText.GetLocalBounds ().height / 2.f);
m_oBmpText.SetOrigin (m_oBmpText.GetLocalBounds ().width / 2.f, m_oBmpText.GetLocalBounds ().height / 2.f);
if (m_eTextAlign == drimi::Gui::Style::Align::Left)
m_oBmpText.SetOrigin (0.f, m_oBmpText.GetLocalBounds ().height / 2.f);
else if (m_eTextAlign == drimi::Gui::Style::Align::Center)
m_oBmpText.SetOrigin (m_oBmpText.GetLocalBounds ().width / 2.f, m_oBmpText.GetLocalBounds ().height / 2.f);
// Update the cursor
m_uiCursorIndex--;
}
@@ -142,17 +147,14 @@ void drimi::Gui::TextLine::Draw ( sf::RenderTarget& sfTarget, sf::RenderStates s
////////////////////////////////////////////////////////////
void drimi::Gui::TextLine::UpdateCursor ( void ) {
// If edition is now impossible
if (m_szText.size () == m_uiMaxTextSize) {
if (m_szText.size ()-1 == m_uiMaxTextSize) {
if (m_cCursor == L'_')
m_cCursor = L'|';
// If edition is now possible
} else if (m_cCursor == L'|')
m_cCursor = L'_';
m_oBmpCursor.SetString (m_cCursor);
if (m_szText.size () == 0)
m_oBmpCursor.SetOrigin (m_oBmpCursor.GetLocalBounds ().width / 2.f, m_oBmpCursor.GetLocalBounds ().height / 2.f);
else
m_oBmpCursor.SetOrigin (0.f, 0.f);
m_oBmpCursor.SetOrigin (0.f, 0.f);
sf::Vector2f sfCursorPos = m_oBmpText.FindCharacterPos (m_uiCursorIndex);
m_oBmpCursor.setPosition (sfCursorPos);
}
@@ -170,7 +172,7 @@ void drimi::Gui::TextLine::MoveCursorLeft ( void ) {
////////////////////////////////////////////////////////////
void drimi::Gui::TextLine::MoveCursorRight ( void ) {
// If the cursor isn't totaly to the right, update it
if (m_uiCursorIndex < m_szText.size ()) {
if (m_uiCursorIndex < m_szText.size ()-1) {
m_uiCursorIndex++;
// Update the cursor appearence
UpdateCursor ();
@@ -195,12 +197,21 @@ void drimi::Gui::TextLine::AnimateCursor ( void ) {
////////////////////////////////////////////////////////////
void drimi::Gui::TextLine::SetPosition ( GLfloat fX, GLfloat fY ) {
sf::FloatRect sfBounds = m_sfBackground.getLocalBounds ();
m_sfBackground.setOrigin (sf::Vector2f (sfBounds.width / 2.f, sfBounds.height / 2.f));
m_sfBackground.setPosition (fX, fY);
m_sfBackground.setPosition (fX, fY);
sf::FloatRect sfBounds = m_sfBackground.getGlobalBounds ();
if (m_eTextAlign == drimi::Gui::Style::Align::Left) {
m_oBmpText.SetOrigin (0.f, m_oBmpText.GetLocalBounds ().height / 2.f);
m_oBmpText.setPosition (5.f + sfBounds.left, sfBounds.top + sfBounds.height / 2.f);
} else if (m_eTextAlign == drimi::Gui::Style::Align::Center) {
m_oBmpText.SetOrigin (m_oBmpText.GetLocalBounds ().width / 2.f, m_oBmpText.GetLocalBounds ().height / 2.f);
m_oBmpText.setPosition (sfBounds.left + sfBounds.width / 2.f, sfBounds.top + sfBounds.height / 2.f);
}
}
m_oBmpText.SetOrigin (m_oBmpText.GetLocalBounds ().width / 2.f, m_oBmpText.GetLocalBounds ().height / 2.f);
m_oBmpText.setPosition (fX, fY);
////////////////////////////////////////////////////////////
void drimi::Gui::TextLine::SetOrigin ( GLfloat fX, GLfloat fY ) {
m_sfBackground.setOrigin (sf::Vector2f (fX, fY));
}
////////////////////////////////////////////////////////////
@@ -215,13 +226,32 @@ void drimi::Gui::TextLine::SetColor ( sf::Color sfTextColor ) {
m_oBmpCursor.SetColor (sfTextColor);
}
////////////////////////////////////////////////////////////
void drimi::Gui::TextLine::SetMaxTextSize ( GLuint uiSize ) {
m_uiMaxTextSize = uiSize;
// Update the cursor appearence
UpdateCursor ();
}
////////////////////////////////////////////////////////////
void drimi::Gui::TextLine::SetText ( const std::string& szText, drimi::BmpFont& oBmpFont ) {
m_szText = szText;
if (szText.size () <= m_uiMaxTextSize)
m_szText = szText;
else
m_szText = szText.substr (0, m_uiMaxTextSize);
m_szText.push_back ('\0');
m_oBmpText.SetFont (oBmpFont);
m_oBmpText.SetString (szText);
m_oBmpText.SetString (m_szText);
m_oBmpCursor.SetFont (oBmpFont);
m_oBmpCursor.SetStyle (sf::Text::Style::Bold);
m_uiCursorIndex = m_szText.size ()-1;
// Update the cursor appearence
UpdateCursor ();
}
////////////////////////////////////////////////////////////
void drimi::Gui::TextLine::SetTextAlign ( drimi::Gui::Style::Align eTextAlign ) {
m_eTextAlign = eTextAlign;
}
////////////////////////////////////////////////////////////

0 comments on commit ca562e4

Please sign in to comment.