Danbias/Code/Game/DanBiasGame/GameClientState/LanMenuState.cpp

138 lines
3.5 KiB
C++
Raw Normal View History

2014-01-30 23:23:37 +01:00
#include "LanMenuState.h"
#include "C_obj/C_Player.h"
#include "C_obj/C_StaticObj.h"
#include "C_obj/C_DynamicObj.h"
#include "DllInterfaces/GFXAPI.h"
2014-01-31 14:14:20 +01:00
#include "LobbyState.h"
#include "GameState.h"
2014-02-12 10:43:06 +01:00
#include "../Network/NetworkAPI/NetworkClient.h"
2014-01-31 14:14:20 +01:00
2014-02-13 10:14:44 +01:00
#include "EventHandler\EventHandler.h"
#include "Buttons\ButtonRectangle.h"
2014-02-14 09:40:53 +01:00
#include "Buttons\TextField.h"
2014-02-13 10:14:44 +01:00
2014-01-30 23:23:37 +01:00
#include <GameServerAPI.h>
2014-02-13 10:14:44 +01:00
#include <string>
2014-01-30 23:23:37 +01:00
2014-02-12 10:43:06 +01:00
using namespace ::DanBias::Client;
2014-02-12 16:31:15 +01:00
using namespace ::Oyster;
2014-02-12 10:43:06 +01:00
using namespace ::Oyster::Network;
2014-02-13 10:14:44 +01:00
using namespace ::Oyster::Event;
using namespace ::Oyster::Math3D;
2014-01-30 23:23:37 +01:00
2014-02-12 16:31:15 +01:00
struct LanMenuState::MyData
2014-01-30 23:23:37 +01:00
{
2014-02-12 16:31:15 +01:00
MyData(){}
2014-01-31 14:14:20 +01:00
2014-02-12 16:31:15 +01:00
GameClientState::ClientState nextState;
NetworkClient *nwClient;
2014-02-13 10:14:44 +01:00
Graphics::API::Texture background;
2014-02-14 09:40:53 +01:00
EventButtonCollection guiElements;
TextField<LanMenuState*> *connectIP;
2014-02-13 10:14:44 +01:00
unsigned short connectPort;
2014-02-12 09:49:08 +01:00
} privData;
2014-01-30 23:23:37 +01:00
2014-02-14 09:40:53 +01:00
void OnButtonInteract_Connect( Oyster::Event::ButtonEvent<LanMenuState*>& e );
2014-02-13 10:14:44 +01:00
2014-02-12 09:49:08 +01:00
LanMenuState::LanMenuState() {}
2014-01-30 23:23:37 +01:00
2014-02-13 10:14:44 +01:00
LanMenuState::~LanMenuState()
{
if( this->privData )
this->Release();
}
2014-01-30 23:23:37 +01:00
2014-02-12 16:31:15 +01:00
bool LanMenuState::Init(Network::NetworkClient* nwClient)
2014-01-30 23:23:37 +01:00
{
2014-02-13 10:14:44 +01:00
this->privData = new MyData();
2014-01-30 23:23:37 +01:00
2014-02-12 16:31:15 +01:00
this->privData->nextState = GameClientState::ClientState_Same;
this->privData->nwClient = nwClient;
2014-01-30 23:23:37 +01:00
2014-02-13 10:14:44 +01:00
this->privData->background = Graphics::API::CreateTexture( L"grass_md.png" );
2014-02-14 09:40:53 +01:00
// create guiElements
ButtonRectangle<LanMenuState*> *guiElements;
2014-02-14 11:26:50 +01:00
guiElements = new ButtonRectangle<LanMenuState*>( L"earth_md.png", L"Connect", Float3(1.0f), OnButtonInteract_Connect, this, Float3(0.5f, 0.2f, 0.5f), Float2(0.3f, 0.1f), ResizeAspectRatio_Width );
2014-02-14 09:40:53 +01:00
this->privData->guiElements.AddButton( guiElements );
this->privData->connectIP = new TextField<LanMenuState*>( L"earth_md.png", Float3(1.0f), this, Float3(0.1f, 0.2f, 0.5f), Float2(0.45f, 0.1f), ResizeAspectRatio_Width );
this->privData->connectIP->ReserveLines( 1 );
(*this->privData->connectIP)[0] = L"127.0.0.1";
this->privData->connectIP->SetTextHeight( 0.1f );
this->privData->connectIP->SetLineSpacing( 0.0f );
2014-02-13 10:14:44 +01:00
2014-02-14 09:40:53 +01:00
this->privData->guiElements.AddButton( this->privData->connectIP );
2014-02-13 10:14:44 +01:00
2014-02-14 09:40:53 +01:00
// bind guiElements collection to the singleton eventhandler
EventHandler::Instance().AddCollection( &this->privData->guiElements );
2014-02-13 10:14:44 +01:00
this->privData->connectPort = 15151;
2014-01-30 23:23:37 +01:00
return true;
}
GameClientState::ClientState LanMenuState::Update(float deltaTime, InputClass* KeyInput)
2014-01-31 14:14:20 +01:00
{
2014-02-13 10:14:44 +01:00
MouseInput mouseState;
2014-01-31 14:14:20 +01:00
{
KeyInput->GetMousePos( mouseState.x, mouseState.y );
2014-02-13 10:14:44 +01:00
mouseState.mouseButtonPressed = KeyInput->IsMousePressed();
2014-01-31 14:14:20 +01:00
}
2014-02-13 10:14:44 +01:00
EventHandler::Instance().Update( mouseState );
2014-01-31 14:14:20 +01:00
2014-02-12 16:31:15 +01:00
return this->privData->nextState;
2014-01-31 14:14:20 +01:00
}
2014-02-12 14:33:56 +01:00
bool LanMenuState::Render( )
2014-01-30 23:23:37 +01:00
{
2014-02-12 16:31:15 +01:00
Graphics::API::NewFrame();
2014-01-30 23:23:37 +01:00
2014-02-12 16:31:15 +01:00
Graphics::API::StartGuiRender();
2014-01-30 23:23:37 +01:00
2014-02-13 10:14:44 +01:00
Graphics::API::RenderGuiElement( this->privData->background, Float2(0.5f), Float2(1.0f) );
2014-02-14 09:40:53 +01:00
this->privData->guiElements.RenderTexture();
Graphics::API::StartTextRender();
this->privData->guiElements.RenderText();
2014-01-30 23:23:37 +01:00
2014-02-12 16:31:15 +01:00
Graphics::API::EndFrame();
2014-01-30 23:23:37 +01:00
return true;
}
bool LanMenuState::Release()
{
privData = NULL;
return true;
2014-02-12 16:31:15 +01:00
}
void LanMenuState::ChangeState( ClientState next )
{
2014-02-13 10:14:44 +01:00
switch( next )
{
case GameClientState::ClientState_Lobby:
// attempt to connect to lobby
2014-02-14 09:40:53 +01:00
if( !this->privData->nwClient->Connect(this->privData->connectPort, (*this->privData->connectIP)[0]) )
2014-02-13 10:14:44 +01:00
return;
break;
default: break;
}
2014-02-12 16:31:15 +01:00
this->privData->nextState = next;
2014-02-13 10:14:44 +01:00
}
2014-02-14 09:40:53 +01:00
void OnButtonInteract_Connect( Oyster::Event::ButtonEvent<LanMenuState*>& e )
2014-02-13 10:14:44 +01:00
{
switch( e.state )
{
case ButtonState_Released:
e.owner->ChangeState( GameClientState::ClientState_LobbyCreate );
break;
default: break;
}
2014-01-30 23:23:37 +01:00
}