@@ -3641,6 +3641,156 @@ static void ShowDemoWindowLayout()
3641
3641
3642
3642
ImGui::TreePop ();
3643
3643
}
3644
+
3645
+ #if IMGUI_HAS_STACK_LAYOUT
3646
+ IMGUI_DEMO_MARKER (" Layout/Stack Layout" );
3647
+ if (ImGui::TreeNode (" Stack Layout" ))
3648
+ {
3649
+ static bool widget_a = true , widget_b = true , widget_c = true ;
3650
+ static bool spring_a = true , spring_ab = true , spring_bc = true , spring_c = true ;
3651
+ static bool minimize_width = false , minimize_height = true ;
3652
+ static bool horizontal = true , draw_springs = true ;
3653
+ static ImVec2 item_spacing = ImGui::GetStyle ().ItemSpacing ;
3654
+ static float a_c_spring_weight = 0 .0f ;
3655
+ static float ab_spring_weight = 0 .5f ;
3656
+ static float alignment = 0 .5f ;
3657
+
3658
+ struct funcs
3659
+ {
3660
+ static void VisibleSpring (float spring_weight)
3661
+ {
3662
+ ImGui::Spring (spring_weight);
3663
+ if (!draw_springs)
3664
+ return ;
3665
+
3666
+ ImVec2 rect_min = ImGui::GetItemRectMin ();
3667
+ ImVec2 rect_max = ImGui::GetItemRectMax ();
3668
+
3669
+ ImVec2 rect_size = ImGui::GetItemRectSize ();
3670
+ if (rect_size.x <= 0 .0f && rect_size.y <= 0 .0f )
3671
+ return ;
3672
+
3673
+ // Draw zig-zag
3674
+ float width = 0 .0f , spacing = 0 .0f ;
3675
+ ImVec2 direction, origin;
3676
+ ImVec2 spacing_min, spring_max;
3677
+
3678
+ if (horizontal)
3679
+ {
3680
+ spacing = floorf (item_spacing.x );
3681
+ width = rect_size.x - spacing;
3682
+ origin = ImVec2 (floorf (rect_min.x ), floorf (rect_min.y + (rect_max.y - rect_min.y ) / 2 ));
3683
+ direction = ImVec2 (1 .0f , 0 .0f );
3684
+ spring_max = ImVec2 (rect_min.x + width, rect_max.y );
3685
+ spacing_min = ImVec2 (rect_min.x + width, rect_min.y );
3686
+ }
3687
+ else
3688
+ {
3689
+ spacing = floorf (item_spacing.y );
3690
+ width = rect_size.y - spacing;
3691
+ origin = ImVec2 (floorf (rect_min.x + (rect_max.x - rect_min.x ) / 2 ), floorf (rect_min.y ));
3692
+ direction = ImVec2 (0 .0f , 1 .0f );
3693
+ spring_max = ImVec2 (rect_max.x , rect_min.y + width);
3694
+ spacing_min = ImVec2 (rect_min.x , rect_min.y + width);
3695
+ }
3696
+
3697
+ if (spring_weight <= 0 .0f && spacing <= 0 .0f )
3698
+ return ;
3699
+
3700
+ ImDrawList* draw_list = ImGui::GetWindowDrawList ();
3701
+
3702
+ draw_list->PushClipRect (rect_min, rect_max, true );
3703
+
3704
+ draw_list->AddRectFilled (rect_min, spring_max, ImColor (80 , 20 , 80 ));
3705
+ draw_list->AddRectFilled (spacing_min, rect_max, ImColor (80 , 20 , 20 ));
3706
+
3707
+ const float zig_zag_size = 3 ;
3708
+ ImVec2 normal = ImVec2 (-direction.y , direction.x );
3709
+
3710
+ draw_list->PathClear ();
3711
+ origin.x += 0 .5f ;
3712
+ origin.y += 0 .5f ;
3713
+ draw_list->PathLineTo (origin);
3714
+ for (float x = zig_zag_size * 0 .5f ; x <= width; x += zig_zag_size)
3715
+ {
3716
+ ImVec2 p;
3717
+ p.x = origin.x + direction.x * x + normal.x * zig_zag_size;
3718
+ p.y = origin.y + direction.y * x + normal.y * zig_zag_size;
3719
+ draw_list->PathLineTo (p);
3720
+ normal = ImVec2 (-normal.x , -normal.y );
3721
+ }
3722
+ draw_list->PathStroke (ImColor (255 , 255 , 255 , 190 ), false , 1 .0f );
3723
+
3724
+ draw_list->PopClipRect ();
3725
+ }
3726
+ };
3727
+
3728
+ ImGui::Checkbox (" Widget A" , &widget_a); ImGui::SameLine ();
3729
+ ImGui::Checkbox (" Widget B" , &widget_b); ImGui::SameLine ();
3730
+ ImGui::Checkbox (" Widget C" , &widget_c);
3731
+ ImGui::Checkbox (" Spring A" , &spring_a); ImGui::SameLine ();
3732
+ ImGui::Checkbox (" Spring AB" , &spring_ab); ImGui::SameLine ();
3733
+ ImGui::Checkbox (" Spring BC" , &spring_bc); ImGui::SameLine ();
3734
+ ImGui::Checkbox (" Spring C" , &spring_c);
3735
+ ImGui::Checkbox (" Horizontal" , &horizontal); ImGui::SameLine ();
3736
+ ImGui::Checkbox (" Minimize Width" , &minimize_width); ImGui::SameLine ();
3737
+ ImGui::Checkbox (" Minimize Height" , &minimize_height);
3738
+ ImGui::Checkbox (" Draw Springs" , &draw_springs); ImGui::SameLine ();
3739
+ ImGui::TextUnformatted (" " ); ImGui::SameLine ();
3740
+ ImGui::ColorButton (" - Spring" , ImColor (80 , 20 , 80 ), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine ();
3741
+ ImGui::TextUnformatted (" Spring" ); ImGui::SameLine ();
3742
+ ImGui::TextUnformatted (" " ); ImGui::SameLine ();
3743
+ ImGui::ColorButton (" - Spacing" , ImColor (80 , 20 , 20 ), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine ();
3744
+ ImGui::TextUnformatted (" Item Spacing" );
3745
+ ImGui::DragFloat (" Item Spacing" , horizontal ? &item_spacing.x : &item_spacing.y , 0 .1f , 0 .0f , 50 .0f );
3746
+ ImGui::DragFloat (" A & C Spring Weight" , &a_c_spring_weight, 0 .002f , 0 .0f , 1 .0f );
3747
+ ImGui::DragFloat (" AB Spring Weight" , &ab_spring_weight, 0 .002f , 0 .0f , 1 .0f );
3748
+ if (ImGui::IsItemHovered ()) ImGui::SetTooltip (" BC Spring Weight = 1 - AB Spring Weight" );
3749
+ ImGui::DragFloat (" Minor Axis Alignment" , &alignment, 0 .002f , 0 .0f , 1 .0f );
3750
+ if (ImGui::IsItemHovered ()) ImGui::SetTooltip (" This is vertical alignment for horizontal layouts and horizontal alignment for vertical layouts." );
3751
+ ImGui::Text (" Layout widgets:" );
3752
+ ImGui::Text (" | Spring A | Widget A | Spring AB | Widget B | Spring BC | Widget C | Spring C |" );
3753
+
3754
+ ImGui::Spacing ();
3755
+
3756
+ ImVec2 widget_size;
3757
+ widget_size.x = floorf (ImGui::GetContentRegionAvail ().x / 4 );
3758
+ widget_size.y = horizontal ? floorf (widget_size.x / 3 ) : widget_size.x ;
3759
+
3760
+ ImVec2 small_widget_size = widget_size;
3761
+ if (horizontal)
3762
+ small_widget_size.y = floorf (small_widget_size.y / 2 );
3763
+ else
3764
+ small_widget_size.x = floorf (small_widget_size.x / 2 );
3765
+
3766
+ ImVec2 layout_size = ImVec2 (widget_size.x * 4 , widget_size.y * 4 );
3767
+ if (minimize_width) layout_size.x = 0 .0f ;
3768
+ if (minimize_height) layout_size.y = 0 .0f ;
3769
+
3770
+ // Minor axis alignment can be set by style or directly in BeginHorizontal/BeginVertical
3771
+ // Example:
3772
+ // ImGui::PushStyleVar(ImGuiStyleVar_LayoutAlign, alignment);
3773
+
3774
+ ImGui::PushStyleVar (ImGuiStyleVar_ItemSpacing, ImVec2 (floorf (item_spacing.x ), floorf (item_spacing.y )));
3775
+
3776
+ if (horizontal) { ImGui::BeginHorizontal (" h1" , layout_size, alignment); } else { ImGui::BeginVertical (" v1" , layout_size, alignment); }
3777
+ if (spring_a) { funcs::VisibleSpring (a_c_spring_weight); }
3778
+ if (widget_a) { ImGui::Button (" Widget A" , widget_size); }
3779
+ if (spring_ab) { funcs::VisibleSpring (ab_spring_weight); }
3780
+ if (widget_b) { ImGui::Button (" Widget B" , small_widget_size); }
3781
+ if (spring_bc) { funcs::VisibleSpring (1 .0f - ab_spring_weight); }
3782
+ if (widget_c) { ImGui::Button (" Widget C" , widget_size); }
3783
+ if (spring_c) { funcs::VisibleSpring (a_c_spring_weight); }
3784
+ if (horizontal) { ImGui::EndHorizontal (); } else { ImGui::EndVertical (); }
3785
+
3786
+ ImGui::PopStyleVar ();
3787
+
3788
+ ImDrawList* draw_list = ImGui::GetWindowDrawList ();
3789
+ draw_list->AddRect (ImGui::GetItemRectMin (), ImGui::GetItemRectMax (), ImGui::GetColorU32 (ImGuiCol_Border));
3790
+
3791
+ ImGui::TreePop ();
3792
+ }
3793
+ #endif // IMGUI_HAS_STACK_LAYOUT
3644
3794
}
3645
3795
3646
3796
static void ShowDemoWindowPopups ()
0 commit comments