From f2a444507cb8b5da6854dd25a3f52c5f4699e4d2 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Wed, 12 Feb 2014 11:35:48 +0100 Subject: [PATCH] GL - FIxed Button state flickering, added flag resizeToScreenAspectRatio. --- .../GameClientState/Buttons/ButtonEllipse.h | 18 +++++------ .../GameClientState/Buttons/ButtonRectangle.h | 16 +++++----- .../GameClientState/Buttons/EventButtonGUI.h | 26 +++++++++++----- .../GameClientState/LoginState.cpp | 31 ++++++++++--------- Code/Misc/EventHandler/EventButton.h | 4 +++ 5 files changed, 56 insertions(+), 39 deletions(-) diff --git a/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonEllipse.h b/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonEllipse.h index e74d4233..291bf07d 100644 --- a/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonEllipse.h +++ b/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonEllipse.h @@ -21,17 +21,17 @@ namespace DanBias ButtonEllipse() : EventButtonGUI(), radius(0) {} - ButtonEllipse(std::wstring textureName, Owner owner, float xPos, float yPos, float textureWidth, float textureHeight) - : EventButtonGUI(textureName, owner, xPos, yPos, textureWidth, textureHeight) + ButtonEllipse(std::wstring textureName, Owner owner, float xPos, float yPos, float textureWidth, float textureHeight, bool resizeToScreenAspectRatio = true) + : EventButtonGUI(textureName, owner, xPos, yPos, textureWidth, textureHeight, resizeToScreenAspectRatio) {} - ButtonEllipse(std::wstring textureName, EventFunc func, float xPos, float yPos, float textureWidth, float textureHeight) - : EventButtonGUI(textureName, func, xPos, yPos, textureWidth, textureHeight) + ButtonEllipse(std::wstring textureName, EventFunc func, float xPos, float yPos, float textureWidth, float textureHeight, bool resizeToScreenAspectRatio = true) + : EventButtonGUI(textureName, func, xPos, yPos, textureWidth, textureHeight, resizeToScreenAspectRatio) {} - ButtonEllipse(std::wstring textureName, EventFunc func, Owner owner, float xPos, float yPos, float textureWidth, float textureHeight) - : EventButtonGUI(textureName, func, owner, xPos, yPos, textureWidth, textureHeight) + ButtonEllipse(std::wstring textureName, EventFunc func, Owner owner, float xPos, float yPos, float textureWidth, float textureHeight, bool resizeToScreenAspectRatio = true) + : EventButtonGUI(textureName, func, owner, xPos, yPos, textureWidth, textureHeight, resizeToScreenAspectRatio) {} - ButtonEllipse(std::wstring textureName, EventFunc func, Owner owner, void* userData, float xPos, float yPos, float textureWidth, float textureHeight) - : EventButtonGUI(textureName, func, owner, userData, xPos, yPos, textureWidth, textureHeight) + ButtonEllipse(std::wstring textureName, EventFunc func, Owner owner, void* userData, float xPos, float yPos, float textureWidth, float textureHeight, bool resizeToScreenAspectRatio = true) + : EventButtonGUI(textureName, func, owner, userData, xPos, yPos, textureWidth, textureHeight, resizeToScreenAspectRatio) {} virtual ~ButtonEllipse() {} @@ -40,9 +40,9 @@ namespace DanBias bool Collision(InputClass* inputObject) { POINT p; + RECT r; GetCursorPos(&p); ScreenToClient(WindowShell::GetHWND(), &p); - RECT r; GetClientRect(WindowShell::GetHWND(), &r); //Should come from the InputClass diff --git a/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonRectangle.h b/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonRectangle.h index 1c725211..a1aac005 100644 --- a/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonRectangle.h +++ b/Code/Game/DanBiasGame/GameClientState/Buttons/ButtonRectangle.h @@ -21,17 +21,17 @@ namespace DanBias ButtonRectangle() : EventButtonGUI(), width(0), height(0) {} - ButtonRectangle(std::wstring textureName, Owner owner, float xPos, float yPos, float width, float height) - : EventButtonGUI(textureName, owner, xPos, yPos, width, height) + ButtonRectangle(std::wstring textureName, Owner owner, float xPos, float yPos, float width, float height, bool resizeToScreenAspectRatio = true) + : EventButtonGUI(textureName, owner, xPos, yPos, width, height, resizeToScreenAspectRatio) {} - ButtonRectangle(std::wstring textureName, EventFunc func, float xPos, float yPos, float width, float height) - : EventButtonGUI(textureName, func, xPos, yPos, width, height) + ButtonRectangle(std::wstring textureName, EventFunc func, float xPos, float yPos, float width, float height, bool resizeToScreenAspectRatio = true) + : EventButtonGUI(textureName, func, xPos, yPos, width, height, resizeToScreenAspectRatio) {} - ButtonRectangle(std::wstring textureName, EventFunc func, Owner owner, float xPos, float yPos, float width, float height) - : EventButtonGUI(textureName, func, owner, xPos, yPos, width, height) + ButtonRectangle(std::wstring textureName, EventFunc func, Owner owner, float xPos, float yPos, float width, float height, bool resizeToScreenAspectRatio = true) + : EventButtonGUI(textureName, func, owner, xPos, yPos, width, height, resizeToScreenAspectRatio) {} - ButtonRectangle(std::wstring textureName, EventFunc func, Owner owner, void* userData, float xPos, float yPos, float width, float height) - : EventButtonGUI(textureName, func, owner, userData, xPos, yPos, width, height) + ButtonRectangle(std::wstring textureName, EventFunc func, Owner owner, void* userData, float xPos, float yPos, float width, float height, bool resizeToScreenAspectRatio = true) + : EventButtonGUI(textureName, func, owner, userData, xPos, yPos, width, height, resizeToScreenAspectRatio) {} virtual ~ButtonRectangle() {} diff --git a/Code/Game/DanBiasGame/GameClientState/Buttons/EventButtonGUI.h b/Code/Game/DanBiasGame/GameClientState/Buttons/EventButtonGUI.h index 73bccc49..e71dd0fa 100644 --- a/Code/Game/DanBiasGame/GameClientState/Buttons/EventButtonGUI.h +++ b/Code/Game/DanBiasGame/GameClientState/Buttons/EventButtonGUI.h @@ -19,25 +19,29 @@ namespace DanBias EventButtonGUI() : EventButton(), xPos(0), yPos(0), width(0), height(0), texture(NULL) {} - EventButtonGUI(std::wstring textureName, Owner owner, float xPos, float yPos, float width, float height) + EventButtonGUI(std::wstring textureName, Owner owner, float xPos, float yPos, float width, float height, bool resizeToScreenAspectRatio = true) : EventButton(owner), xPos(xPos), yPos(yPos), width(width), height(height), texture(NULL) { CreateTexture(textureName); + if(resizeToScreenAspectRatio) ResizeWithAspectRatio(); } - EventButtonGUI(std::wstring textureName, EventFunc func, float xPos, float yPos, float width, float height) + EventButtonGUI(std::wstring textureName, EventFunc func, float xPos, float yPos, float width, float height, bool resizeToScreenAspectRatio = true) : EventButton(func), xPos(xPos), yPos(yPos), width(width), height(height), texture(NULL) { CreateTexture(textureName); + if(resizeToScreenAspectRatio) ResizeWithAspectRatio(); } - EventButtonGUI(std::wstring textureName, EventFunc func, Owner owner, float xPos, float yPos, float width, float height) + EventButtonGUI(std::wstring textureName, EventFunc func, Owner owner, float xPos, float yPos, float width, float height, bool resizeToScreenAspectRatio = true) : EventButton(func, owner), xPos(xPos), yPos(yPos), width(width), height(height), texture(NULL) { CreateTexture(textureName); + if(resizeToScreenAspectRatio) ResizeWithAspectRatio(); } - EventButtonGUI(std::wstring textureName, EventFunc func, Owner owner, void* userData, float xPos, float yPos, float width, float height) + EventButtonGUI(std::wstring textureName, EventFunc func, Owner owner, void* userData, float xPos, float yPos, float width, float height, bool resizeToScreenAspectRatio = true) : EventButton(func, owner, userData), xPos(xPos), yPos(yPos), width(width), height(height), texture(NULL) { CreateTexture(textureName); + if(resizeToScreenAspectRatio) ResizeWithAspectRatio(); } virtual ~EventButtonGUI() { @@ -60,26 +64,32 @@ namespace DanBias if(EventButton::GetState() == ButtonState_None) { - Oyster::Graphics::API::RenderGuiElement(texture, Oyster::Math::Float2(xPos, yPos), Oyster::Math::Float2(width, height), Oyster::Math::Float3(1, 1, 1)); + Oyster::Graphics::API::RenderGuiElement(texture, Oyster::Math::Float2(xPos, yPos), Oyster::Math::Float2(width, height), Oyster::Math::Float3(1.0f, 1.0f, 1.0f)); } else if(EventButton::GetState() == ButtonState_Hover) { - Oyster::Graphics::API::RenderGuiElement(texture, Oyster::Math::Float2(xPos, yPos), Oyster::Math::Float2(width, height), Oyster::Math::Float3(0, 1, 0)); + Oyster::Graphics::API::RenderGuiElement(texture, Oyster::Math::Float2(xPos, yPos), Oyster::Math::Float2(width, height), Oyster::Math::Float3(0.0f, 1.0f, 0.0f)); } else { - Oyster::Graphics::API::RenderGuiElement(texture, Oyster::Math::Float2(xPos, yPos), Oyster::Math::Float2(width, height), Oyster::Math::Float3(1, 0, 0)); + Oyster::Graphics::API::RenderGuiElement(texture, Oyster::Math::Float2(xPos, yPos), Oyster::Math::Float2(width, height), Oyster::Math::Float3(1.0f, 0.0f, 0.0f)); } } } + void ResizeWithAspectRatio() + { + RECT r; + GetClientRect(WindowShell::GetHWND(), &r); + height *= (float)r.right/(float)r.bottom; + } + protected: float xPos, yPos; float width, height; Oyster::Graphics::API::Texture texture; - }; } } diff --git a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp index 7424b7b2..b4dcdc45 100644 --- a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp @@ -26,7 +26,7 @@ struct LoginState::myData //Menu button collection EventButtonCollection* collection; - + bool createGame; int testNumber; }privData; @@ -51,7 +51,7 @@ void LoginState::ButtonCallback(Oyster::Event::ButtonEvent& e) switch(type) { case Create: - /*if(e.state == ButtonState_None) + if(e.state == ButtonState_None) { int a = 0; std::cout << "None" << std::endl; @@ -76,7 +76,8 @@ void LoginState::ButtonCallback(Oyster::Event::ButtonEvent& e) //Change to create state or something similar int a = 0; std::cout << "Released" << std::endl; - }*/ + e.owner->privData->createGame = true; + } break; case Options: break; @@ -110,22 +111,23 @@ bool LoginState::Init(Oyster::Network::NetworkClient* nwClient) //Create menu buttons privData->collection = new EventButtonCollection; EventHandler::Instance().AddCollection(privData->collection); - privData->collection->AddButton(new ButtonEllipse(L"circle.png", &LoginState::ButtonCallback, this, (void*)Create, 0.2f, 0.2f, 0.1f, 0.2f)); - privData->collection->AddButton(new ButtonEllipse(L"circle.png", &LoginState::ButtonCallback, this, (void*)Create, 0.2f, 0.3f, 0.1f, 0.2f)); - privData->collection->AddButton(new ButtonEllipse(L"circle.png", &LoginState::ButtonCallback, this, (void*)Create, 0.2f, 0.4f, 0.1f, 0.2f)); - privData->collection->AddButton(new ButtonEllipse(L"circle.png", &LoginState::ButtonCallback, this, (void*)Create, 0.2f, 0.5f, 0.1f, 0.2f)); + privData->collection->AddButton(new ButtonEllipse(L"circle.png", &LoginState::ButtonCallback, this, (void*)Options, 0.2f, 0.2f, 0.1f, 0.1f)); + privData->collection->AddButton(new ButtonEllipse(L"circle.png", &LoginState::ButtonCallback, this, (void*)Options, 0.2f, 0.3f, 0.1f, 0.1f)); + privData->collection->AddButton(new ButtonEllipse(L"circle.png", &LoginState::ButtonCallback, this, (void*)Options, 0.2f, 0.4f, 0.1f, 0.1f)); + privData->collection->AddButton(new ButtonEllipse(L"circle.png", &LoginState::ButtonCallback, this, (void*)Options, 0.2f, 0.5f, 0.1f, 0.1f)); - privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.15f, 0.05f, 0.1f, 0.1f)); - privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.25f, 0.05f, 0.1f, 0.1f)); - privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.35f, 0.05f, 0.1f, 0.1f)); - privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.45f, 0.05f, 0.1f, 0.1f)); + privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Options, 0.15f, 0.05f, 0.1f, 0.1f)); + privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Options, 0.25f, 0.05f, 0.1f, 0.1f)); + privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Options, 0.35f, 0.05f, 0.1f, 0.1f)); + privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Options, 0.45f, 0.05f, 0.1f, 0.1f)); privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.5f, 0.5f, 0.3f, 0.3f)); //Incr/decr buttons privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Incr, 0.85f, 0.2f, 0.1f, 0.1f)); privData->collection->AddButton(new ButtonRectangle(L"button.png", &LoginState::ButtonCallback, this, (void*)Decr, 0.55f, 0.2f, 0.1f, 0.1f)); - + + privData->createGame = false; privData->testNumber = 0; return true; @@ -182,7 +184,7 @@ GameClientState::ClientState LoginState::Update(float deltaTime, InputClass* Key // check data from server // create game - if( KeyInput->IsKeyPressed(DIK_C)) + if( KeyInput->IsKeyPressed(DIK_C) || privData->createGame) { DanBias::GameServerAPI::ServerInitDesc desc; @@ -239,8 +241,9 @@ bool LoginState::Render(float dt) wchar_t temp[10]; _itow_s(privData->testNumber, temp, 10); number = temp; + Oyster::Graphics::API::StartTextRender(); - Oyster::Graphics::API::RenderText(number, Oyster::Math::Float2(0.7, 0.2), Oyster::Math::Float2(0.1, 0.1), Oyster::Math::Float3(1, 0, 0)); + Oyster::Graphics::API::RenderText(number, Oyster::Math::Float2(0.7f, 0.2f), Oyster::Math::Float2(0.1f, 0.1f*(1008.0f/730.0f)), Oyster::Math::Float3(1.0f, 0.0f, 0.0f)); Oyster::Graphics::API::EndFrame(); return true; diff --git a/Code/Misc/EventHandler/EventButton.h b/Code/Misc/EventHandler/EventButton.h index 6e99fcc7..b891889d 100644 --- a/Code/Misc/EventHandler/EventButton.h +++ b/Code/Misc/EventHandler/EventButton.h @@ -160,6 +160,10 @@ namespace Oyster clicked = true; currentState = ButtonState_Pressed; } + else + { + currentState = ButtonState_Hover; + } break; case ButtonState_Released: currentState = ButtonState_Hover;