R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ChatWidget.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** r-type
4** File description:
5** ChatWidget - In-game chat component
6*/
7
8#pragma once
9
10#include <cstdint>
11#include <deque>
12#include <functional>
13#include <memory>
14#include <string>
15#include <vector>
17#include "UI/ITextInput.hpp"
18#include "UI/IUIFactory.hpp"
19
20namespace Game {
21
26 uint32_t playerId;
27 std::string playerName;
28 std::string message;
29 uint64_t timestamp;
30
31 ChatMessageData(uint32_t id, const std::string &name, const std::string &msg, uint64_t ts)
32 : playerId(id), playerName(name), message(msg), timestamp(ts) {}
33 };
34
44 class ChatWidget {
45 public:
46 explicit ChatWidget(UI::IUIFactory &uiFactory, Graphics::IGraphics &graphics);
47 ~ChatWidget() = default;
48
52 void Initialize();
53
57 void Update();
58
62 void Render();
63
67 void SetOnMessageSent(std::function<void(const std::string &)> callback);
68
72 void AddMessage(uint32_t playerId, const std::string &playerName, const std::string &message,
73 uint64_t timestamp);
74
78 void ClearMessages();
79
83 void SetVisible(bool visible);
84
88 bool IsVisible() const { return _visible; }
89
93 void SetPosition(float x, float y);
94
95 private:
96 void OnTextChanged(const std::string &text);
97 void SendMessage();
98 std::string FormatMessage(const ChatMessageData &msg);
99
102
103 std::shared_ptr<UI::ITextInput> _textInput;
104 std::function<void(const std::string &)> _onMessageSent;
105
106 std::deque<ChatMessageData> _messages;
107 bool _visible = false;
108 float _posX = 0.0f;
109 float _posY = 0.0f;
110
111 // Configuration
112 static constexpr size_t MAX_MESSAGE_BUFFER = 50;
113 static constexpr size_t MAX_CHAR_PER_MESSAGE = 256;
114 static constexpr size_t MAX_VISIBLE_MESSAGES = 6;
115 static constexpr float WIDGET_WIDTH = 280.0f;
116 static constexpr float WIDGET_HEIGHT = 180.0f;
117 static constexpr float INPUT_HEIGHT = 26.0f;
118 static constexpr float MESSAGE_SPACING = 3.0f;
119 static constexpr float MESSAGE_LINE_HEIGHT = 16.0f;
120 static constexpr float PADDING = 8.0f;
121 };
122
123} // namespace Game
In-game chat widget.
void SetPosition(float x, float y)
Set widget position.
static constexpr size_t MAX_VISIBLE_MESSAGES
~ChatWidget()=default
static constexpr float PADDING
static constexpr float INPUT_HEIGHT
void Render()
Render the chat widget.
static constexpr float MESSAGE_SPACING
std::shared_ptr< UI::ITextInput > _textInput
void SetOnMessageSent(std::function< void(const std::string &)> callback)
Set callback for when a message is sent.
std::string FormatMessage(const ChatMessageData &msg)
void ClearMessages()
Clear all messages.
static constexpr float WIDGET_HEIGHT
void SetVisible(bool visible)
Set widget visibility.
static constexpr float MESSAGE_LINE_HEIGHT
void OnTextChanged(const std::string &text)
static constexpr size_t MAX_MESSAGE_BUFFER
UI::IUIFactory & _uiFactory
static constexpr size_t MAX_CHAR_PER_MESSAGE
static constexpr float WIDGET_WIDTH
Graphics::IGraphics & _graphics
std::deque< ChatMessageData > _messages
bool IsVisible() const
Check if widget is visible.
void Initialize()
Initialize the chat widget.
void Update()
Update the chat widget.
void AddMessage(uint32_t playerId, const std::string &playerName, const std::string &message, uint64_t timestamp)
Add a message to the chat history.
std::function< void(const std::string &)> _onMessageSent
Abstract interface for graphics rendering operations.
Definition IGraphics.hpp:32
Abstract factory interface for creating UI elements.
Chat message data.
ChatMessageData(uint32_t id, const std::string &name, const std::string &msg, uint64_t ts)