diff options
Diffstat (limited to 'game/client/play_menu.cc')
| -rw-r--r-- | game/client/play_menu.cc | 151 |
1 files changed, 93 insertions, 58 deletions
diff --git a/game/client/play_menu.cc b/game/client/play_menu.cc index 7ded71a..e6b8d2c 100644 --- a/game/client/play_menu.cc +++ b/game/client/play_menu.cc @@ -1,4 +1,5 @@ #include "client/pch.hh" + #include "client/play_menu.hh" #include "core/config.hh" @@ -16,9 +17,9 @@ #include "client/session.hh" constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration; -constexpr static const char *DEFAULT_SERVER_NAME = "Voxelius Server"; -constexpr static const char *SERVERS_TXT = "servers.txt"; -constexpr static const char *WARNING_TOAST = "[!]"; +constexpr static const char* DEFAULT_SERVER_NAME = "Voxelius Server"; +constexpr static const char* SERVERS_TXT = "servers.txt"; +constexpr static const char* WARNING_TOAST = "[!]"; constexpr static std::size_t MAX_SERVER_ITEM_NAME = 24; @@ -34,13 +35,13 @@ struct ServerStatusItem final { std::string password; std::string hostname; std::uint16_t port; - + // Things pulled from bother events std::uint32_t protocol_version; std::uint16_t num_players; std::uint16_t max_players; std::string motd; - + // Unique identifier that monotonically // grows with each new server added and // doesn't reset with each server removed @@ -70,23 +71,27 @@ static std::string input_hostname; static std::string input_password; static unsigned int next_identity; -static std::deque<ServerStatusItem *> servers_deque; -static ServerStatusItem *selected_server; +static std::deque<ServerStatusItem*> servers_deque; +static ServerStatusItem* selected_server; static bool editing_server; static bool adding_server; static bool needs_focus; -static void parse_hostname(ServerStatusItem *item, const std::string &hostname) +static void parse_hostname(ServerStatusItem* item, const std::string& hostname) { auto parts = strtools::split(hostname, ":"); - if(!parts[0].empty()) + if(!parts[0].empty()) { item->hostname = parts[0]; - else item->hostname = std::string("localhost"); + } else { + item->hostname = std::string("localhost"); + } - if(parts.size() >= 2) + if(parts.size() >= 2) { item->port = cxpr::clamp<std::uint16_t>(strtoul(parts[1].c_str(), nullptr, 10), 1024, UINT16_MAX); - else item->port = protocol::PORT; + } else { + item->port = protocol::PORT; + } } static void add_new_server(void) @@ -116,9 +121,11 @@ static void edit_selected_server(void) { input_itemname = selected_server->name; - if(selected_server->port != protocol::PORT) + if(selected_server->port != protocol::PORT) { input_hostname = fmt::format("{}:{}", selected_server->hostname, selected_server->port); - else input_hostname = selected_server->hostname; + } else { + input_hostname = selected_server->hostname; + } input_password = selected_server->password; @@ -142,24 +149,26 @@ static void remove_selected_server(void) static void join_selected_server(void) { - if(session::peer) - return; - session::connect(selected_server->hostname.c_str(), selected_server->port, selected_server->password.c_str()); + if(!session::peer) { + session::connect(selected_server->hostname.c_str(), selected_server->port, selected_server->password.c_str()); + } } -static void on_glfw_key(const GlfwKeyEvent &event) +static void on_glfw_key(const GlfwKeyEvent& event) { if((event.key == GLFW_KEY_ESCAPE) && (event.action == GLFW_PRESS)) { if(globals::gui_screen == GUI_PLAY_MENU) { if(editing_server) { - if(adding_server) + if(adding_server) { remove_selected_server(); - input_itemname.clear(); - input_hostname.clear(); - input_password.clear(); - editing_server = false; - adding_server = false; - return; + } else { + input_itemname.clear(); + input_hostname.clear(); + input_password.clear(); + editing_server = false; + adding_server = false; + return; + } } globals::gui_screen = GUI_MAIN_MENU; @@ -169,7 +178,7 @@ static void on_glfw_key(const GlfwKeyEvent &event) } } -static void on_language_set(const LanguageSetEvent &event) +static void on_language_set(const LanguageSetEvent& event) { str_tab_servers = language::resolve_gui("play_menu.tab.servers"); @@ -188,7 +197,7 @@ static void on_language_set(const LanguageSetEvent &event) str_outdated_server = language::resolve("play_menu.outdated_server"); } -static void on_bother_response(const BotherResponseEvent &event) +static void on_bother_response(const BotherResponseEvent& event) { for(auto item : servers_deque) { if(item->identity == event.identity) { @@ -198,8 +207,7 @@ static void on_bother_response(const BotherResponseEvent &event) item->max_players = UINT16_MAX; item->motd = str_status_fail; item->status = item_status::FAILURE; - } - else { + } else { item->protocol_version = event.protocol_version; item->num_players = event.num_players; item->max_players = event.max_players; @@ -212,16 +220,16 @@ static void on_bother_response(const BotherResponseEvent &event) } } -static void layout_server_item(ServerStatusItem *item) +static void layout_server_item(ServerStatusItem* item) { // Preserve the cursor at which we draw stuff - const ImVec2 &cursor = ImGui::GetCursorScreenPos(); - const ImVec2 &padding = ImGui::GetStyle().FramePadding; - const ImVec2 &spacing = ImGui::GetStyle().ItemSpacing; + const ImVec2& cursor = ImGui::GetCursorScreenPos(); + const ImVec2& padding = ImGui::GetStyle().FramePadding; + const ImVec2& spacing = ImGui::GetStyle().ItemSpacing; const float item_width = ImGui::GetContentRegionAvail().x; const float line_height = ImGui::GetTextLineHeightWithSpacing(); - const std::string sid = fmt::format("###play_menu.servers.{}", static_cast<void *>(item)); + const std::string sid = fmt::format("###play_menu.servers.{}", static_cast<void*>(item)); if(ImGui::Selectable(sid.c_str(), (item == selected_server), 0, ImVec2(0.0, 2.0f * (line_height + padding.y + spacing.y)))) { selected_server = item; editing_server = false; @@ -232,7 +240,7 @@ static void layout_server_item(ServerStatusItem *item) join_selected_server(); } - ImDrawList *draw_list = ImGui::GetWindowDrawList(); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); if(item == selected_server) { const ImVec2 start = ImVec2(cursor.x, cursor.y); @@ -257,16 +265,20 @@ static void layout_server_item(ServerStatusItem *item) if(ImGui::IsMouseHoveringRect(warning_pos, warning_end)) { ImGui::BeginTooltip(); - if(item->protocol_version < protocol::VERSION) + + if(item->protocol_version < protocol::VERSION) { ImGui::TextUnformatted(str_outdated_server.c_str(), str_outdated_server.c_str() + str_outdated_server.size()); - else ImGui::TextUnformatted(str_outdated_client.c_str(), str_outdated_client.c_str() + str_outdated_client.size()); + } else { + ImGui::TextUnformatted(str_outdated_client.c_str(), str_outdated_client.c_str() + str_outdated_client.size()); + } + ImGui::EndTooltip(); } } } ImU32 motd_color = {}; - const std::string *motd_text; + const std::string* motd_text; switch(item->status) { case item_status::UNKNOWN: @@ -291,7 +303,7 @@ static void layout_server_item(ServerStatusItem *item) draw_list->AddText(motd_pos, motd_color, motd_text->c_str(), motd_text->c_str() + motd_text->size()); } -static void layout_server_edit(ServerStatusItem *item) +static void layout_server_edit(ServerStatusItem* item) { if(needs_focus) { ImGui::SetKeyboardFocusHere(); @@ -341,12 +353,14 @@ static void layout_server_edit(ServerStatusItem *item) static void layout_servers(void) { if(ImGui::BeginListBox("###play_menu.servers.listbox", ImVec2(-1.0f, -1.0f))) { - for(ServerStatusItem *item : servers_deque) { - if(editing_server && (item == selected_server)) + for(ServerStatusItem* item : servers_deque) { + if(editing_server && item == selected_server) { layout_server_edit(item); - else layout_server_item(item); + } else { + layout_server_item(item); + } } - + ImGui::EndListBox(); } } @@ -357,45 +371,60 @@ static void layout_servers_buttons(void) // Can only join when selected and not editing ImGui::BeginDisabled(!selected_server || editing_server); - if(ImGui::Button(str_join.c_str(), ImVec2(-0.50f * avail_width, 0.0f))) + + if(ImGui::Button(str_join.c_str(), ImVec2(-0.50f * avail_width, 0.0f))) { join_selected_server(); + } + ImGui::EndDisabled(); ImGui::SameLine(); // Can only connect directly when not editing anything ImGui::BeginDisabled(editing_server); - if(ImGui::Button(str_connect.c_str(), ImVec2(-1.00f, 0.0f))) + + if(ImGui::Button(str_connect.c_str(), ImVec2(-1.00f, 0.0f))) { globals::gui_screen = GUI_DIRECT_CONNECTION; + } + ImGui::EndDisabled(); // Can only add when not editing anything ImGui::BeginDisabled(editing_server); - if(ImGui::Button(str_add.c_str(), ImVec2(-0.75f * avail_width, 0.0f))) + + if(ImGui::Button(str_add.c_str(), ImVec2(-0.75f * avail_width, 0.0f))) { add_new_server(); + } + ImGui::EndDisabled(); ImGui::SameLine(); // Can only edit when selected and not editing ImGui::BeginDisabled(!selected_server || editing_server); - if(ImGui::Button(str_edit.c_str(), ImVec2(-0.50f * avail_width, 0.0f))) + + if(ImGui::Button(str_edit.c_str(), ImVec2(-0.50f * avail_width, 0.0f))) { edit_selected_server(); + } + ImGui::EndDisabled(); ImGui::SameLine(); // Can only remove when selected and not editing ImGui::BeginDisabled(!selected_server || editing_server); - if(ImGui::Button(str_remove.c_str(), ImVec2(-0.25f * avail_width, 0.0f))) + + if(ImGui::Button(str_remove.c_str(), ImVec2(-0.25f * avail_width, 0.0f))) { remove_selected_server(); + } + ImGui::EndDisabled(); ImGui::SameLine(); if(ImGui::Button(str_refresh.c_str(), ImVec2(-1.0f, 0.0f))) { - for(ServerStatusItem *item : servers_deque) { + for(ServerStatusItem* item : servers_deque) { if(item->status != item_status::PINGING) { - if(editing_server && (item == selected_server)) - continue; - item->status = item_status::UNKNOWN; - bother::cancel(item->identity); + if(!editing_server || item != selected_server) { + item->status = item_status::UNKNOWN; + bother::cancel(item->identity); + } } } } @@ -426,13 +455,17 @@ void play_menu::init(void) parse_hostname(item, parts[0]); - if(parts.size() >= 2) + if(parts.size() >= 2) { item->password = parts[1]; - else item->password = std::string(); + } else { + item->password = std::string(); + } - if(parts.size() >= 3) + if(parts.size() >= 3) { item->name = parts[2].substr(0, MAX_SERVER_ITEM_NAME); - else item->name = DEFAULT_SERVER_NAME; + } else { + item->name = DEFAULT_SERVER_NAME; + } servers_deque.push_back(item); } @@ -482,8 +515,10 @@ void play_menu::layout(void) } if(ImGui::BeginTabItem(str_tab_servers.c_str())) { - if(ImGui::BeginChild("###play_menu.servers.child", ImVec2(0.0f, -2.0f * ImGui::GetFrameHeightWithSpacing()))) + if(ImGui::BeginChild("###play_menu.servers.child", ImVec2(0.0f, -2.0f * ImGui::GetFrameHeightWithSpacing()))) { layout_servers(); + } + ImGui::EndChild(); layout_servers_buttons(); |
