-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremotedialog.cpp
255 lines (228 loc) · 9.04 KB
/
remotedialog.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
sueca - An implementation of the Portuguese game "Sueca" in C++ and wxWidgets
Copyright (C) 2003-2024 Rodrigo Araujo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "remotedialog.hpp"
#include <wx/statbox.h>
#include <wx/textctrl.h>
#include <wx/stattext.h>
#include <wx/msgdlg.h>
#include "main.hpp"
// Remote connection dialog implementation
BEGIN_EVENT_TABLE( RemoteDialog, wxDialog )
EVT_RADIOBUTTON( wxID_ANY, RemoteDialog::OnSelectPosition )
EVT_BUTTON( wxID_OK, RemoteDialog::OnConnect )
EVT_BUTTON( wxID_CANCEL, RemoteDialog::OnCancel )
EVT_CLOSE( RemoteDialog::OnClose )
EVT_CHAT_PANEL_MESSAGE( RemoteDialog::OnChatMessage )
END_EVENT_TABLE();
const char* noconnstr = "<not connected>";
const char* connect_caption = "C&onnect";
RemoteDialog::RemoteDialog( wxWindow* parent ):
wxDialog( parent, wxID_ANY, wxString( "Connect to remote game" ) ), positions( 4 )
{
Sueca& app = wxGetApp();
app.rmtdlg = this;
handler = new RemoteHandler();
wxBoxSizer* top_sizer = new wxBoxSizer( wxVERTICAL );
// IP entry
wxBoxSizer* ip_sizer = new wxBoxSizer( wxHORIZONTAL );
wxStaticText* ip_text = new wxStaticText( this, wxID_ANY, "Host address");
ip_sizer->Add( ip_text, 0, wxRIGHT | wxALIGN_CENTER, 5 );
disconnectedlist.Append( ip_text );
ip_entry = new wxTextCtrl( this, wxID_ANY, app.connect_ip_address, wxDefaultPosition, wxSize( 120, wxID_ANY ) );
ip_sizer->Add( ip_entry, 0, wxALIGN_CENTER | wxRIGHT, 10 );
disconnectedlist.Append( ip_entry );
// Port entry
wxStaticText* port_text = new wxStaticText( this, wxID_ANY, "Port");
ip_sizer->Add( port_text, 0, wxRIGHT | wxALIGN_CENTER, 5 );
disconnectedlist.Append( port_text );
port_entry = new wxTextCtrl( this, wxID_ANY, wxString::Format( "%u", app.ip_port ), wxDefaultPosition, wxSize( 60, wxID_ANY ) );
ip_sizer->Add( port_entry, 0, wxALIGN_CENTER );
disconnectedlist.Append( port_entry );
top_sizer->Add( ip_sizer, 0, wxBOTTOM, 10 );
// Positions
wxStaticBox* pos_box = new wxStaticBox( this, wxID_ANY, "Player positions" );
wxStaticBoxSizer* pos_sizer =
new wxStaticBoxSizer( pos_box, wxVERTICAL );
invisible = new wxRadioButton( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxRB_GROUP );
invisible->Show( false );
invisible->SetValue( true );
wxRadioButton* p1button = new wxRadioButton( this, ID_RADIOBUTTON_P1, noconnstr );
p1button->Enable( false );
connectedlist.Append( p1button );
wxRadioButton* p2button = new wxRadioButton( this, ID_RADIOBUTTON_P2, noconnstr );
p2button->Enable( false );
connectedlist.Append( p2button );
wxRadioButton* p3button = new wxRadioButton( this, ID_RADIOBUTTON_P3, noconnstr );
p3button->Enable( false );
connectedlist.Append( p3button );
wxRadioButton* p4button = new wxRadioButton( this, ID_RADIOBUTTON_P4, noconnstr );
p4button->Enable( false );
connectedlist.Append( p4button );
wxBoxSizer* posmidsizer = new wxBoxSizer( wxHORIZONTAL );
posmidsizer->Add( p4button, 0, wxALIGN_CENTER );
posmidsizer->Add( 10, -1, 1, wxEXPAND );
posmidsizer->Add( p2button, 0, wxALIGN_CENTER );
pos_sizer->Add( p3button, 0, wxALIGN_CENTER);
pos_sizer->Add( -1, 10, 1, wxEXPAND );
pos_sizer->Add( posmidsizer, 1, wxEXPAND);
pos_sizer->Add( -1, 10, 1, wxEXPAND );
pos_sizer->Add( p1button, 0, wxALIGN_CENTER);
top_sizer->Add( pos_sizer, 0, wxEXPAND | wxBOTTOM, 10 );
// Chat stuff
wxStaticBox* chat_box = new wxStaticBox( this, wxID_ANY, "Chat" );
chat_box->Enable( false );
connectedlist.Append( chat_box );
chat = new ChatPanel( this );
chat->Enable( false );
connectedlist.Append( chat );
wxStaticBoxSizer* chat_sizer =
new wxStaticBoxSizer( chat_box, wxVERTICAL );
chat_sizer->Add( chat, 0, wxEXPAND );
top_sizer->Add( chat_sizer, 0, wxEXPAND | wxBOTTOM, 10 );
// Buttons
wxBoxSizer* button_sizer = new wxBoxSizer( wxHORIZONTAL );
connect_button = new wxButton( this, wxID_OK, connect_caption );
button_sizer->Add( connect_button, 0, wxRIGHT, 10 );
button_sizer->Add( new wxButton( this, wxID_CANCEL, "&Cancel" ) );
top_sizer->Add( button_sizer, 0, wxTOP | wxALIGN_CENTER_HORIZONTAL, 5 );
// Invisible border
main_sizer = new wxBoxSizer( wxVERTICAL );
main_sizer->Add( top_sizer, 0, wxALL, 15 );
SetSizer( main_sizer );
main_sizer->SetSizeHints( this );
connect_button->SetDefault();
CentreOnParent();
// Game slots
positions["bottom"] = posarray[ID_RADIOBUTTON_P1] = new RemotePosition( "bottom", p1button );
positions["right"] = posarray[ID_RADIOBUTTON_P2] = new RemotePosition( "right", p2button );
positions["top"] = posarray[ID_RADIOBUTTON_P3] = new RemotePosition( "top", p3button );
positions["left"] = posarray[ID_RADIOBUTTON_P4] = new RemotePosition( "left", p4button );
}
void RemoteDialog::ReLayout()
{
for( int i = ID_RADIOBUTTON_P1; i <= ID_RADIOBUTTON_P4; i++ ) {
wxControl* control = posarray[i]->button;
control->SetSizeHints( -1, -1 );
}
main_sizer->Layout();
main_sizer->SetSizeHints( this );
}
void RemoteDialog::OnSelectPosition( wxCommandEvent& event )
{
RemotePosition* pos = posarray[event.GetId()];
handler->Send( "position:" + pos->name );
event.StopPropagation();
}
void RemoteDialog::ConnectionEndWarn( wxString message )
{
wxMessageBox( message, "Error", wxOK | wxICON_EXCLAMATION );
chat->Write( wxString::Format( "*** %s", message.c_str() ) );
NoConnectionState();
}
void RemoteDialog::NoConnectionState()
{
invisible->SetValue( true );
// Re-enable connection related controls
for( wxWindowList::Node* node = disconnectedlist.GetFirst(); node; node = node->GetNext() )
node->GetData()->Enable( true );
// Disable connection related controls
for( wxWindowList::Node* node = connectedlist.GetFirst(); node; node = node->GetNext() )
node->GetData()->Enable( false );
for( int i = ID_RADIOBUTTON_P1; i <= ID_RADIOBUTTON_P4; i++ ) {
wxRadioButton* button = posarray[i]->button;
button->SetLabel( noconnstr );
}
connect_button->SetLabel( connect_caption );
ReLayout();
}
void RemoteDialog::OnConnect( wxCommandEvent& event )
{
if( handler->GetSocket() ) {
if( handler->IsConnected() )
// Disconnect
chat->Write( "*** Disconnected." );
else
// Stop
chat->Write( "*** Connection aborted." );
handler->SetSocket( NULL );
NoConnectionState();
}
else {
// Connect
// Keep settings as we apply them
Sueca& app = wxGetApp();
// Check IP address
wxString host = ip_entry->GetValue();
host.Trim( true );
host.Trim( false );
if( ! host.Len() ) {
wxMessageBox( "Invalid address.", "Error", wxOK | wxICON_ERROR );
return;
}
// Check port
unsigned long newport;
if( !port_entry->GetValue().ToULong( &newport ) || !newport || newport > PORT_MAX ) {
wxMessageBox( "Invalid port.", "Error", wxOK | wxICON_ERROR );
return;
}
app.connect_ip_address = host;
app.ip_port = newport;
// Connect to specified addresses
wxIPV4address addr;
addr.Service( newport );
addr.Hostname( host );
wxSocketClient* socket = new wxSocketClient( wxSOCKET_NOWAIT );
handler->SetSocket( socket );
socket->Connect( addr, false );
// We're now trying to connect, so disable connection related controls
for( wxWindowList::Node* node = disconnectedlist.GetFirst(); node; node = node->GetNext() )
node->GetData()->Enable( false );
chat->Write( wxString::Format( "*** Connecting to %s:%hu...", host.c_str(), short(newport) ) );
connect_button->SetLabel( "St&op" );
}
}
void RemoteDialog::Done( bool cancel )
{
if( cancel )
delete handler;
wxGetApp().rmtdlg = NULL;
if( IsModal() )
EndModal(0);
else
Destroy();
}
void RemoteDialog::OnChatMessage( ChatPanelMsgEvt& event )
{
handler->Send( "say:" + event.message );
}
RemotePosition* RemoteDialog::SetPosition( wxString& name, const wxString& key )
{
RemPosMap::iterator it = positions.find( key );
// Ignore possibly corrupted server
if( it == positions.end() ||
( ! ValidName( name ) && name.Len() > 0 ) )
return NULL;
RemotePosition* pos = it->second;
if( name.Len() ) {
pos->button->SetLabel( name );
pos->button->Enable( false );
}
else {
pos->button->SetLabel( freestr );
pos->button->Enable( true );
}
return pos;
}