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-17 14:46:06 -06:00
|
|
|
#include "Engine.h"
|
2012-01-19 07:44:59 -06:00
|
|
|
#include "Misc.h"
|
2012-01-08 11:39:03 -06:00
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
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),
|
2012-01-17 14:46:06 -06:00
|
|
|
isTogglable(false),
|
2012-01-20 17:42:17 -06:00
|
|
|
toggle(false),
|
2012-01-19 07:44:59 -06:00
|
|
|
actionCallback(NULL),
|
|
|
|
textPosition(ui::Point(0, 0)),
|
|
|
|
textVAlign(AlignMiddle),
|
2012-02-01 12:45:59 -06:00
|
|
|
textHAlign(AlignLeft),
|
2012-01-27 14:19:42 -06:00
|
|
|
Enabled(true),
|
|
|
|
icon(NoIcon)
|
2012-01-14 12:51:24 -06:00
|
|
|
{
|
2012-01-23 16:53:57 -06:00
|
|
|
activeText = background = Colour(0, 0, 0);
|
|
|
|
text = activeBackground = border = activeBorder = Colour(255, 255, 255);
|
2012-05-14 14:47:14 -05:00
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
TextPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::TextPosition()
|
|
|
|
{
|
2012-02-01 12:45:59 -06:00
|
|
|
buttonDisplayText = ButtonText;
|
|
|
|
if(buttonDisplayText.length())
|
|
|
|
{
|
|
|
|
if(Graphics::textwidth((char *)buttonDisplayText.c_str()) > Size.X - (icon? 22 : 0))
|
|
|
|
{
|
|
|
|
int position = Graphics::textwidthx((char *)buttonDisplayText.c_str(), Size.X - (icon? 38 : 22));
|
|
|
|
buttonDisplayText = buttonDisplayText.erase(position, buttonDisplayText.length()-position);
|
|
|
|
buttonDisplayText += "...";
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 11:13:43 -05:00
|
|
|
|
|
|
|
// Values 3 and 10 are for vertical padding of 3 pixels, middle uses 7 as that's the height of a capital
|
2012-01-19 07:44:59 -06:00
|
|
|
switch(textVAlign)
|
|
|
|
{
|
|
|
|
case AlignTop:
|
|
|
|
textPosition.Y = 3;
|
|
|
|
break;
|
|
|
|
case AlignMiddle:
|
|
|
|
textPosition.Y = (Size.Y-10)/2;
|
|
|
|
break;
|
|
|
|
case AlignBottom:
|
2012-04-22 11:13:43 -05:00
|
|
|
textPosition.Y = Size.Y-10;
|
2012-01-19 07:44:59 -06:00
|
|
|
break;
|
|
|
|
}
|
2012-01-14 12:51:24 -06:00
|
|
|
|
2012-01-27 14:19:42 -06:00
|
|
|
if(icon)
|
2012-01-19 07:44:59 -06:00
|
|
|
{
|
2012-01-27 14:19:42 -06:00
|
|
|
switch(textHAlign)
|
|
|
|
{
|
|
|
|
case AlignLeft:
|
|
|
|
textPosition.X = 3+17;
|
|
|
|
break;
|
|
|
|
case AlignCentre:
|
2012-02-01 12:45:59 -06:00
|
|
|
textPosition.X = (((Size.X-14)-Graphics::textwidth((char *)buttonDisplayText.c_str()))/2)+17;
|
2012-01-27 14:19:42 -06:00
|
|
|
break;
|
|
|
|
case AlignRight:
|
2012-02-01 12:45:59 -06:00
|
|
|
textPosition.X = (((Size.X-14)-Graphics::textwidth((char *)buttonDisplayText.c_str()))-2)+17;
|
2012-01-27 14:19:42 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch(textHAlign)
|
|
|
|
{
|
|
|
|
case AlignLeft:
|
|
|
|
textPosition.X = 3;
|
|
|
|
break;
|
|
|
|
case AlignCentre:
|
2012-02-01 12:45:59 -06:00
|
|
|
textPosition.X = (Size.X-Graphics::textwidth((char *)buttonDisplayText.c_str()))/2;
|
2012-01-27 14:19:42 -06:00
|
|
|
break;
|
|
|
|
case AlignRight:
|
2012-02-01 12:45:59 -06:00
|
|
|
textPosition.X = (Size.X-Graphics::textwidth((char *)buttonDisplayText.c_str()))-2;
|
2012-01-27 14:19:42 -06:00
|
|
|
break;
|
|
|
|
}
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 14:19:42 -06:00
|
|
|
void Button::SetIcon(Icon icon)
|
|
|
|
{
|
|
|
|
this->icon = icon;
|
|
|
|
TextPosition();
|
|
|
|
}
|
|
|
|
|
2012-01-19 07:44:59 -06:00
|
|
|
void Button::SetText(std::string buttonText)
|
|
|
|
{
|
|
|
|
ButtonText = buttonText;
|
|
|
|
TextPosition();
|
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-17 14:46:06 -06:00
|
|
|
Graphics * g = ui::Engine::Ref().g;
|
2012-01-15 13:35:40 -06:00
|
|
|
Point Position = screenPos;
|
2012-01-22 08:45:37 -06:00
|
|
|
if(Enabled)
|
2012-01-08 11:39:03 -06:00
|
|
|
{
|
2012-01-22 08:45:37 -06:00
|
|
|
if(isButtonDown || (isTogglable && toggle))
|
|
|
|
{
|
2012-04-22 11:13:43 -05:00
|
|
|
g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, activeBackground.Red, activeBackground.Green, activeBackground.Blue, 255);
|
2012-01-23 16:53:57 -06:00
|
|
|
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, activeBorder.Red, activeBorder.Green, activeBorder.Blue, 255);
|
2012-02-01 12:45:59 -06:00
|
|
|
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, activeText.Red, activeText.Green, activeText.Blue, 255);
|
2012-01-22 08:45:37 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-22 11:13:43 -05:00
|
|
|
g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, background.Red, background.Green, background.Blue, 255);
|
2012-01-23 16:53:57 -06:00
|
|
|
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, border.Red, border.Green, border.Blue, 255);
|
2012-02-01 12:45:59 -06:00
|
|
|
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, text.Red, text.Green, text.Blue, 255);
|
2012-01-22 08:45:37 -06:00
|
|
|
}
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-22 11:13:43 -05:00
|
|
|
g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, background.Red, background.Green, background.Blue, 180);
|
2012-01-22 08:45:37 -06:00
|
|
|
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 180, 180, 180, 255);
|
2012-02-01 12:45:59 -06:00
|
|
|
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y+1, buttonDisplayText, 180, 180, 180, 255);
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
2012-01-27 14:19:42 -06:00
|
|
|
if(icon)
|
|
|
|
g->draw_icon(Position.X+3, Position.Y+textPosition.Y, icon);
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
|
2012-01-25 19:13:33 -06:00
|
|
|
void Button::OnMouseUp(int x, int y, unsigned int button)
|
2012-01-08 11:39:03 -06:00
|
|
|
{
|
|
|
|
if(button != 1)
|
|
|
|
{
|
2012-01-27 14:19:42 -06:00
|
|
|
return;
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if(isButtonDown)
|
|
|
|
{
|
2012-01-14 12:51:24 -06:00
|
|
|
DoAction();
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
isButtonDown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::OnMouseClick(int x, int y, unsigned int button)
|
|
|
|
{
|
2012-01-27 14:19:42 -06:00
|
|
|
if(button != 1) return;
|
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()
|
|
|
|
{
|
2012-01-22 08:45:37 -06:00
|
|
|
if(!Enabled)
|
|
|
|
return;
|
2012-01-17 14:46:06 -06:00
|
|
|
if(actionCallback)
|
|
|
|
actionCallback->ActionCallback(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::SetActionCallback(ButtonAction * action)
|
|
|
|
{
|
2012-01-27 03:38:56 -06:00
|
|
|
if(actionCallback)
|
|
|
|
delete actionCallback;
|
2012-01-17 14:46:06 -06:00
|
|
|
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 */
|