Skip to content

Ui root node is not sized properly with custom UiCamera's projection #2405

@Daniikk1012

Description

@Daniikk1012

Bevy version

0.5

Operating system & version

Tested on Linux, but probably affects all other systems

What you did

I have this system that resizes for all the cameras with OrthographicsProjection and CameraAspectRatio compoents:

pub enum CameraAspectRatio {                                                      
    Auto {                                                                        
        min_width: f32,                                                           
        min_height: f32,                                                          
    },                                                                            
    FixedWidth(f32),                                                              
    FixedHeight(f32),                                                             
}

pub fn camera_aspect_ratio(                                                      
    windows: Res<Windows>,                                                       
    mut query:                                                                    
        Query<(&mut OrthographicProjection, &Camera, &CameraAspectRatio)>,        
) {                                                                               
    for (mut projection, camera, ratio) in query.iter_mut() {                     
        if let (ScalingMode::None, Some(window)) =                                
            (&projection.scaling_mode, windows.get(camera.window))                
        {                                                                         
            let window_size = Vec2::new(window.width(), window.height());         
                                                                                  
            let size = match *ratio {                                             
                CameraAspectRatio::Auto { min_width, min_height } => if           
                    window_size.x / window_size.y > min_width / min_height        
                {                                                                
                    Vec2::new(                                                    
                        min_height * window_size.x / window_size.y,               
                        min_height,                                               
                    )                                                             
                } else {                                                          
                    Vec2::new(                                                    
                        min_width, min_width * window_size.y / window_size.x,    
                    )                                                             
                },                                                                
                CameraAspectRatio::FixedWidth(width) => Vec2::new(                
                    width,                                                        
                    width * window_size.y / window_size.x,                        
                ),                                                                
                CameraAspectRatio::FixedHeight(height) => Vec2::new(              
                    height * window_size.x / window_size.y,                       
                    height,                                                       
                ),                                                                
            };                                                                   
                                                                                 
            match projection.window_origin {                                     
                WindowOrigin::Center => {                                        
                    projection.left = -size.x / 2.0;                             
                    projection.right = size.x / 2.0;                             
                                                                                 
                    projection.bottom = -size.y / 2.0;                           
                    projection.top = size.y / 2.0;                               
                },                                                               
                WindowOrigin::BottomLeft => {                                    
                    projection.left = 0.0;                                       
                    projection.right = size.x;                                   
                                                                                 
                    projection.bottom = 0.0;                                     
                    projection.top = size.y;                                     
                },                                                               
            }                                                                    
        }                                                                        
    }                                                                            
}

The system works fine and the game itself displays correctly. However when I try to create a root node with size set to 100% by 100% it does not fill the whole window, but instead its virtual size gets limited to the value of the real screen size:

    commands                                                                      
        .spawn_bundle(NodeBundle {                                                
            material: materials.add(Color::rgba(1.0, 1.0, 1.0, 0.5).into()),      
            style: Style {                                                        
                size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),        
                flex_direction: FlexDirection::Row,                               
                align_items: AlignItems::FlexEnd,                                 
                justify_content: JustifyContent::SpaceBetween,                    
                ..Default::default()                                              
            },                                                                    
            ..Default::default()                                                  
        })

изображение

What you expected to happen

The root node should fill the whole window.

What actually happened

The root node's size was limited to the window's logical size and was not enough to fill the virtual camera size (1920 by 1080)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-UIGraphical user interfaces, styles, layouts, and widgetsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions