WIP: GUI Toolkit
This commit is contained in:
109
View.cc
Normal file
109
View.cc
Normal file
@@ -0,0 +1,109 @@
|
||||
#include "View.hh"
|
||||
|
||||
View::View(int width, int height, std::string name)
|
||||
: Widget(width, height, std::move(name))
|
||||
{
|
||||
}
|
||||
|
||||
View::~View()
|
||||
{
|
||||
}
|
||||
|
||||
void View::addChild(std::unique_ptr<Widget> child)
|
||||
{
|
||||
_setParent(*child);
|
||||
children_.push_back(make_tuple(child->getName(), std::move(child), SDL_Rect{0, 0, 0, 0}));
|
||||
|
||||
layout();
|
||||
}
|
||||
|
||||
Widget& View::getChildByName(std::string name)
|
||||
{
|
||||
if (name == "")
|
||||
throw ChildNotFoundException{};
|
||||
|
||||
for(auto& ent : children_) {
|
||||
if (std::get<0>(ent) == name)
|
||||
return *std::get<1>(ent);
|
||||
}
|
||||
|
||||
throw ChildNotFoundException{};
|
||||
}
|
||||
|
||||
Widget const& View::getChildByName(std::string name) const
|
||||
{
|
||||
if (name == "")
|
||||
throw ChildNotFoundException{};
|
||||
|
||||
for(auto& ent : children_) {
|
||||
if (std::get<0>(ent) == name)
|
||||
return *std::get<1>(ent);
|
||||
}
|
||||
|
||||
throw ChildNotFoundException{};
|
||||
}
|
||||
|
||||
std::unique_ptr<Widget> View::removeChild(std::string name)
|
||||
{
|
||||
if (name == "")
|
||||
throw ChildNotFoundException{};
|
||||
|
||||
for(auto it = children_.begin(); it != children_.end(); ++it) {
|
||||
if (std::get<0>(*it) == name) {
|
||||
auto child = std::move(std::get<1>(*it));
|
||||
children_.erase(it);
|
||||
_clearParent(*child);
|
||||
_layout(*child);
|
||||
|
||||
layout();
|
||||
|
||||
return child;
|
||||
}
|
||||
}
|
||||
|
||||
throw ChildNotFoundException{};
|
||||
}
|
||||
|
||||
std::unique_ptr<Widget> View::removeChild(Widget& child)
|
||||
{
|
||||
for(auto it = children_.begin(); it != children_.end(); ++it) {
|
||||
if (std::get<1>(*it).get() == &child) {
|
||||
auto child = std::move(std::get<1>(*it));
|
||||
children_.erase(it);
|
||||
|
||||
_clearParent(*child);
|
||||
_layout(*child);
|
||||
|
||||
layout();
|
||||
|
||||
return child;
|
||||
}
|
||||
}
|
||||
|
||||
throw ChildNotFoundException{};
|
||||
}
|
||||
|
||||
|
||||
void View::render(SDL_Surface *dst, SDL_Rect *dstRect) const
|
||||
{
|
||||
Widget::render(dst, dstRect);
|
||||
|
||||
SDL_Rect rect;
|
||||
if (dstRect)
|
||||
memcpy(&rect, dstRect, sizeof(SDL_Rect));
|
||||
else {
|
||||
rect.x = 0;
|
||||
rect.y = 0;
|
||||
rect.w = dst->w;
|
||||
rect.h = dst->h;
|
||||
}
|
||||
|
||||
for (auto& ent : children_) {
|
||||
SDL_Rect childDst = std::get<2>(ent), childDstCliped;
|
||||
childDst.x += rect.x;
|
||||
childDst.y += rect.y;
|
||||
SDL_IntersectRect(&rect, &childDst, &childDstCliped);
|
||||
|
||||
std::get<1>(ent)->render(dst, &childDst);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user