From a0b0f36d290d6d2454c833518068a058fa288777 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 10 Jan 2021 11:37:47 +0100 Subject: [PATCH] Move egui/math into new crate emath --- CHANGELOG.md | 1 + Cargo.lock | 8 ++++ Cargo.toml | 1 + egui/Cargo.toml | 3 ++ egui/src/containers/area.rs | 2 +- egui/src/containers/collapsing_header.rs | 6 +-- egui/src/containers/resize.rs | 2 +- egui/src/containers/scroll_area.rs | 8 ++-- egui/src/containers/window.rs | 2 +- egui/src/id.rs | 2 +- egui/src/introspection.rs | 1 - egui/src/layers.rs | 4 +- egui/src/layout.rs | 6 +-- egui/src/lib.rs | 5 +- egui/src/memory.rs | 26 +++++------ egui/src/paint/color.rs | 4 +- egui/src/paint/command.rs | 2 +- egui/src/paint/fonts.rs | 14 +++--- egui/src/paint/galley.rs | 8 ++-- egui/src/paint/shadow.rs | 2 +- egui/src/paint/tessellator.rs | 4 +- egui/src/style.rs | 28 ++++++------ egui/src/types.rs | 10 ++-- egui/src/util/undoer.rs | 6 +-- egui/src/widgets/drag_value.rs | 4 +- egui/src/widgets/slider.rs | 8 ++-- egui/src/widgets/text_edit.rs | 10 ++-- egui_demo_lib/src/apps/fractal_clock.rs | 4 +- egui_glium/Cargo.toml | 2 +- emath/Cargo.toml | 24 ++++++++++ emath/README.md | 5 ++ {egui/src/math => emath/src}/align.rs | 10 ++-- egui/src/math/mod.rs => emath/src/lib.rs | 56 ++++++++++++++++++++--- {egui/src/math => emath/src}/pos2.rs | 2 +- {egui/src/math => emath/src}/rect.rs | 2 +- {egui/src/math => emath/src}/rot2.rs | 0 {egui/src/math => emath/src}/smart_aim.rs | 0 {egui/src/math => emath/src}/vec2.rs | 2 +- 38 files changed, 187 insertions(+), 97 deletions(-) create mode 100644 emath/Cargo.toml create mode 100644 emath/README.md rename {egui/src/math => emath/src}/align.rs (91%) rename egui/src/math/mod.rs => emath/src/lib.rs (82%) rename {egui/src/math => emath/src}/pos2.rs (99%) rename {egui/src/math => emath/src}/rect.rs (99%) rename {egui/src/math => emath/src}/rot2.rs (100%) rename {egui/src/math => emath/src}/smart_aim.rs (100%) rename {egui/src/math => emath/src}/vec2.rs (99%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 256776752..9101f7462 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Center window titles. * Tweak size and alignment of some emojis to match other text. +* Rename feature "serde" to "persistence". ### Fixed 🐛 diff --git a/Cargo.lock b/Cargo.lock index 6a6b766ee..d7b815f7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -642,6 +642,7 @@ version = "0.7.0" dependencies = [ "ahash", "atomic_refcell", + "emath", "parking_lot", "rusttype", "serde", @@ -703,6 +704,13 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "emath" +version = "0.7.0" +dependencies = [ + "serde", +] + [[package]] name = "epi" version = "0.7.0" diff --git a/Cargo.toml b/Cargo.toml index 7ef283e1a..c65093846 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "egui_glium", "egui_web", "egui", + "emath", "epi", ] diff --git a/egui/Cargo.toml b/egui/Cargo.toml index 9deed276e..f44316d48 100644 --- a/egui/Cargo.toml +++ b/egui/Cargo.toml @@ -19,6 +19,8 @@ include = [ [lib] [dependencies] +emath = { path = "../emath" } + ahash = { version = "0.6", features = ["std"], default-features = false } atomic_refcell = { version = "0.1", optional = true } # Used instead of parking_lot when you are always using Egui in a single thread. About as fast as parking_lot. Panics on multi-threaded use of egui::Context. parking_lot = { version = "0.11", optional = true } # Using parking_lot over std::sync::Mutex gives 50% speedups in some real-world scenarios @@ -27,6 +29,7 @@ serde = { version = "1", features = ["derive", "rc"], optional = true } [features] default = ["atomic_refcell", "default_fonts"] +persistence = ["serde", "emath/serde"] # If set, egui will use `include_bytes!` to bundle some fonts. # If you plan on specifying your own fonts you may disable this feature. diff --git a/egui/src/containers/area.rs b/egui/src/containers/area.rs index 6af447f6a..b0ce1551d 100644 --- a/egui/src/containers/area.rs +++ b/egui/src/containers/area.rs @@ -8,7 +8,7 @@ use crate::*; /// State that is persisted between frames #[derive(Clone, Copy, Debug)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] pub(crate) struct State { /// Last known pos pub pos: Pos2, diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index 650044904..ce02b00a7 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -7,8 +7,8 @@ use crate::{ }; #[derive(Clone, Copy, Debug)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -#[cfg_attr(feature = "serde", serde(default))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", serde(default))] pub(crate) struct State { open: bool, @@ -114,7 +114,7 @@ pub(crate) fn paint_icon(ui: &mut Ui, openness: f32, response: &Response) { let rect = Rect::from_center_size(rect.center(), vec2(rect.width(), rect.height()) * 0.75); let mut points = vec![rect.left_top(), rect.right_top(), rect.center_bottom()]; use std::f32::consts::TAU; - let rotation = Rot2::from_angle(remap(openness, 0.0..=1.0, -TAU / 4.0..=0.0)); + let rotation = math::Rot2::from_angle(remap(openness, 0.0..=1.0, -TAU / 4.0..=0.0)); for p in &mut points { *p = rect.center() + rotation * (*p - rect.center()); } diff --git a/egui/src/containers/resize.rs b/egui/src/containers/resize.rs index d4bf8842e..b8c29ae79 100644 --- a/egui/src/containers/resize.rs +++ b/egui/src/containers/resize.rs @@ -1,7 +1,7 @@ use crate::*; #[derive(Clone, Copy, Debug)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] pub(crate) struct State { /// This is the size that the user has picked by dragging the resize handles. /// This may be smaller and/or larger than the actual size. diff --git a/egui/src/containers/scroll_area.rs b/egui/src/containers/scroll_area.rs index 64340965f..0a27d0548 100644 --- a/egui/src/containers/scroll_area.rs +++ b/egui/src/containers/scroll_area.rs @@ -1,8 +1,8 @@ use crate::*; #[derive(Clone, Copy, Debug)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -#[cfg_attr(feature = "serde", serde(default))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", serde(default))] pub(crate) struct State { /// Positive offset means scrolling down/right offset: Vec2, @@ -10,7 +10,7 @@ pub(crate) struct State { show_scroll: bool, /// Momentum, used for kinetic scrolling - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub vel: Vec2, /// Mouse offset relative to the top of the handle when started moving the handle. scroll_start_offset_from_top: Option, @@ -177,7 +177,7 @@ impl Prepared { // We take the scroll target so only this ScrollArea will use it. let scroll_target = content_ui.ctx().frame_state().scroll_target.take(); if let Some((scroll_y, align)) = scroll_target { - let center_factor = align.scroll_center_factor(); + let center_factor = align.to_factor(); let top = content_ui.min_rect().top(); let visible_range = top..=top + content_ui.clip_rect().height(); diff --git a/egui/src/containers/window.rs b/egui/src/containers/window.rs index a070f1e82..7e1ee3ed0 100644 --- a/egui/src/containers/window.rs +++ b/egui/src/containers/window.rs @@ -720,7 +720,7 @@ impl TitleBar { self.title_label = self.title_label.text_color(style.fg_stroke.color); let full_top_rect = Rect::from_x_y_ranges(self.rect.x_range(), self.min_rect.y_range()); - let text_pos = align::center_size_in_rect(self.title_galley.size, full_top_rect); + let text_pos = math::align::center_size_in_rect(self.title_galley.size, full_top_rect); let text_pos = text_pos.left_top() - 2.0 * Vec2::Y; // HACK: center on x-height of text (looks better) self.title_label .paint_galley(ui, text_pos, self.title_galley); diff --git a/egui/src/id.rs b/egui/src/id.rs index 516561887..fd3fb5a5d 100644 --- a/egui/src/id.rs +++ b/egui/src/id.rs @@ -28,7 +28,7 @@ use std::hash::Hash; /// Then there are widgets that need no identifiers at all, like labels, /// because they have no state nor are interacted with. #[derive(Clone, Copy, Hash, Eq, PartialEq)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] pub struct Id(u64); impl Id { diff --git a/egui/src/introspection.rs b/egui/src/introspection.rs index a01c9a47b..5bb1e3c88 100644 --- a/egui/src/introspection.rs +++ b/egui/src/introspection.rs @@ -1,6 +1,5 @@ //! uis for egui types. use crate::{ - math::*, paint::{self, PaintCmd, Texture, Triangles}, *, }; diff --git a/egui/src/layers.rs b/egui/src/layers.rs index 68acd66d1..2abdb72a7 100644 --- a/egui/src/layers.rs +++ b/egui/src/layers.rs @@ -4,7 +4,7 @@ use crate::{math::Rect, paint::PaintCmd, Id, *}; /// Different layer categories #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] pub enum Order { /// Painted behind all floating windows Background, @@ -40,7 +40,7 @@ impl Order { /// An identifier for a paint layer. /// Also acts as an identifier for [`Area`]:s. #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] pub struct LayerId { pub order: Order, pub id: Id, diff --git a/egui/src/layout.rs b/egui/src/layout.rs index b44efdef6..fd42bd3f2 100644 --- a/egui/src/layout.rs +++ b/egui/src/layout.rs @@ -66,8 +66,8 @@ impl Region { /// Layout direction, one of `LeftToRight`, `RightToLeft`, `TopDown`, `BottomUp`. #[derive(Clone, Copy, Debug, PartialEq)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", serde(rename_all = "snake_case"))] pub enum Direction { LeftToRight, RightToLeft, @@ -95,7 +95,7 @@ impl Direction { /// The layout of a [`Ui`][`crate::Ui`], e.g. "vertical & centered". #[derive(Clone, Copy, Debug, PartialEq)] -// #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +// #[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] pub struct Layout { /// Main axis direction main_dir: Direction, diff --git a/egui/src/lib.rs b/egui/src/lib.rs index 6550914a6..263bb27f4 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -86,7 +86,6 @@ mod input; mod introspection; mod layers; mod layout; -pub mod math; mod memory; pub mod menu; pub mod paint; @@ -97,6 +96,8 @@ mod ui; pub mod util; pub mod widgets; +pub use emath as math; + pub use { containers::*, context::{Context, CtxRef}, @@ -104,7 +105,7 @@ pub use { input::*, layers::*, layout::*, - math::*, + math::{clamp, lerp, pos2, remap, remap_clamp, vec2, Align, Align2, NumExt, Pos2, Rect, Vec2}, memory::Memory, paint::{ color, Color32, FontDefinitions, FontFamily, PaintCmd, PaintJobs, Rgba, Stroke, TextStyle, diff --git a/egui/src/memory.rs b/egui/src/memory.rs index 625b31471..3b8456c13 100644 --- a/egui/src/memory.rs +++ b/egui/src/memory.rs @@ -18,50 +18,50 @@ use crate::{ /// /// If you want this to persist when closing your app you should serialize `Memory` and store it. #[derive(Clone, Debug, Default)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -#[cfg_attr(feature = "serde", serde(default))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", serde(default))] pub struct Memory { pub(crate) options: Options, - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) interaction: Interaction, // states of various types of widgets pub(crate) collapsing_headers: HashMap, - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) menu_bar: HashMap, pub(crate) resize: HashMap, pub(crate) scroll_areas: HashMap, pub(crate) text_edit: HashMap, - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) window_interaction: Option, /// For temporary edit of e.g. a slider value. /// Couples with [`Interaction::kb_focus_id`]. - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) temp_edit_string: Option, pub(crate) areas: Areas, /// Used by color picker - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) color_cache: Cache, /// Which popup-window is open (if any)? /// Could be a combo box, color picker, menu etc. - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] popup: Option, - #[cfg_attr(feature = "serde", serde(skip))] + #[cfg_attr(feature = "persistence", serde(skip))] everything_is_visible: bool, } // ---------------------------------------------------------------------------- #[derive(Clone, Debug, Default)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -#[cfg_attr(feature = "serde", serde(default))] +#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "persistence", serde(default))] pub(crate) struct Options { /// The default style for new `Ui`:s. pub(crate) style: std::sync::Arc