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

174 lines
4.0 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"
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-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),
2012-05-15 12:13:17 -05:00
Enabled(true)
2012-01-14 12:51:24 -06:00
{
TextPosition();
}
void Button::TextPosition()
{
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-05-15 12:13:17 -05:00
int position = Graphics::textwidthx((char *)buttonDisplayText.c_str(), Size.X - (Appearance.icon? 38 : 22));
buttonDisplayText = buttonDisplayText.erase(position, buttonDisplayText.length()-position);
buttonDisplayText += "...";
}
}
2012-05-15 12:13:17 -05:00
Component::TextPosition(buttonDisplayText);
}
void Button::SetIcon(Icon icon)
{
2012-05-15 12:13:17 -05:00
Appearance.icon = icon;
TextPosition();
}
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-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;
Point Position = screenPos;
if(Enabled)
2012-01-08 11:39:03 -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);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, buttonDisplayText, Appearance.TextActive.Red, Appearance.TextActive.Green, Appearance.TextActive.Blue, 255);
}
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);
g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, buttonDisplayText, Appearance.TextInactive.Red, Appearance.TextInactive.Green, Appearance.TextInactive.Blue, 255);
}
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);
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 180, 180, 180, 255);
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
}
void Button::OnMouseUp(int x, int y, unsigned int button)
2012-01-08 11:39:03 -06:00
{
if(button != 1)
{
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)
{
if(button != 1) return;
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-05-23 13:31:01 -05:00
if(!Enabled)
return;
if(actionCallback)
actionCallback->MouseEnterCallback(this);
2012-01-08 11:39:03 -06: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()
{
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 */