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),
|
2012-05-15 12:13:17 -05:00
|
|
|
Enabled(true)
|
2012-01-14 12:51:24 -06: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())
|
|
|
|
{
|
2012-05-15 12:13:17 -05:00
|
|
|
if(Graphics::textwidth((char *)buttonDisplayText.c_str()) > Size.X - (Appearance.icon? 22 : 0))
|
2012-02-01 12:45:59 -06:00
|
|
|
{
|
2012-05-15 12:13:17 -05:00
|
|
|
int position = Graphics::textwidthx((char *)buttonDisplayText.c_str(), Size.X - (Appearance.icon? 38 : 22));
|
2012-02-01 12:45:59 -06:00
|
|
|
buttonDisplayText = buttonDisplayText.erase(position, buttonDisplayText.length()-position);
|
|
|
|
buttonDisplayText += "...";
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 11:13:43 -05:00
|
|
|
|
2012-05-15 12:13:17 -05:00
|
|
|
Component::TextPosition(buttonDisplayText);
|
2012-01-19 07:44:59 -06:00
|
|
|
}
|
|
|
|
|
2012-01-27 14:19:42 -06:00
|
|
|
void Button::SetIcon(Icon icon)
|
|
|
|
{
|
2012-05-15 12:13:17 -05:00
|
|
|
Appearance.icon = icon;
|
2012-01-27 14:19:42 -06:00
|
|
|
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-05-15 12:13:17 -05:00
|
|
|
if(!drawn)
|
|
|
|
{
|
|
|
|
TextPosition();
|
|
|
|
drawn = true;
|
|
|
|
}
|
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-05-15 12:13:17 -05:00
|
|
|
g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, Appearance.BackgroundActive.Red, Appearance.BackgroundActive.Green, Appearance.BackgroundActive.Blue, 255);
|
|
|
|
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BorderActive.Red, Appearance.BorderActive.Green, Appearance.BorderActive.Blue, 255);
|
2012-05-22 14:30:23 -05:00
|
|
|
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, buttonDisplayText, Appearance.TextActive.Red, Appearance.TextActive.Green, Appearance.TextActive.Blue, 255);
|
2012-01-22 08:45:37 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-15 12:13:17 -05:00
|
|
|
g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, Appearance.BackgroundInactive.Red, Appearance.BackgroundInactive.Green, Appearance.BackgroundInactive.Blue, 255);
|
|
|
|
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BorderInactive.Red, Appearance.BorderInactive.Green, Appearance.BorderInactive.Blue, 255);
|
2012-05-22 14:30:23 -05:00
|
|
|
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, buttonDisplayText, Appearance.TextInactive.Red, Appearance.TextInactive.Green, Appearance.TextInactive.Blue, 255);
|
2012-01-22 08:45:37 -06:00
|
|
|
}
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-15 12:13:17 -05:00
|
|
|
g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, Appearance.BackgroundInactive.Red, Appearance.BackgroundInactive.Green, Appearance.BackgroundInactive.Blue, 180);
|
2012-05-23 14:25:35 -05:00
|
|
|
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BackgroundDisabled.Red, Appearance.BackgroundDisabled.Green, Appearance.BackgroundDisabled.Blue, Appearance.BackgroundDisabled.Alpha);
|
2012-05-15 12:13:17 -05:00
|
|
|
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, buttonDisplayText, 180, 180, 180, 255);
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
2012-05-15 12:13:17 -05:00
|
|
|
if(Appearance.icon)
|
|
|
|
g->draw_icon(Position.X+iconPosition.X, Position.Y+iconPosition.Y, Appearance.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
|
|
|
{
|
2012-05-23 13:49:44 -05:00
|
|
|
isMouseInside = true;
|
2012-05-23 13:31:01 -05:00
|
|
|
if(!Enabled)
|
|
|
|
return;
|
|
|
|
if(actionCallback)
|
|
|
|
actionCallback->MouseEnterCallback(this);
|
2012-01-08 11:39:03 -06:00
|
|
|
}
|
|
|
|
|
2012-05-23 13:49:44 -05:00
|
|
|
|
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 */
|