Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenient methods for Image #10221

Merged
merged 4 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
use bevy_asset::Asset;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::system::{lifetimeless::SRes, Resource, SystemParamItem};
use bevy_math::Vec2;
use bevy_math::{UVec2, Vec2};
use bevy_reflect::Reflect;
use serde::{Deserialize, Serialize};
use std::hash::Hash;
Expand Down Expand Up @@ -256,17 +256,34 @@ impl Image {

/// Returns the aspect ratio (height/width) of a 2D image.
pub fn aspect_2d(&self) -> f32 {
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
self.texture_descriptor.size.height as f32 / self.texture_descriptor.size.width as f32
self.height() as f32 / self.width() as f32
}

/// Returns the size of a 2D image.
pub fn size(&self) -> Vec2 {
/// Returns the size of a 2D image as f32.
pub fn size_f32(&self) -> Vec2 {
Vec2::new(
self.texture_descriptor.size.width as f32,
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
self.texture_descriptor.size.height as f32,
)
}

/// Returns the size of a 2D image.
pub fn size(&self) -> UVec2 {
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
UVec2::new(
self.texture_descriptor.size.width,
self.texture_descriptor.size.height,
)
}

/// Returns the size of a 2D image.
pub fn width(&self) -> u32 {
self.texture_descriptor.size.width
}

pub fn height(&self) -> u32 {
self.texture_descriptor.size.height
}

/// Resizes the image to the new size, by removing information or appending 0 to the `data`.
/// Does not properly resize the contents of the image, but only its internal `data` buffer.
pub fn resize(&mut self, size: Extent3d) {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub fn calculate_bounds_2d(
for (entity, sprite, texture_handle) in &sprites_without_aabb {
if let Some(size) = sprite
.custom_size
.or_else(|| images.get(texture_handle).map(|image| image.size()))
.or_else(|| images.get(texture_handle).map(|image| image.size_f32()))
{
let aabb = Aabb {
center: (-sprite.anchor.as_vec() * size).extend(0.0).into(),
Expand Down
Loading