ImageCornerRadius

In the end I think people might be surprised by the default behavior to
clip corners, so I've opted to split this behavior into a separate
component that defaults to no corner radius.
This commit is contained in:
Jonathan Johnson 2024-10-07 12:52:18 -07:00
parent 2ed140a0fe
commit af208519eb
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
2 changed files with 25 additions and 6 deletions

View file

@ -68,7 +68,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `WrapperWidget::activate`'s default implementation now activates the wrapped - `WrapperWidget::activate`'s default implementation now activates the wrapped
widget. widget.
- `Space` now intercepts mouse events if its color has a non-zero alpha channel. - `Space` now intercepts mouse events if its color has a non-zero alpha channel.
- `Image` now honors `CornerRadius`. Thanks to @danbulant for this change!
### Fixed ### Fixed
@ -230,6 +229,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Scroll::preserve_max_scroll` controls whether the scroll view automatically - `Scroll::preserve_max_scroll` controls whether the scroll view automatically
scrolls when currently scrolled to the maximum and its child grows. The scrolls when currently scrolled to the maximum and its child grows. The
default is `true`, which was the behavior before this flag was added. default is `true`, which was the behavior before this flag was added.
- `Image` now supports `ImageCornerRadius`. Thanks to @danbulant for helping
with this change!
[139]: https://github.com/khonsulabs/cushy/issues/139 [139]: https://github.com/khonsulabs/cushy/issues/139

View file

@ -2,12 +2,14 @@
use figures::units::{Px, UPx}; use figures::units::{Px, UPx};
use figures::{FloatConversion, IntoSigned, IntoUnsigned, Point, Rect, ScreenScale, Size, Zero}; use figures::{FloatConversion, IntoSigned, IntoUnsigned, Point, Rect, ScreenScale, Size, Zero};
use kludgine::shapes::Shape; use kludgine::shapes::{CornerRadii, Shape};
use kludgine::{AnyTexture, CollectedTexture, Color, LazyTexture, SharedTexture, Texture, TextureRegion}; use kludgine::{
AnyTexture, CollectedTexture, Color, LazyTexture, SharedTexture, Texture, TextureRegion,
};
use crate::animation::ZeroToOne; use crate::animation::ZeroToOne;
use crate::context::{LayoutContext, Trackable}; use crate::context::{LayoutContext, Trackable};
use crate::styles::components::CornerRadius; use crate::styles::Dimension;
use crate::value::{IntoValue, Source, Value}; use crate::value::{IntoValue, Source, Value};
use crate::widget::Widget; use crate::widget::Widget;
use crate::ConstraintLimit; use crate::ConstraintLimit;
@ -159,7 +161,7 @@ impl Widget for Image {
fn redraw(&mut self, context: &mut crate::context::GraphicsContext<'_, '_, '_, '_>) { fn redraw(&mut self, context: &mut crate::context::GraphicsContext<'_, '_, '_, '_>) {
self.contents.invalidate_when_changed(context); self.contents.invalidate_when_changed(context);
let opacity = self.opacity.get_tracking_redraw(context); let opacity = self.opacity.get_tracking_redraw(context);
let radii = context.get(&CornerRadius); let radii = context.get(&ImageCornerRadius);
let radii = radii.map(|r| r.into_px(context.gfx.scale())); let radii = radii.map(|r| r.into_px(context.gfx.scale()));
self.contents.map(|texture| { self.contents.map(|texture| {
@ -167,7 +169,16 @@ impl Widget for Image {
if radii.is_zero() { if radii.is_zero() {
context.gfx.draw_texture(texture, rect, opacity); context.gfx.draw_texture(texture, rect, opacity);
} else { } else {
context.gfx.draw_textured_shape(&Shape::textured_round_rect(rect, radii, Rect::from(texture.size()), Color::WHITE), texture, opacity); context.gfx.draw_textured_shape(
&Shape::textured_round_rect(
rect,
radii,
Rect::from(texture.size()),
Color::WHITE,
),
texture,
opacity,
);
} }
}); });
} }
@ -254,3 +265,10 @@ pub enum Aspect {
/// size it can be to cover the entire surface. /// size it can be to cover the entire surface.
Fill, Fill,
} }
define_components! {
Image {
/// The corner radius to use to clip when rendering an [`Image`].
ImageCornerRadius(CornerRadii<Dimension>, "corner_radius", CornerRadii::ZERO)
}
}