diff --git a/src/ui/include/growl/ui/pack.h b/src/ui/include/growl/ui/pack.h index a31495f1..fec70cea 100644 --- a/src/ui/include/growl/ui/pack.h +++ b/src/ui/include/growl/ui/pack.h @@ -51,32 +51,84 @@ struct PackInfo { float computedHeight = 0; }; +template class Packer { public: - Packer(Node* node, PackInfo* pack) + Packer(T* node, PackInfo* pack) : node{node} , pack{pack} {} - Node* getNode() { + T* getNode() { return node; } - Packer& expand(); - Packer& fill(); - Packer& width(Value width); - Packer& height(Value height); - Packer& align(Align align); + Packer& expand() { + pack->expand = true; + return *this; + } + + Packer& fill() { + pack->fill = true; + return *this; + } + + Packer& width(Value width) { + pack->prefWidth = width; + return *this; + } + + Packer& height(Value height) { + pack->prefHeight = height; + return *this; + } + + Packer& align(Align align) { + pack->alignment = align; + return *this; + } + + Packer& margin(Value margin) { + pack->marginTop = margin; + pack->marginBottom = margin; + pack->marginLeft = margin; + pack->marginRight = margin; + return *this; + } - Packer& margin(Value margin); - Packer& marginTop(Value margin); - Packer& marginBottom(Value margin); - Packer& marginLeft(Value margin); - Packer& marginRight(Value margin); - Packer& marginX(Value margin); - Packer& marginY(Value margin); + Packer& marginTop(Value margin) { + pack->marginTop = margin; + return *this; + } + + Packer& marginBottom(Value margin) { + pack->marginBottom = margin; + return *this; + } + + Packer& marginLeft(Value margin) { + pack->marginLeft = margin; + return *this; + } + + Packer& marginRight(Value margin) { + pack->marginRight = margin; + return *this; + } + + Packer& marginX(Value margin) { + pack->marginLeft = margin; + pack->marginRight = margin; + return *this; + } + + Packer& marginY(Value margin) { + pack->marginTop = margin; + pack->marginBottom = margin; + return *this; + } private: - Node* node; + T* node; PackInfo* pack; }; diff --git a/src/ui/include/growl/ui/widget.h b/src/ui/include/growl/ui/widget.h index 6d0f6660..5e9b60cd 100644 --- a/src/ui/include/growl/ui/widget.h +++ b/src/ui/include/growl/ui/widget.h @@ -10,7 +10,13 @@ class Widget : public Node { Widget(std::string&& name); virtual Node* addChild(std::unique_ptr node) override; - Packer addWithLayout(std::unique_ptr child); + + template + Packer addWithLayout(Args&&... args) { + Node* n = addChild(std::make_unique(std::forward(args)...)); + return Packer{ + static_cast(n), &(pack_info.at(pack_info.size() - 1))}; + } void invalidate(); void invalidateHierarchy(); diff --git a/src/ui/src/pack.cpp b/src/ui/src/pack.cpp index 01417f4a..87523b5f 100644 --- a/src/ui/src/pack.cpp +++ b/src/ui/src/pack.cpp @@ -16,68 +16,3 @@ float Value::evaluate(Node* context) { } return 0; } - -Packer& Packer::expand() { - pack->expand = true; - return *this; -} - -Packer& Packer::fill() { - pack->fill = true; - return *this; -} - -Packer& Packer::width(Value width) { - pack->prefWidth = width; - return *this; -} - -Packer& Packer::height(Value height) { - pack->prefHeight = height; - return *this; -} - -Packer& Packer::align(Align align) { - pack->alignment = align; - return *this; -} - -Packer& Packer::margin(Value margin) { - pack->marginTop = margin; - pack->marginBottom = margin; - pack->marginLeft = margin; - pack->marginRight = margin; - return *this; -} - -Packer& Packer::marginTop(Value margin) { - pack->marginTop = margin; - return *this; -} - -Packer& Packer::marginBottom(Value margin) { - pack->marginBottom = margin; - return *this; -} - -Packer& Packer::marginLeft(Value margin) { - pack->marginLeft = margin; - return *this; -} - -Packer& Packer::marginRight(Value margin) { - pack->marginRight = margin; - return *this; -} - -Packer& Packer::marginX(Value margin) { - pack->marginLeft = margin; - pack->marginRight = margin; - return *this; -} - -Packer& Packer::marginY(Value margin) { - pack->marginTop = margin; - pack->marginBottom = margin; - return *this; -} diff --git a/src/ui/src/widget.cpp b/src/ui/src/widget.cpp index c5d3c198..8cae17c8 100644 --- a/src/ui/src/widget.cpp +++ b/src/ui/src/widget.cpp @@ -19,11 +19,6 @@ Node* Widget::addChild(std::unique_ptr node) { return Node::addChild(std::move(node)); } -Packer Widget::addWithLayout(std::unique_ptr child) { - Node* n = addChild(std::move(child)); - return Packer{n, &(pack_info.at(pack_info.size() - 1))}; -} - void Widget::invalidate() { invalidated = true; }