diff --git a/example/src/main.cpp b/example/src/main.cpp index 6e51ed4..bd5daa4 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -10,6 +10,7 @@ #include "widget/panel_widget/mbox.h" #include "utf8.h" +#include "widget/panel_widget/moverlay.h" void test_color() { const char* test_cases[] = { @@ -77,7 +78,7 @@ int main(int argc, char* argv[]) { [ mnew(mtext_block, .text(config_info_str) - .font_size(24) + .font_size(15) ) ] ], @@ -93,13 +94,47 @@ int main(int argc, char* argv[]) { mnew(mtext_block, .text(U"Hello, World! 你好,世界!\n换行测试1111,测试测试测试测试,测试测试😀🐵🙏 😃🐵🙏") // .text(U"😀🐵🙏😀🐵🙏") - .font_size(24) - ) + .font_size(15) + ) ] ] ] ); + const auto& window2 = mwindow::create({ 800, 600 }, L"Hello, World!"); + window2->set_content( + mnew(moverlay) + [ + mslot(moverlay) + .h_alignment(horizontal_alignment_t::center) + .v_alignment(vertical_alignment_t::center) + +mnew(mbutton) + [ + mslot(mbutton) + .margin({ 10 }) + [ + mnew(mtext_block, + .text(U"测试测试") + .font_size(24)) + ] + ], + + mslot(moverlay) + .h_alignment(horizontal_alignment_t::center) + .v_alignment(vertical_alignment_t::center) + +mnew(mbutton) + [ + mslot(mbutton) + .margin({ 10 }) + [ + mnew(mtext_block, + .text(U"测试测试21111") + .font_size(15)) + ] + ] + ] + ); + mirage_app::get().run(); return 0; } diff --git a/src/mirage_widget/src/widget/panel_widget/moverlay.cpp b/src/mirage_widget/src/widget/panel_widget/moverlay.cpp new file mode 100644 index 0000000..938f0b6 --- /dev/null +++ b/src/mirage_widget/src/widget/panel_widget/moverlay.cpp @@ -0,0 +1,43 @@ +#include "moverlay.h" + +#include "widget_utils/layout_utils.h" + +void moverlay::arrange_children(const geometry_t& in_allotted_geometry) { + const auto& container_size = in_allotted_geometry.get_local_size(); + if (container_size.x() <= 0 || container_size.y() <= 0) { + return; + } + + for (auto& slot: get_slots()) { + const auto& child_widget = slot.get(); + if (!child_widget) { + continue; + } + + if (!has_any_flag(child_widget->get_visibility(), visibility_t::any_layout)) { + continue; + } + + arrange_single_child(in_allotted_geometry, child_widget, slot.h_alignment(), slot.v_alignment(), slot.margin()); + } +} + +auto moverlay::compute_desired_size(float in_layout_scale_multiplier) const -> Eigen::Vector2f +{ + Eigen::Vector2f result; + for (const auto& slot : get_slots()) + { + const auto& child_widget = slot.get(); + if (!child_widget) { + continue; + } + + if (!has_any_flag(child_widget->get_visibility(), visibility_t::any_layout)) { + continue; + } + + const auto& child_desired_size = child_widget->compute_desired_size(in_layout_scale_multiplier); + result = result.cwiseMax(child_desired_size); + } + return result; +} diff --git a/src/mirage_widget/src/widget/panel_widget/moverlay.h b/src/mirage_widget/src/widget/panel_widget/moverlay.h new file mode 100644 index 0000000..111b757 --- /dev/null +++ b/src/mirage_widget/src/widget/panel_widget/moverlay.h @@ -0,0 +1,27 @@ +#pragma once +#include "widget/mpanel_widget.h" + +struct moverlay_slot : mpanel_widget_slot { + SLOT_ATTRIBUTE(margin_t, margin) + SLOT_ATTRIBUTE(float, opacity) + SLOT_ATTRIBUTE(float, z_index) + SLOT_ATTRIBUTE(horizontal_alignment_t, h_alignment) + SLOT_ATTRIBUTE(vertical_alignment_t, v_alignment) + + // /** + // * @brief 构造函数,设置默认对齐方式 + // * + // * 初始化插槽,默认将子组件在水平和垂直方向上都设为拉伸填充模式 + // */ +public: + moverlay_slot() { + h_alignment(horizontal_alignment_t::stretch); + v_alignment(vertical_alignment_t::stretch); + } +}; + +class moverlay : public mpanel_widget { +public: + void arrange_children(const geometry_t& in_allotted_geometry) override; + auto compute_desired_size(float in_layout_scale_multiplier) const -> Eigen::Vector2f override; +};