Initial commit.
This commit is contained in:
commit
59c6161fdb
134 changed files with 4751 additions and 0 deletions
134
src/ui_elements/widgets/Widget.cpp
Normal file
134
src/ui_elements/widgets/Widget.cpp
Normal file
|
@ -0,0 +1,134 @@
|
|||
#include "RectangleElement.h"
|
||||
#include "Widget.h"
|
||||
|
||||
Widget::Widget()
|
||||
: mLocation(DiscretePoint(0, 0)),
|
||||
mWidth(100),
|
||||
mHeight(100),
|
||||
mLayers(),
|
||||
mChildren(),
|
||||
mBackgroundColor(Color::Create(200, 0, 0))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Widget::~Widget()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Widget::AddWidget(WidgetPtr widget)
|
||||
{
|
||||
mChildren.push_back(widget);
|
||||
}
|
||||
|
||||
void Widget::SetSize(unsigned width, unsigned height)
|
||||
{
|
||||
mWidth = width;
|
||||
mHeight = height;
|
||||
}
|
||||
|
||||
std::shared_ptr<Widget> Widget::Create()
|
||||
{
|
||||
return std::make_shared<Widget>();
|
||||
}
|
||||
|
||||
DiscretePoint Widget::GetLocation()
|
||||
{
|
||||
return mLocation;
|
||||
}
|
||||
|
||||
std::vector<VisualLayerPtr> Widget::GetLayers()
|
||||
{
|
||||
return mLayers;
|
||||
}
|
||||
|
||||
void Widget::AddChildLayers(PaintEventPtr event)
|
||||
{
|
||||
for(std::size_t idx=0; idx<mChildren.size(); idx++)
|
||||
{
|
||||
auto child = mChildren[idx];
|
||||
child->SetSize(mWidth, mHeight);
|
||||
child->SetLocation(mLocation);
|
||||
child->OnPaintEvent(event);
|
||||
auto layers = child->GetLayers();
|
||||
mLayers.insert(mLayers.end(), layers.begin(), layers.end());
|
||||
}
|
||||
}
|
||||
|
||||
void Widget::OnPaintEvent(PaintEventPtr event)
|
||||
{
|
||||
mLayers.clear();
|
||||
|
||||
AddBackground(event);
|
||||
AddChildLayers(event);
|
||||
}
|
||||
|
||||
bool Widget::Contains(const DiscretePoint& loc)
|
||||
{
|
||||
if(loc.GetX() < mLocation.GetX()) return false;
|
||||
if(loc.GetX() > mLocation.GetX() + mWidth) return false;
|
||||
if(loc.GetY() > mLocation.GetY() + mHeight) return false;
|
||||
if(loc.GetY() < mLocation.GetY()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Widget::OnMouseEvent(MouseEventPtr event)
|
||||
{
|
||||
bool inChild = false;
|
||||
for(const auto& child : mChildren)
|
||||
{
|
||||
if(child->OnMouseEvent(event))
|
||||
{
|
||||
inChild = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!inChild)
|
||||
{
|
||||
if(Contains(event->GetClientLocation()))
|
||||
{
|
||||
OnMyMouseEvent(event);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Widget::OnMyMouseEvent(MouseEventPtr event)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Widget::AddBackground(PaintEventPtr event)
|
||||
{
|
||||
auto shape = RectangleElement::Create(mLocation, mWidth, mHeight);
|
||||
shape->SetFillColor(mBackgroundColor);
|
||||
auto shapeLayer = VisualLayer::Create();
|
||||
shapeLayer->SetShape(shape);
|
||||
mLayers.push_back(shapeLayer);
|
||||
}
|
||||
|
||||
void Widget::SetBackgroundColor(ColorPtr color)
|
||||
{
|
||||
mBackgroundColor = color;
|
||||
}
|
||||
|
||||
void Widget::SetLocation(const DiscretePoint& loc)
|
||||
{
|
||||
mLocation = loc;
|
||||
}
|
||||
|
||||
unsigned Widget::GetWidth()
|
||||
{
|
||||
return mWidth;
|
||||
}
|
||||
|
||||
unsigned Widget::GetHeight()
|
||||
{
|
||||
return mHeight;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue