2019-01-10 09:55:38 +00:00
|
|
|
use crate::{
|
2019-01-12 23:55:56 +00:00
|
|
|
fonts::TextStyle,
|
2019-01-14 13:54:06 +00:00
|
|
|
layout::{make_id, Align, Direction, GuiResponse, Id, Region},
|
2019-01-10 09:55:38 +00:00
|
|
|
math::{remap_clamp, vec2, Vec2},
|
2019-01-13 18:15:11 +00:00
|
|
|
types::{Color, GuiCmd, PaintCmd},
|
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,
|
2019-01-12 23:55:56 +00:00
|
|
|
text_style: TextStyle,
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn text_style(mut self, text_style: TextStyle) -> Self {
|
|
|
|
self.text_style = text_style;
|
|
|
|
self
|
2019-01-10 09:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn label<S: Into<String>>(text: S) -> Label {
|
|
|
|
Label::new(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-14 13:54:06 +00:00
|
|
|
region.add_text(interact.rect.min(), self.text_style, text);
|
2019-01-10 09:55:38 +00:00
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub struct Button {
|
|
|
|
text: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Button {
|
|
|
|
pub fn new<S: Into<String>>(text: S) -> Self {
|
|
|
|
Button { text: text.into() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Widget for Button {
|
|
|
|
fn add_to(self, region: &mut Region) -> GuiResponse {
|
|
|
|
let id = region.make_child_id(&self.text);
|
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 =
|
2019-01-10 09:55:38 +00:00
|
|
|
region.reserve_space(text_size + 2.0 * region.options().button_padding, Some(id));
|
2019-01-14 13:54:06 +00:00
|
|
|
let text_cursor = interact.rect.min() + region.options().button_padding;
|
2019-01-14 13:26:02 +00:00
|
|
|
region.add_graphic(GuiCmd::Button { interact });
|
2019-01-12 23:55:56 +00:00
|
|
|
region.add_text(text_cursor, text_style, text);
|
2019-01-10 09:55:38 +00:00
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Checkbox<'a> {
|
|
|
|
checked: &'a mut bool,
|
|
|
|
text: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Checkbox<'a> {
|
|
|
|
pub fn new<S: Into<String>>(checked: &'a mut bool, text: S) -> Self {
|
|
|
|
Checkbox {
|
|
|
|
checked,
|
|
|
|
text: text.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Checkbox<'a> {
|
|
|
|
fn add_to(self, region: &mut Region) -> GuiResponse {
|
|
|
|
let id = region.make_child_id(&self.text);
|
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-01-10 09:55:38 +00:00
|
|
|
region.options().button_padding
|
|
|
|
+ vec2(region.options().start_icon_width, 0.0)
|
|
|
|
+ text_size
|
|
|
|
+ region.options().button_padding,
|
|
|
|
Some(id),
|
|
|
|
);
|
2019-01-14 13:54:06 +00:00
|
|
|
let text_cursor = interact.rect.min()
|
|
|
|
+ region.options().button_padding
|
|
|
|
+ vec2(region.options().start_icon_width, 0.0);
|
2019-01-10 09:55:38 +00:00
|
|
|
if interact.clicked {
|
|
|
|
*self.checked = !*self.checked;
|
|
|
|
}
|
|
|
|
region.add_graphic(GuiCmd::Checkbox {
|
|
|
|
checked: *self.checked,
|
|
|
|
interact,
|
|
|
|
});
|
2019-01-12 23:55:56 +00:00
|
|
|
region.add_text(text_cursor, text_style, text);
|
2019-01-10 09:55:38 +00:00
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RadioButton {
|
|
|
|
checked: bool,
|
|
|
|
text: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RadioButton {
|
|
|
|
pub fn new<S: Into<String>>(checked: bool, text: S) -> Self {
|
|
|
|
RadioButton {
|
|
|
|
checked,
|
|
|
|
text: text.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let id = region.make_child_id(&self.text);
|
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-01-10 09:55:38 +00:00
|
|
|
region.options().button_padding
|
|
|
|
+ vec2(region.options().start_icon_width, 0.0)
|
|
|
|
+ text_size
|
|
|
|
+ region.options().button_padding,
|
|
|
|
Some(id),
|
|
|
|
);
|
2019-01-14 13:54:06 +00:00
|
|
|
let text_cursor = interact.rect.min()
|
|
|
|
+ region.options().button_padding
|
|
|
|
+ vec2(region.options().start_icon_width, 0.0);
|
2019-01-10 09:55:38 +00:00
|
|
|
region.add_graphic(GuiCmd::RadioButton {
|
|
|
|
checked: self.checked,
|
|
|
|
interact,
|
|
|
|
});
|
2019-01-12 23:55:56 +00:00
|
|
|
region.add_text(text_cursor, text_style, text);
|
2019-01-10 09:55:38 +00:00
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Slider<'a> {
|
|
|
|
value: &'a mut f32,
|
|
|
|
min: f32,
|
|
|
|
max: f32,
|
|
|
|
id: Option<Id>,
|
|
|
|
text: Option<String>,
|
|
|
|
text_on_top: Option<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Slider<'a> {
|
|
|
|
pub fn new(value: &'a mut f32, min: f32, max: f32) -> Self {
|
|
|
|
Slider {
|
|
|
|
value,
|
|
|
|
min,
|
|
|
|
max,
|
|
|
|
id: None,
|
|
|
|
text: None,
|
|
|
|
text_on_top: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn id(mut self, id: Id) -> Self {
|
|
|
|
self.id = Some(id);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn text<S: Into<String>>(mut self, text: S) -> Self {
|
|
|
|
self.text = Some(text.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Slider<'a> {
|
|
|
|
fn add_to(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();
|
|
|
|
let full_text = format!("{}: {:.3}", text, self.value);
|
|
|
|
let id = Some(self.id.unwrap_or(make_id(text)));
|
|
|
|
let mut naked = self;
|
|
|
|
naked.id = id;
|
|
|
|
naked.text = None;
|
|
|
|
|
|
|
|
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);
|
|
|
|
region.add_text(pos, text_style, text);
|
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| {
|
|
|
|
region.add(label(full_text));
|
|
|
|
});
|
|
|
|
|
2019-01-14 13:26:02 +00:00
|
|
|
response
|
2019-01-10 09:55:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-14 13:26:02 +00:00
|
|
|
let height = font.line_spacing().max(region.options().clickable_diameter);
|
|
|
|
|
2019-01-10 09:55:38 +00:00
|
|
|
let value = self.value;
|
|
|
|
let min = self.min;
|
|
|
|
let max = self.max;
|
|
|
|
debug_assert!(min <= max);
|
|
|
|
let id = region.combined_id(self.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
|
|
|
},
|
|
|
|
id,
|
|
|
|
);
|
|
|
|
|
|
|
|
if interact.active {
|
|
|
|
*value = remap_clamp(
|
|
|
|
region.input().mouse_pos.x,
|
2019-01-14 13:26:02 +00:00
|
|
|
interact.rect.min().x,
|
|
|
|
interact.rect.max().x,
|
2019-01-10 09:55:38 +00:00
|
|
|
min,
|
|
|
|
max,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
region.add_graphic(GuiCmd::Slider {
|
|
|
|
interact,
|
|
|
|
max,
|
|
|
|
min,
|
|
|
|
value: *value,
|
|
|
|
});
|
|
|
|
|
|
|
|
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![
|
2019-01-14 13:54:06 +00:00
|
|
|
vec2(interact.rect.center().x, interact.rect.min().y),
|
|
|
|
vec2(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![
|
2019-01-14 13:54:06 +00:00
|
|
|
vec2(interact.rect.min().x, interact.rect.center().y),
|
|
|
|
vec2(interact.rect.max().x, interact.rect.center().y),
|
2019-01-13 18:15:11 +00:00
|
|
|
],
|
|
|
|
interact,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let paint_cmd = PaintCmd::Line {
|
|
|
|
points,
|
|
|
|
color: Color::WHITE,
|
|
|
|
width: self.line_width,
|
|
|
|
};
|
|
|
|
region.add_graphic(GuiCmd::PaintCommands(vec![paint_cmd]));
|
|
|
|
region.response(interact)
|
|
|
|
}
|
|
|
|
}
|