Add VerticalBoxLayout automatic sizing and modify demo program
This commit is contained in:
parent
2c15569339
commit
c4d9672e0b
3 changed files with 26 additions and 33 deletions
|
@ -28,8 +28,6 @@ void HorizontalBoxLayout::run() {
|
|||
child->rect().set_x(current_point.x());
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -9,19 +9,30 @@ void VerticalBoxLayout::run() {
|
|||
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();
|
||||
for (auto child : children) {
|
||||
if (child->absolute()) {
|
||||
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_y(current_point.y());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
42
src/main.cpp
42
src/main.cpp
|
@ -8,7 +8,9 @@
|
|||
#include "RGB.hpp"
|
||||
#include "DocumentLayout.hpp"
|
||||
#include "Events.hpp"
|
||||
#include "src/HorizontalBoxLayout.hpp"
|
||||
#include "src/Styles.hpp"
|
||||
#include "src/VerticalBoxLayout.hpp"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
@ -18,37 +20,19 @@ int main() {
|
|||
window.spawn_window();
|
||||
|
||||
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>();
|
||||
second_widget->set_layout<Raven::DocumentLayout>(20.0);
|
||||
second_widget->resize(800, 800);
|
||||
for (int i = 0; i < 12; i++) {
|
||||
auto row = main_widget->add<Raven::Widget>();
|
||||
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>();
|
||||
inner_widget->set_layout<Raven::DocumentLayout>(8.0);
|
||||
inner_widget->resize(600, 600);
|
||||
|
||||
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();
|
||||
};
|
||||
button->on_click = [button]() {
|
||||
int number = std::stoi(button->text());
|
||||
button->set_text(std::to_string(++number));
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
window.run(true);
|
||||
|
|
Loading…
Reference in a new issue