File: PasswordEnterWnd.cpp

package info (click to toggle)
freeorion 0.5.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 194,920 kB
  • sloc: cpp: 186,821; python: 40,979; ansic: 1,164; xml: 721; makefile: 32; sh: 7
file content (97 lines) | stat: -rw-r--r-- 4,116 bytes parent folder | download | duplicates (2)
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
#include "PasswordEnterWnd.h"

#include <GG/GUI.h>

#include "../util/i18n.h"
#include "../client/ClientNetworking.h"
#include "../client/human/GGHumanClientApp.h"

namespace {
    constexpr GG::X WINDOW_WIDTH{300};
    constexpr GG::Y WINDOW_HEIGHT{250};
}

PasswordEnterWnd::PasswordEnterWnd() :
    CUIWnd(UserString("AUTHENTICATION_WINDOW_TITLE"), GG::X{80}, GG::Y{130},
           WINDOW_WIDTH, WINDOW_HEIGHT,
           GG::INTERACTIVE | GG::DRAGABLE | GG::MODAL)
{}

void PasswordEnterWnd::CompleteConstruction() {
    CUIWnd::CompleteConstruction();

    auto auth_desc_label = GG::Wnd::Create<CUILabel>(UserString("AUTHENTICATION_DESC"), GG::FORMAT_LEFT | GG::FORMAT_WORDBREAK);
    auto player_name_label = GG::Wnd::Create<CUILabel>(UserString("PLAYER_NAME_LABEL"), GG::FORMAT_LEFT);
    m_player_name_edit = GG::Wnd::Create<CUIEdit>("");
    auto password_label = GG::Wnd::Create<CUILabel>(UserString("PASSWORD_LABEL"), GG::FORMAT_LEFT);
    m_password_edit = GG::Wnd::Create<CensoredCUIEdit>("");
    m_ok_bn = Wnd::Create<CUIButton>(UserString("OK"));
    m_cancel_bn = Wnd::Create<CUIButton>(UserString("CANCEL"));

    static constexpr GG::X OK_CANCEL_BUTTON_WIDTH{100};
    static constexpr int CONTROL_MARGIN = 5;

    auto layout = GG::Wnd::Create<GG::Layout>(GG::X0, GG::Y0, GG::X1, GG::Y1, 4, 4, CONTROL_MARGIN);
    layout->SetMinimumColumnWidth(0, std::max(player_name_label->MinUsableSize().x,
                                              password_label->MinUsableSize().x) + CONTROL_MARGIN);
    layout->SetColumnStretch(1, 1.0);
    layout->SetMinimumColumnWidth(2, OK_CANCEL_BUTTON_WIDTH + CONTROL_MARGIN);
    layout->SetMinimumColumnWidth(3, OK_CANCEL_BUTTON_WIDTH + CONTROL_MARGIN);
    layout->SetMinimumRowHeight(0, auth_desc_label->Height() + (2 * CONTROL_MARGIN));
    layout->SetMinimumRowHeight(1, m_player_name_edit->Height() + CONTROL_MARGIN);
    layout->SetMinimumRowHeight(2, m_password_edit->Height() + CONTROL_MARGIN);
    layout->SetMinimumRowHeight(3, m_ok_bn->MinUsableSize().y + CONTROL_MARGIN);

    layout->Add(auth_desc_label, 0, 0, 1, 4, GG::ALIGN_VCENTER);
    layout->Add(player_name_label, 1, 0, 1, 1, GG::ALIGN_VCENTER);
    layout->Add(m_player_name_edit, 1, 1, 1, 3, GG::ALIGN_VCENTER);
    layout->Add(password_label, 2, 0, 1, 1, GG::ALIGN_VCENTER);
    layout->Add(m_password_edit, 2, 1, 1, 3, GG::ALIGN_VCENTER);
    layout->Add(m_ok_bn, 3, 2);
    layout->Add(m_cancel_bn, 3, 3);

    SetLayout(layout);

    ResetDefaultPosition();

    m_ok_bn->LeftClickedSignal.connect(boost::bind(&PasswordEnterWnd::OkClicked, this));
    m_cancel_bn->LeftClickedSignal.connect(boost::bind(&PasswordEnterWnd::CancelClicked, this));
}

GG::Rect PasswordEnterWnd::CalculatePosition() const {
    GG::Pt new_ul((GG::GUI::GetGUI()->AppWidth() - WINDOW_WIDTH) / 2,
                  (GG::GUI::GetGUI()->AppHeight() - WINDOW_HEIGHT) / 2);
    GG::Pt new_sz(WINDOW_WIDTH, WINDOW_HEIGHT);
    return GG::Rect(new_ul, new_ul + new_sz);
}

void PasswordEnterWnd::ModalInit()
{ GG::GUI::GetGUI()->SetFocusWnd(m_password_edit); }

void PasswordEnterWnd::KeyPress(GG::Key key, uint32_t key_code_point, GG::Flags<GG::ModKey> mod_keys) {
    if (key == GG::Key::GGK_ESCAPE) { // Same behaviour as if "Cancel" was pressed
        CancelClicked();
    } else if (key == GG::Key::GGK_RETURN || key == GG::Key::GGK_KP_ENTER) {
        if (GG::GUI::GetGUI()->FocusWnd() == m_player_name_edit) {
            GG::GUI::GetGUI()->SetFocusWnd(m_password_edit);
        } else if (GG::GUI::GetGUI()->FocusWnd() == m_password_edit) {
            OkClicked();
        }
    }
}

void PasswordEnterWnd::SetPlayerName(const std::string& player_name) {
    m_player_name_edit->SetText(player_name);
    m_password_edit->SetText("");
}

void PasswordEnterWnd::OkClicked() {
    GGHumanClientApp::GetApp()->Networking().SendMessage(
        AuthResponseMessage(*m_player_name_edit, m_password_edit->RawText()));
    // hide window
    GGHumanClientApp::GetApp()->Remove(shared_from_this());
}

void PasswordEnterWnd::CancelClicked()
{ GGHumanClientApp::GetApp()->CancelMultiplayerGameFromLobby(); }