2019-02-10 14:30:48 +00:00
|
|
|
#![allow(clippy::new_without_default_derive)]
|
|
|
|
|
2019-01-10 09:55:38 +00:00
|
|
|
use crate::{
|
2020-04-17 21:44:14 +00:00
|
|
|
layout::{Direction, GuiResponse},
|
2020-04-17 13:29:48 +00:00
|
|
|
*,
|
2019-01-10 09:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// Anything implementing Widget can be added to a Region with Region::add
|
|
|
|
pub trait Widget {
|
|
|
|
fn add_to(self, region: &mut Region) -> GuiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub struct Label {
|
|
|
|
text: String,
|
2020-04-12 10:07:51 +00:00
|
|
|
text_style: TextStyle, // TODO: Option<TextStyle>, where None means "use the default for the region"
|
2019-01-16 15:28:43 +00:00
|
|
|
text_color: Option<Color>,
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Label {
|
|
|
|
pub fn new<S: Into<String>>(text: S) -> Self {
|
2019-01-12 23:55:56 +00:00
|
|
|
Label {
|
|
|
|
text: text.into(),
|
|
|
|
text_style: TextStyle::Body,
|
2019-01-16 15:28:43 +00:00
|
|
|
text_color: None,
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn text_style(mut self, text_style: TextStyle) -> Self {
|
|
|
|
self.text_style = text_style;
|
|
|
|
self
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
2019-01-16 15:28:43 +00:00
|
|
|
|
|
|
|
pub fn text_color(mut self, text_color: Color) -> Self {
|
|
|
|
self.text_color = Some(text_color);
|
|
|
|
self
|
|
|
|
}
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 07:48:32 +00:00
|
|
|
/// Usage: label!("Foo: {}", bar)
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! label {
|
|
|
|
($fmt:expr) => (Label::new($fmt));
|
|
|
|
($fmt:expr, $($arg:tt)*) => (Label::new(format!($fmt, $($arg)*)));
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Widget for Label {
|
|
|
|
fn add_to(self, region: &mut Region) -> GuiResponse {
|
2019-01-12 23:55:56 +00:00
|
|
|
let font = ®ion.fonts()[self.text_style];
|
|
|
|
let (text, text_size) = font.layout_multiline(&self.text, region.width());
|
2019-01-14 13:26:02 +00:00
|
|
|
let interact = region.reserve_space(text_size, None);
|
2019-01-16 15:28:43 +00:00
|
|
|
region.add_text(interact.rect.min(), self.text_style, text, self.text_color);
|
2019-01-10 09:55:38 +00:00
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub struct Button {
|
|
|
|
text: String,
|
2019-01-16 15:28:43 +00:00
|
|
|
text_color: Option<Color>,
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Button {
|
|
|
|
pub fn new<S: Into<String>>(text: S) -> Self {
|
2019-01-16 15:28:43 +00:00
|
|
|
Button {
|
|
|
|
text: text.into(),
|
|
|
|
text_color: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn text_color(mut self, text_color: Color) -> Self {
|
|
|
|
self.text_color = Some(text_color);
|
|
|
|
self
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Widget for Button {
|
|
|
|
fn add_to(self, region: &mut Region) -> GuiResponse {
|
2020-04-19 09:13:24 +00:00
|
|
|
let id = region.make_position_id();
|
2019-01-12 23:55:56 +00:00
|
|
|
let text_style = TextStyle::Button;
|
|
|
|
let font = ®ion.fonts()[text_style];
|
|
|
|
let (text, text_size) = font.layout_multiline(&self.text, region.width());
|
2019-03-11 14:39:54 +00:00
|
|
|
let padding = region.style().button_padding;
|
2019-01-19 16:09:00 +00:00
|
|
|
let mut size = text_size + 2.0 * padding;
|
2019-03-11 14:39:54 +00:00
|
|
|
size.y = size.y.max(region.style().clickable_diameter);
|
2019-01-19 16:09:00 +00:00
|
|
|
let interact = region.reserve_space(size, Some(id));
|
|
|
|
let text_cursor = interact.rect.left_center() + vec2(padding.x, -0.5 * text_size.y);
|
2019-03-11 14:59:49 +00:00
|
|
|
region.add_paint_cmd(PaintCmd::Rect {
|
|
|
|
corner_radius: 10.0,
|
|
|
|
fill_color: Some(region.style().interact_fill_color(&interact)),
|
|
|
|
outline: None,
|
|
|
|
rect: interact.rect,
|
|
|
|
});
|
2019-01-16 15:28:43 +00:00
|
|
|
region.add_text(text_cursor, text_style, text, self.text_color);
|
2019-01-10 09:55:38 +00:00
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Checkbox<'a> {
|
|
|
|
checked: &'a mut bool,
|
|
|
|
text: String,
|
2019-01-16 15:28:43 +00:00
|
|
|
text_color: Option<Color>,
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Checkbox<'a> {
|
|
|
|
pub fn new<S: Into<String>>(checked: &'a mut bool, text: S) -> Self {
|
|
|
|
Checkbox {
|
|
|
|
checked,
|
|
|
|
text: text.into(),
|
2019-01-16 15:28:43 +00:00
|
|
|
text_color: None,
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-16 15:28:43 +00:00
|
|
|
|
|
|
|
pub fn text_color(mut self, text_color: Color) -> Self {
|
|
|
|
self.text_color = Some(text_color);
|
|
|
|
self
|
|
|
|
}
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Checkbox<'a> {
|
|
|
|
fn add_to(self, region: &mut Region) -> GuiResponse {
|
2020-04-19 09:13:24 +00:00
|
|
|
let id = region.make_position_id();
|
2019-01-12 23:55:56 +00:00
|
|
|
let text_style = TextStyle::Button;
|
|
|
|
let font = ®ion.fonts()[text_style];
|
|
|
|
let (text, text_size) = font.layout_multiline(&self.text, region.width());
|
2019-01-14 13:26:02 +00:00
|
|
|
let interact = region.reserve_space(
|
2019-03-11 14:39:54 +00:00
|
|
|
region.style().button_padding
|
|
|
|
+ vec2(region.style().start_icon_width, 0.0)
|
2019-01-10 09:55:38 +00:00
|
|
|
+ text_size
|
2019-03-11 14:39:54 +00:00
|
|
|
+ region.style().button_padding,
|
2019-01-10 09:55:38 +00:00
|
|
|
Some(id),
|
|
|
|
);
|
2019-01-14 13:54:06 +00:00
|
|
|
let text_cursor = interact.rect.min()
|
2019-03-11 14:39:54 +00:00
|
|
|
+ region.style().button_padding
|
|
|
|
+ vec2(region.style().start_icon_width, 0.0);
|
2019-01-10 09:55:38 +00:00
|
|
|
if interact.clicked {
|
|
|
|
*self.checked = !*self.checked;
|
|
|
|
}
|
2019-03-11 14:59:49 +00:00
|
|
|
let (small_icon_rect, big_icon_rect) = region.style().icon_rectangles(&interact.rect);
|
|
|
|
region.add_paint_cmd(PaintCmd::Rect {
|
|
|
|
corner_radius: 3.0,
|
|
|
|
fill_color: Some(region.style().interact_fill_color(&interact)),
|
|
|
|
outline: None,
|
|
|
|
rect: big_icon_rect,
|
2019-01-10 09:55:38 +00:00
|
|
|
});
|
2019-03-11 14:59:49 +00:00
|
|
|
|
|
|
|
let stroke_color = region.style().interact_stroke_color(&interact);
|
|
|
|
|
|
|
|
if *self.checked {
|
|
|
|
region.add_paint_cmd(PaintCmd::Line {
|
|
|
|
points: vec![
|
2020-04-18 23:05:49 +00:00
|
|
|
pos2(small_icon_rect.min().x, small_icon_rect.center().y),
|
|
|
|
pos2(small_icon_rect.center().x, small_icon_rect.max().y),
|
|
|
|
pos2(small_icon_rect.max().x, small_icon_rect.min().y),
|
2019-03-11 14:59:49 +00:00
|
|
|
],
|
|
|
|
color: stroke_color,
|
|
|
|
width: region.style().line_width,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:28:43 +00:00
|
|
|
region.add_text(text_cursor, text_style, text, self.text_color);
|
2019-01-10 09:55:38 +00:00
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RadioButton {
|
|
|
|
checked: bool,
|
|
|
|
text: String,
|
2019-01-16 15:28:43 +00:00
|
|
|
text_color: Option<Color>,
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RadioButton {
|
|
|
|
pub fn new<S: Into<String>>(checked: bool, text: S) -> Self {
|
|
|
|
RadioButton {
|
|
|
|
checked,
|
|
|
|
text: text.into(),
|
2019-01-16 15:28:43 +00:00
|
|
|
text_color: None,
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-16 15:28:43 +00:00
|
|
|
|
|
|
|
pub fn text_color(mut self, text_color: Color) -> Self {
|
|
|
|
self.text_color = Some(text_color);
|
|
|
|
self
|
|
|
|
}
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn radio<S: Into<String>>(checked: bool, text: S) -> RadioButton {
|
|
|
|
RadioButton::new(checked, text)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Widget for RadioButton {
|
|
|
|
fn add_to(self, region: &mut Region) -> GuiResponse {
|
2020-04-19 09:13:24 +00:00
|
|
|
let id = region.make_position_id();
|
2019-01-12 23:55:56 +00:00
|
|
|
let text_style = TextStyle::Button;
|
|
|
|
let font = ®ion.fonts()[text_style];
|
|
|
|
let (text, text_size) = font.layout_multiline(&self.text, region.width());
|
2019-01-14 13:26:02 +00:00
|
|
|
let interact = region.reserve_space(
|
2019-03-11 14:39:54 +00:00
|
|
|
region.style().button_padding
|
|
|
|
+ vec2(region.style().start_icon_width, 0.0)
|
2019-01-10 09:55:38 +00:00
|
|
|
+ text_size
|
2019-03-11 14:39:54 +00:00
|
|
|
+ region.style().button_padding,
|
2019-01-10 09:55:38 +00:00
|
|
|
Some(id),
|
|
|
|
);
|
2019-01-14 13:54:06 +00:00
|
|
|
let text_cursor = interact.rect.min()
|
2019-03-11 14:39:54 +00:00
|
|
|
+ region.style().button_padding
|
|
|
|
+ vec2(region.style().start_icon_width, 0.0);
|
2019-03-11 14:59:49 +00:00
|
|
|
|
|
|
|
let fill_color = region.style().interact_fill_color(&interact);
|
|
|
|
let stroke_color = region.style().interact_stroke_color(&interact);
|
|
|
|
|
|
|
|
let (small_icon_rect, big_icon_rect) = region.style().icon_rectangles(&interact.rect);
|
|
|
|
|
|
|
|
region.add_paint_cmd(PaintCmd::Circle {
|
|
|
|
center: big_icon_rect.center(),
|
|
|
|
fill_color: Some(fill_color),
|
|
|
|
outline: None,
|
2020-04-15 09:47:03 +00:00
|
|
|
radius: big_icon_rect.size().x / 2.0,
|
2019-01-10 09:55:38 +00:00
|
|
|
});
|
2019-03-11 14:59:49 +00:00
|
|
|
|
|
|
|
if self.checked {
|
|
|
|
region.add_paint_cmd(PaintCmd::Circle {
|
|
|
|
center: small_icon_rect.center(),
|
|
|
|
fill_color: Some(stroke_color),
|
|
|
|
outline: None,
|
2020-04-15 09:47:03 +00:00
|
|
|
radius: small_icon_rect.size().x / 2.0,
|
2019-03-11 14:59:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-16 15:28:43 +00:00
|
|
|
region.add_text(text_cursor, text_style, text, self.text_color);
|
2019-01-10 09:55:38 +00:00
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-04-19 09:13:24 +00:00
|
|
|
/// Combined into one function (rather than two) to make it easier
|
|
|
|
/// for the borrow checker.
|
|
|
|
type SliderGetSet<'a> = Box<dyn 'a + FnMut(Option<f32>) -> f32>;
|
|
|
|
|
2019-01-10 09:55:38 +00:00
|
|
|
pub struct Slider<'a> {
|
2020-04-19 09:13:24 +00:00
|
|
|
get_set_value: SliderGetSet<'a>,
|
2019-01-10 09:55:38 +00:00
|
|
|
min: f32,
|
|
|
|
max: f32,
|
|
|
|
text: Option<String>,
|
2019-02-10 15:10:08 +00:00
|
|
|
precision: usize,
|
2019-01-16 15:28:43 +00:00
|
|
|
text_color: Option<Color>,
|
2019-01-10 09:55:38 +00:00
|
|
|
text_on_top: Option<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Slider<'a> {
|
2020-04-19 09:13:24 +00:00
|
|
|
fn from_get_set(get_set_value: impl 'a + FnMut(Option<f32>) -> f32) -> Self {
|
|
|
|
Slider {
|
|
|
|
get_set_value: Box::new(get_set_value),
|
|
|
|
min: std::f32::NAN,
|
|
|
|
max: std::f32::NAN,
|
|
|
|
text: None,
|
|
|
|
precision: 3,
|
|
|
|
text_on_top: None,
|
|
|
|
text_color: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 15:10:08 +00:00
|
|
|
pub fn f32(value: &'a mut f32, min: f32, max: f32) -> Self {
|
2019-01-10 09:55:38 +00:00
|
|
|
Slider {
|
2020-04-19 09:13:24 +00:00
|
|
|
min,
|
|
|
|
max,
|
|
|
|
precision: 3,
|
|
|
|
..Self::from_get_set(move |v: Option<f32>| {
|
2019-02-10 15:10:08 +00:00
|
|
|
if let Some(v) = v {
|
|
|
|
*value = v
|
|
|
|
}
|
|
|
|
*value
|
2020-04-19 09:13:24 +00:00
|
|
|
})
|
2019-02-10 15:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn i32(value: &'a mut i32, min: i32, max: i32) -> Self {
|
|
|
|
Slider {
|
2020-04-19 09:13:24 +00:00
|
|
|
min: min as f32,
|
|
|
|
max: max as f32,
|
|
|
|
precision: 0,
|
|
|
|
..Self::from_get_set(move |v: Option<f32>| {
|
2019-02-10 15:10:08 +00:00
|
|
|
if let Some(v) = v {
|
|
|
|
*value = v.round() as i32
|
|
|
|
}
|
|
|
|
*value as f32
|
2020-04-19 09:13:24 +00:00
|
|
|
})
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 12:31:55 +00:00
|
|
|
pub fn usize(value: &'a mut usize, min: usize, max: usize) -> Self {
|
|
|
|
Slider {
|
2020-04-19 09:13:24 +00:00
|
|
|
min: min as f32,
|
|
|
|
max: max as f32,
|
|
|
|
precision: 0,
|
|
|
|
..Self::from_get_set(move |v: Option<f32>| {
|
2019-03-11 12:31:55 +00:00
|
|
|
if let Some(v) = v {
|
|
|
|
*value = v.round() as usize
|
|
|
|
}
|
|
|
|
*value as f32
|
2020-04-19 09:13:24 +00:00
|
|
|
})
|
2019-03-11 12:31:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 09:55:38 +00:00
|
|
|
pub fn text<S: Into<String>>(mut self, text: S) -> Self {
|
|
|
|
self.text = Some(text.into());
|
|
|
|
self
|
|
|
|
}
|
2019-01-16 15:28:43 +00:00
|
|
|
|
|
|
|
pub fn text_color(mut self, text_color: Color) -> Self {
|
|
|
|
self.text_color = Some(text_color);
|
|
|
|
self
|
|
|
|
}
|
2019-04-25 16:07:36 +00:00
|
|
|
|
|
|
|
pub fn precision(mut self, precision: usize) -> Self {
|
|
|
|
self.precision = precision;
|
|
|
|
self
|
|
|
|
}
|
2020-04-12 10:07:51 +00:00
|
|
|
|
|
|
|
fn get_value_f32(&mut self) -> f32 {
|
|
|
|
(self.get_set_value)(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_value_f32(&mut self, mut value: f32) {
|
|
|
|
if self.precision == 0 {
|
|
|
|
value = value.round();
|
|
|
|
}
|
|
|
|
(self.get_set_value)(Some(value));
|
|
|
|
}
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Slider<'a> {
|
2019-02-10 15:10:08 +00:00
|
|
|
fn add_to(mut self, region: &mut Region) -> GuiResponse {
|
2019-01-12 23:55:56 +00:00
|
|
|
let text_style = TextStyle::Button;
|
|
|
|
let font = ®ion.fonts()[text_style];
|
|
|
|
|
2019-01-10 09:55:38 +00:00
|
|
|
if let Some(text) = &self.text {
|
|
|
|
let text_on_top = self.text_on_top.unwrap_or_default();
|
2019-01-16 15:28:43 +00:00
|
|
|
let text_color = self.text_color;
|
2020-04-12 10:07:51 +00:00
|
|
|
let value = (self.get_set_value)(None);
|
|
|
|
let full_text = format!("{}: {:.*}", text, self.precision, value);
|
2020-04-19 09:13:24 +00:00
|
|
|
|
|
|
|
let naked = Slider { text: None, ..self };
|
2019-01-10 09:55:38 +00:00
|
|
|
|
|
|
|
if text_on_top {
|
2019-01-12 23:55:56 +00:00
|
|
|
let (text, text_size) = font.layout_multiline(&full_text, region.width());
|
2019-01-14 13:54:06 +00:00
|
|
|
let pos = region.reserve_space_without_padding(text_size);
|
2019-01-16 15:28:43 +00:00
|
|
|
region.add_text(pos, text_style, text, text_color);
|
2019-01-10 09:55:38 +00:00
|
|
|
naked.add_to(region)
|
|
|
|
} else {
|
|
|
|
region.columns(2, |columns| {
|
2019-01-14 13:26:02 +00:00
|
|
|
let response = naked.add_to(&mut columns[0]);
|
2019-01-14 13:54:06 +00:00
|
|
|
|
2019-01-14 13:26:02 +00:00
|
|
|
columns[1].available_space.y = response.rect.size().y;
|
2019-01-14 13:54:06 +00:00
|
|
|
columns[1].horizontal(Align::Center, |region| {
|
2019-01-21 07:48:32 +00:00
|
|
|
region.add(Label::new(full_text));
|
2019-01-14 13:54:06 +00:00
|
|
|
});
|
|
|
|
|
2019-01-14 13:26:02 +00:00
|
|
|
response
|
2019-01-10 09:55:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-11 14:39:54 +00:00
|
|
|
let height = font.line_spacing().max(region.style().clickable_diameter);
|
2019-01-14 13:26:02 +00:00
|
|
|
|
2019-01-10 09:55:38 +00:00
|
|
|
let min = self.min;
|
|
|
|
let max = self.max;
|
|
|
|
debug_assert!(min <= max);
|
2020-04-19 09:13:24 +00:00
|
|
|
let id = region.make_position_id();
|
2019-01-14 13:26:02 +00:00
|
|
|
let interact = region.reserve_space(
|
2019-01-10 09:55:38 +00:00
|
|
|
Vec2 {
|
|
|
|
x: region.available_space.x,
|
2019-01-14 13:26:02 +00:00
|
|
|
y: height,
|
2019-01-10 09:55:38 +00:00
|
|
|
},
|
2020-04-19 09:13:24 +00:00
|
|
|
Some(id),
|
2019-01-10 09:55:38 +00:00
|
|
|
);
|
|
|
|
|
2019-02-10 14:30:48 +00:00
|
|
|
if let Some(mouse_pos) = region.input().mouse_pos {
|
|
|
|
if interact.active {
|
2020-04-12 10:07:51 +00:00
|
|
|
self.set_value_f32(remap_clamp(
|
2019-02-10 14:30:48 +00:00
|
|
|
mouse_pos.x,
|
|
|
|
interact.rect.min().x,
|
|
|
|
interact.rect.max().x,
|
|
|
|
min,
|
|
|
|
max,
|
2020-04-12 10:07:51 +00:00
|
|
|
));
|
2019-02-10 14:30:48 +00:00
|
|
|
}
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 14:59:49 +00:00
|
|
|
// Paint it:
|
|
|
|
{
|
2020-04-12 10:07:51 +00:00
|
|
|
let value = self.get_value_f32();
|
2019-03-11 14:59:49 +00:00
|
|
|
|
|
|
|
let rect = interact.rect;
|
|
|
|
let thickness = rect.size().y;
|
2020-04-15 09:47:03 +00:00
|
|
|
let thin_size = vec2(rect.size().x, thickness / 5.0);
|
2019-03-11 14:59:49 +00:00
|
|
|
let thin_rect = Rect::from_center_size(rect.center(), thin_size);
|
|
|
|
let marker_center_x = remap_clamp(value, min, max, rect.min().x, rect.max().x);
|
|
|
|
|
|
|
|
region.add_paint_cmd(PaintCmd::Rect {
|
|
|
|
corner_radius: 4.0,
|
|
|
|
fill_color: Some(region.style().background_fill_color()),
|
2020-04-19 09:11:41 +00:00
|
|
|
outline: Some(Outline::new(1.0, color::gray(200, 255))), // TODO
|
2019-03-11 14:59:49 +00:00
|
|
|
rect: thin_rect,
|
|
|
|
});
|
|
|
|
|
|
|
|
region.add_paint_cmd(PaintCmd::Circle {
|
2020-04-18 23:05:49 +00:00
|
|
|
center: pos2(marker_center_x, thin_rect.center().y),
|
2019-03-11 14:59:49 +00:00
|
|
|
fill_color: Some(region.style().interact_fill_color(&interact)),
|
2020-04-19 09:11:41 +00:00
|
|
|
outline: Some(Outline::new(
|
|
|
|
1.5,
|
|
|
|
region.style().interact_stroke_color(&interact),
|
|
|
|
)),
|
2019-03-11 14:59:49 +00:00
|
|
|
radius: thickness / 3.0,
|
|
|
|
});
|
|
|
|
}
|
2019-01-10 09:55:38 +00:00
|
|
|
|
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2019-01-13 18:15:11 +00:00
|
|
|
|
|
|
|
pub struct Separator {
|
|
|
|
line_width: f32,
|
|
|
|
width: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Separator {
|
|
|
|
pub fn new() -> Separator {
|
|
|
|
Separator {
|
|
|
|
line_width: 2.0,
|
|
|
|
width: 6.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn line_width(&mut self, line_width: f32) -> &mut Self {
|
|
|
|
self.line_width = line_width;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn width(&mut self, width: f32) -> &mut Self {
|
|
|
|
self.width = width;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Widget for Separator {
|
|
|
|
fn add_to(self, region: &mut Region) -> GuiResponse {
|
|
|
|
let available_space = region.available_space;
|
|
|
|
let (points, interact) = match region.direction() {
|
|
|
|
Direction::Horizontal => {
|
2019-01-14 13:54:06 +00:00
|
|
|
let interact = region.reserve_space(vec2(self.width, available_space.y), None);
|
2019-01-13 18:15:11 +00:00
|
|
|
(
|
|
|
|
vec![
|
2020-04-18 23:05:49 +00:00
|
|
|
pos2(interact.rect.center().x, interact.rect.min().y),
|
|
|
|
pos2(interact.rect.center().x, interact.rect.max().y),
|
2019-01-13 18:15:11 +00:00
|
|
|
],
|
|
|
|
interact,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Direction::Vertical => {
|
2019-01-14 13:54:06 +00:00
|
|
|
let interact = region.reserve_space(vec2(available_space.x, self.width), None);
|
2019-01-13 18:15:11 +00:00
|
|
|
(
|
|
|
|
vec![
|
2020-04-18 23:05:49 +00:00
|
|
|
pos2(interact.rect.min().x, interact.rect.center().y),
|
|
|
|
pos2(interact.rect.max().x, interact.rect.center().y),
|
2019-01-13 18:15:11 +00:00
|
|
|
],
|
|
|
|
interact,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
2019-03-11 14:59:49 +00:00
|
|
|
region.add_paint_cmd(PaintCmd::Line {
|
2019-01-13 18:15:11 +00:00
|
|
|
points,
|
2019-04-25 16:07:36 +00:00
|
|
|
color: color::WHITE,
|
2019-01-13 18:15:11 +00:00
|
|
|
width: self.line_width,
|
2019-03-11 14:59:49 +00:00
|
|
|
});
|
2019-01-13 18:15:11 +00:00
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|