bevy/crates
Marco Meijer 55cc93a7be fix: setting content size to rect size if image has a rect (#16457)
# Objective

When using a rect for a ui image, its content size is still equal to the
size of the full image instead of the size of the rect.

## Solution

Use the rect size if it is present.

## Testing

I tested it using all 4 possible combinations of having a rect and
texture atlas or not. See the showcase section.

---

## Showcase

<details>
  <summary>Click to view showcase</summary>

```rust
use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
        .add_systems(Startup, create_ui)
        .run();
}

fn create_ui(
    mut commands: Commands,
    assets: Res<AssetServer>,
    mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
    mut ui_scale: ResMut<UiScale>,
) {
    let texture = assets.load("textures/fantasy_ui_borders/numbered_slices.png");
    let layout = TextureAtlasLayout::from_grid(UVec2::splat(16), 3, 3, None, None);
    let texture_atlas_layout = texture_atlas_layouts.add(layout);

    ui_scale.0 = 2.;

    commands.spawn(Camera2d::default());

    commands
        .spawn(Node {
            display: Display::Flex,
            align_items: AlignItems::Center,
            ..default()
        })
        .with_children(|parent| {
            // nothing
            parent.spawn(ImageNode::new(texture.clone()));

            // with rect
            parent.spawn(ImageNode::new(texture.clone()).with_rect(Rect::new(0., 0., 16., 16.)));

            // with rect and texture atlas
            parent.spawn(
                ImageNode::from_atlas_image(
                    texture.clone(),
                    TextureAtlas {
                        layout: texture_atlas_layout.clone(),
                        index: 1,
                    },
                )
                .with_rect(Rect::new(0., 0., 8., 8.)),
            );

            // with texture atlas
            parent.spawn(ImageNode::from_atlas_image(
                texture.clone(),
                TextureAtlas {
                    layout: texture_atlas_layout.clone(),
                    index: 2,
                },
            ));
        });
}
```

Before this change:
<img width="529" alt="Screenshot 2024-11-21 at 11 55 45"
src="https://github.com/user-attachments/assets/23196003-08ca-4049-8409-fe349bd5aa54">

After the change:
<img width="400" alt="Screenshot 2024-11-21 at 11 54 54"
src="https://github.com/user-attachments/assets/e2cd6ebf-859c-40a1-9fc4-43bb28b024e5">


</details>
2024-11-22 21:24:04 +01:00
..
bevy_a11y Remove accesskit re-export from bevy_a11y (#16257) 2024-11-11 22:14:07 +01:00
bevy_animation Fix dynamic linking failures from the AnimationEventFn change. (#16476) 2024-11-22 21:24:03 +01:00
bevy_app AnimationEvent -> Event and other improvements (#16440) 2024-11-22 21:20:11 +01:00
bevy_asset Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_audio AudioPlayer::new() (#16287) 2024-11-11 22:08:10 +01:00
bevy_color Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_core Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_core_pipeline Fix missing import (#16337) 2024-11-11 22:18:05 +01:00
bevy_derive Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_dev_tools Headless by features (#16401) 2024-11-17 18:39:27 +01:00
bevy_diagnostic Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_dylib Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_ecs Fix adding a subtree of required components to an existing tree replacing shallower required component constructors (#16441) 2024-11-22 21:24:03 +01:00
bevy_encase_derive Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_gilrs Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_gizmos Use normal constructors for EasingCurve, FunctionCurve, ConstantCurve (#16367) 2024-11-16 15:30:10 +01:00
bevy_gltf Use normal constructors for EasingCurve, FunctionCurve, ConstantCurve (#16367) 2024-11-16 15:30:10 +01:00
bevy_hierarchy Fix bevy_hierarchy failing to compile without reflect feature (#16428) 2024-11-19 22:10:25 +01:00
bevy_image Fix off-by-one error on Image::get_color_at and Image::set_color_at. (#16475) 2024-11-22 21:24:03 +01:00
bevy_input Revert most of #16222 and add gamepad accessors (#16425) 2024-11-19 22:10:24 +01:00
bevy_internal Fix the picking backend features not actually disabling the features (#16470) 2024-11-22 21:24:03 +01:00
bevy_log Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_macro_utils Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_math Use normal constructors for EasingCurve, FunctionCurve, ConstantCurve (#16367) 2024-11-16 15:30:10 +01:00
bevy_mesh Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_mikktspace Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_pbr Do not re-check visibility or re-render shadow maps for point and spot lights for each view (#15156) 2024-11-17 19:06:11 +01:00
bevy_picking Fix the picking backend features not actually disabling the features (#16470) 2024-11-22 21:24:03 +01:00
bevy_ptr Reduce compile time of bevy_ptr::OwnedPtr::make function (#15644) 2024-11-17 19:06:07 +01:00
bevy_reflect bevy_reflect: Add ReflectSerializerProcessor (#15548) 2024-11-17 19:06:13 +01:00
bevy_remote Expose BRP system scheduling and add system set (#16400) 2024-11-17 18:39:34 +01:00
bevy_render Make render world sync marker components Copy (#16461) 2024-11-22 21:24:04 +01:00
bevy_scene Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_sprite Add flags to SpritePlugin and UiPlugin to allow disabling their picking backend (without needing to disable features). (#16473) 2024-11-22 21:24:03 +01:00
bevy_state Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_tasks Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_text AnimationEvent -> Event and other improvements (#16440) 2024-11-22 21:20:11 +01:00
bevy_time Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_transform Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_ui fix: setting content size to rect size if image has a rect (#16457) 2024-11-22 21:24:04 +01:00
bevy_utils Fix detailed_trace! (#16452) 2024-11-22 21:16:58 +01:00
bevy_window Release 0.15.0-rc.3 2024-11-05 23:26:31 +01:00
bevy_winit Remove accesskit re-export from bevy_a11y (#16257) 2024-11-11 22:14:07 +01:00