2012-01-08 11:39:03 -06:00
|
|
|
/*
|
|
|
|
* Button.cpp
|
|
|
|
*
|
|
|
|
* Created on: Jan 8, 2012
|
|
|
|
* Author: Simon
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "interface/Button.h"
|
|
|
|
#include "Graphics.h"
|
2012-01-15 13:35:40 -06:00
|
|
|
#include "Global.h"
|
2012-01-08 11:39:03 -06:00
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
Button::Button(State* parent_state, std::string buttonText):
|
|
|
|
Component(parent_state),
|
|
|
|
ButtonText(buttonText),
|
|
|
|
isMouseInside(false),
|
2012-01-15 13:35:40 -06:00
|
|
|
isButtonDown(false),
|
|
|
|
isTogglable(false)
|
2012-01-08 11:39:03 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
Button::Button(Point position, Point size, std::string buttonText):
|
|
|
|
Component(position, size),
|
|
|
|
ButtonText(buttonText),
|
|
|
|
isMouseInside(false),
|
2012-01-15 13:35:40 -06:00
|
|
|
isButtonDown(false),
|
|
|
|
isTogglable(false)
|
2012-01-08 11:39:03 -06:00
|
|
|
{
|
2012-01-14 12:51:24 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Button::Button(std::string buttonText):
|
|
|
|
Component(),
|
|
|
|
ButtonText(buttonText),
|
|
|
|
isMouseInside(false),
|
2012-01-15 13:35:40 -06:00
|
|
|
isButtonDown(false),
|
|
|
|
isTogglable(false)
|
2012-01-14 12:51:24 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-15 13:35:40 -06:00
|
|
|
void Button::SetTogglable(bool togglable)
|
|
|
|
{
|
|
|
|
toggle = false;
|
|
|
|
isTogglable = togglable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::GetTogglable()
|
|
|
|
{
|
|
|
|
return isTogglable;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Button::GetToggleState()
|
|
|
|
{
|
|
|
|
return toggle;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Button::SetToggleState(bool state)
|
|
|
|
{
|
|
|
|
toggle = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
void Button::Draw(const Point& screenPos)
|
|
|
|
{
|
2012-01-15 13:35:40 -06:00
|
|
|
Graphics * g = Global::Ref().g;
|
|
|
|
Point Position = screenPos;
|
2012-01-14 12:51:24 -06:00
|
|
|
// = reinterpret_cast<Graphics*>(userdata);
|
2012-01-08 11:39:03 -06:00
|
|
|
//TODO: Cache text location, that way we don't have the text alignment code here
|
2012-01-15 13:35:40 -06:00
|
|
|
if(isButtonDown || (isTogglable && toggle))
|
2012-01-08 11:39:03 -06:00
|
|
|
{
|
2012-01-14 12:51:24 -06:00
|
|
|
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
|
|
|
|
g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2, ButtonText, 0, 0, 0, 255);
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(isMouseInside)
|
2012-01-14 12:51:24 -06:00
|
|
|
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 20, 20, 20, 255);
|
|
|
|
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
|
|
|
|
g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2, ButtonText, 255, 255, 255, 255);
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
/*sf::RenderWindow* rw = reinterpret_cast<sf::RenderWindow*>(userdata); //it better be a RenderWindow or so help your god
|
|
|
|
|
|
|
|
//Draw component here
|
|
|
|
sf::Text textGraphic(ButtonText);
|
|
|
|
textGraphic.SetCharacterSize(11);
|
|
|
|
if(isButtonDown)
|
|
|
|
textGraphic.SetColor(sf::Color::Black);
|
|
|
|
else
|
|
|
|
textGraphic.SetColor(sf::Color::White);
|
|
|
|
sf::FloatRect tempRect = textGraphic.GetRect();
|
|
|
|
textGraphic.SetPosition(ceil(X + Width/2 - tempRect.Width/2), ceil(Y + Height/2 - tempRect.Height/2));
|
|
|
|
|
|
|
|
if(isMouseInside)
|
|
|
|
{
|
|
|
|
if(isButtonDown)
|
|
|
|
rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
|
|
|
|
else
|
|
|
|
rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::Black, 2.f, sf::Color::White));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(isButtonDown)
|
|
|
|
rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
|
|
|
|
else
|
|
|
|
rw->Draw(sf::Shape::Rectangle(X+1, Y+1, Width-2, Width-2, sf::Color::Black, 1.f, sf::Color::White));
|
|
|
|
}
|
|
|
|
|
|
|
|
rw->Draw(textGraphic);*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::OnMouseUnclick(int x, int y, unsigned int button)
|
|
|
|
{
|
2012-01-14 12:51:24 -06:00
|
|
|
std::cout << "Unclick!" << std::endl;
|
2012-01-08 11:39:03 -06:00
|
|
|
if(button != 1)
|
|
|
|
{
|
|
|
|
return; //left click only!
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isButtonDown)
|
|
|
|
{
|
2012-01-14 12:51:24 -06:00
|
|
|
DoAction();
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
isButtonDown = false;
|
|
|
|
}
|
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
//void Button::OnMouseUp(int x, int y, unsigned int button) //mouse unclick is called before this
|
|
|
|
//{
|
|
|
|
// if(button != 1) return; //left click only!
|
2012-01-08 11:39:03 -06:00
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
// isButtonDown = false;
|
|
|
|
//}
|
2012-01-08 11:39:03 -06:00
|
|
|
|
|
|
|
void Button::OnMouseClick(int x, int y, unsigned int button)
|
|
|
|
{
|
|
|
|
if(button != 1) return; //left click only!
|
2012-01-15 13:35:40 -06:00
|
|
|
if(isTogglable)
|
|
|
|
{
|
|
|
|
toggle = !toggle;
|
|
|
|
}
|
2012-01-08 11:39:03 -06:00
|
|
|
isButtonDown = true;
|
|
|
|
}
|
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
void Button::OnMouseEnter(int x, int y)
|
2012-01-08 11:39:03 -06:00
|
|
|
{
|
|
|
|
isMouseInside = true;
|
|
|
|
}
|
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
void Button::OnMouseLeave(int x, int y)
|
2012-01-08 11:39:03 -06:00
|
|
|
{
|
|
|
|
isMouseInside = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::DoAction()
|
|
|
|
{
|
|
|
|
std::cout << "Do action!"<<std::endl;
|
|
|
|
}
|
|
|
|
|
2012-01-14 12:51:24 -06:00
|
|
|
Button::~Button()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-08 11:39:03 -06:00
|
|
|
} /* namespace ui */
|