Skip to content

Commit

Permalink
Merge pull request #174 from danbulant/image-corner-radii
Browse files Browse the repository at this point in the history
Make images respect border radii
  • Loading branch information
ecton authored Oct 6, 2024
2 parents 432a84b + 8d11a90 commit 2239894
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/widgets/image.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! A widget that displays an image/texture.
use figures::units::{Px, UPx};
use figures::{FloatConversion, IntoSigned, IntoUnsigned, Point, Rect, Size, Zero};
use kludgine::{AnyTexture, CollectedTexture, LazyTexture, SharedTexture, Texture, TextureRegion};
use figures::{FloatConversion, IntoSigned, IntoUnsigned, Point, Rect, ScreenScale, Size, Zero};
use kludgine::shapes::Shape;
use kludgine::{AnyTexture, CollectedTexture, Color, LazyTexture, SharedTexture, Texture, TextureRegion};

use crate::animation::ZeroToOne;
use crate::context::{LayoutContext, Trackable};
use crate::styles::components::CornerRadius;
use crate::value::{IntoValue, Source, Value};
use crate::widget::Widget;
use crate::ConstraintLimit;
Expand Down Expand Up @@ -157,9 +159,16 @@ impl Widget for Image {
fn redraw(&mut self, context: &mut crate::context::GraphicsContext<'_, '_, '_, '_>) {
self.contents.invalidate_when_changed(context);
let opacity = self.opacity.get_tracking_redraw(context);
let radii = context.get(&CornerRadius);
let radii = radii.map(|r| r.into_px(context.gfx.scale()));

self.contents.map(|texture| {
let rect = self.calculate_image_rect(texture, context.gfx.size(), context);
context.gfx.draw_texture(texture, rect, opacity);
if radii.is_zero() {
context.gfx.draw_texture(texture, rect, opacity);
} else {
context.gfx.draw_textured_shape(&Shape::textured_round_rect(rect, radii, Rect::from(texture.size()), Color::WHITE), texture, opacity);
}
});
}

Expand Down

0 comments on commit 2239894

Please sign in to comment.