egui/emigui_wasm/src/app.rs

105 lines
3.4 KiB
Rust
Raw Normal View History

use emigui::{math::*, types::*, widgets::*, Align, Region, TextStyle};
2018-12-26 09:46:23 +00:00
pub struct App {
2018-12-26 21:17:33 +00:00
checked: bool,
2018-12-26 09:46:23 +00:00
count: i32,
radio: i32,
2018-12-26 16:32:58 +00:00
size: Vec2,
2018-12-26 16:32:58 +00:00
corner_radius: f32,
stroke_width: f32,
2018-12-26 09:46:23 +00:00
}
2018-12-26 21:17:33 +00:00
impl Default for App {
fn default() -> App {
2018-12-26 16:32:58 +00:00
App {
2019-01-05 15:23:40 +00:00
checked: true,
radio: 0,
2018-12-26 16:32:58 +00:00
count: 0,
size: vec2(100.0, 50.0),
2018-12-26 16:32:58 +00:00
corner_radius: 5.0,
stroke_width: 2.0,
}
}
2018-12-26 21:17:33 +00:00
}
2018-12-26 16:32:58 +00:00
2019-01-12 22:07:30 +00:00
impl App {
pub fn show_gui(&mut self, gui: &mut Region) {
2019-01-12 23:55:56 +00:00
gui.add(label("Emigui").text_style(TextStyle::Heading));
2019-01-12 22:07:30 +00:00
gui.add(label("Emigui is an Immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL."));
2019-01-13 18:15:11 +00:00
gui.add(Separator::new());
2019-01-12 22:07:30 +00:00
gui.add(label(format!(
2019-01-19 16:10:28 +00:00
"Screen size: {} x {}, pixels_per_point: {}",
2018-12-27 23:51:40 +00:00
gui.input().screen_size.x,
gui.input().screen_size.y,
2019-01-19 16:10:28 +00:00
gui.input().pixels_per_point,
)));
gui.add(label(format!(
"mouse_pos: {} x {}",
gui.input().mouse_pos.x,
gui.input().mouse_pos.y,
)));
gui.add(label(format!(
"gui cursor: {} x {}",
gui.cursor().x,
gui.cursor().y,
)));
2018-12-27 23:51:40 +00:00
gui.horizontal(Align::Min, |gui| {
gui.add(label("Text can have").text_color(srgba(110, 255, 110, 255)));
gui.add(label("color").text_color(srgba(128, 140, 255, 255)));
});
gui.add(label("Hover me")).tooltip_text(
2018-12-28 22:53:15 +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.",
);
gui.add(Checkbox::new(&mut self.checked, "checkbox"));
2018-12-26 21:17:33 +00:00
gui.horizontal(Align::Min, |gui| {
if gui.add(radio(self.radio == 0, "First")).clicked {
self.radio = 0;
2018-12-28 09:39:08 +00:00
}
if gui.add(radio(self.radio == 1, "Second")).clicked {
self.radio = 1;
}
if gui.add(radio(self.radio == 2, "Final")).clicked {
self.radio = 2;
2018-12-28 09:39:08 +00:00
}
});
gui.horizontal(Align::Min, |gui| {
if gui
.add(Button::new("Click me"))
.tooltip_text("This will just increase a counter.")
.clicked
{
self.count += 1;
2018-12-28 09:39:08 +00:00
}
gui.add(label(format!(
"The button have been clicked {} times",
self.count
)));
2018-12-28 09:39:08 +00:00
});
2018-12-26 21:26:15 +00:00
gui.foldable("Test box rendering", |gui| {
gui.add(Slider::new(&mut self.size.x, 0.0, 500.0).text("width"));
gui.add(Slider::new(&mut self.size.y, 0.0, 500.0).text("height"));
gui.add(Slider::new(&mut self.corner_radius, 0.0, 50.0).text("corner_radius"));
gui.add(Slider::new(&mut self.stroke_width, 0.0, 10.0).text("stroke_width"));
2018-12-26 16:32:58 +00:00
let pos = gui.reserve_space(self.size, None).rect.min();
gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Rect {
corner_radius: self.corner_radius,
fill_color: Some(srgba(136, 136, 136, 255)),
2019-01-14 13:26:02 +00:00
rect: Rect::from_min_size(pos, self.size),
outline: Some(Outline {
width: self.stroke_width,
color: srgba(255, 255, 255, 255),
}),
}]));
});
2018-12-26 22:08:50 +00:00
}
}