mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 05:10:03 -04:00
Break into two crates
This commit is contained in:
17
emgui_wasm/Cargo.toml
Normal file
17
emgui_wasm/Cargo.toml
Normal file
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "emgui_wasm"
|
||||
version = "0.1.0"
|
||||
authors = ["Emil Ernerfeldt <emilernerfeldt@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
lazy_static = "1"
|
||||
serde = "1"
|
||||
serde_json = "1"
|
||||
wasm-bindgen = "0.2"
|
||||
# web-sys = { version = "0.3.5", features = ['console', 'Performance', 'Window'] }
|
||||
|
||||
emgui = { path = "../emgui" }
|
||||
122
emgui_wasm/src/app.rs
Normal file
122
emgui_wasm/src/app.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
use emgui::{math::*, types::*, Layout};
|
||||
|
||||
pub trait GuiSettings {
|
||||
fn show_gui(&mut self, gui: &mut Layout);
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
checked: bool,
|
||||
count: i32,
|
||||
selected_alternative: i32,
|
||||
|
||||
width: f32,
|
||||
height: f32,
|
||||
corner_radius: f32,
|
||||
stroke_width: f32,
|
||||
}
|
||||
|
||||
impl Default for App {
|
||||
fn default() -> App {
|
||||
App {
|
||||
checked: false,
|
||||
selected_alternative: 0,
|
||||
count: 0,
|
||||
width: 100.0,
|
||||
height: 50.0,
|
||||
corner_radius: 5.0,
|
||||
stroke_width: 2.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GuiSettings for App {
|
||||
fn show_gui(&mut self, gui: &mut Layout) {
|
||||
gui.label(format!(
|
||||
"Screen size: {} x {}",
|
||||
gui.input().screen_size.x,
|
||||
gui.input().screen_size.y,
|
||||
));
|
||||
|
||||
gui.label("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.checkbox("checkbox", &mut self.checked);
|
||||
|
||||
gui.horizontal(|gui| {
|
||||
if gui.radio("First", self.selected_alternative == 0).clicked {
|
||||
self.selected_alternative = 0;
|
||||
}
|
||||
if gui.radio("Second", self.selected_alternative == 1).clicked {
|
||||
self.selected_alternative = 1;
|
||||
}
|
||||
if gui.radio("Final", self.selected_alternative == 2).clicked {
|
||||
self.selected_alternative = 2;
|
||||
}
|
||||
});
|
||||
|
||||
if gui
|
||||
.button("Click me")
|
||||
.tooltip_text("This will just increase a counter.")
|
||||
.clicked
|
||||
{
|
||||
self.count += 1;
|
||||
}
|
||||
|
||||
gui.label(format!("This is a multiline label.\nThe button have been clicked {} times.\nBelow are more options.", self.count));
|
||||
|
||||
gui.foldable("Box rendering options", |gui| {
|
||||
gui.slider_f32("width", &mut self.width, 0.0, 500.0);
|
||||
gui.slider_f32("height", &mut self.height, 0.0, 500.0);
|
||||
gui.slider_f32("corner_radius", &mut self.corner_radius, 0.0, 50.0);
|
||||
gui.slider_f32("stroke_width", &mut self.stroke_width, 0.0, 10.0);
|
||||
});
|
||||
|
||||
gui.add_paint_command(GuiCmd::PaintCommands(vec![PaintCmd::Rect {
|
||||
corner_radius: self.corner_radius,
|
||||
fill_color: Some(srgba(136, 136, 136, 255)),
|
||||
pos: vec2(300.0, 100.0),
|
||||
size: vec2(self.width, self.height),
|
||||
outline: Some(Outline {
|
||||
width: self.stroke_width,
|
||||
color: srgba(255, 255, 255, 255),
|
||||
}),
|
||||
}]));
|
||||
|
||||
gui.foldable("LayoutOptions", |gui| {
|
||||
let mut options = gui.options().clone();
|
||||
options.show_gui(gui);
|
||||
gui.set_options(options);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl GuiSettings for emgui::LayoutOptions {
|
||||
fn show_gui(&mut self, gui: &mut Layout) {
|
||||
if gui.button("Reset LayoutOptions").clicked {
|
||||
*self = Default::default();
|
||||
}
|
||||
gui.slider_f32("char_size.x", &mut self.char_size.x, 0.0, 20.0);
|
||||
gui.slider_f32("char_size.y", &mut self.char_size.y, 0.0, 20.0);
|
||||
gui.slider_f32("item_spacing.x", &mut self.item_spacing.x, 0.0, 10.0);
|
||||
gui.slider_f32("item_spacing.y", &mut self.item_spacing.y, 0.0, 10.0);
|
||||
gui.slider_f32("window_padding.x", &mut self.window_padding.x, 0.0, 10.0);
|
||||
gui.slider_f32("window_padding.y", &mut self.window_padding.y, 0.0, 10.0);
|
||||
gui.slider_f32("indent", &mut self.indent, 0.0, 100.0);
|
||||
gui.slider_f32("width", &mut self.width, 0.0, 1000.0);
|
||||
gui.slider_f32("button_padding.x", &mut self.button_padding.x, 0.0, 20.0);
|
||||
gui.slider_f32("button_padding.y", &mut self.button_padding.y, 0.0, 20.0);
|
||||
gui.slider_f32("start_icon_width", &mut self.start_icon_width, 0.0, 60.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl GuiSettings for emgui::Style {
|
||||
fn show_gui(&mut self, gui: &mut Layout) {
|
||||
if gui.button("Reset Style").clicked {
|
||||
*self = Default::default();
|
||||
}
|
||||
gui.checkbox("debug_rects", &mut self.debug_rects);
|
||||
gui.slider_f32("line_width", &mut self.line_width, 0.0, 10.0);
|
||||
gui.slider_f32("font_size", &mut self.font_size, 5.0, 32.0);
|
||||
}
|
||||
}
|
||||
42
emgui_wasm/src/lib.rs
Normal file
42
emgui_wasm/src/lib.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
#![deny(warnings)]
|
||||
|
||||
extern crate lazy_static;
|
||||
extern crate serde_json;
|
||||
extern crate wasm_bindgen;
|
||||
// extern crate web_sys;
|
||||
|
||||
extern crate emgui;
|
||||
|
||||
use std::sync::Mutex;
|
||||
|
||||
use emgui::{Emgui, RawInput};
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
pub mod app;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn show_gui(raw_input_json: &str) -> String {
|
||||
// TODO: faster interface than JSON
|
||||
let raw_input: RawInput = serde_json::from_str(raw_input_json).unwrap();
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref APP: Mutex<app::App> = Default::default();
|
||||
static ref EMGUI: Mutex<Emgui> = Default::default();
|
||||
}
|
||||
|
||||
let mut emgui = EMGUI.lock().unwrap();
|
||||
emgui.new_frame(raw_input);
|
||||
|
||||
use crate::app::GuiSettings;
|
||||
APP.lock().unwrap().show_gui(&mut emgui.layout);
|
||||
|
||||
let mut style = emgui.style.clone();
|
||||
emgui.layout.foldable("Style", |gui| {
|
||||
style.show_gui(gui);
|
||||
});
|
||||
emgui.style = style;
|
||||
|
||||
let commands = emgui.paint();
|
||||
serde_json::to_string(&commands).unwrap()
|
||||
}
|
||||
Reference in New Issue
Block a user