Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef BOX_H
- #define BOX_H
- #include "abstract_widgets.h"
- namespace widgets {
- struct box final : container {
- enum struct kind { VERTICAL, HORIZONTAL };
- private:
- const kind type;
- std::vector<std::unique_ptr<widget>> children;
- public:
- explicit box(kind b) : type(b) {
- }
- [[nodiscard]] int width() const final {
- return w_width;
- }
- [[nodiscard]] int height() const final {
- return w_height;
- }
- widget *child_at(int x, int y) final {
- if (this->widget::child_at(x, y) != nullptr) {
- int cur_x = 0;
- int cur_y = 0;
- for (auto &widget : children) {
- if (cur_x <= x && x < cur_x + widget->width() &&
- type == kind::HORIZONTAL) {
- int lower_border = (w_height - widget->height()) / 2;
- return widget->child_at(x - cur_x, y - lower_border);
- } else if (cur_y <= y && y < cur_y + widget->height() &&
- type == kind::VERTICAL) {
- int lower_border = (w_width - widget->width()) / 2;
- return widget->child_at(x - lower_border, y - cur_y);
- }
- cur_x += widget->width();
- cur_y += widget->height();
- }
- }
- return nullptr;
- }
- [[nodiscard]] int size() const {
- return static_cast<int>(children.size());
- }
- [[nodiscard]] widget *get(int index) const {
- return children[index].get();
- }
- widget *add(std::unique_ptr<widget> child) {
- add_parent(child.get());
- children.emplace_back(std::move(child));
- update_layout();
- return children.back().get();
- }
- auto remove(int index) {
- auto temp = std::move(children[index]);
- if (temp != nullptr) {
- remove_parent(temp.get());
- }
- children.erase(children.begin() + index); // hz
- update_layout();
- return temp;
- }
- void update_layout() final {
- w_width = 0;
- w_height = 0;
- for (auto &widget : children) {
- if (type == kind::HORIZONTAL) {
- w_width += widget->width();
- w_height = std::max(w_height, widget->height());
- } else {
- w_height += widget->height();
- w_width = std::max(w_width, widget->width());
- }
- }
- }
- };
- inline auto make_box(widgets::box::kind temp) {
- return std::make_unique<box>(temp);
- }
- } // namespace widgets
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement