The-Powder-Toy/src/interface/Button.cpp

201 lines
3.7 KiB
C++
Raw Normal View History

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"
#include "Global.h"
2012-01-17 14:46:06 -06:00
#include "Engine.h"
#include "Misc.h"
2012-01-08 11:39:03 -06:00
namespace ui {
2012-01-17 14:46:06 -06:00
Button::Button(Window* parent_state, std::string buttonText):
2012-01-14 12:51:24 -06:00
Component(parent_state),
ButtonText(buttonText),
isMouseInside(false),
isButtonDown(false),
2012-01-17 14:46:06 -06:00
isTogglable(false),
2012-01-20 17:42:17 -06:00
toggle(false),
actionCallback(NULL),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre)
2012-01-08 11:39:03 -06:00
{
TextPosition();
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),
isButtonDown(false),
2012-01-17 14:46:06 -06:00
isTogglable(false),
2012-01-20 17:42:17 -06:00
toggle(false),
actionCallback(NULL),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre)
2012-01-08 11:39:03 -06:00
{
TextPosition();
2012-01-14 12:51:24 -06:00
}
Button::Button(std::string buttonText):
Component(),
ButtonText(buttonText),
isMouseInside(false),
isButtonDown(false),
2012-01-17 14:46:06 -06:00
isTogglable(false),
2012-01-20 17:42:17 -06:00
toggle(false),
actionCallback(NULL),
textPosition(ui::Point(0, 0)),
textVAlign(AlignMiddle),
textHAlign(AlignCentre)
2012-01-14 12:51:24 -06:00
{
TextPosition();
}
void Button::TextPosition()
{
//Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2
switch(textVAlign)
{
case AlignTop:
textPosition.Y = 3;
break;
case AlignMiddle:
textPosition.Y = (Size.Y-10)/2;
break;
case AlignBottom:
textPosition.Y = Size.Y-11;
break;
}
2012-01-14 12:51:24 -06:00
switch(textHAlign)
{
case AlignLeft:
textPosition.X = 3;
break;
case AlignCentre:
textPosition.X = (Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2;
break;
case AlignRight:
textPosition.X = (Size.X-Graphics::textwidth((char *)ButtonText.c_str()))-2;
break;
}
}
void Button::SetText(std::string buttonText)
{
ButtonText = buttonText;
TextPosition();
2012-01-14 12:51:24 -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-17 14:46:06 -06:00
Graphics * g = ui::Engine::Ref().g;
Point Position = screenPos;
if(isButtonDown || (isTogglable && toggle))
2012-01-08 11:39:03 -06:00
{
2012-01-17 14:46:06 -06:00
g->fillrect(Position.X-1, Position.Y-1, Size.X+2, Size.Y+2, 255, 255, 255, 255);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, 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+textPosition.X, Position.Y+textPosition.Y, ButtonText, 255, 255, 255, 255);
2012-01-08 11:39:03 -06:00
}
}
void Button::OnMouseUnclick(int x, int y, unsigned int button)
{
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!
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-17 14:46:06 -06:00
//if(actionCallback)
// (*(actionCallback))();
if(actionCallback)
actionCallback->ActionCallback(this);
}
void Button::SetActionCallback(ButtonAction * action)
{
actionCallback = action;
2012-01-08 11:39:03 -06:00
}
2012-01-14 12:51:24 -06:00
Button::~Button()
{
2012-01-20 16:07:49 -06:00
if(actionCallback)
delete actionCallback;
2012-01-14 12:51:24 -06:00
}
2012-01-08 11:39:03 -06:00
} /* namespace ui */