Add VerticalBoxLayout automatic sizing and modify demo program

This commit is contained in:
hippoz 2022-06-15 19:15:19 +03:00
parent 2c15569339
commit c4d9672e0b
No known key found for this signature in database
GPG key ID: 7C52899193467641
3 changed files with 26 additions and 33 deletions

View file

@ -28,8 +28,6 @@ void HorizontalBoxLayout::run() {
child->rect().set_x(current_point.x()); child->rect().set_x(current_point.x());
child->rect().set_y(current_point.y()); child->rect().set_y(current_point.y());
//std::cout << child->rect().x() << ", " << child->rect().y() << ", " << child->rect().width() << ", " << child->rect().height() << std::endl;
current_point.add(child->rect().width() + m_margin, 0); current_point.add(child->rect().width() + m_margin, 0);
} }

View file

@ -9,19 +9,30 @@ void VerticalBoxLayout::run() {
return; return;
} }
Point current_point { m_target->rect().x() + m_margin, m_target->rect().y() + m_margin }; Point current_point { m_margin, m_margin };
double max_width_so_far = 0;
double requested_height = 0;
auto& children = m_target->children(); auto& children = m_target->children();
for (auto child : children) { for (auto child : children) {
if (child->absolute()) { if (child->absolute()) {
continue; continue;
} }
if (child->rect().width() > max_width_so_far) {
max_width_so_far = child->rect().height();
}
requested_height += child->rect().height() + m_margin;
child->rect().set_x(current_point.x()); child->rect().set_x(current_point.x());
child->rect().set_y(current_point.y()); child->rect().set_y(current_point.y());
current_point.add(0, child->rect().height() + m_margin); current_point.add(0, child->rect().height() + m_margin);
} }
m_target->rect().set_height(requested_height + m_margin);
m_target->rect().set_width(max_width_so_far + m_margin * 2);
} }
} }

View file

@ -8,7 +8,9 @@
#include "RGB.hpp" #include "RGB.hpp"
#include "DocumentLayout.hpp" #include "DocumentLayout.hpp"
#include "Events.hpp" #include "Events.hpp"
#include "src/HorizontalBoxLayout.hpp"
#include "src/Styles.hpp" #include "src/Styles.hpp"
#include "src/VerticalBoxLayout.hpp"
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
@ -18,37 +20,19 @@ int main() {
window.spawn_window(); window.spawn_window();
auto main_widget = window.set_main_widget<Raven::Widget>(); auto main_widget = window.set_main_widget<Raven::Widget>();
main_widget->set_layout<Raven::DocumentLayout>(10.0); main_widget->set_layout<Raven::VerticalBoxLayout>(6.0);
auto second_widget = main_widget->add<Raven::Widget>(); for (int i = 0; i < 12; i++) {
second_widget->set_layout<Raven::DocumentLayout>(20.0); auto row = main_widget->add<Raven::Widget>();
second_widget->resize(800, 800); row->set_layout<Raven::HorizontalBoxLayout>(6.0);
for (int i = 0; i < 12; i++) {
auto button = row->add<Raven::Button>("0");
auto inner_widget = second_widget->add<Raven::Widget>(); button->on_click = [button]() {
inner_widget->set_layout<Raven::DocumentLayout>(8.0); int number = std::stoi(button->text());
inner_widget->resize(600, 600); button->set_text(std::to_string(++number));
};
int number = 0; }
for (int i = 0; i < 250; i++) {
auto button = inner_widget->add<Raven::Button>("click me");
button->set_style(&Raven::accent_button_style);
auto label = inner_widget->add<Raven::Label>("click one of the buttons!");
button->on_click = [&]() {
number += 10;
window.start_batch();
auto& children = inner_widget->children();
for (auto& child : children) {
if (child->type() == Raven::WidgetType::Button) {
std::static_pointer_cast<Raven::Button>(child)->set_text(std::to_string(number));
}
if (child->type() == Raven::WidgetType::Label) {
std::static_pointer_cast<Raven::Label>(child)->set_text(std::to_string(number));
}
}
window.end_batch();
};
} }
window.run(true); window.run(true);