summaryrefslogtreecommitdiffstats
path: root/game/client/gui/direct_connection.cc
blob: 0290bd357ab2986bb0cb006451e94a370b5a49b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "client/pch.hh"

#include "client/gui/direct_connection.hh"

#include "core/config/boolean.hh"
#include "core/utils/string.hh"

#include "shared/protocol.hh"

#include "client/gui/gui_screen.hh"
#include "client/gui/language.hh"
#include "client/io/glfw.hh"

#include "client/game.hh"
#include "client/globals.hh"
#include "client/session.hh"

constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration;

static std::string str_title;
static std::string str_connect;
static std::string str_cancel;

static std::string str_hostname;
static std::string str_password;

static std::string direct_hostname;
static std::string direct_password;

static void on_glfw_key(const io::GlfwKeyEvent& event)
{
    if((event.key == GLFW_KEY_ESCAPE) && (event.action == GLFW_PRESS)) {
        if(globals::gui_screen == GUI_DIRECT_CONNECTION) {
            globals::gui_screen = GUI_PLAY_MENU;
            return;
        }
    }
}

static void on_language_set(const gui::LanguageSetEvent& event)
{
    str_title = gui::language::resolve("direct_connection.title");
    str_connect = gui::language::resolve_gui("direct_connection.connect");
    str_cancel = gui::language::resolve_gui("direct_connection.cancel");

    str_hostname = gui::language::resolve("direct_connection.hostname");
    str_password = gui::language::resolve("direct_connection.password");
}

static void connect_to_server(void)
{
    auto parts = utils::split(direct_hostname, ":");
    std::string parsed_hostname;
    std::uint16_t parsed_port;

    if(!parts[0].empty()) {
        parsed_hostname = parts[0];
    }
    else {
        parsed_hostname = std::string("localhost");
    }

    if(parts.size() >= 2) {
        parsed_port = math::clamp<std::uint16_t>(strtoul(parts[1].c_str(), nullptr, 10), 1024, UINT16_MAX);
    }
    else {
        parsed_port = protocol::PORT;
    }

    session::connect(parsed_hostname.c_str(), parsed_port, direct_password.c_str());
}

void gui::direct_connection::init(void)
{
    globals::dispatcher.sink<io::GlfwKeyEvent>().connect<&on_glfw_key>();
    globals::dispatcher.sink<LanguageSetEvent>().connect<&on_language_set>();
}

void gui::direct_connection::layout(void)
{
    auto viewport = ImGui::GetMainViewport();
    auto window_start = ImVec2(0.25f * viewport->Size.x, 0.20f * viewport->Size.y);
    auto window_size = ImVec2(0.50f * viewport->Size.x, 0.80f * viewport->Size.y);

    ImGui::SetNextWindowPos(window_start);
    ImGui::SetNextWindowSize(window_size);

    if(ImGui::Begin("###UIDirectConnect", nullptr, WINDOW_FLAGS)) {
        const float title_width = ImGui::CalcTextSize(str_title.c_str()).x;
        ImGui::SetCursorPosX(0.5f * (window_size.x - title_width));
        ImGui::TextUnformatted(str_title.c_str());

        ImGui::Dummy(ImVec2(0.0f, 16.0f * globals::gui_scale));

        ImGuiInputTextFlags hostname_flags = ImGuiInputTextFlags_CharsNoBlank;

        if(client_game::streamer_mode.get_value()) {
            // Hide server hostname to avoid things like
            // followers flooding the server that is streamed online
            hostname_flags |= ImGuiInputTextFlags_Password;
        }

        auto avail_width = ImGui::GetContentRegionAvail().x;

        ImGui::PushItemWidth(avail_width);

        ImGui::InputText("###UIDirectConnect_hostname", &direct_hostname, hostname_flags);

        if(ImGui::BeginItemTooltip()) {
            ImGui::PushTextWrapPos(ImGui::GetFontSize() * 16.0f);
            ImGui::TextUnformatted(str_hostname.c_str());
            ImGui::PopTextWrapPos();
            ImGui::EndTooltip();
        }

        ImGui::InputText("###UIDirectConnect_password", &direct_password, ImGuiInputTextFlags_Password);

        if(ImGui::BeginItemTooltip()) {
            ImGui::PushTextWrapPos(ImGui::GetFontSize() * 16.0f);
            ImGui::TextUnformatted(str_password.c_str());
            ImGui::PopTextWrapPos();
            ImGui::EndTooltip();
        }

        ImGui::PopItemWidth();

        ImGui::Dummy(ImVec2(0.0f, 4.0f * globals::gui_scale));

        ImGui::BeginDisabled(utils::is_whitespace(direct_hostname));

        if(ImGui::Button(str_connect.c_str(), ImVec2(avail_width, 0.0f))) {
            connect_to_server();
        }

        ImGui::EndDisabled();

        if(ImGui::Button(str_cancel.c_str(), ImVec2(avail_width, 0.0f))) {
            globals::gui_screen = GUI_PLAY_MENU;
        }
    }

    ImGui::End();
}