refactor: create MainMenu and GameMenu classes
This commit is contained in:
84
src/render/gui/MainMenu.cpp
Normal file
84
src/render/gui/MainMenu.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "render/gui/MainMenu.h"
|
||||
|
||||
#include "game/client/Client.h"
|
||||
|
||||
#include "game/server/Server.h"
|
||||
|
||||
namespace td {
|
||||
namespace gui {
|
||||
|
||||
MainMenu::MainMenu(client::Client* client) : GuiWidget(client) {
|
||||
m_ConnectAddress.reserve(256);
|
||||
}
|
||||
|
||||
MainMenu::~MainMenu(){
|
||||
if(m_Server != nullptr)
|
||||
m_Server->stop();
|
||||
}
|
||||
|
||||
void MainMenu::render() {
|
||||
ImGui::Begin("Main Menu");
|
||||
if (ImGui::Button("Rejoindre une partie##join")) {
|
||||
ImGui::OpenPopup("Rejoindre une partie##join_popup");
|
||||
}
|
||||
if (ImGui::Button("Créer une partie")) {
|
||||
ImGui::OpenPopup("Créer une partie##create_popup");
|
||||
}
|
||||
if (ImGui::Button("Options")) {
|
||||
// TODO: add settings
|
||||
}
|
||||
|
||||
if (ImGui::BeginPopup("Rejoindre une partie##join_popup")) {
|
||||
ImGui::InputText("Server Adress", &m_ConnectAddress.front(), m_ConnectAddress.capacity());
|
||||
ImGui::InputInt("Port", &m_ConnectPort, -1);
|
||||
if (ImGui::Button("Rejoindre")) {
|
||||
getClient()->connect(td::network::Dns::Resolve(m_ConnectAddress), m_ConnectPort);
|
||||
m_TriedToConnect = true;
|
||||
}
|
||||
if (m_TriedToConnect) {
|
||||
ImGui::Text("Impossible de se connecter");
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
} else {
|
||||
m_TriedToConnect = false;
|
||||
}
|
||||
|
||||
if (ImGui::BeginPopup("Créer une partie##create_popup")) {
|
||||
ImGui::InputInt("Server Port", &m_ServerPort, -1);
|
||||
ImGui::Text("%s", std::string("Fichier de monde sélectionné : " + (m_WorldFilePath.empty() ? std::string("Aucun") : m_WorldFilePath)).c_str());
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Ouvrir un fichier")) {
|
||||
ImGui::OpenPopup("WorldFileDialog");
|
||||
}
|
||||
if (m_FileDialog.showFileDialog("WorldFileDialog", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(600, 300), ".tdmap")) {
|
||||
m_WorldFilePath = m_FileDialog.selected_path;
|
||||
}
|
||||
if (ImGui::Button("Créer")) {
|
||||
if (!startServer()) {
|
||||
m_TriedToCreate = true;
|
||||
} else {
|
||||
getClient()->connect(td::network::Dns::Resolve("localhost"), m_ServerPort);
|
||||
}
|
||||
}
|
||||
if (m_TriedToCreate)
|
||||
ImGui::Text("Failed to launch server");
|
||||
ImGui::EndPopup();
|
||||
} else {
|
||||
m_TriedToConnect = false;
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
bool MainMenu::startServer(){
|
||||
if (m_WorldFilePath.empty())
|
||||
return false;
|
||||
m_Server = std::make_unique<td::server::Server>(m_WorldFilePath);
|
||||
if (!m_Server->start(m_ServerPort)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace gui
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user