GL - FIxed Button state flickering, added flag resizeToScreenAspectRatio.

This commit is contained in:
Pontus Fransson 2014-02-12 11:35:48 +01:00
parent 3a80a71143
commit f2a444507c
5 changed files with 56 additions and 39 deletions

View File

@ -21,17 +21,17 @@ namespace DanBias
ButtonEllipse() ButtonEllipse()
: EventButtonGUI(), radius(0) : EventButtonGUI(), radius(0)
{} {}
ButtonEllipse(std::wstring textureName, Owner owner, float xPos, float yPos, float textureWidth, float 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) : EventButtonGUI(textureName, owner, xPos, yPos, textureWidth, textureHeight, resizeToScreenAspectRatio)
{} {}
ButtonEllipse(std::wstring textureName, EventFunc func, float xPos, float yPos, float textureWidth, float 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) : EventButtonGUI(textureName, func, xPos, yPos, textureWidth, textureHeight, resizeToScreenAspectRatio)
{} {}
ButtonEllipse(std::wstring textureName, EventFunc func, Owner owner, float xPos, float yPos, float textureWidth, float 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) : 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) 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) : EventButtonGUI(textureName, func, owner, userData, xPos, yPos, textureWidth, textureHeight, resizeToScreenAspectRatio)
{} {}
virtual ~ButtonEllipse() virtual ~ButtonEllipse()
{} {}
@ -40,9 +40,9 @@ namespace DanBias
bool Collision(InputClass* inputObject) bool Collision(InputClass* inputObject)
{ {
POINT p; POINT p;
RECT r;
GetCursorPos(&p); GetCursorPos(&p);
ScreenToClient(WindowShell::GetHWND(), &p); ScreenToClient(WindowShell::GetHWND(), &p);
RECT r;
GetClientRect(WindowShell::GetHWND(), &r); GetClientRect(WindowShell::GetHWND(), &r);
//Should come from the InputClass //Should come from the InputClass

View File

@ -21,17 +21,17 @@ namespace DanBias
ButtonRectangle() ButtonRectangle()
: EventButtonGUI(), width(0), height(0) : EventButtonGUI(), width(0), height(0)
{} {}
ButtonRectangle(std::wstring textureName, Owner owner, float xPos, float yPos, float width, float 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) : EventButtonGUI(textureName, owner, xPos, yPos, width, height, resizeToScreenAspectRatio)
{} {}
ButtonRectangle(std::wstring textureName, EventFunc func, float xPos, float yPos, float width, float 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) : EventButtonGUI(textureName, func, xPos, yPos, width, height, resizeToScreenAspectRatio)
{} {}
ButtonRectangle(std::wstring textureName, EventFunc func, Owner owner, float xPos, float yPos, float width, float 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) : 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) 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) : EventButtonGUI(textureName, func, owner, userData, xPos, yPos, width, height, resizeToScreenAspectRatio)
{} {}
virtual ~ButtonRectangle() virtual ~ButtonRectangle()
{} {}

View File

@ -19,25 +19,29 @@ namespace DanBias
EventButtonGUI() EventButtonGUI()
: EventButton(), xPos(0), yPos(0), width(0), height(0), texture(NULL) : 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) : EventButton(owner), xPos(xPos), yPos(yPos), width(width), height(height), texture(NULL)
{ {
CreateTexture(textureName); 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) : EventButton(func), xPos(xPos), yPos(yPos), width(width), height(height), texture(NULL)
{ {
CreateTexture(textureName); 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) : EventButton(func, owner), xPos(xPos), yPos(yPos), width(width), height(height), texture(NULL)
{ {
CreateTexture(textureName); 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) : EventButton(func, owner, userData), xPos(xPos), yPos(yPos), width(width), height(height), texture(NULL)
{ {
CreateTexture(textureName); CreateTexture(textureName);
if(resizeToScreenAspectRatio) ResizeWithAspectRatio();
} }
virtual ~EventButtonGUI() virtual ~EventButtonGUI()
{ {
@ -60,26 +64,32 @@ namespace DanBias
if(EventButton<Owner>::GetState() == ButtonState_None) if(EventButton<Owner>::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<Owner>::GetState() == ButtonState_Hover) else if(EventButton<Owner>::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 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: protected:
float xPos, yPos; float xPos, yPos;
float width, height; float width, height;
Oyster::Graphics::API::Texture texture; Oyster::Graphics::API::Texture texture;
}; };
} }
} }

View File

@ -26,7 +26,7 @@ struct LoginState::myData
//Menu button collection //Menu button collection
EventButtonCollection* collection; EventButtonCollection* collection;
bool createGame;
int testNumber; int testNumber;
}privData; }privData;
@ -51,7 +51,7 @@ void LoginState::ButtonCallback(Oyster::Event::ButtonEvent<LoginState*>& e)
switch(type) switch(type)
{ {
case Create: case Create:
/*if(e.state == ButtonState_None) if(e.state == ButtonState_None)
{ {
int a = 0; int a = 0;
std::cout << "None" << std::endl; std::cout << "None" << std::endl;
@ -76,7 +76,8 @@ void LoginState::ButtonCallback(Oyster::Event::ButtonEvent<LoginState*>& e)
//Change to create state or something similar //Change to create state or something similar
int a = 0; int a = 0;
std::cout << "Released" << std::endl; std::cout << "Released" << std::endl;
}*/ e.owner->privData->createGame = true;
}
break; break;
case Options: case Options:
break; break;
@ -110,22 +111,23 @@ bool LoginState::Init(Oyster::Network::NetworkClient* nwClient)
//Create menu buttons //Create menu buttons
privData->collection = new EventButtonCollection; privData->collection = new EventButtonCollection;
EventHandler::Instance().AddCollection(privData->collection); EventHandler::Instance().AddCollection(privData->collection);
privData->collection->AddButton(new ButtonEllipse<LoginState*>(L"circle.png", &LoginState::ButtonCallback, this, (void*)Create, 0.2f, 0.2f, 0.1f, 0.2f)); privData->collection->AddButton(new ButtonEllipse<LoginState*>(L"circle.png", &LoginState::ButtonCallback, this, (void*)Options, 0.2f, 0.2f, 0.1f, 0.1f));
privData->collection->AddButton(new ButtonEllipse<LoginState*>(L"circle.png", &LoginState::ButtonCallback, this, (void*)Create, 0.2f, 0.3f, 0.1f, 0.2f)); privData->collection->AddButton(new ButtonEllipse<LoginState*>(L"circle.png", &LoginState::ButtonCallback, this, (void*)Options, 0.2f, 0.3f, 0.1f, 0.1f));
privData->collection->AddButton(new ButtonEllipse<LoginState*>(L"circle.png", &LoginState::ButtonCallback, this, (void*)Create, 0.2f, 0.4f, 0.1f, 0.2f)); privData->collection->AddButton(new ButtonEllipse<LoginState*>(L"circle.png", &LoginState::ButtonCallback, this, (void*)Options, 0.2f, 0.4f, 0.1f, 0.1f));
privData->collection->AddButton(new ButtonEllipse<LoginState*>(L"circle.png", &LoginState::ButtonCallback, this, (void*)Create, 0.2f, 0.5f, 0.1f, 0.2f)); privData->collection->AddButton(new ButtonEllipse<LoginState*>(L"circle.png", &LoginState::ButtonCallback, this, (void*)Options, 0.2f, 0.5f, 0.1f, 0.1f));
privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.15f, 0.05f, 0.1f, 0.1f)); privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Options, 0.15f, 0.05f, 0.1f, 0.1f));
privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.25f, 0.05f, 0.1f, 0.1f)); privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Options, 0.25f, 0.05f, 0.1f, 0.1f));
privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.35f, 0.05f, 0.1f, 0.1f)); privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Options, 0.35f, 0.05f, 0.1f, 0.1f));
privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.45f, 0.05f, 0.1f, 0.1f)); privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Options, 0.45f, 0.05f, 0.1f, 0.1f));
privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.5f, 0.5f, 0.3f, 0.3f)); privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Create, 0.5f, 0.5f, 0.3f, 0.3f));
//Incr/decr buttons //Incr/decr buttons
privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Incr, 0.85f, 0.2f, 0.1f, 0.1f)); privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Incr, 0.85f, 0.2f, 0.1f, 0.1f));
privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Decr, 0.55f, 0.2f, 0.1f, 0.1f)); privData->collection->AddButton(new ButtonRectangle<LoginState*>(L"button.png", &LoginState::ButtonCallback, this, (void*)Decr, 0.55f, 0.2f, 0.1f, 0.1f));
privData->createGame = false;
privData->testNumber = 0; privData->testNumber = 0;
return true; return true;
@ -182,7 +184,7 @@ GameClientState::ClientState LoginState::Update(float deltaTime, InputClass* Key
// check data from server // check data from server
// create game // create game
if( KeyInput->IsKeyPressed(DIK_C)) if( KeyInput->IsKeyPressed(DIK_C) || privData->createGame)
{ {
DanBias::GameServerAPI::ServerInitDesc desc; DanBias::GameServerAPI::ServerInitDesc desc;
@ -239,8 +241,9 @@ bool LoginState::Render(float dt)
wchar_t temp[10]; wchar_t temp[10];
_itow_s(privData->testNumber, temp, 10); _itow_s(privData->testNumber, temp, 10);
number = temp; number = temp;
Oyster::Graphics::API::StartTextRender(); 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(); Oyster::Graphics::API::EndFrame();
return true; return true;

View File

@ -160,6 +160,10 @@ namespace Oyster
clicked = true; clicked = true;
currentState = ButtonState_Pressed; currentState = ButtonState_Pressed;
} }
else
{
currentState = ButtonState_Hover;
}
break; break;
case ButtonState_Released: case ButtonState_Released:
currentState = ButtonState_Hover; currentState = ButtonState_Hover;