egui/emgui_wasm/src/app.rs

122 lines
4.1 KiB
Rust
Raw Normal View History

use emgui::{math::*, types::*, widgets::*, Region};
2018-12-26 22:08:50 +00:00
pub trait GuiSettings {
2019-01-06 15:34:01 +00:00
fn show_gui(&mut self, gui: &mut Region);
2018-12-26 22:08:50 +00:00
}
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,
2018-12-26 21:26:15 +00:00
selected_alternative: 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,
2018-12-26 21:26:15 +00:00
selected_alternative: 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
2018-12-26 22:08:50 +00:00
impl GuiSettings for App {
2019-01-06 15:34:01 +00:00
fn show_gui(&mut self, gui: &mut Region) {
gui.add(label(format!(
2018-12-27 23:51:40 +00:00
"Screen size: {} x {}",
gui.input().screen_size.x,
gui.input().screen_size.y,
)));
2018-12-27 23:51:40 +00:00
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
2018-12-28 09:39:08 +00:00
gui.horizontal(|gui| {
if gui
.add(radio(self.selected_alternative == 0, "First"))
.clicked
{
2018-12-28 09:39:08 +00:00
self.selected_alternative = 0;
}
if gui
.add(radio(self.selected_alternative == 1, "Second"))
.clicked
{
2018-12-28 09:39:08 +00:00
self.selected_alternative = 1;
}
if gui
.add(radio(self.selected_alternative == 2, "Final"))
.clicked
{
2018-12-28 09:39:08 +00:00
self.selected_alternative = 2;
}
});
2018-12-26 21:26:15 +00:00
2018-12-28 22:53:15 +00:00
if gui
.add(Button::new("Click me"))
2018-12-28 22:53:15 +00:00
.tooltip_text("This will just increase a counter.")
.clicked
{
2018-12-26 09:46:23 +00:00
self.count += 1;
}
gui.add(label(format!("This is a multiline label.\nThe button have been clicked {} times.\nBelow are more options.", self.count)));
2018-12-26 16:01:46 +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.cursor();
gui.add_graphic(GuiCmd::PaintCommands(vec![PaintCmd::Rect {
corner_radius: self.corner_radius,
fill_color: Some(srgba(136, 136, 136, 255)),
pos,
size: self.size,
outline: Some(Outline {
width: self.stroke_width,
color: srgba(255, 255, 255, 255),
}),
}]));
gui.reserve_space(self.size, None);
});
2018-12-26 22:08:50 +00:00
}
}
2018-12-30 20:08:29 +00:00
impl GuiSettings for emgui::LayoutOptions {
2019-01-06 15:34:01 +00:00
fn show_gui(&mut self, gui: &mut Region) {
if gui.add(Button::new("Reset LayoutOptions")).clicked {
2018-12-26 22:08:50 +00:00
*self = Default::default();
}
gui.add(Slider::new(&mut self.item_spacing.x, 0.0, 10.0).text("item_spacing.x"));
gui.add(Slider::new(&mut self.item_spacing.y, 0.0, 10.0).text("item_spacing.y"));
gui.add(Slider::new(&mut self.window_padding.x, 0.0, 10.0).text("window_padding.x"));
gui.add(Slider::new(&mut self.window_padding.y, 0.0, 10.0).text("window_padding.y"));
gui.add(Slider::new(&mut self.indent, 0.0, 100.0).text("indent"));
gui.add(Slider::new(&mut self.button_padding.x, 0.0, 20.0).text("button_padding.x"));
gui.add(Slider::new(&mut self.button_padding.y, 0.0, 20.0).text("button_padding.y"));
gui.add(Slider::new(&mut self.start_icon_width, 0.0, 60.0).text("start_icon_width"));
2018-12-26 09:46:23 +00:00
}
}
2018-12-27 17:19:06 +00:00
2018-12-30 20:08:29 +00:00
impl GuiSettings for emgui::Style {
2019-01-06 15:34:01 +00:00
fn show_gui(&mut self, gui: &mut Region) {
if gui.add(Button::new("Reset Style")).clicked {
2018-12-27 17:19:06 +00:00
*self = Default::default();
}
gui.add(Checkbox::new(&mut self.debug_rects, "debug_rects"));
gui.add(Slider::new(&mut self.line_width, 0.0, 10.0).text("line_width"));
2018-12-27 17:19:06 +00:00
}
}