Browse Source

make label into a macro

pull/1/head
Emil Ernerfeldt 6 years ago
parent
commit
ef4f0908f2
  1. 32
      emigui/src/emigui.rs
  2. 8
      emigui/src/layout.rs
  3. 9
      emigui/src/widgets.rs
  4. 88
      emigui_wasm/src/app.rs
  5. 8
      emigui_wasm/src/lib.rs
  6. 4
      emigui_wasm/src/webgl.rs

32
emigui/src/emigui.rs

@ -1,7 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use crate::{ use crate::{
layout, label, layout,
layout::{show_popup, LayoutOptions, Region}, layout::{show_popup, LayoutOptions, Region},
math::{clamp, remap_clamp, vec2}, math::{clamp, remap_clamp, vec2},
mesher::Vertex, mesher::Vertex,
@ -47,10 +47,11 @@ fn show_font_sizes(font_sizes: &mut FontSizes, gui: &mut Region) {
} }
fn show_font_texture(texture: &Texture, gui: &mut Region) { fn show_font_texture(texture: &Texture, gui: &mut Region) {
gui.add(label(format!( gui.add(label!(
"Font texture size: {} x {} (hover to zoom)", "Font texture size: {} x {} (hover to zoom)",
texture.width, texture.height texture.width,
))); texture.height
));
let mut size = vec2(texture.width as f32, texture.height as f32); let mut size = vec2(texture.width as f32, texture.height as f32);
if size.x > gui.width() { if size.x > gui.width() {
size *= gui.width() / size.x; size *= gui.width() / size.x;
@ -192,11 +193,24 @@ impl Emigui {
}); });
region.foldable("Stats", |gui| { region.foldable("Stats", |gui| {
gui.add(label(format!("num_vertices: {}", self.stats.num_vertices))); gui.add(label!(
gui.add(label(format!( "Screen size: {} x {} points, pixels_per_point: {}",
"num_triangles: {}", gui.input().screen_size.x,
self.stats.num_triangles gui.input().screen_size.y,
))); gui.input().pixels_per_point,
));
gui.add(label!(
"mouse_pos: {} x {}",
gui.input().mouse_pos.x,
gui.input().mouse_pos.y,
));
gui.add(label!(
"gui cursor: {} x {}",
gui.cursor().x,
gui.cursor().y,
));
gui.add(label!("num_vertices: {}", self.stats.num_vertices));
gui.add(label!("num_triangles: {}", self.stats.num_triangles));
}); });
} }
} }

8
emigui/src/layout.rs

@ -9,7 +9,7 @@ use crate::{
fonts::{Fonts, TextStyle}, fonts::{Fonts, TextStyle},
math::*, math::*,
types::*, types::*,
widgets::{label, Widget}, widgets::{Label, Widget},
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -85,7 +85,7 @@ impl GuiResponse {
/// Show this text if the item was hovered /// Show this text if the item was hovered
pub fn tooltip_text<S: Into<String>>(&mut self, text: S) -> &mut Self { pub fn tooltip_text<S: Into<String>>(&mut self, text: S) -> &mut Self {
self.tooltip(|popup| { self.tooltip(|popup| {
popup.add(label(text)); popup.add(Label::new(text));
}) })
} }
} }
@ -437,8 +437,8 @@ impl Region {
/// Temporarily split split a vertical layout into several columns. /// Temporarily split split a vertical layout into several columns.
/// ///
/// region.columns(2, |columns| { /// region.columns(2, |columns| {
/// columns[0].add(emigui::widgets::label("First column")); /// columns[0].add(emigui::widgets::label!("First column"));
/// columns[1].add(emigui::widgets::label("Second column")); /// columns[1].add(emigui::widgets::label!("Second column"));
/// }); /// });
pub fn columns<F, R>(&mut self, num_columns: usize, add_contents: F) -> R pub fn columns<F, R>(&mut self, num_columns: usize, add_contents: F) -> R
where where

9
emigui/src/widgets.rs

@ -40,8 +40,11 @@ impl Label {
} }
} }
pub fn label<S: Into<String>>(text: S) -> Label { /// Usage: label!("Foo: {}", bar)
Label::new(text) #[macro_export]
macro_rules! label {
($fmt:expr) => (Label::new($fmt));
($fmt:expr, $($arg:tt)*) => (Label::new(format!($fmt, $($arg)*)));
} }
impl Widget for Label { impl Widget for Label {
@ -264,7 +267,7 @@ impl<'a> Widget for Slider<'a> {
columns[1].available_space.y = response.rect.size().y; columns[1].available_space.y = response.rect.size().y;
columns[1].horizontal(Align::Center, |region| { columns[1].horizontal(Align::Center, |region| {
region.add(label(full_text)); region.add(Label::new(full_text));
}); });
response response

88
emigui_wasm/src/app.rs

@ -1,4 +1,4 @@
use emigui::{math::*, types::*, widgets::*, Align, Region, TextStyle}; use emigui::{label, math::*, types::*, widgets::*, Align, Region, TextStyle};
pub struct App { pub struct App {
checked: bool, checked: bool,
@ -25,62 +25,46 @@ impl Default for App {
impl App { impl App {
pub fn show_gui(&mut self, gui: &mut Region) { pub fn show_gui(&mut self, gui: &mut Region) {
gui.add(label("Emigui").text_style(TextStyle::Heading)); 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(label!("Emigui is an Immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL."));
gui.add(Separator::new()); gui.add(Separator::new());
gui.add(label(format!( gui.foldable("Widget examples", |gui| {
"Screen size: {} x {}, pixels_per_point: {}", gui.horizontal(Align::Min, |gui| {
gui.input().screen_size.x, gui.add(label!("Text can have").text_color(srgba(110, 255, 110, 255)));
gui.input().screen_size.y, gui.add(label!("color").text_color(srgba(128, 140, 255, 255)));
gui.input().pixels_per_point, 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(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,
)));
gui.horizontal(Align::Min, |gui| { gui.add(Checkbox::new(&mut self.checked, "checkbox"));
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(
"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 {
gui.horizontal(Align::Min, |gui| { self.radio = 0;
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 == 1, "Second")).clicked { }
self.radio = 1; if gui.add(radio(self.radio == 2, "Final")).clicked {
} self.radio = 2;
if gui.add(radio(self.radio == 2, "Final")).clicked { }
self.radio = 2; });
}
});
gui.horizontal(Align::Min, |gui| { gui.horizontal(Align::Min, |gui| {
if gui if gui
.add(Button::new("Click me")) .add(Button::new("Click me"))
.tooltip_text("This will just increase a counter.") .tooltip_text("This will just increase a counter.")
.clicked .clicked
{ {
self.count += 1; self.count += 1;
} }
gui.add(label(format!( gui.add(label!(
"The button have been clicked {} times", "The button have been clicked {} times",
self.count self.count
))); ));
});
}); });
gui.foldable("Test box rendering", |gui| { gui.foldable("Test box rendering", |gui| {

8
emigui_wasm/src/lib.rs

@ -5,7 +5,7 @@ extern crate wasm_bindgen;
extern crate emigui; extern crate emigui;
use emigui::{widgets::label, Align, Emigui, RawInput}; use emigui::{label, widgets::Label, Align, Emigui, RawInput};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -48,12 +48,12 @@ impl State {
self.app.show_gui(&mut region); self.app.show_gui(&mut region);
self.emigui.example(&mut region); self.emigui.example(&mut region);
region.add(label("WebGl painter info:")); region.add(label!("WebGl painter info:"));
region.indent(|region| { region.indent(|region| {
region.add(label(self.webgl_painter.debug_info())); region.add(label!(self.webgl_painter.debug_info()));
}); });
region.add(label(format!("Everything: {:.1} ms", self.everything_ms))); region.add(label!("Everything: {:.1} ms", self.everything_ms));
let frame = self.emigui.paint(); let frame = self.emigui.paint();
let result = let result =

4
emigui_wasm/src/webgl.rs

@ -49,8 +49,8 @@ impl Painter {
gl.bind_texture(Gl::TEXTURE_2D, Some(&gl_texture)); gl.bind_texture(Gl::TEXTURE_2D, Some(&gl_texture));
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_WRAP_S, Gl::CLAMP_TO_EDGE as i32); gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_WRAP_S, Gl::CLAMP_TO_EDGE as i32);
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_WRAP_T, Gl::CLAMP_TO_EDGE as i32); gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_WRAP_T, Gl::CLAMP_TO_EDGE as i32);
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_MIN_FILTER, Gl::NEAREST as i32); gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_MIN_FILTER, Gl::LINEAR as i32);
gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_MAG_FILTER, Gl::NEAREST as i32); gl.tex_parameteri(Gl::TEXTURE_2D, Gl::TEXTURE_MAG_FILTER, Gl::LINEAR as i32);
// -------------------------------------------------------------------- // --------------------------------------------------------------------

Loading…
Cancel
Save