📄 myconsole.cpp
字号:
#include "myconsole.h"
#include "world.h"
#include "msgchat.h"
#include "msglogon.h"
MyConsole::MyConsole()
{
setThreaded( true );
}
MyConsole::~MyConsole()
{
}
bool MyConsole::OnInit(void)
{
_frame = new MyFrame(wxT("MyConsole"));
_frame->_world= & getWorld();
_frame->Show(true);
return true;
}
void MyConsole::operator () (void)
{
try{
int argc= getWorld().getArgc();
char** argv= getWorld().getArgv();
wxEntry( NULL, NULL );
}catch( exception& e ){
cerr << "wxWidgets error: " << e.what() << endl;
}catch( ... ){ cerr << "wxWidgets error: " << "Unknown exception" << endl;
}
}
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU( wxID_ABOUT, MyFrame::OnAbout )
EVT_MENU( wxID_EXIT, MyFrame::OnQuit )
EVT_BUTTON( MY_LOGON, MyFrame::OnLogon )
EVT_BUTTON( MY_LOGOUT, MyFrame::OnLogout )
EVT_TEXT_ENTER( MY_INPUT, MyFrame::OnEnter )
END_EVENT_TABLE()
MyFrame::MyFrame( const wxString& __title )
: wxFrame( NULL, wxID_ANY, __title, wxDefaultPosition, wxSize( 640, 480 ) )
{
_conn= NULL;
wxMenu *fileMenu = new wxMenu;
wxMenu *helpMenu = new wxMenu;
helpMenu->Append( wxID_ABOUT, wxT( "&About...\tF1" ), wxT( "Show about dialog" ) );
fileMenu->Append( wxID_EXIT, wxT( "E&xit\tAlt-X" ), wxT( "Quit this program" ) );
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append( fileMenu, wxT( "&File" ) );
menuBar->Append( helpMenu, wxT( "&Help" ) );
SetMenuBar( menuBar );
CreateStatusBar( 2 );
SetStatusText( wxT( "Console ready" ), 0 );
SetStatusText( wxT( "Offline" ), 1 );
wxBoxSizer *sizer0 = new wxBoxSizer( wxHORIZONTAL );
wxBoxSizer *sizer1 = new wxBoxSizer( wxVERTICAL );
_output= new wxTextCtrl( this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY|wxTE_RICH2|wxTE_AUTO_URL );
_input= new wxTextCtrl( this, MY_INPUT, "", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER|wxTE_RICH2 );
sizer1->Add( _output, 1, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
sizer1->Add( _input, 0, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
wxBoxSizer *sizer2 = new wxBoxSizer( wxVERTICAL );
wxBoxSizer *sizer3 = new wxBoxSizer( wxHORIZONTAL );
_ip= new wxTextCtrl( this, wxID_ANY, "127.0.0.1" );
_port= new wxTextCtrl( this, wxID_ANY, "55555" );
sizer3->Add( _ip, 3, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
sizer3->Add( _port, 1, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
wxBoxSizer *sizer4 = new wxBoxSizer( wxHORIZONTAL );
_name_ctl= new wxTextCtrl( this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_RICH2 );
_password_ctl= new wxTextCtrl( this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD|wxTE_RICH2 );
sizer4->Add( _name_ctl, 1, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
sizer4->Add( _password_ctl, 1, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
wxBoxSizer *sizer5 = new wxBoxSizer( wxHORIZONTAL );
_logon_button= new wxButton( this, MY_LOGON, "Logon" );
_logout_button= new wxButton( this, MY_LOGOUT, "Logout" );
_logout_button->Enable( false );
sizer5->Add( _logon_button, 1, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
sizer5->Add( _logout_button, 1, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
sizer2->Add( sizer3, 0, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
sizer2->Add( sizer4, 0, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
sizer2->Add( sizer5, 0, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
sizer0->Add( sizer1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
sizer0->Add( sizer2, 1, wxALL|wxEXPAND|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL );
SetSizer( sizer0 );
(*this) << "------------Welcome------------" << "\n";
}
MyFrame::~MyFrame()
{
wxCommandEvent ev;
OnLogout( ev );
_world->setState( SHUT_DOWN );
}
MyFrame& MyFrame::operator << ( const string __str )
{
(*_output) << wxString( __str.c_str() );
return *this;
}
void MyFrame::OnQuit( wxCommandEvent& _event )
{
(*this) << "------------Quiting------------" << "\n";
Close();
}
void MyFrame::OnAbout( wxCommandEvent& _event )
{
wxString msg;
msg.Printf( wxT("MyConsole in MMO with %s"), wxVERSION_STRING );
wxMessageBox( msg, wxT("About MyConsole"), wxOK | wxICON_INFORMATION, this );
}
void MyFrame::OnLogon( wxCommandEvent& _event )
{
string ip= _ip->GetValue();
long port= 0;
bool port_ok= _port->GetValue().ToLong( &port );
_name= _name_ctl->GetValue();
_password= _password_ctl->GetValue();
if( !_conn && ip!="" && port_ok && _name!="" ){
_conn= _world->getNetwork().connect( ip.c_str(), port );
if( _conn ){
_conn->setRemote( this );
MsgLogon* logon= new MsgLogon( _name, _password );
_conn->send( shared_ptr<Message>(logon) );
_ip->SetEditable( false );
_port->SetEditable( false );
_name_ctl->SetEditable( false );
_password_ctl->SetEditable( false );
_logon_button->Enable( false );
_logout_button->Enable( true );
SetStatusText( wxT( "Ready to communicate" ), 0 );
SetStatusText( wxT( "Online" ), 1 );
(*this) << "------------Logoned------------" << "\n";
}else{
SetStatusText( wxT( "Console ready" ), 0 );
SetStatusText( wxT( "Can't connect to remote host" ), 1 );
}
}
}
void MyFrame::OnLogout( wxCommandEvent& _event )
{
if( _conn ){
_world->getNetwork().disconnect( _conn );
_conn= NULL;
_ip->SetEditable( true );
_port->SetEditable( true );
_name_ctl->SetEditable( true );
_password_ctl->SetEditable( true );
_logon_button->Enable( true );
_logout_button->Enable( false );
SetStatusText( wxT( "Console ready" ), 0 );
SetStatusText( wxT( "Offline" ), 1 );
_ip->SetSelection( -1, -1 );
_port->SetSelection( -1, -1 );
_name_ctl->SetSelection( -1, -1 );
_password_ctl->SetSelection( -1, -1 );
(*this) << "------------Logouted-----------" << "\n";
}
}
void MyFrame::OnEnter( wxCommandEvent& _event )
{
if( _conn ){
string msgstr= _input->GetValue();
MsgChat* msg= new MsgChat( msgstr );
_conn->send( shared_ptr<Message>(msg) );
_input->SetSelection( -1, -1 );
//cout << "msg sent" << endl;
}else{
SetStatusText( wxT( "Can't send message while offline" ), 0 );
SetStatusText( wxT( "Offline" ), 1 );
}
}
void MyFrame::disconnected(void)
{
if( _conn ){
_world->getNetwork().disconnect( _conn );
_conn= NULL;
_ip->SetEditable( true );
_port->SetEditable( true );
_name_ctl->SetEditable( true );
_password_ctl->SetEditable( true );
_logon_button->Enable( true );
_logout_button->Enable( false );
SetStatusText( wxT( "Console ready" ), 0 );
SetStatusText( wxT( "Offline" ), 1 );
_ip->SetSelection( -1, -1 );
_port->SetSelection( -1, -1 );
_name_ctl->SetSelection( -1, -1 );
_password_ctl->SetSelection( -1, -1 );
(*this) << "----------Disconnected---------" << "\n";
}
}
DECLARE_APP(MyConsole)
IMPLEMENT_APP_NO_MAIN(MyConsole)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -