Line data Source code
1 : #include <client.hpp> 2 : #include <iostream> 3 : 4 : #define CHAT_MAX_SIZE 300 5 : 6 : #define NUMBER_LINE 9 7 : 8 : #define CHAT_START_POSITION 5 9 : #define CHAT_SIZE_X 300 10 : #define CHAT_SIZE_Y 265 11 : #define CHAT_OFFSET 20 12 : #define CHAT_TEXT_SIZE 30 13 : #define CHAT_FONT_SIZE 15 14 : 15 : #define BUTTON_TEXT_ENTER 1 16 : 17 : #define STARTING_MESSAGE "Game is starting" 18 : 19 : #ifndef RESOURCES_PATH 20 : #define RESOURCES_PATH "../resources" 21 : #endif 22 : 23 : const sf::Color TEXT_COLOR = sf::Color(255, 255, 255, 180); 24 : 25 : using namespace client; 26 : 27 : /*! 28 : * @brief Load all the chat 29 : */ 30 0 : Chat::Chat() 31 : { 32 0 : if (!chatFont.loadFromFile(RESOURCES_PATH "/font/Calibri.ttf")) 33 : { 34 0 : std::cerr << "Font not loaded" << std::endl; 35 : } 36 : 37 : unsigned i; 38 0 : for(i = 0; i < gameChat.size(); i++ ) 39 : { 40 0 : gameChat[i].setString(""); 41 0 : gameChat[i].setFont(chatFont); 42 0 : gameChat[i].setCharacterSize(CHAT_FONT_SIZE); 43 0 : gameChat[i].setFillColor(sf::Color::Black); 44 0 : gameChat[i].setPosition(CHAT_OFFSET, CHAT_START_POSITION + CHAT_SIZE_X + CHAT_OFFSET * i); 45 : } 46 0 : chatButton.emplace_back( 47 0 : sf::Vector2f(CHAT_SIZE_X + 2 * CHAT_OFFSET, CHAT_SIZE_Y), 48 0 : sf::Vector2f(0, CHAT_SIZE_X), 49 : TEXT_COLOR, 50 0 : false); 51 : 52 0 : chatButton.emplace_back( 53 0 : sf::Vector2f(CHAT_SIZE_X, CHAT_TEXT_SIZE), 54 0 : sf::Vector2f(CHAT_OFFSET, CHAT_START_POSITION + CHAT_SIZE_X + CHAT_OFFSET * (i + 1)), 55 : TEXT_COLOR, 56 0 : false); 57 : 58 0 : chatButton.back().setText(CHAT_FONT_SIZE, sf::Vector2f(0, 0), "", chatFont, CHAT_SIZE_X); 59 : 60 0 : updateChat("", "", STARTING_MESSAGE); 61 0 : } 62 : 63 : /*! 64 : * @brief Move the index of text to i -- 65 : */ 66 0 : void Chat::incrementChat() 67 : { 68 0 : for(unsigned i = 1; i < gameChat.size(); i++ ) 69 : { 70 0 : gameChat[i-1].setString(gameChat[i].getString()); 71 : } 72 0 : } 73 : 74 : /*! 75 : * @brief update the Chat with a string 76 : * @param time when the message is sent 77 : * @param username user of the player that send the message 78 : * @param sendMessage message to be add to the chat 79 : */ 80 0 : void Chat::updateChat(std::string time, std::string username, std::string sendMessage) 81 : { 82 0 : std::unique_lock<std::mutex> lock(mutexChat); 83 0 : message.clear(); 84 0 : lock.unlock(); 85 0 : chatButton[BUTTON_TEXT_ENTER].buttonText->setString(""); 86 : 87 0 : if (!username.empty()) 88 : { 89 0 : sendMessage = time + " [" + username + "]: " + sendMessage; 90 : } 91 : 92 0 : incrementChat(); 93 0 : std::string secondLine = ""; 94 : 95 0 : gameChat[NUMBER_LINE].setString(sendMessage); 96 : 97 0 : while (gameChat[NUMBER_LINE].getGlobalBounds().width > CHAT_MAX_SIZE) 98 : { 99 0 : std::string nextChar = gameChat[NUMBER_LINE].getString(); 100 0 : nextChar = nextChar.back(); 101 0 : secondLine.insert(0, nextChar); 102 0 : sendMessage.pop_back(); 103 0 : gameChat[NUMBER_LINE].setString(sendMessage); 104 0 : } 105 : 106 0 : if (!secondLine.empty()) 107 : { 108 0 : updateChat("", "", secondLine); 109 : } 110 0 : } 111 : 112 : /*! 113 : * @brief Add a char to the chat message 114 : */ 115 0 : void Chat::addChatChar(std::string ch) 116 : { 117 0 : std::unique_lock<std::mutex> lock(mutexChat); 118 0 : message += ch; 119 0 : lock.unlock(); 120 0 : chatButton[BUTTON_TEXT_ENTER].buttonText->setString(chatButton[BUTTON_TEXT_ENTER].buttonText->getString() + ch); 121 : 122 0 : while(chatButton[BUTTON_TEXT_ENTER].buttonText->getGlobalBounds().width > CHAT_MAX_SIZE) 123 : { 124 0 : std::string newString = chatButton[BUTTON_TEXT_ENTER].buttonText->getString(); 125 0 : newString.erase(0, 1); 126 0 : chatButton[BUTTON_TEXT_ENTER].buttonText->setString(newString); 127 0 : } 128 0 : chatButton[BUTTON_TEXT_ENTER].centerText(true); 129 0 : } 130 : 131 : /*! 132 : * @brief Delete a char to the chat message 133 : */ 134 0 : void Chat::deleteChatChar() 135 : { 136 0 : std::string newString = chatButton[BUTTON_TEXT_ENTER].buttonText->getString(); 137 0 : if (!newString.empty()) 138 : { 139 0 : newString.pop_back(); 140 0 : std::lock_guard<std::mutex> lock(mutexChat); 141 0 : message.pop_back(); 142 0 : } 143 0 : chatButton[BUTTON_TEXT_ENTER].buttonText->setString(newString); 144 0 : chatButton[BUTTON_TEXT_ENTER].centerText(false); 145 0 : } 146 : 147 : /*! 148 : * @brief display the entire Chat 149 : */ 150 0 : void Chat::drawChat(std::shared_ptr<sf::RenderWindow> window) 151 : { 152 0 : for (unsigned i = 0; i < chatButton.size(); i++) 153 : { 154 0 : window->draw(*chatButton[i].buttonRect); 155 : } 156 0 : window->draw(*chatButton[BUTTON_TEXT_ENTER].buttonText); 157 0 : for (auto &chat : gameChat) 158 : { 159 0 : window->draw(chat); 160 : } 161 0 : }