diff --git a/voxygen/src/ui/ice/renderer/style/checkbox.rs b/voxygen/src/ui/ice/renderer/style/checkbox.rs new file mode 100644 index 0000000000..a113c0911b --- /dev/null +++ b/voxygen/src/ui/ice/renderer/style/checkbox.rs @@ -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, + check: Option, +} + +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 { self.check } + + pub fn bg_checked(&self) -> Option { + self.background.as_ref().map(|b| b.checked) + } + + pub fn bg_hover(&self) -> Option { self.background.as_ref().map(|b| b.hover) } + + pub fn bg_hover_checked(&self) -> Option { + self.background.as_ref().map(|b| b.hover_checked) + } + + pub fn bg_default(&self) -> Option { + self.background.as_ref().map(|b| b.default) + } +} + +impl Default for Style { + fn default() -> Self { + Self { + background: None, + check: None, + } + } +} diff --git a/voxygen/src/ui/ice/renderer/style/mod.rs b/voxygen/src/ui/ice/renderer/style/mod.rs index 04297da836..8dc82f0179 100644 --- a/voxygen/src/ui/ice/renderer/style/mod.rs +++ b/voxygen/src/ui/ice/renderer/style/mod.rs @@ -1,4 +1,5 @@ pub mod button; +pub mod checkbox; pub mod container; pub mod scrollable; pub mod slider; diff --git a/voxygen/src/ui/ice/renderer/widget/checkbox.rs b/voxygen/src/ui/ice/renderer/widget/checkbox.rs new file mode 100644 index 0000000000..8641ec7144 --- /dev/null +++ b/voxygen/src/ui/ice/renderer/widget/checkbox.rs @@ -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() + }, + ) + } +} diff --git a/voxygen/src/ui/ice/renderer/widget/mod.rs b/voxygen/src/ui/ice/renderer/widget/mod.rs index 8f9c84758e..c58b3aa152 100644 --- a/voxygen/src/ui/ice/renderer/widget/mod.rs +++ b/voxygen/src/ui/ice/renderer/widget/mod.rs @@ -1,6 +1,7 @@ mod aspect_ratio_container; mod background_container; mod button; +mod checkbox; mod column; mod compound_graphic; mod container;