Shrink size of `Shadow` by using `i8/u8` instead of `f32` (#5568)
* Part of https://github.com/emilk/egui/issues/4019
This commit is contained in:
parent
d58d13781d
commit
ee4ab08c8a
|
|
@ -76,7 +76,7 @@ pub struct Frame {
|
||||||
#[test]
|
#[test]
|
||||||
fn frame_size() {
|
fn frame_size() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
std::mem::size_of::<Frame>(), 44,
|
std::mem::size_of::<Frame>(), 32,
|
||||||
"Frame changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
|
"Frame changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
|
||||||
);
|
);
|
||||||
assert!(
|
assert!(
|
||||||
|
|
|
||||||
|
|
@ -1293,9 +1293,9 @@ impl Visuals {
|
||||||
|
|
||||||
window_rounding: Rounding::same(6),
|
window_rounding: Rounding::same(6),
|
||||||
window_shadow: Shadow {
|
window_shadow: Shadow {
|
||||||
offset: vec2(10.0, 20.0),
|
offset: [10, 20],
|
||||||
blur: 15.0,
|
blur: 15,
|
||||||
spread: 0.0,
|
spread: 0,
|
||||||
color: Color32::from_black_alpha(96),
|
color: Color32::from_black_alpha(96),
|
||||||
},
|
},
|
||||||
window_fill: Color32::from_gray(27),
|
window_fill: Color32::from_gray(27),
|
||||||
|
|
@ -1307,9 +1307,9 @@ impl Visuals {
|
||||||
panel_fill: Color32::from_gray(27),
|
panel_fill: Color32::from_gray(27),
|
||||||
|
|
||||||
popup_shadow: Shadow {
|
popup_shadow: Shadow {
|
||||||
offset: vec2(6.0, 10.0),
|
offset: [6, 10],
|
||||||
blur: 8.0,
|
blur: 8,
|
||||||
spread: 0.0,
|
spread: 0,
|
||||||
color: Color32::from_black_alpha(96),
|
color: Color32::from_black_alpha(96),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -1349,9 +1349,9 @@ impl Visuals {
|
||||||
error_fg_color: Color32::from_rgb(255, 0, 0), // red
|
error_fg_color: Color32::from_rgb(255, 0, 0), // red
|
||||||
|
|
||||||
window_shadow: Shadow {
|
window_shadow: Shadow {
|
||||||
offset: vec2(10.0, 20.0),
|
offset: [10, 20],
|
||||||
blur: 15.0,
|
blur: 15,
|
||||||
spread: 0.0,
|
spread: 0,
|
||||||
color: Color32::from_black_alpha(25),
|
color: Color32::from_black_alpha(25),
|
||||||
},
|
},
|
||||||
window_fill: Color32::from_gray(248),
|
window_fill: Color32::from_gray(248),
|
||||||
|
|
@ -1360,9 +1360,9 @@ impl Visuals {
|
||||||
panel_fill: Color32::from_gray(248),
|
panel_fill: Color32::from_gray(248),
|
||||||
|
|
||||||
popup_shadow: Shadow {
|
popup_shadow: Shadow {
|
||||||
offset: vec2(6.0, 10.0),
|
offset: [6, 10],
|
||||||
blur: 8.0,
|
blur: 8,
|
||||||
spread: 0.0,
|
spread: 0,
|
||||||
color: Color32::from_black_alpha(25),
|
color: Color32::from_black_alpha(25),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -2456,13 +2456,13 @@ impl Widget for &mut Shadow {
|
||||||
ui.vertical(|ui| {
|
ui.vertical(|ui| {
|
||||||
crate::Grid::new("shadow_ui").show(ui, |ui| {
|
crate::Grid::new("shadow_ui").show(ui, |ui| {
|
||||||
ui.add(
|
ui.add(
|
||||||
DragValue::new(&mut offset.x)
|
DragValue::new(&mut offset[0])
|
||||||
.speed(1.0)
|
.speed(1.0)
|
||||||
.range(-100.0..=100.0)
|
.range(-100.0..=100.0)
|
||||||
.prefix("x: "),
|
.prefix("x: "),
|
||||||
);
|
);
|
||||||
ui.add(
|
ui.add(
|
||||||
DragValue::new(&mut offset.y)
|
DragValue::new(&mut offset[1])
|
||||||
.speed(1.0)
|
.speed(1.0)
|
||||||
.range(-100.0..=100.0)
|
.range(-100.0..=100.0)
|
||||||
.prefix("y: "),
|
.prefix("y: "),
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,9 @@ impl Default for FrameDemo {
|
||||||
outer_margin: 24.0.into(),
|
outer_margin: 24.0.into(),
|
||||||
rounding: 14.0.into(),
|
rounding: 14.0.into(),
|
||||||
shadow: egui::Shadow {
|
shadow: egui::Shadow {
|
||||||
offset: [8.0, 12.0].into(),
|
offset: [8, 12],
|
||||||
blur: 16.0,
|
blur: 16,
|
||||||
spread: 0.0,
|
spread: 0,
|
||||||
color: egui::Color32::from_black_alpha(180),
|
color: egui::Color32::from_black_alpha(180),
|
||||||
},
|
},
|
||||||
fill: egui::Color32::from_rgba_unmultiplied(97, 0, 255, 128),
|
fill: egui::Color32::from_rgba_unmultiplied(97, 0, 255, 128),
|
||||||
|
|
|
||||||
|
|
@ -5,33 +5,41 @@ use crate::{Color32, Marginf, Rect, RectShape, Rounding, Vec2};
|
||||||
/// Can be used for a rectangular shadow with a soft penumbra.
|
/// Can be used for a rectangular shadow with a soft penumbra.
|
||||||
///
|
///
|
||||||
/// Very similar to a box-shadow in CSS.
|
/// Very similar to a box-shadow in CSS.
|
||||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||||
pub struct Shadow {
|
pub struct Shadow {
|
||||||
/// Move the shadow by this much.
|
/// Move the shadow by this much.
|
||||||
///
|
///
|
||||||
/// For instance, a value of `[1.0, 2.0]` will move the shadow 1 point to the right and 2 points down,
|
/// For instance, a value of `[1.0, 2.0]` will move the shadow 1 point to the right and 2 points down,
|
||||||
/// causing a drop-shadow effect.
|
/// causing a drop-shadow effect.
|
||||||
pub offset: Vec2,
|
pub offset: [i8; 2],
|
||||||
|
|
||||||
/// The width of the blur, i.e. the width of the fuzzy penumbra.
|
/// The width of the blur, i.e. the width of the fuzzy penumbra.
|
||||||
///
|
///
|
||||||
/// A value of 0.0 means a sharp shadow.
|
/// A value of 0 means a sharp shadow.
|
||||||
pub blur: f32,
|
pub blur: u8,
|
||||||
|
|
||||||
/// Expand the shadow in all directions by this much.
|
/// Expand the shadow in all directions by this much.
|
||||||
pub spread: f32,
|
pub spread: u8,
|
||||||
|
|
||||||
/// Color of the opaque center of the shadow.
|
/// Color of the opaque center of the shadow.
|
||||||
pub color: Color32,
|
pub color: Color32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn shadow_size() {
|
||||||
|
assert_eq!(
|
||||||
|
std::mem::size_of::<Shadow>(), 8,
|
||||||
|
"Shadow changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
impl Shadow {
|
impl Shadow {
|
||||||
/// No shadow at all.
|
/// No shadow at all.
|
||||||
pub const NONE: Self = Self {
|
pub const NONE: Self = Self {
|
||||||
offset: Vec2::ZERO,
|
offset: [0, 0],
|
||||||
blur: 0.0,
|
blur: 0,
|
||||||
spread: 0.0,
|
spread: 0,
|
||||||
color: Color32::TRANSPARENT,
|
color: Color32::TRANSPARENT,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -45,11 +53,14 @@ impl Shadow {
|
||||||
spread,
|
spread,
|
||||||
color,
|
color,
|
||||||
} = *self;
|
} = *self;
|
||||||
|
let [offset_x, offset_y] = offset;
|
||||||
|
|
||||||
let rect = rect.translate(offset).expand(spread);
|
let rect = rect
|
||||||
let rounding = rounding.into() + Rounding::from(spread.abs());
|
.translate(Vec2::new(offset_x as _, offset_y as _))
|
||||||
|
.expand(spread as _);
|
||||||
|
let rounding = rounding.into() + Rounding::from(spread);
|
||||||
|
|
||||||
RectShape::filled(rect, rounding, color).with_blur_width(blur)
|
RectShape::filled(rect, rounding, color).with_blur_width(blur as _)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// How much larger than the parent rect are we in each direction?
|
/// How much larger than the parent rect are we in each direction?
|
||||||
|
|
@ -60,11 +71,14 @@ impl Shadow {
|
||||||
spread,
|
spread,
|
||||||
color: _,
|
color: _,
|
||||||
} = *self;
|
} = *self;
|
||||||
|
let spread = spread as f32;
|
||||||
|
let blur = blur as f32;
|
||||||
|
let [offset_x, offset_y] = offset;
|
||||||
Marginf {
|
Marginf {
|
||||||
left: spread + 0.5 * blur - offset.x,
|
left: spread + 0.5 * blur - offset_x as f32,
|
||||||
right: spread + 0.5 * blur + offset.x,
|
right: spread + 0.5 * blur + offset_x as f32,
|
||||||
top: spread + 0.5 * blur - offset.y,
|
top: spread + 0.5 * blur - offset_y as f32,
|
||||||
bottom: spread + 0.5 * blur + offset.y,
|
bottom: spread + 0.5 * blur + offset_y as f32,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue