1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 15:13:12 -04:00

Split example app from emigui_wasm

This commit is contained in:
Emil Ernerfeldt
2019-02-09 23:00:07 +01:00
parent 7dcda029ec
commit 1beed16053
11 changed files with 223 additions and 124 deletions

88
example/src/app.rs Normal file
View File

@@ -0,0 +1,88 @@
use emigui::{label, math::*, types::*, widgets::*, Align, Region, TextStyle};
pub struct App {
checked: bool,
count: i32,
radio: i32,
size: Vec2,
corner_radius: f32,
stroke_width: f32,
}
impl Default for App {
fn default() -> App {
App {
checked: true,
radio: 0,
count: 0,
size: vec2(100.0, 50.0),
corner_radius: 5.0,
stroke_width: 2.0,
}
}
}
impl App {
pub fn show_gui(&mut self, gui: &mut Region) {
gui.add(label!("Emigui!").text_style(TextStyle::Heading));
gui.add(label!("Emigui is an Immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL."));
gui.add(Separator::new());
gui.foldable("Widget examples", |gui| {
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!("and tooltips (hover me)")).tooltip_text(
"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"));
gui.horizontal(Align::Min, |gui| {
if gui.add(radio(self.radio == 0, "First")).clicked {
self.radio = 0;
}
if gui.add(radio(self.radio == 1, "Second")).clicked {
self.radio = 1;
}
if gui.add(radio(self.radio == 2, "Final")).clicked {
self.radio = 2;
}
});
gui.horizontal(Align::Min, |gui| {
if gui
.add(Button::new("Click me"))
.tooltip_text("This will just increase a counter.")
.clicked
{
self.count += 1;
}
gui.add(label!(
"The button have been clicked {} times",
self.count
));
});
});
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"));
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)),
rect: Rect::from_min_size(pos, self.size),
outline: Some(Outline {
width: self.stroke_width,
color: srgba(255, 255, 255, 255),
}),
}]));
});
}
}

79
example/src/lib.rs Normal file
View File

@@ -0,0 +1,79 @@
#![deny(warnings)]
extern crate serde_json;
extern crate wasm_bindgen;
extern crate emigui;
extern crate emigui_wasm;
use emigui::{label, widgets::Label, Align, Emigui, RawInput};
use wasm_bindgen::prelude::*;
mod app;
fn now_ms() -> f64 {
web_sys::window()
.expect("should have a Window")
.performance()
.expect("should have a Performance")
.now()
}
#[wasm_bindgen]
pub struct State {
app: app::App,
emigui: Emigui,
webgl_painter: emigui_wasm::webgl::Painter,
everything_ms: f64,
}
impl State {
fn new(canvas_id: &str, pixels_per_point: f32) -> Result<State, JsValue> {
Ok(State {
app: Default::default(),
emigui: Emigui::new(pixels_per_point),
webgl_painter: emigui_wasm::webgl::Painter::new(canvas_id)?,
everything_ms: 0.0,
})
}
fn run(&mut self, raw_input: RawInput) -> Result<(), JsValue> {
let everything_start = now_ms();
self.emigui.new_frame(raw_input);
let mut region = self.emigui.whole_screen_region();
let mut region = region.centered_column(region.width().min(480.0), Align::Min);
self.app.show_gui(&mut region);
self.emigui.example(&mut region);
region.add(label!("WebGl painter info:"));
region.indent(|region| {
region.add(label!(self.webgl_painter.debug_info()));
});
region.add(label!("Everything: {:.1} ms", self.everything_ms));
let frame = self.emigui.paint();
let result =
self.webgl_painter
.paint(&frame, self.emigui.texture(), raw_input.pixels_per_point);
self.everything_ms = now_ms() - everything_start;
result
}
}
#[wasm_bindgen]
pub fn new_webgl_gui(canvas_id: &str, pixels_per_point: f32) -> Result<State, JsValue> {
State::new(canvas_id, pixels_per_point)
}
#[wasm_bindgen]
pub fn run_gui(state: &mut State, raw_input_json: &str) -> Result<(), JsValue> {
// TODO: nicer interface than JSON
let raw_input: RawInput = serde_json::from_str(raw_input_json).unwrap();
state.run(raw_input)
}