Danbias/Code/Game/GameClient/GameClientState/MainState.cpp

182 lines
5.5 KiB
C++
Raw Permalink Normal View History

2014-02-12 09:49:08 +01:00
#include "MainState.h"
2014-01-30 11:58:44 +01:00
#include "DllInterfaces/GFXAPI.h"
#include "OysterMath.h"
#include "C_obj/C_Player.h"
#include "C_obj/C_StaticObj.h"
#include "C_obj/C_DynamicObj.h"
#include <GameServerAPI.h>
2014-02-12 10:43:06 +01:00
#include "NetworkClient.h"
2014-01-30 11:58:44 +01:00
2014-02-12 14:33:56 +01:00
#include "EventHandler\EventHandler.h"
#include "Buttons\ButtonRectangle.h"
#include "Buttons\ButtonEllipse.h"
2014-02-12 14:33:56 +01:00
2014-02-12 10:43:06 +01:00
using namespace ::DanBias::Client;
2014-02-12 15:05:36 +01:00
using namespace ::Oyster;
2014-02-12 10:43:06 +01:00
using namespace ::Oyster::Math3D;
using namespace ::Oyster::Network;
2014-02-12 15:05:36 +01:00
using namespace ::Oyster::Event;
2014-02-12 10:43:06 +01:00
using namespace ::Utility::DynamicMemory;
2014-02-12 14:33:56 +01:00
using namespace ::Utility::StaticArray;
2014-01-30 11:58:44 +01:00
2014-02-12 10:43:06 +01:00
struct MainState::MyData
2014-01-30 11:58:44 +01:00
{
2014-02-12 10:43:06 +01:00
MyData() {}
2014-02-12 16:31:15 +01:00
GameClientState::ClientState nextState;
2014-02-12 10:43:06 +01:00
NetworkClient *nwClient;
2014-02-20 16:15:47 +01:00
::Input::Mouse *mouseInput;
2014-02-21 12:31:31 +01:00
Float3 mousePos;
Graphics::API::Texture background, mouseCursor;
EventButtonCollection guiElements;
2014-02-12 10:43:06 +01:00
};
2014-01-30 11:58:44 +01:00
void OnButtonInteract_Create( Oyster::Event::ButtonEvent<MainState*>& e );
void OnButtonInteract_Settings( Oyster::Event::ButtonEvent<MainState*>& e );
void OnButtonInteract_Join( Oyster::Event::ButtonEvent<MainState*>& e );
void OnButtonInteract_Quit( Oyster::Event::ButtonEvent<MainState*>& e );
2014-02-12 14:33:56 +01:00
2014-02-17 11:27:43 +01:00
MainState::MainState() {}
2014-01-30 11:58:44 +01:00
2014-02-17 11:27:43 +01:00
MainState::~MainState()
2014-02-12 16:31:15 +01:00
{
if( this->privData )
this->Release();
}
2014-01-30 11:58:44 +01:00
2014-02-17 11:27:43 +01:00
bool MainState::Init( SharedStateContent &shared )
2014-01-30 11:58:44 +01:00
{
2014-02-12 10:43:06 +01:00
this->privData = new MyData();
2014-02-12 16:31:15 +01:00
this->privData->nextState = GameClientState::ClientState_Same;
2014-02-17 11:27:43 +01:00
this->privData->nwClient = shared.network;
2014-02-20 16:15:47 +01:00
this->privData->mouseInput = shared.mouseDevice;
2014-02-21 12:09:38 +01:00
//this->privData->mouseInput->
2014-02-21 12:31:31 +01:00
this->privData->mousePos = Float3( 0.0f );
2014-02-12 16:31:15 +01:00
this->privData->background = Graphics::API::CreateTexture( L"color_white.png" );
2014-02-25 15:52:15 +01:00
this->privData->mouseCursor = Graphics::API::CreateTexture( L"cursor.png" );
2014-02-12 16:31:15 +01:00
2014-02-12 14:33:56 +01:00
// create buttons
ButtonRectangle<MainState*> *button;
Float4 TextCol = Float4(0.1f,0.1f,0.1f,1.0f);
Float4 BackCol = Float4(1.0f);
Float4 HoverCol = Float4(1.2f);
Float4 PressCol = Float4(1.5f);
2014-02-12 17:20:42 +01:00
//button = new ButtonRectangle<MainState*>( L"color_white.png", L"Create",TextCol, BackCol, HoverCol, PressCol, OnButtonInteract_Create, this, Float3(0.5f, 0.2f, 0.5f), Float2(0.3f, 0.1f));
//this->privData->guiElements.AddButton( button );
button = new ButtonRectangle<MainState*>( L"noedge-btn-lan.png", L"", TextCol, BackCol, HoverCol, PressCol, OnButtonInteract_Join, this, Float3(0.5f, 0.2f, 0.5f), Float2(0.5f, 0.18f));
this->privData->guiElements.AddButton( button );
2014-02-12 17:20:42 +01:00
button = new ButtonRectangle<MainState*>( L"noedge-btn-settings.png", L"", TextCol, BackCol, HoverCol, PressCol, OnButtonInteract_Settings, this, Float3(0.5f, 0.4f, 0.5f), Float2(0.5f, 0.18f));
this->privData->guiElements.AddButton( button );
2014-02-12 17:20:42 +01:00
button = new ButtonRectangle<MainState*>( L"noedge-btn-quit.png", L"", TextCol, BackCol, HoverCol, PressCol, OnButtonInteract_Quit, this, Float3(0.5f, 0.8f, 0.5f), Float2(0.5f, 0.18f));
this->privData->guiElements.AddButton( button );
2014-02-12 14:33:56 +01:00
// bind button collection to the singleton eventhandler
EventHandler::Instance().AddCollection( &this->privData->guiElements );
2014-02-12 14:33:56 +01:00
2014-01-30 11:58:44 +01:00
return true;
}
2014-02-17 11:27:43 +01:00
GameClientState::ClientState MainState::Update( float deltaTime )
2014-01-30 11:58:44 +01:00
{
2014-02-13 13:30:14 +01:00
MouseInput mouseState;
{
2014-02-21 21:04:30 +01:00
::Input::Struct::SAIPointFloat2D pos;
this->privData->mouseInput->GetNormalizedPosition( pos );
2014-02-13 13:30:14 +01:00
2014-02-21 12:31:31 +01:00
this->privData->mousePos.x = mouseState.x = pos.x;
this->privData->mousePos.y = mouseState.y = pos.y;
2014-02-20 16:15:47 +01:00
mouseState.mouseButtonPressed = this->privData->mouseInput->IsBtnDown( ::Input::Enum::SAMI_MouseLeftBtn );
}
2014-02-13 13:30:14 +01:00
EventHandler::Instance().Update( mouseState );
2014-02-12 16:31:15 +01:00
return this->privData->nextState;
2014-01-30 11:58:44 +01:00
}
2014-02-12 09:49:08 +01:00
bool MainState::Render()
2014-01-30 11:58:44 +01:00
{
2014-02-12 15:05:36 +01:00
Graphics::API::NewFrame();
Graphics::API::StartGuiRender();
2014-01-30 11:58:44 +01:00
2014-02-25 15:52:15 +01:00
if(this->privData->mouseInput->IsBtnDown(Input::Enum::SAMI_MouseLeftBtn))
Graphics::API::RenderGuiElement( this->privData->mouseCursor, this->privData->mousePos, Float2(0.15f), Float4(1.0f) );
else
Graphics::API::RenderGuiElement( this->privData->mouseCursor, this->privData->mousePos, Float2(0.15f, 0.24), Float4(1.0f) );
Graphics::API::RenderGuiElement( this->privData->background, Float3(0.5f, 0.5f, 0.9f), Float2(1.0f), Float4(0.0f, 0.0f, 0.0f, 1.0f) );
this->privData->guiElements.RenderTexture();
Graphics::API::StartTextRender();
this->privData->guiElements.RenderText();
2014-01-30 11:58:44 +01:00
2014-02-12 15:05:36 +01:00
Graphics::API::EndFrame();
2014-01-30 11:58:44 +01:00
return true;
}
2014-02-12 09:49:08 +01:00
bool MainState::Release()
2014-01-30 11:58:44 +01:00
{
2014-02-13 13:30:14 +01:00
if( this->privData )
2014-02-12 17:20:42 +01:00
{
Graphics::API::DeleteTexture( this->privData->background );
2014-02-21 12:31:31 +01:00
Graphics::API::DeleteTexture( this->privData->mouseCursor );
EventHandler::Instance().ReleaseCollection( &this->privData->guiElements );
2014-02-12 14:33:56 +01:00
2014-02-13 13:30:14 +01:00
this->privData = NULL;
2014-02-12 17:20:42 +01:00
// button collection will be autoreleased from EventHandler
}
2014-01-30 11:58:44 +01:00
return true;
}
2014-02-12 16:31:15 +01:00
void MainState::ChangeState( ClientState next )
{
this->privData->nextState = next;
}
void OnButtonInteract_Create( Oyster::Event::ButtonEvent<MainState*>& e )
2014-02-12 14:33:56 +01:00
{
2014-02-12 16:31:15 +01:00
switch( e.state )
{
case ButtonState_Released:
e.owner->ChangeState( GameClientState::ClientState_LobbyCreate );
break;
default: break;
}
}
2014-02-12 14:33:56 +01:00
void OnButtonInteract_Settings( Oyster::Event::ButtonEvent<MainState*>& e )
{
switch( e.state )
{
case ButtonState_Released:
//e.owner->ChangeState( GameClientState::ClientState_LobbyCreate );
break;
default: break;
}
}
void OnButtonInteract_Join( Oyster::Event::ButtonEvent<MainState*>& e )
2014-02-12 16:31:15 +01:00
{
switch( e.state )
{
case ButtonState_Released:
e.owner->ChangeState( GameClientState::ClientState_Lan );
break;
default: break;
}
2014-02-12 14:33:56 +01:00
}
2014-02-12 16:31:15 +01:00
void OnButtonInteract_Quit( Oyster::Event::ButtonEvent<MainState*>& e )
2014-02-12 16:31:15 +01:00
{
switch( e.state )
{
case ButtonState_Released:
e.owner->ChangeState( GameClientState::ClientState_Quit );
break;
default: break;
}
}