#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
//this function collects entered text and adds it to an sf::Text object
//it processes all standard characters and backspaces.
//it will automatically enter line breaks at a desired width to accommodate text wrapping
//first argument: passes in a reference to the sf::Text object who's string will be modified.
//second argument: passes in the event queue so the function know which character to add to the string
//third argument: passes in the size of each line before the function automatically enters a line break
//fourth argument: passes in the maximum amount of character the sf::Text object will hold
void processTextInput(sf::Text&, sf::Event, int, int);
//this function takes the text object from PrecessTextInput() and positions it on the window
//it will scroll the text in the text box upward once the text reaches the bottom of the text box.
//likewise, it will scroll the text back down if you begin to remove lines.
//first argument: passes in the sf::Text object that will be modified by this function
//second argument: passes in an sf::vector2f that holds the height and width of the text box.
//third argument: passes in an sf::Vector2f that hold the x axis and the y axis
//of the text box's top left corner
void positionText(sf::Text&, sf::Vector2f, sf::Vector2f);
int main()
{
sf::Font font;
font.loadFromFile("coopbl.ttf");
std::string string= "";
sf::Text text(string, font, 20);
sf::Vector2f position(10,200);
sf::Vector2f dimensions(400,100);
text.setPosition(position.x, position.y);
text.setColor(sf::Color::Red);
sf::RenderWindow window(sf::VideoMode(800, 400), "SFML works!");
while (window.isOpen())
{
sf::Event Event;
while(window.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if(Event.key.code == sf::Keyboard::Escape)
{
window.close();
}
break;
case sf::Event::TextEntered:
{
processTextInput(text, Event, dimensions.x, 500);
}
}
}
positionText(text, dimensions, position);
window.draw(text);
window.display();
window.clear(sf::Color::White);
}
return 0;
};
void processTextInput(sf::Text &text, sf::Event Event, int lineWidth, int charLimit)
{
std::string string = text.getString();
if(Event.text.unicode >= 32)
{
if(text.findCharacterPos(string.size()).x > lineWidth - text.getCharacterSize())
{
int i = string.size() - 1;
bool done = false;
while(!done)
{
if(string[i] == ' ')
{
done = true;
}
else if(string[i] == '\n')
{
done = true;
i = string.size() -1;
}
else if(i <= 0)
{
i = string.size() -1;
done = true;
}
else
{
i--;
}
}
if(i == string.size() - 1)
{
string += '\n';
}
else
{
string[i] = '\n';
}
}
if(string.size() <= charLimit)
{
string += Event.text.unicode;
}
}
else if (Event.text.unicode == 8)
{
string = string.substr(0, string.length() - 1);
}
text.setString(string);
};
void positionText(sf::Text &text, sf::Vector2f dimensions, sf::Vector2f position)
{
int textBot = text.getPosition().y + text.getLocalBounds().height;
int chatBot = position.y + dimensions.y;
std::string string = text.getString();
if(textBot > chatBot - text.getCharacterSize())
{
text.move(0, (chatBot - text.getCharacterSize()) - (text.findCharacterPos(string.size() - 1).y + text.getCharacterSize()));
}
else if(text.getLocalBounds().height > dimensions.y - text.getCharacterSize() && textBot < chatBot + text.getCharacterSize())
{
text.move(0, (text.findCharacterPos(string.size() - 1).y + text.getCharacterSize()) - (chatBot - text.getCharacterSize()));
}
};