Skip to content

Commit

Permalink
Added new nine-patch features
Browse files Browse the repository at this point in the history
  • Loading branch information
StarArawn committed Mar 24, 2024
1 parent 5371ec1 commit 2dfb514
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/nine_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ fn startup(
nine_patch={NinePatch {
handle: image,
border: Edge::all(15.0),
..Default::default()
}}
styles={KStyle {
width: StyleProp::Value(Units::Pixels(512.0)),
Expand Down
5 changes: 3 additions & 2 deletions src/render/nine_patch/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn extract_nine_patch(
layout: crate::layout::Rect,
handle: Handle<Image>,
border: Edge<f32>,
scale: f32,
opacity_layer: u32,
images: &Assets<Image>,
dpi: f32,
Expand All @@ -28,8 +29,8 @@ pub fn extract_nine_patch(
let image_size = image
.map(|i| {
Vec2::new(
i.texture_descriptor.size.width as f32,
i.texture_descriptor.size.height as f32,
i.texture_descriptor.size.width as f32 * scale,
i.texture_descriptor.size.height as f32 * scale,
)
})
.unwrap()
Expand Down
3 changes: 2 additions & 1 deletion src/render_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,13 @@ impl RenderPrimitive for KStyle {
);
}
}
RenderCommand::NinePatch { border, handle } => {
RenderCommand::NinePatch { border, handle, scale } => {
let mut nines = crate::render::nine_patch::extract_nine_patch(
camera_entity,
*layout,
handle,
border,
scale,
opacity_layer,
images,
dpi,
Expand Down
1 change: 1 addition & 0 deletions src/styles/render_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum RenderCommand {
NinePatch {
border: Edge<f32>,
handle: Handle<Image>,
scale: f32,
},
#[cfg(feature = "svg")]
Svg {
Expand Down
16 changes: 15 additions & 1 deletion src/widgets/nine_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ use crate::{
widget::Widget,
};

#[derive(Component, PartialEq, Clone, Default, Debug)]
#[derive(Component, PartialEq, Clone, Debug)]
pub struct NinePatch {
/// The handle to image
pub handle: Handle<Image>,
/// The size of each edge (in pixels)
pub border: Edge<f32>,
/// Scale of the nine patch
/// defaults to 1.0
pub scale: f32,
}

impl Default for NinePatch {
fn default() -> Self {
Self {
handle: Default::default(),
border: Default::default(),
scale: 1.0
}
}
}

impl Widget for NinePatch {}
Expand Down Expand Up @@ -83,6 +96,7 @@ pub fn nine_patch_render(
render_command: RenderCommand::NinePatch {
border: nine_patch.border,
handle: nine_patch.handle.clone_weak(),
scale: nine_patch.scale,
}
.into(),
..Default::default()
Expand Down

0 comments on commit 2dfb514

Please sign in to comment.