61 lines
1.5 KiB
C++
61 lines
1.5 KiB
C++
#include "Button.h"
|
|
#include <iostream>
|
|
|
|
Button::Button()
|
|
: Widget(),
|
|
mLabel(),
|
|
mCachedColor(255, 255, 255),
|
|
mClickFunc()
|
|
{
|
|
mClickedColor = Color::Create(180, 180, 180);
|
|
}
|
|
|
|
std::unique_ptr<Button> Button::Create()
|
|
{
|
|
return std::make_unique<Button>();
|
|
}
|
|
|
|
void Button::SetOnClickFunction(clickFunc func)
|
|
{
|
|
mClickFunc = func;
|
|
}
|
|
|
|
void Button::SetLabel(const std::string& text)
|
|
{
|
|
mLabel = text;
|
|
}
|
|
|
|
void Button::OnMyMouseEvent(const MouseEvent* event)
|
|
{
|
|
if(event->GetAction() == MouseEvent::Action::Pressed)
|
|
{
|
|
mCachedColor = *mBackgroundColor;
|
|
SetBackgroundColor(Color::Create(*mClickedColor));
|
|
if(mClickFunc)
|
|
{
|
|
mClickFunc(this);
|
|
}
|
|
}
|
|
else if(event->GetAction() == MouseEvent::Action::Released)
|
|
{
|
|
SetBackgroundColor(Color::Create(mCachedColor));
|
|
}
|
|
}
|
|
|
|
void Button::OnPaintEvent(const PaintEvent* event)
|
|
{
|
|
mMyLayers.clear();
|
|
AddBackground(event);
|
|
if(!mLabel.empty())
|
|
{
|
|
unsigned fontOffset = unsigned(mLabel.size()) * 4;
|
|
auto middle = DiscretePoint(mLocation.GetX() + mSize.mWidth/2 - fontOffset,
|
|
mLocation.GetY() + mSize.mHeight/2 + 4);
|
|
auto textLayer = VisualLayer::Create();
|
|
auto textElement = TextElement::Create(mLabel, middle);
|
|
textElement->SetFillColor(Color::Create(*mBackgroundColor));
|
|
textLayer->SetText(std::move(textElement));
|
|
mMyLayers.push_back(std::move(textLayer));
|
|
}
|
|
CopyMyLayers();
|
|
}
|