Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy |
4 |
|
|
** File description: |
5 |
|
|
** Chat |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#ifndef CHAT_HPP_ |
9 |
|
|
#define CHAT_HPP_ |
10 |
|
|
|
11 |
|
|
#include <iostream> |
12 |
|
|
#include <SFML/Graphics.hpp> |
13 |
|
|
#include <vector> |
14 |
|
|
#include <string> |
15 |
|
|
#include <list> |
16 |
|
|
|
17 |
|
|
class Chat { |
18 |
|
|
public: |
19 |
|
✗ |
Chat(sf::Font font, int maxMsg) : _font(font), _maxMsg(maxMsg) |
20 |
|
|
{ |
21 |
|
✗ |
_text.setFont(_font); |
22 |
|
✗ |
_text.setCharacterSize(17); |
23 |
|
✗ |
_text.setFillColor(sf::Color::White); |
24 |
|
✗ |
_text.setPosition(0, 0); |
25 |
|
✗ |
} |
26 |
|
✗ |
~Chat() { |
27 |
|
|
|
28 |
|
✗ |
} |
29 |
|
|
|
30 |
|
✗ |
void addMessage(std::string msg, sf::Color color = sf::Color::White) { |
31 |
|
✗ |
msg = msg.substr(0, 42); |
32 |
|
✗ |
if ((int)_messages.size() >= _maxMsg) { |
33 |
|
✗ |
_messages.pop_back(); |
34 |
|
|
} |
35 |
|
✗ |
_messages.push_front(std::make_pair(msg, color)); |
36 |
|
✗ |
} |
37 |
|
|
|
38 |
|
✗ |
void draw(sf::RenderWindow &window) { |
39 |
|
|
int i = 0; |
40 |
|
✗ |
for (auto msg : _messages) { |
41 |
|
✗ |
_text.setString(msg.first); |
42 |
|
✗ |
_text.setPosition(_pos.x, _pos.y - i * 20); |
43 |
|
✗ |
_text.setFillColor(msg.second); |
44 |
|
✗ |
if (i == _maxMsg - 1) |
45 |
|
✗ |
_text.setFillColor(sf::Color(msg.second.r, msg.second.g, msg.second.b, 35)); |
46 |
|
✗ |
if (i == _maxMsg - 2) |
47 |
|
✗ |
_text.setFillColor(sf::Color(msg.second.r, msg.second.g, msg.second.b, 100)); |
48 |
|
✗ |
if (i == _maxMsg - 3) |
49 |
|
✗ |
_text.setFillColor(sf::Color(msg.second.r, msg.second.g, msg.second.b, 200)); |
50 |
|
✗ |
window.draw(_text); |
51 |
|
✗ |
i++; |
52 |
|
|
} |
53 |
|
✗ |
} |
54 |
|
|
|
55 |
|
|
void setPosition(sf::Vector2f pos) { |
56 |
|
✗ |
_pos = pos; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
protected: |
60 |
|
|
private: |
61 |
|
|
sf::Text _text; |
62 |
|
|
sf::Font _font; |
63 |
|
|
std::list<std::pair<std::string, sf::Color>> _messages; |
64 |
|
|
int _maxMsg; |
65 |
|
|
sf::Vector2f _pos; |
66 |
|
|
}; |
67 |
|
|
|
68 |
|
|
#endif /* !CHAT_HPP_ */ |
69 |
|
|
|