Loading...
Searching...
No Matches
ChatWindow.h
Go to the documentation of this file.
1//
2// Created by Dave Smith on 4/30/26.
3//
4
5#ifndef CHATWINDOW_H
6#define CHATWINDOW_H
7
8
13#include <functional>
14#include <vector>
15#include <memory>
16#include <string>
17
19// A reusable text-messaging-style chat window.
20//
21// Data flows in via addMessage() — the window has no knowledge of WebSockets or
22// any transport layer. The caller is responsible for delivering messages and for
23// handling outbound messages via onSend().
24//
25// Call setSize() to resize (e.g. panel.addComponent(chat, true) fill support).
26namespace ml
27{
28 class ChatWindow : public ml::Component<Messenger>
29 {
30 static constexpr float INPUT_H = 54.f;
31 static constexpr float BTN_W = 72.f;
32 static constexpr float EDGE_PAD = 8.f;
33
34 // _size must be declared before _scrollPane so its in-class initializer
35 // runs first and can be used to compute the scroll pane's initial dimensions.
36 sf::Vector2f _size{400.f, 500.f};
37
38 ml::ScrollPane _scrollPane;
39 ml::TextInput _input;
40 ml::RectangleButton _sendBtn;
41
42
43 std::vector<ChatMessage> _messages;
44 std::vector<std::unique_ptr<ChatBubble>> _bubbles;
45 sf::Vector2f _position;
46 std::function<void(const std::string&)> _onSend;
47
48 void applyLayout();
49 void doSend();
50 void rebuildBubbles();
51
52 public:
54
55 // Append a message bubble. Called by the app layer when a message arrives
56 // (from WebSocket, local input echo, etc.).
57 void addMessage(const ChatMessage& msg);
58 void clear();
59
60 // Register a callback fired when the user sends a message.
61 // The callback receives the raw text; the caller is responsible for
62 // transmitting it and echoing it back via addMessage() if desired.
63 void onSend(std::function<void(const std::string&)> callback);
64
65 // Resize the window — enables use with Panel::addComponent(chat, true).
66 void setSize(const sf::Vector2f& size);
67
68 void setPosition(const sf::Vector2f& pos) override;
69 sf::Vector2f getPosition() const override;
71 void setRecipientId(const std::string& id);
72 const std::string& getRecipientId() const;
73
74 // No draw override — the framework default (ComponentBase::draw) walks
75 // the registered children in layer order, which is exactly what this
76 // window used to do explicitly.
77 };
78}
79
80
81#endif //CHATWINDOW_H
sf::Vector2f getPosition() const override
const std::string & getRecipientId() const
void setRecipientId(const std::string &id)
void addMessage(const ChatMessage &msg)
sf::FloatRect getGlobalBounds() const override
void setPosition(const sf::Vector2f &pos) override
void setSize(const sf::Vector2f &size)
void onSend(std::function< void(const std::string &)> callback)
Primary base class for all user-facing Malena components.
Definition Component.h:292
A rectangular button with a centered text label.
Single-line rich text input with horizontal scrolling.
Definition TextInput.h:48
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f