2020-04-12 10:07:51 +00:00
|
|
|
use crate::{color::*, label, math::*, widgets::*, Align, Outline, PaintCmd, Region};
|
2018-12-26 09:46:23 +00:00
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
/// Showcase some region code
|
|
|
|
pub struct ExampleApp {
|
2018-12-26 21:17:33 +00:00
|
|
|
checked: bool,
|
2019-03-11 12:32:44 +00:00
|
|
|
count: usize,
|
|
|
|
radio: usize,
|
2018-12-26 16:32:58 +00:00
|
|
|
|
2019-01-06 23:03:29 +00:00
|
|
|
size: Vec2,
|
2018-12-26 16:32:58 +00:00
|
|
|
corner_radius: f32,
|
|
|
|
stroke_width: f32,
|
2019-03-11 12:32:44 +00:00
|
|
|
num_boxes: usize,
|
|
|
|
|
|
|
|
num_columns: usize,
|
2019-03-11 12:32:54 +00:00
|
|
|
|
|
|
|
slider_value: usize,
|
2018-12-26 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
impl Default for ExampleApp {
|
|
|
|
fn default() -> ExampleApp {
|
|
|
|
ExampleApp {
|
2019-01-05 15:23:40 +00:00
|
|
|
checked: true,
|
2019-01-16 15:28:43 +00:00
|
|
|
radio: 0,
|
2018-12-26 16:32:58 +00:00
|
|
|
count: 0,
|
2019-01-06 23:03:29 +00:00
|
|
|
size: vec2(100.0, 50.0),
|
2018-12-26 16:32:58 +00:00
|
|
|
corner_radius: 5.0,
|
|
|
|
stroke_width: 2.0,
|
2019-02-10 15:10:08 +00:00
|
|
|
num_boxes: 1,
|
2019-03-11 12:32:44 +00:00
|
|
|
|
|
|
|
num_columns: 2,
|
2019-03-11 12:32:54 +00:00
|
|
|
|
|
|
|
slider_value: 100,
|
2018-12-26 16:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-26 21:17:33 +00:00
|
|
|
}
|
2018-12-26 16:32:58 +00:00
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
impl ExampleApp {
|
|
|
|
pub fn ui(&mut self, region: &mut Region) {
|
|
|
|
region.foldable("About Emigui", |region| {
|
|
|
|
region.add(label!("Emigui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL."));
|
|
|
|
});
|
|
|
|
|
|
|
|
region.foldable("Widget examples", |region| {
|
|
|
|
region.horizontal(Align::Min, |region| {
|
|
|
|
region.add(label!("Text can have").text_color(srgba(110, 255, 110, 255)));
|
|
|
|
region.add(label!("color").text_color(srgba(128, 140, 255, 255)));
|
|
|
|
region.add(label!("and tooltips (hover me)")).tooltip_text(
|
2019-01-21 07:48:32 +00:00
|
|
|
"This is a multiline tooltip that demonstrates that you can easily add tooltips to any element.\nThis is the second line.\nThis is the third.",
|
|
|
|
);
|
|
|
|
});
|
2018-12-27 23:51:40 +00:00
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
region.add(Checkbox::new(&mut self.checked, "checkbox"));
|
2018-12-28 22:53:15 +00:00
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
region.horizontal(Align::Min, |region| {
|
|
|
|
if region.add(radio(self.radio == 0, "First")).clicked {
|
2019-01-21 07:48:32 +00:00
|
|
|
self.radio = 0;
|
|
|
|
}
|
2020-04-12 10:07:51 +00:00
|
|
|
if region.add(radio(self.radio == 1, "Second")).clicked {
|
2019-01-21 07:48:32 +00:00
|
|
|
self.radio = 1;
|
|
|
|
}
|
2020-04-12 10:07:51 +00:00
|
|
|
if region.add(radio(self.radio == 2, "Final")).clicked {
|
2019-01-21 07:48:32 +00:00
|
|
|
self.radio = 2;
|
|
|
|
}
|
|
|
|
});
|
2019-01-16 15:28:43 +00:00
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
region.horizontal(Align::Min, |region| {
|
|
|
|
if region
|
2019-01-21 07:48:32 +00:00
|
|
|
.add(Button::new("Click me"))
|
|
|
|
.tooltip_text("This will just increase a counter.")
|
|
|
|
.clicked
|
|
|
|
{
|
|
|
|
self.count += 1;
|
|
|
|
}
|
2020-04-12 10:07:51 +00:00
|
|
|
region.add(label!(
|
2019-01-21 07:48:32 +00:00
|
|
|
"The button have been clicked {} times",
|
|
|
|
self.count
|
|
|
|
));
|
|
|
|
});
|
2018-12-28 09:39:08 +00:00
|
|
|
});
|
2018-12-26 21:26:15 +00:00
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
region.foldable("Layouts", |region| {
|
|
|
|
region.add(Slider::usize(&mut self.num_columns, 1, 10).text("Columns"));
|
|
|
|
region.columns(self.num_columns, |cols| {
|
2019-03-11 12:32:44 +00:00
|
|
|
for (i, col) in cols.iter_mut().enumerate() {
|
|
|
|
col.add(label!("Column {} out of {}", i + 1, self.num_columns));
|
|
|
|
if i + 1 == self.num_columns {
|
|
|
|
if col.add(Button::new("Delete this")).clicked {
|
|
|
|
self.num_columns -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
region.foldable("Test box rendering", |region| {
|
|
|
|
region.add(Slider::f32(&mut self.size.x, 0.0, 500.0).text("width"));
|
|
|
|
region.add(Slider::f32(&mut self.size.y, 0.0, 500.0).text("height"));
|
|
|
|
region.add(Slider::f32(&mut self.corner_radius, 0.0, 50.0).text("corner_radius"));
|
|
|
|
region.add(Slider::f32(&mut self.stroke_width, 0.0, 10.0).text("stroke_width"));
|
|
|
|
region.add(Slider::usize(&mut self.num_boxes, 0, 5).text("num_boxes"));
|
2018-12-26 16:32:58 +00:00
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
let pos = region
|
2019-02-10 15:10:08 +00:00
|
|
|
.reserve_space(
|
|
|
|
vec2(self.size.x * (self.num_boxes as f32), self.size.y),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.rect
|
|
|
|
.min();
|
|
|
|
|
2019-02-10 19:56:59 +00:00
|
|
|
let mut cmds = vec![];
|
2019-02-10 15:10:08 +00:00
|
|
|
for i in 0..self.num_boxes {
|
2019-02-10 19:56:59 +00:00
|
|
|
cmds.push(PaintCmd::Rect {
|
2019-02-10 15:10:08 +00:00
|
|
|
corner_radius: self.corner_radius,
|
2019-03-11 14:30:32 +00:00
|
|
|
fill_color: Some(gray(136, 255)),
|
2019-02-10 15:10:08 +00:00
|
|
|
rect: Rect::from_min_size(
|
2020-04-18 23:05:49 +00:00
|
|
|
pos2(pos.x + (i as f32) * (self.size.x * 1.1), pos.y),
|
2019-02-10 15:10:08 +00:00
|
|
|
self.size,
|
|
|
|
),
|
2020-04-19 09:11:41 +00:00
|
|
|
outline: Some(Outline::new(self.stroke_width, gray(255, 255))),
|
2019-02-10 19:56:59 +00:00
|
|
|
});
|
2019-02-10 15:10:08 +00:00
|
|
|
}
|
2020-04-12 10:07:51 +00:00
|
|
|
region.add_paint_cmds(cmds);
|
2019-01-06 23:03:29 +00:00
|
|
|
});
|
2019-03-11 12:32:54 +00:00
|
|
|
|
2020-04-12 10:07:51 +00:00
|
|
|
region.foldable("Slider example", |region| {
|
|
|
|
value_ui(&mut self.slider_value, region);
|
2019-03-11 12:32:54 +00:00
|
|
|
});
|
2020-04-19 09:13:24 +00:00
|
|
|
|
|
|
|
region.foldable("Name clash example", |region| {
|
2020-04-20 21:42:11 +00:00
|
|
|
region.add_label("\
|
2020-04-19 09:13:24 +00:00
|
|
|
Regions that store state require unique identifiers so we can track their state between frames. \
|
2020-04-20 21:42:11 +00:00
|
|
|
Identifiers are normally derived from the titles of the widget.");
|
2020-04-19 09:13:24 +00:00
|
|
|
|
2020-04-20 21:42:11 +00:00
|
|
|
region.add_label("\
|
2020-04-19 09:13:24 +00:00
|
|
|
For instance, foldable regions needs to store wether or not they are open. \
|
|
|
|
If you fail to give them unique names then clicking one will open both. \
|
2020-04-20 21:42:11 +00:00
|
|
|
To help you debug this, a error message is printed on screen:");
|
2020-04-19 09:13:24 +00:00
|
|
|
|
|
|
|
region.foldable("Foldable", |region| {
|
2020-04-20 21:42:11 +00:00
|
|
|
region.add_label("Contents of first folddable region");
|
2020-04-19 09:13:24 +00:00
|
|
|
});
|
|
|
|
region.foldable("Foldable", |region| {
|
2020-04-20 21:42:11 +00:00
|
|
|
region.add_label("Contents of second folddable region");
|
2020-04-19 09:13:24 +00:00
|
|
|
});
|
|
|
|
|
2020-04-20 21:42:11 +00:00
|
|
|
region.add_label("\
|
|
|
|
Most widgets don't need unique names, but are tracked \
|
|
|
|
based on their position on screen. For instance, buttons:");
|
2020-04-19 09:13:24 +00:00
|
|
|
region.add(Button::new("Button"));
|
|
|
|
region.add(Button::new("Button"));
|
|
|
|
});
|
2018-12-26 22:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 10:07:51 +00:00
|
|
|
|
|
|
|
pub fn value_ui(value: &mut usize, region: &mut Region) {
|
|
|
|
region.add(Slider::usize(value, 1, 1000));
|
|
|
|
if region.add(Button::new("Double it")).clicked {
|
|
|
|
*value *= 2;
|
|
|
|
}
|
|
|
|
region.add(label!("Value: {}", value));
|
|
|
|
}
|