R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ServerListMenu.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** r-type
4** File description:
5** ServerListMenu - Menu for selecting a server from saved list
6*/
7
8#pragma once
9
10#include <functional>
11#include <memory>
12#include <string>
13#include <vector>
14
16#include "Menu/BaseMenu.hpp"
17#include "UI/IUIFactory.hpp"
18
19namespace Game {
28 class ServerListMenu : public BaseMenu {
29 public:
30 struct ServerInfo {
31 std::string name;
32 std::string ip;
33 uint16_t port;
34
35 ServerInfo(const std::string &n, const std::string &i, uint16_t p) : name(n), ip(i), port(p) {}
36 };
37
38 explicit ServerListMenu(UI::IUIFactory &uiFactory, Graphics::IGraphics &graphics);
39 ~ServerListMenu() override = default;
40
45 void SetOnServerSelected(std::function<void(const std::string &, uint16_t)> onServerSelected);
46
50 void SetOnAddServer(std::function<void()> onAddServer);
51
55 void SetOnBack(std::function<void()> onBack);
56
63 void AddServer(const std::string &name, const std::string &ip, uint16_t port);
64
69 void RemoveServer(size_t index);
70
74 [[nodiscard]] const std::vector<ServerInfo> &GetServers() const { return _servers; }
75
80 void SetConnectionError(const std::string &errorMsg);
81
86
92 void SetConnecting(bool connecting, const std::string &serverName = "");
93
94 void Initialize() override;
95 void Update() override;
96 void Render() override;
97
98 private:
99 void OnServerClicked(size_t index);
100 void OnAddServerClicked();
101 void OnBackClicked();
102 void OnDeleteServerClicked(size_t index);
103 void RebuildServerList();
105
106 std::function<void(const std::string &, uint16_t)> _onServerSelected{};
107 std::function<void()> _onAddServer{};
108 std::function<void()> _onBack{};
109
111
112 std::vector<ServerInfo> _servers;
113 std::vector<std::shared_ptr<UI::IButton>> _serverButtons;
114 std::vector<std::shared_ptr<UI::IButton>> _deleteButtons;
115
116 std::shared_ptr<UI::IButton> _addServerButton;
117 std::shared_ptr<UI::IButton> _backButton;
118
119 std::string _connectionError;
121
122 bool _isConnecting{false};
124
125 static constexpr size_t MAX_SERVERS = 5;
126 static constexpr float SERVER_BUTTON_WIDTH = 400.0f;
127 static constexpr float SERVER_BUTTON_HEIGHT = 50.0f;
128 static constexpr float SERVER_BUTTON_SPACING = 15.0f;
129 static constexpr float LIST_START_Y = 100.0f;
130 static constexpr int MAX_VISIBLE_SERVERS = 8;
131 };
132} // namespace Game
Base class for all menu implementations.
Definition BaseMenu.hpp:26
Server list menu for selecting and managing servers.
~ServerListMenu() override=default
void SetOnAddServer(std::function< void()> onAddServer)
Set callback triggered when "Add Server" button is clicked.
static constexpr float LIST_START_Y
std::function< void(const std::string &, uint16_t)> _onServerSelected
void ClearConnectionError()
Clear connection error message.
std::string _connectingServerName
std::shared_ptr< UI::IButton > _addServerButton
static constexpr int MAX_VISIBLE_SERVERS
static constexpr float SERVER_BUTTON_SPACING
static constexpr size_t MAX_SERVERS
void SetOnBack(std::function< void()> onBack)
Set callback triggered when the Back button is clicked.
void Update() override
Update menu state (should be called every frame).
std::vector< ServerInfo > _servers
std::vector< std::shared_ptr< UI::IButton > > _deleteButtons
std::vector< std::shared_ptr< UI::IButton > > _serverButtons
std::shared_ptr< UI::IButton > _backButton
void AddServer(const std::string &name, const std::string &ip, uint16_t port)
Add a server to the list.
static constexpr float SERVER_BUTTON_HEIGHT
void OnDeleteServerClicked(size_t index)
std::function< void()> _onBack
static constexpr float SERVER_BUTTON_WIDTH
void OnServerClicked(size_t index)
void SetConnectionError(const std::string &errorMsg)
Set connection error message.
void Render() override
Render menu (should be called every frame).
void SetConnecting(bool connecting, const std::string &serverName="")
Set connecting state.
void Initialize() override
Initialize menu (must be implemented by derived classes).
const std::vector< ServerInfo > & GetServers() const
Get the list of servers.
Graphics::IGraphics & _graphics
std::function< void()> _onAddServer
void SetOnServerSelected(std::function< void(const std::string &, uint16_t)> onServerSelected)
Set callback triggered when a server is selected.
void RemoveServer(size_t index)
Remove a server from the list.
Abstract interface for graphics rendering operations.
Definition IGraphics.hpp:32
Abstract factory interface for creating UI elements.
ServerInfo(const std::string &n, const std::string &i, uint16_t p)
std::string name
Display name (e.g., "Local Server", "EU Server")