mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Add checkbox rendering logic for iced
This commit is contained in:
parent
37a6ea7abc
commit
5323bb3ce8
74
voxygen/src/ui/ice/renderer/style/checkbox.rs
Normal file
74
voxygen/src/ui/ice/renderer/style/checkbox.rs
Normal file
@ -0,0 +1,74 @@
|
||||
use super::super::super::widget::image;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Background {
|
||||
default: image::Handle,
|
||||
hover: image::Handle,
|
||||
press: image::Handle,
|
||||
}
|
||||
|
||||
impl Background {
|
||||
fn new(image: image::Handle) -> Self {
|
||||
Self {
|
||||
default: image,
|
||||
hover: image,
|
||||
press: image,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Style {
|
||||
background: Option<Background>,
|
||||
checked: Option<image::Handle>,
|
||||
}
|
||||
|
||||
impl Style {
|
||||
pub fn new(image: image::Handle, checked: image::Handle) -> Self {
|
||||
Self {
|
||||
background: Some(Background::new(image)),
|
||||
checked: Some(checked),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn 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 press_image(mut self, image: image::Handle) -> Self {
|
||||
self.background = Some(match self.background {
|
||||
Some(mut background) => {
|
||||
background.press = image;
|
||||
background
|
||||
},
|
||||
None => Background::new(image),
|
||||
});
|
||||
self
|
||||
}
|
||||
|
||||
pub fn pressed(&self) -> Option<image::Handle> { self.background.as_ref().map(|b| b.press) }
|
||||
|
||||
pub fn checked(&self) -> Option<image::Handle> { self.checked }
|
||||
|
||||
pub fn hovered(&self) -> Option<image::Handle> { self.background.as_ref().map(|b| b.hover) }
|
||||
|
||||
pub fn background(&self) -> Option<image::Handle> {
|
||||
self.background.as_ref().map(|b| b.default)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Style {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
background: None,
|
||||
checked: None,
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
pub mod button;
|
||||
pub mod checkbox;
|
||||
pub mod container;
|
||||
pub mod scrollable;
|
||||
pub mod slider;
|
||||
|
64
voxygen/src/ui/ice/renderer/widget/checkbox.rs
Normal file
64
voxygen/src/ui/ice/renderer/widget/checkbox.rs
Normal file
@ -0,0 +1,64 @@
|
||||
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, _) => style.pressed(),
|
||||
(_, true) => style.hovered(),
|
||||
_ => style.background(),
|
||||
};
|
||||
|
||||
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
|
||||
.pressed()
|
||||
.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()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
mod aspect_ratio_container;
|
||||
mod background_container;
|
||||
mod button;
|
||||
mod checkbox;
|
||||
mod column;
|
||||
mod compound_graphic;
|
||||
mod container;
|
||||
|
Loading…
Reference in New Issue
Block a user