修复布局系统

This commit is contained in:
daiqingshuang 2025-03-24 13:22:47 +08:00
parent 43aa8ce56d
commit db735b94ea
6 changed files with 40 additions and 28 deletions

View File

@ -21,10 +21,19 @@ int main(int argc, char* argv[]) {
auto weak_vbox = widget_manager::get().new_widget<mv_box>(window.get());
auto vbox = weak_vbox.lock();
vbox->add_slot<mbutton>().margin({ 5 });
vbox->add_slot<mbutton>().margin({ 5 }).stretch(1);
vbox->add_slot<mbutton>().margin({ 5 });
vbox->add_slot<mbutton>().margin({ 5 });
vbox->add_child<mbutton>().margin({ 5 });
vbox->add_child<mbutton>().margin({ 5 }).stretch(1);
auto weak_hbox = vbox->add_child<mh_box>().stretch(2).get();
auto hbox = std::static_pointer_cast<mh_box>(weak_hbox.lock());
hbox->add_child<mbutton>().margin({ 5 });
hbox->add_child<mbutton>().margin({ 5 }).stretch();
hbox->add_child<mbutton>().margin({ 5 });
vbox->add_child<mbutton>().margin({ 5 });
vbox->add_child<mbutton>().margin({ 5 });
window->set_content(vbox);

View File

@ -39,23 +39,17 @@ hit_test_handle mbutton::on_click(const Eigen::Vector2f& in_position, mouse_butt
mborder::on_click(in_position, in_button);
std::cout << this << ": Button clicked!" << in_position.x() << ", " << in_position.y() << std::endl;
color_ = {0, 0, 1, 1};
auto parent = get_parent();
// if (std::shared_ptr<mh_box> hbox = std::dynamic_pointer_cast<mh_box>(parent)) {
// hbox->add_slot()
// .stretch()
// .margin({ 5 })
// [
// std::make_shared<mbutton>()
// ];
// }
// if (std::shared_ptr<mv_box> vbox = std::dynamic_pointer_cast<mv_box>(parent)) {
// vbox->add_slot()
// .stretch()
// .margin({ 5 })
// [
// std::make_shared<mbutton>()
// ];
// }
auto parent = get_parent_widget();
if (std::shared_ptr<mh_box> hbox = std::dynamic_pointer_cast<mh_box>(parent.lock())) {
hbox->add_child<mbutton>()
.stretch()
.margin({ 5 });
}
if (std::shared_ptr<mv_box> vbox = std::dynamic_pointer_cast<mv_box>(parent.lock())) {
vbox->add_child<mbutton>()
.stretch()
.margin({ 5 });
}
return hit_test_handle::handled();
}

View File

@ -148,6 +148,7 @@ void arrange_box_children(
pos.y() += info.margin_cross_start;
size.x() = info.size;
size.y() -= info.margin_start + info.margin_end;
} else {
if (is_reversed)
pos.y() = container_primary_size - position - info.size;
@ -155,10 +156,13 @@ void arrange_box_children(
pos.y() = position;
pos.x() += info.margin_cross_start;
size.x() -= info.margin_start + info.margin_end;
size.y() = info.size;
}
info.widget->set_geometry(in_allotted_geometry.make_child(pos, size));
const auto& child_geometry = in_allotted_geometry.make_child(pos, size);
info.widget->set_geometry(child_geometry);
info.widget->arrange_children(child_geometry);
position += info.size + info.margin_end;
}

View File

@ -20,6 +20,10 @@ void mwidget::init_component(mustache::EntityManager& in_entity_manager) {
in_entity_manager.assign<widget_invalidate>(key_);
}
void mwidget::set_geometry(const geometry_t& in_geometry) {
get_component_ref<widget_layout>().geometry = in_geometry;
}
void mwidget::cache_desired_size(float in_layout_scale_multiplier, bool in_force) {
if (auto* layout = get_component<widget_layout>()) {
if (!in_force && layout->desired_size.has_value())

View File

@ -38,11 +38,12 @@ public:
virtual void init_component(mustache::EntityManager& in_entity_manager);
virtual void on_paint(mirage_paint_context& in_context) = 0;
void set_geometry(const geometry_t& in_geometry) { get_component_ref<widget_layout>().geometry = in_geometry; }
const auto& get_geometry() const { return get_component_ref<widget_layout>().geometry; }
void set_geometry(const geometry_t& in_geometry);
const auto& get_geometry() const { return get_component_ref<widget_layout>().geometry; }
virtual auto compute_desired_size(float in_layout_scale_multiplier) const -> Eigen::Vector2f = 0;
void cache_desired_size(float in_layout_scale_multiplier, bool in_force = false);
const auto& get_desired_size() const { return get_component_ref<widget_layout>().desired_size; }
void cache_desired_size(float in_layout_scale_multiplier, bool in_force = false);
const auto& get_desired_size() const { return get_component_ref<widget_layout>().desired_size; }
virtual void arrange_children(const geometry_t& in_allotted_geometry) = 0;
const auto& get_key() const { return key_; }

View File

@ -74,7 +74,7 @@ public:
*
*/
template<typename T>
auto add_slot() -> SlotType& {
auto add_child() -> SlotType& {
auto& manager = widget_manager::get();
auto widget_entity = manager.new_widget<T>(get_window());
auto w = widget_entity.lock();
@ -85,7 +85,7 @@ public:
return slot;
}
template<typename T, typename... Args>
auto add_slot(Args&&... in_args) -> SlotType& {
auto add_child(Args&&... in_args) -> SlotType& {
auto& manager = widget_manager::get();
auto widget_entity = manager.new_widget<T>(get_window(), std::forward<Args>(in_args)...);
auto w = widget_entity.lock();