Merge branch 'capucho/iced-checkbox' into 'master'

Add checkbox rendering logic for iced

See merge request veloren/veloren!2827
This commit is contained in:
Imbris 2021-09-15 17:30:57 +00:00
commit a3ac3b51ce
4 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,93 @@
use super::super::super::widget::image;
#[derive(Clone, Copy)]
struct Background {
default: image::Handle,
hover: image::Handle,
checked: image::Handle,
hover_checked: image::Handle,
}
impl Background {
fn new(image: image::Handle) -> Self {
Self {
default: image,
hover: image,
checked: image,
hover_checked: image,
}
}
}
#[derive(Clone, Copy)]
pub struct Style {
background: Option<Background>,
check: Option<image::Handle>,
}
impl Style {
pub fn new(image: image::Handle, check: image::Handle) -> Self {
Self {
background: Some(Background::new(image)),
check: Some(check),
}
}
pub fn bg_hover_image(mut self, image: image::Handle) -> Self {
self.background = Some(match self.background {
Some(mut background) => {
background.hover = image;
background
},
None => Background::new(image),
});
self
}
pub fn bg_checked_image(mut self, image: image::Handle) -> Self {
self.background = Some(match self.background {
Some(mut background) => {
background.checked = image;
background
},
None => Background::new(image),
});
self
}
pub fn bg_hover_checked_image(mut self, image: image::Handle) -> Self {
self.background = Some(match self.background {
Some(mut background) => {
background.hover_checked = image;
background
},
None => Background::new(image),
});
self
}
pub fn check(&self) -> Option<image::Handle> { self.check }
pub fn bg_checked(&self) -> Option<image::Handle> {
self.background.as_ref().map(|b| b.checked)
}
pub fn bg_hover(&self) -> Option<image::Handle> { self.background.as_ref().map(|b| b.hover) }
pub fn bg_hover_checked(&self) -> Option<image::Handle> {
self.background.as_ref().map(|b| b.hover_checked)
}
pub fn bg_default(&self) -> Option<image::Handle> {
self.background.as_ref().map(|b| b.default)
}
}
impl Default for Style {
fn default() -> Self {
Self {
background: None,
check: None,
}
}
}

View File

@ -1,4 +1,5 @@
pub mod button;
pub mod checkbox;
pub mod container;
pub mod scrollable;
pub mod slider;

View File

@ -0,0 +1,65 @@
use super::super::{super::Rotation, style, IcedRenderer, Primitive};
use iced::{checkbox, mouse, Rectangle};
impl checkbox::Renderer for IcedRenderer {
// TODO: what if this gets large enough to not be copied around?
type Style = style::checkbox::Style;
const DEFAULT_SIZE: u16 = 20;
const DEFAULT_SPACING: u16 = 15;
fn draw(
&mut self,
bounds: Rectangle,
is_checked: bool,
is_mouse_over: bool,
(label, _): Self::Output,
style: &Self::Style,
) -> Self::Output {
let default_rect = || Primitive::Rectangle {
bounds,
linear_color: vek::Rgba::broadcast(1.0),
};
let background_image = match (is_checked, is_mouse_over) {
(true, true) => style.bg_hover_checked(),
(true, false) => style.bg_checked(),
(false, true) => style.bg_hover(),
(false, false) => style.bg_default(),
};
let background = background_image
.map(|image| Primitive::Image {
handle: (image, Rotation::None),
bounds,
color: vek::Rgba::broadcast(255),
source_rect: None,
})
.unwrap_or_else(default_rect);
(
Primitive::Group {
primitives: if is_checked {
let check = style
.check()
.map(|image| Primitive::Image {
handle: (image, Rotation::None),
bounds,
color: vek::Rgba::broadcast(255),
source_rect: None,
})
.unwrap_or_else(default_rect);
vec![background, check, label]
} else {
vec![background, label]
},
},
if is_mouse_over {
mouse::Interaction::Pointer
} else {
mouse::Interaction::default()
},
)
}
}

View File

@ -1,6 +1,7 @@
mod aspect_ratio_container;
mod background_container;
mod button;
mod checkbox;
mod column;
mod compound_graphic;
mod container;