From 87288634e9b5f1bef54fc5da9381fe8c2862fdba Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 11 Mar 2019 15:39:54 +0100 Subject: [PATCH] Refactor: Merge LayoutOptions and Style --- emigui/src/emigui.rs | 49 +++++++++------------- emigui/src/layout.rs | 94 ++++++++++++++----------------------------- emigui/src/lib.rs | 2 +- emigui/src/style.rs | 61 +++++++++++++--------------- emigui/src/widgets.rs | 26 ++++++------ 5 files changed, 92 insertions(+), 140 deletions(-) diff --git a/emigui/src/emigui.rs b/emigui/src/emigui.rs index c31d7126..ad49da36 100644 --- a/emigui/src/emigui.rs +++ b/emigui/src/emigui.rs @@ -2,10 +2,10 @@ use std::sync::Arc; use crate::{ label, layout, - layout::{show_popup, LayoutOptions, Region}, + layout::{show_popup, Region}, math::{clamp, remap_clamp, vec2}, mesher::{Mesher, Vertex}, - style, + style::Style, types::{Color, GuiCmd, GuiInput, PaintCmd}, widgets::*, FontSizes, Fonts, Mesh, RawInput, Texture, @@ -17,26 +17,19 @@ struct Stats { num_triangles: usize, } -fn show_options(options: &mut LayoutOptions, gui: &mut Region) { - if gui.add(Button::new("Reset LayoutOptions")).clicked { - *options = Default::default(); - } - gui.add(Slider::f32(&mut options.item_spacing.x, 0.0, 10.0).text("item_spacing.x")); - gui.add(Slider::f32(&mut options.item_spacing.y, 0.0, 10.0).text("item_spacing.y")); - gui.add(Slider::f32(&mut options.window_padding.x, 0.0, 10.0).text("window_padding.x")); - gui.add(Slider::f32(&mut options.window_padding.y, 0.0, 10.0).text("window_padding.y")); - gui.add(Slider::f32(&mut options.indent, 0.0, 100.0).text("indent")); - gui.add(Slider::f32(&mut options.button_padding.x, 0.0, 20.0).text("button_padding.x")); - gui.add(Slider::f32(&mut options.button_padding.y, 0.0, 20.0).text("button_padding.y")); - gui.add(Slider::f32(&mut options.clickable_diameter, 0.0, 60.0).text("clickable_diameter")); - gui.add(Slider::f32(&mut options.start_icon_width, 0.0, 60.0).text("start_icon_width")); -} - -fn show_style(style: &mut style::Style, gui: &mut Region) { - if gui.add(Button::new("Reset Style")).clicked { +fn show_style(style: &mut Style, gui: &mut Region) { + if gui.add(Button::new("Reset style")).clicked { *style = Default::default(); } - gui.add(Checkbox::new(&mut style.debug_rects, "debug_rects")); + gui.add(Slider::f32(&mut style.item_spacing.x, 0.0, 10.0).text("item_spacing.x")); + gui.add(Slider::f32(&mut style.item_spacing.y, 0.0, 10.0).text("item_spacing.y")); + gui.add(Slider::f32(&mut style.window_padding.x, 0.0, 10.0).text("window_padding.x")); + gui.add(Slider::f32(&mut style.window_padding.y, 0.0, 10.0).text("window_padding.y")); + gui.add(Slider::f32(&mut style.indent, 0.0, 100.0).text("indent")); + gui.add(Slider::f32(&mut style.button_padding.x, 0.0, 20.0).text("button_padding.x")); + gui.add(Slider::f32(&mut style.button_padding.y, 0.0, 20.0).text("button_padding.y")); + gui.add(Slider::f32(&mut style.clickable_diameter, 0.0, 60.0).text("clickable_diameter")); + gui.add(Slider::f32(&mut style.start_icon_width, 0.0, 60.0).text("start_icon_width")); gui.add(Slider::f32(&mut style.line_width, 0.0, 10.0).text("line_width")); } @@ -119,7 +112,6 @@ fn show_font_texture(texture: &Texture, gui: &mut Region) { pub struct Emigui { pub last_input: RawInput, pub data: Arc, - pub style: style::Style, stats: Stats, } @@ -128,7 +120,6 @@ impl Emigui { Emigui { last_input: Default::default(), data: Arc::new(layout::Data::new(pixels_per_point)), - style: Default::default(), stats: Default::default(), } } @@ -151,7 +142,7 @@ impl Emigui { let size = self.data.input.screen_size; layout::Region { data: self.data.clone(), - options: self.data.options(), + style: self.data.style(), id: Default::default(), dir: layout::Direction::Vertical, align: layout::Align::Center, @@ -163,7 +154,7 @@ impl Emigui { pub fn paint(&mut self) -> Mesh { let gui_commands = self.data.graphics.lock().unwrap().drain(); - let paint_commands = style::into_paint_commands(gui_commands, &self.style); + let paint_commands = crate::style::into_paint_commands(gui_commands, &self.data.style()); let mut mesher = Mesher::new(self.last_input.pixels_per_point); mesher.paint(&self.data.fonts, &paint_commands); @@ -174,14 +165,10 @@ impl Emigui { } pub fn example(&mut self, region: &mut Region) { - region.foldable("LayoutOptions", |gui| { - let mut options = self.data.options(); - show_options(&mut options, gui); - self.data.set_options(options); - }); - region.foldable("Style", |gui| { - show_style(&mut self.style, gui); + let mut style = self.data.style(); + show_style(&mut style, gui); + self.data.set_style(style); }); region.foldable("Fonts", |gui| { diff --git a/emigui/src/layout.rs b/emigui/src/layout.rs index d8b7167e..6369a381 100644 --- a/emigui/src/layout.rs +++ b/emigui/src/layout.rs @@ -8,49 +8,13 @@ use crate::{ font::TextFragment, fonts::{Fonts, TextStyle}, math::*, + style::Style, types::*, widgets::{Label, Widget}, }; // ---------------------------------------------------------------------------- -#[derive(Clone, Copy, Debug, Serialize)] -pub struct LayoutOptions { - /// Horizontal and vertical padding within a window frame. - pub window_padding: Vec2, - - /// Button size is text size plus this on each side - pub button_padding: Vec2, - - /// Horizontal and vertical spacing between widgets - pub item_spacing: Vec2, - - /// Indent foldable regions etc by this much. - pub indent: f32, - - /// Anything clickable is (at least) this wide. - pub clickable_diameter: f32, - - /// Checkboxes, radio button and foldables have an icon at the start. - /// The text starts after this many pixels. - pub start_icon_width: f32, -} - -impl Default for LayoutOptions { - fn default() -> Self { - LayoutOptions { - window_padding: vec2(6.0, 6.0), - button_padding: vec2(5.0, 3.0), - item_spacing: vec2(8.0, 4.0), - indent: 21.0, - clickable_diameter: 34.0, - start_icon_width: 20.0, - } - } -} - -// ---------------------------------------------------------------------------- - // TODO: rename GuiResponse pub struct GuiResponse { /// The mouse is hovering above this @@ -168,10 +132,10 @@ impl GraphicLayers { // ---------------------------------------------------------------------------- // TODO: give a better name. -/// Contains the input, options and output of all GUI commands. +/// Contains the input, style and output of all GUI commands. pub struct Data { - /// The default options for new regions - pub(crate) options: Mutex, + /// The default style for new regions + pub(crate) style: Mutex