Refactor texture image filtering

- Support only `Linear` or `Nearest`
- Simplify `Layer` groups
- Move `FilterMethod` to `Image` and `image::Viewer`
This commit is contained in:
Héctor Ramón Jiménez 2023-11-11 07:02:01 +01:00
parent 75c9afc608
commit a5125d6fea
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
12 changed files with 250 additions and 160 deletions

View file

@ -1,4 +1,4 @@
use iced::alignment;
use iced::alignment::{self, Alignment};
use iced::theme;
use iced::widget::{
checkbox, column, container, horizontal_space, image, radio, row,
@ -126,7 +126,10 @@ impl Steps {
Step::Toggler {
can_continue: false,
},
Step::Image { width: 300 },
Step::Image {
width: 300,
filter_method: image::FilterMethod::Linear,
},
Step::Scrollable,
Step::TextInput {
value: String::new(),
@ -195,6 +198,7 @@ enum Step {
},
Image {
width: u16,
filter_method: image::FilterMethod,
},
Scrollable,
TextInput {
@ -215,6 +219,7 @@ pub enum StepMessage {
TextColorChanged(Color),
LanguageSelected(Language),
ImageWidthChanged(u16),
ImageUseNearestToggled(bool),
InputChanged(String),
ToggleSecureInput(bool),
ToggleTextInputIcon(bool),
@ -265,6 +270,15 @@ impl<'a> Step {
*width = new_width;
}
}
StepMessage::ImageUseNearestToggled(use_nearest) => {
if let Step::Image { filter_method, .. } = self {
*filter_method = if use_nearest {
image::FilterMethod::Nearest
} else {
image::FilterMethod::Linear
};
}
}
StepMessage::InputChanged(new_value) => {
if let Step::TextInput { value, .. } = self {
*value = new_value;
@ -330,7 +344,10 @@ impl<'a> Step {
Step::Toggler { can_continue } => Self::toggler(*can_continue),
Step::Slider { value } => Self::slider(*value),
Step::Text { size, color } => Self::text(*size, *color),
Step::Image { width } => Self::image(*width),
Step::Image {
width,
filter_method,
} => Self::image(*width, *filter_method),
Step::RowsAndColumns { layout, spacing } => {
Self::rows_and_columns(*layout, *spacing)
}
@ -525,16 +542,25 @@ impl<'a> Step {
)
}
fn image(width: u16) -> Column<'a, StepMessage> {
fn image(
width: u16,
filter_method: image::FilterMethod,
) -> Column<'a, StepMessage> {
Self::container("Image")
.push("An image that tries to keep its aspect ratio.")
.push(ferris(width))
.push(ferris(width, filter_method))
.push(slider(100..=500, width, StepMessage::ImageWidthChanged))
.push(
text(format!("Width: {width} px"))
.width(Length::Fill)
.horizontal_alignment(alignment::Horizontal::Center),
)
.push(checkbox(
"Use nearest interpolation",
filter_method == image::FilterMethod::Nearest,
StepMessage::ImageUseNearestToggled,
))
.align_items(Alignment::Center)
}
fn scrollable() -> Column<'a, StepMessage> {
@ -555,7 +581,7 @@ impl<'a> Step {
.horizontal_alignment(alignment::Horizontal::Center),
)
.push(vertical_space(4096))
.push(ferris(300))
.push(ferris(300, image::FilterMethod::Linear))
.push(
text("You made it!")
.width(Length::Fill)
@ -646,7 +672,10 @@ impl<'a> Step {
}
}
fn ferris<'a>(width: u16) -> Container<'a, StepMessage> {
fn ferris<'a>(
width: u16,
filter_method: image::FilterMethod,
) -> Container<'a, StepMessage> {
container(
// This should go away once we unify resource loading on native
// platforms
@ -655,6 +684,7 @@ fn ferris<'a>(width: u16) -> Container<'a, StepMessage> {
} else {
image(format!("{}/images/ferris.png", env!("CARGO_MANIFEST_DIR")))
}
.filter_method(filter_method)
.width(width),
)
.width(Length::Fill)