Danbias/Code/Game/DanBiasGame/GameClientState/Buttons/EventButtonGUI.h

129 lines
4.2 KiB
C
Raw Normal View History

//////////////////////////////////////
// Created by Pontus Fransson 2014 //
//////////////////////////////////////
#ifndef DANBIAS_CLIENT_EVENT_BUTTON_GUI_H
#define DANBIAS_CLIENT_EVENT_BUTTON_GUI_H
#include "../Misc/EventHandler/EventButton.h"
2014-02-12 09:08:38 +01:00
#include "../OysterGraphics/DllInterfaces/GFXAPI.h"
2014-02-13 15:32:54 +01:00
namespace DanBias
{
namespace Client
{
2014-02-13 15:32:54 +01:00
/*Dictates if the texture should be resized based on the screen aspect ratio.
*/
enum ResizeAspectRatio
{
ResizeAspectRatio_None,
ResizeAspectRatio_Width,
ResizeAspectRatio_Height,
ResizeAspectRatio_Count,
ResizeAspectRatio_Unknown = -1
};
template <typename Owner>
class EventButtonGUI : public Oyster::Event::EventButton<Owner>
{
public:
EventButtonGUI()
2014-02-13 15:32:54 +01:00
: EventButton(), pos(0, 0), size(0, 0), texture(NULL), buttonText(""), textColor(0, 0, 0)
{}
2014-02-13 15:32:54 +01:00
EventButtonGUI(std::wstring textureName, std::wstring buttonText, Oyster::Math::Float3 textColor, Owner owner, Oyster::Math::Float3 pos, Oyster::Math::Float2 size, ResizeAspectRatio resize = ResizeAspectRatio_Height)
: EventButton(owner), pos(pos), size(size), texture(NULL), buttonText(buttonText), textColor(textColor)
{
CreateTexture(textureName);
2014-02-13 15:32:54 +01:00
if(resize != ResizeAspectRatio_None) ResizeWithAspectRatio(resize);
}
2014-02-13 15:32:54 +01:00
EventButtonGUI(std::wstring textureName, std::wstring buttonText, Oyster::Math::Float3 textColor, EventFunc func, Oyster::Math::Float3 pos, Oyster::Math::Float2 size, ResizeAspectRatio resize = ResizeAspectRatio_Height)
: EventButton(func), pos(pos), size(size), texture(NULL), buttonText(buttonText), textColor(textColor)
{
CreateTexture(textureName);
2014-02-13 15:32:54 +01:00
if(resize != ResizeAspectRatio_None) ResizeWithAspectRatio(resize);
}
2014-02-13 15:32:54 +01:00
EventButtonGUI(std::wstring textureName, std::wstring buttonText, Oyster::Math::Float3 textColor, EventFunc func, Owner owner, Oyster::Math::Float3 pos, Oyster::Math::Float2 size, ResizeAspectRatio resize = ResizeAspectRatio_Height)
: EventButton(func, owner), pos(pos), size(size), texture(NULL), buttonText(buttonText), textColor(textColor)
{
CreateTexture(textureName);
2014-02-13 15:32:54 +01:00
if(resize != ResizeAspectRatio_None) ResizeWithAspectRatio(resize);
}
2014-02-13 15:32:54 +01:00
EventButtonGUI(std::wstring textureName, std::wstring buttonText, Oyster::Math::Float3 textColor, EventFunc func, Owner owner, void* userData, Oyster::Math::Float3 pos, Oyster::Math::Float2 size, ResizeAspectRatio resize = ResizeAspectRatio_Height)
: EventButton(func, owner, userData), pos(pos), size(size), texture(NULL), buttonText(buttonText), textColor(textColor)
{
CreateTexture(textureName);
2014-02-13 15:32:54 +01:00
if(resize != ResizeAspectRatio_None) ResizeWithAspectRatio(resize);
}
2014-02-12 09:08:38 +01:00
virtual ~EventButtonGUI()
{
Oyster::Graphics::API::DeleteTexture(texture);
texture = NULL;
}
void CreateTexture(std::wstring textureName)
{
//Create texture
texture = Oyster::Graphics::API::CreateTexture(textureName);
}
2014-02-13 15:32:54 +01:00
virtual void RenderTexture()
{
2014-02-11 15:06:46 +01:00
if(EventButton<Owner>::Enabled())
{
//Render att xPos and yPos
2014-02-12 09:08:38 +01:00
//With width and height
if(EventButton<Owner>::GetState() == ButtonState_None)
{
2014-02-13 15:32:54 +01:00
Oyster::Graphics::API::RenderGuiElement(texture, pos.xy, size, Oyster::Math::Float3(1.0f, 1.0f, 1.0f));
2014-02-12 09:08:38 +01:00
}
else if(EventButton<Owner>::GetState() == ButtonState_Hover)
{
2014-02-13 15:32:54 +01:00
Oyster::Graphics::API::RenderGuiElement(texture, pos.xy, size, Oyster::Math::Float3(0.0f, 1.0f, 0.0f));
2014-02-12 09:08:38 +01:00
}
else
{
2014-02-13 15:32:54 +01:00
Oyster::Graphics::API::RenderGuiElement(texture, pos.xy, size, Oyster::Math::Float3(1.0f, 0.0f, 0.0f));
2014-02-12 09:08:38 +01:00
}
2014-02-11 15:06:46 +01:00
}
}
2014-02-13 15:32:54 +01:00
virtual void RenderText()
{
if(buttonText.size() > 0)
{
Oyster::Graphics::API::RenderText(buttonText, pos.xy, size, textColor);
}
}
private:
void ResizeWithAspectRatio(ResizeAspectRatio resize)
{
RECT r;
GetClientRect(WindowShell::GetHWND(), &r);
2014-02-13 15:32:54 +01:00
if(resize == ResizeAspectRatio_Height)
size.y *= (float)r.right/(float)r.bottom;
else if(resize == ResizeAspectRatio_Width)
size.x *= (float)r.bottom/(float)r.right;
}
protected:
2014-02-13 15:32:54 +01:00
Oyster::Math::Float3 pos;
Oyster::Math::Float2 size;
2014-02-12 09:08:38 +01:00
2014-02-13 15:32:54 +01:00
Oyster::Graphics::API::Texture texture;
std::wstring buttonText;
Oyster::Math::Float3 textColor;
};
}
}
#endif