diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e43bd03..bb07e0bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Tweak size and alignment of some emojis to match other text. * Rename `PaintCmd` to `Shape`. * Rename feature "serde" to "persistence". +* Break out the modules `math` and `paint` into separate crates `emath` and `epaint`. ### Fixed 🐛 diff --git a/Cargo.lock b/Cargo.lock index d7b815f7..4c6710a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -640,11 +640,7 @@ dependencies = [ name = "egui" version = "0.7.0" dependencies = [ - "ahash", - "atomic_refcell", - "emath", - "parking_lot", - "rusttype", + "epaint", "serde", ] @@ -711,6 +707,18 @@ dependencies = [ "serde", ] +[[package]] +name = "epaint" +version = "0.7.0" +dependencies = [ + "ahash", + "atomic_refcell", + "emath", + "parking_lot", + "rusttype", + "serde", +] + [[package]] name = "epi" version = "0.7.0" diff --git a/Cargo.toml b/Cargo.toml index c6509384..b1d2c7f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "egui_web", "egui", "emath", + "epaint", "epi", ] diff --git a/check.sh b/check.sh index 188afc66..bf7a7853 100755 --- a/check.sh +++ b/check.sh @@ -6,7 +6,7 @@ cargo check --workspace --all-targets --all-features cargo check -p egui_demo_app --lib --target wasm32-unknown-unknown cargo check -p egui_demo_app --lib --target wasm32-unknown-unknown --all-features cargo fmt --all -- --check -CARGO_INCREMENTAL=0 cargo clippy --workspace --all-targets --all-features -- -D warnings -W clippy::all #-W clippy::pedantic -W clippy::restriction -W clippy::nursery +CARGO_INCREMENTAL=0 cargo clippy --workspace --all-targets --all-features -- -D warnings -W clippy::all cargo test --workspace --all-targets --all-features cargo test --workspace --doc diff --git a/egui/Cargo.toml b/egui/Cargo.toml index f44316d4..4659e57b 100644 --- a/egui/Cargo.toml +++ b/egui/Cargo.toml @@ -13,27 +13,22 @@ keywords = ["gui", "imgui", "immediate", "portable", "gamedev"] include = [ "**/*.rs", "Cargo.toml", - "fonts/*.ttf", ] [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 -rusttype = "0.9" +epaint = { path = "../epaint", default-features = false } serde = { version = "1", features = ["derive", "rc"], optional = true } [features] -default = ["atomic_refcell", "default_fonts"] -persistence = ["serde", "emath/serde"] +default = ["default_fonts", "single_threaded"] +persistence = ["serde", "epaint/persistence"] # If set, egui will use `include_bytes!` to bundle some fonts. # If you plan on specifying your own fonts you may disable this feature. -default_fonts = [] +default_fonts = ["epaint/default_fonts"] # Only needed if you plan to use the same egui::Context from multiple threads. -multi_threaded = ["parking_lot"] +single_threaded = ["epaint/single_threaded"] +multi_threaded = ["epaint/multi_threaded"] diff --git a/egui/src/context.rs b/egui/src/context.rs index c6f5cc80..3cea398b 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -871,24 +871,3 @@ impl Context { self.set_style(style); } } - -impl paint::TessellationOptions { - pub fn ui(&mut self, ui: &mut Ui) { - let Self { - aa_size: _, - anti_alias, - coarse_tessellation_culling, - debug_paint_clip_rects, - debug_paint_text_rects, - debug_ignore_clip_rects, - } = self; - ui.checkbox(anti_alias, "Antialias"); - ui.checkbox( - coarse_tessellation_culling, - "Do coarse culling in the tessellator", - ); - ui.checkbox(debug_ignore_clip_rects, "Ignore clip rectangles (debug)"); - ui.checkbox(debug_paint_clip_rects, "Paint clip rectangles (debug)"); - ui.checkbox(debug_paint_text_rects, "Paint text bounds (debug)"); - } -} diff --git a/egui/src/introspection.rs b/egui/src/introspection.rs index de677d27..09368486 100644 --- a/egui/src/introspection.rs +++ b/egui/src/introspection.rs @@ -1,69 +1,145 @@ //! uis for egui types. -use crate::{ - paint::{self, Shape, Texture, Triangles}, - *, -}; +use crate::*; -impl Texture { - pub fn ui(&self, ui: &mut Ui) { - // Show font texture in demo Ui - ui.label(format!( - "Texture size: {} x {} (hover to zoom)", - self.width, self.height - )); - if self.width <= 1 || self.height <= 1 { - return; - } - let mut size = vec2(self.width as f32, self.height as f32); - if size.x > ui.available_width() { - size *= ui.available_width() / size.x; - } - let (rect, response) = ui.allocate_at_least(size, Sense::hover()); - let mut triangles = Triangles::default(); - triangles.add_rect_with_uv( - rect, - [pos2(0.0, 0.0), pos2(1.0, 1.0)].into(), - Color32::WHITE, - ); - ui.painter().add(Shape::triangles(triangles)); +impl Widget for &epaint::Texture { + fn ui(self, ui: &mut Ui) -> Response { + use epaint::Triangles; - let (tex_w, tex_h) = (self.width as f32, self.height as f32); - - response.on_hover_ui(|ui| { - let pos = ui - .input() - .mouse - .pos - .unwrap_or_else(|| ui.min_rect().left_top()); - let (_id, zoom_rect) = ui.allocate_space(vec2(128.0, 128.0)); - let u = remap_clamp(pos.x, rect.x_range(), 0.0..=tex_w); - let v = remap_clamp(pos.y, rect.y_range(), 0.0..=tex_h); - - let texel_radius = 32.0; - let u = u.at_least(texel_radius).at_most(tex_w - texel_radius); - let v = v.at_least(texel_radius).at_most(tex_h - texel_radius); - - let uv_rect = Rect::from_min_max( - pos2((u - texel_radius) / tex_w, (v - texel_radius) / tex_h), - pos2((u + texel_radius) / tex_w, (v + texel_radius) / tex_h), - ); + ui.vertical(|ui| { + // Show font texture in demo Ui + ui.label(format!( + "Texture size: {} x {} (hover to zoom)", + self.width, self.height + )); + if self.width <= 1 || self.height <= 1 { + return; + } + let mut size = vec2(self.width as f32, self.height as f32); + if size.x > ui.available_width() { + size *= ui.available_width() / size.x; + } + let (rect, response) = ui.allocate_at_least(size, Sense::hover()); let mut triangles = Triangles::default(); - triangles.add_rect_with_uv(zoom_rect, uv_rect, Color32::WHITE); + triangles.add_rect_with_uv( + rect, + [pos2(0.0, 0.0), pos2(1.0, 1.0)].into(), + Color32::WHITE, + ); ui.painter().add(Shape::triangles(triangles)); - }); + + let (tex_w, tex_h) = (self.width as f32, self.height as f32); + + response.on_hover_ui(|ui| { + let pos = ui + .input() + .mouse + .pos + .unwrap_or_else(|| ui.min_rect().left_top()); + let (_id, zoom_rect) = ui.allocate_space(vec2(128.0, 128.0)); + let u = remap_clamp(pos.x, rect.x_range(), 0.0..=tex_w); + let v = remap_clamp(pos.y, rect.y_range(), 0.0..=tex_h); + + let texel_radius = 32.0; + let u = u.at_least(texel_radius).at_most(tex_w - texel_radius); + let v = v.at_least(texel_radius).at_most(tex_h - texel_radius); + + let uv_rect = Rect::from_min_max( + pos2((u - texel_radius) / tex_w, (v - texel_radius) / tex_h), + pos2((u + texel_radius) / tex_w, (v + texel_radius) / tex_h), + ); + let mut triangles = Triangles::default(); + triangles.add_rect_with_uv(zoom_rect, uv_rect, Color32::WHITE); + ui.painter().add(Shape::triangles(triangles)); + }); + }) + .1 } } -impl paint::text::FontDefinitions { - pub fn ui(&mut self, ui: &mut Ui) { - for (text_style, (_family, size)) in self.family_and_size.iter_mut() { - // TODO: radio button for family - ui.add( - Slider::f32(size, 4.0..=40.0) - .max_decimals(0) - .text(format!("{:?}", text_style)), - ); - } - crate::reset_button(ui, self); +impl Widget for &mut epaint::text::FontDefinitions { + fn ui(self, ui: &mut Ui) -> Response { + ui.vertical(|ui| { + for (text_style, (_family, size)) in self.family_and_size.iter_mut() { + // TODO: radio button for family + ui.add( + Slider::f32(size, 4.0..=40.0) + .max_decimals(0) + .text(format!("{:?}", text_style)), + ); + } + crate::reset_button(ui, self); + }) + .1 + } +} + +impl Widget for &epaint::stats::PaintStats { + fn ui(self, ui: &mut Ui) -> Response { + ui.vertical(|ui| { + ui.label( + "Egui generates intermediate level shapes like circles and text. \ + These are later tessellated into triangles.", + ); + ui.advance_cursor(10.0); + + ui.style_mut().body_text_style = TextStyle::Monospace; + + let epaint::stats::PaintStats { + shapes, + shape_text, + shape_path, + shape_mesh, + shape_vec, + jobs, + vertices, + indices, + } = self; + + ui.label("Intermediate:"); + label(ui, shapes, "shapes").on_hover_text("Boxes, circles, etc"); + label(ui, shape_text, "text"); + label(ui, shape_path, "paths"); + label(ui, shape_mesh, "meshes"); + label(ui, shape_vec, "nested"); + ui.advance_cursor(10.0); + + ui.label("Tessellated:"); + label(ui, jobs, "jobs").on_hover_text("Number of separate clip rectangles"); + label(ui, vertices, "vertices"); + label(ui, indices, "indices").on_hover_text("Three 32-bit indices per triangles"); + ui.advance_cursor(10.0); + + // ui.label("Total:"); + // ui.label(self.total().format("")); + }) + .1 + } +} + +pub fn label(ui: &mut Ui, alloc_info: &epaint::stats::AllocInfo, what: &str) -> Response { + ui.add(Label::new(alloc_info.format(what)).multiline(false)) +} + +impl Widget for &mut epaint::TessellationOptions { + fn ui(self, ui: &mut Ui) -> Response { + ui.vertical(|ui| { + let epaint::TessellationOptions { + aa_size: _, + anti_alias, + coarse_tessellation_culling, + debug_paint_clip_rects, + debug_paint_text_rects, + debug_ignore_clip_rects, + } = self; + ui.checkbox(anti_alias, "Antialias"); + ui.checkbox( + coarse_tessellation_culling, + "Do coarse culling in the tessellator", + ); + ui.checkbox(debug_ignore_clip_rects, "Ignore clip rectangles (debug)"); + ui.checkbox(debug_paint_clip_rects, "Paint clip rectangles (debug)"); + ui.checkbox(debug_paint_text_rects, "Paint text bounds (debug)"); + }) + .1 } } diff --git a/egui/src/layers.rs b/egui/src/layers.rs index 41545a97..b95e4cb8 100644 --- a/egui/src/layers.rs +++ b/egui/src/layers.rs @@ -1,4 +1,4 @@ -use ahash::AHashMap; +use epaint::ahash::AHashMap; use crate::{math::Rect, paint::Shape, Id, *}; diff --git a/egui/src/lib.rs b/egui/src/lib.rs index 3e5ac43b..eebb1c3b 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -88,7 +88,6 @@ mod layers; mod layout; mod memory; pub mod menu; -pub mod paint; mod painter; pub mod style; mod types; @@ -97,6 +96,17 @@ pub mod util; pub mod widgets; pub use emath as math; +pub use epaint as paint; +pub use epaint::emath; + +pub use emath::{ + clamp, lerp, pos2, remap, remap_clamp, vec2, Align, Align2, NumExt, Pos2, Rect, Vec2, +}; +pub use epaint::{ + color, mutex, + text::{FontDefinitions, FontFamily, TextStyle}, + Color32, PaintJobs, Rgba, Shape, Stroke, Texture, TextureId, +}; pub use { containers::*, @@ -105,18 +115,11 @@ pub use { input::*, layers::*, layout::*, - math::{clamp, lerp, pos2, remap, remap_clamp, vec2, Align, Align2, NumExt, Pos2, Rect, Vec2}, memory::Memory, - paint::{ - color, - text::{FontDefinitions, FontFamily, TextStyle}, - Color32, PaintJobs, Rgba, Shape, Stroke, Texture, TextureId, - }, painter::Painter, style::Style, types::*, ui::Ui, - util::mutex, widgets::*, }; diff --git a/egui/src/memory.rs b/egui/src/memory.rs index 2c196fa4..dea1441f 100644 --- a/egui/src/memory.rs +++ b/egui/src/memory.rs @@ -178,7 +178,7 @@ impl Memory { } } - pub(crate) fn end_frame(&mut self, used_ids: &ahash::AHashMap) { + pub(crate) fn end_frame(&mut self, used_ids: &epaint::ahash::AHashMap) { self.areas.end_frame(); if let Some(kb_focus_id) = self.interaction.kb_focus_id { diff --git a/egui/src/style.rs b/egui/src/style.rs index 26c47f8b..327e29ad 100644 --- a/egui/src/style.rs +++ b/egui/src/style.rs @@ -448,7 +448,7 @@ impl Selection { let Self { bg_fill, stroke } = self; ui_color(ui, bg_fill, "bg_fill"); - stroke.ui(ui, "stroke"); + stroke_ui(ui, stroke, "stroke"); } } @@ -463,10 +463,10 @@ impl WidgetVisuals { } = self; ui_color(ui, bg_fill, "bg_fill"); - bg_stroke.ui(ui, "bg_stroke"); + stroke_ui(ui, bg_stroke, "bg_stroke"); ui.add(Slider::f32(corner_radius, 0.0..=10.0).text("corner_radius")); ui_color(ui, fg_fill, "fg_fill"); - fg_stroke.ui(ui, "fg_stroke (text)"); + stroke_ui(ui, fg_stroke, "fg_stroke (text)"); } } @@ -495,7 +495,7 @@ impl Visuals { ui_color(ui, dark_bg_color, "dark_bg_color"); ui_color(ui, hyperlink_color, "hyperlink_color"); ui.add(Slider::f32(window_corner_radius, 0.0..=20.0).text("window_corner_radius")); - window_shadow.ui(ui, "Window shadow:"); + shadow_ui(ui, window_shadow, "Window shadow:"); ui.add(Slider::f32(resize_corner_size, 0.0..=20.0).text("resize_corner_size")); ui.add(Slider::f32(text_cursor_width, 0.0..=2.0).text("text_cursor_width")); ui.add(Slider::f32(clip_rect_margin, 0.0..=20.0).text("clip_rect_margin")); @@ -513,36 +513,6 @@ impl Visuals { } } -impl Stroke { - pub fn ui(&mut self, ui: &mut crate::Ui, text: &str) { - let Self { width, color } = self; - ui.horizontal(|ui| { - ui.add(DragValue::f32(width).speed(0.1).range(0.0..=5.0)) - .on_hover_text("Width"); - ui.color_edit_button_srgba(color); - ui.label(text); - - // stroke preview: - let (_id, stroke_rect) = ui.allocate_space(ui.style().spacing.interact_size); - let left = stroke_rect.left_center(); - let right = stroke_rect.right_center(); - ui.painter().line_segment([left, right], (*width, *color)); - }); - } -} - -impl Shadow { - pub fn ui(&mut self, ui: &mut crate::Ui, text: &str) { - let Self { extrusion, color } = self; - ui.horizontal(|ui| { - ui.label(text); - ui.add(DragValue::f32(extrusion).speed(1.0).range(0.0..=100.0)) - .on_hover_text("Extrusion"); - ui.color_edit_button_srgba(color); - }); - } -} - // TODO: improve and standardize ui_slider_vec2 fn ui_slider_vec2( ui: &mut Ui, diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 3308be34..8a602001 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -3,13 +3,7 @@ use std::{hash::Hash, sync::Arc}; use crate::{ - color::*, - containers::*, - layout::*, - mutex::MutexGuard, - paint::{text::Fonts, *}, - widgets::*, - *, + color::*, containers::*, layout::*, mutex::MutexGuard, paint::text::Fonts, widgets::*, *, }; /// This is what you use to place widgets. @@ -874,9 +868,9 @@ impl Ui { /// If the user clicks the button, a full color picker is shown. /// The given color is in `sRGBA` space with premultiplied alpha pub fn color_edit_button_srgba_premultiplied(&mut self, srgba: &mut [u8; 4]) -> Response { - let mut color = Color32(*srgba); + let mut color = Color32::from_rgba_premultiplied(srgba[0], srgba[1], srgba[2], srgba[3]); let response = self.color_edit_button_srgba(&mut color); - *srgba = color.0; + *srgba = color.to_array(); response } diff --git a/egui/src/util/mod.rs b/egui/src/util/mod.rs index 84862cf2..4c4ce33c 100644 --- a/egui/src/util/mod.rs +++ b/egui/src/util/mod.rs @@ -2,7 +2,6 @@ pub(crate) mod cache; mod history; -pub mod mutex; pub mod undoer; pub(crate) use cache::Cache; diff --git a/egui/src/widgets/drag_value.rs b/egui/src/widgets/drag_value.rs index 2e5e9ebf..0a369bf2 100644 --- a/egui/src/widgets/drag_value.rs +++ b/egui/src/widgets/drag_value.rs @@ -2,7 +2,7 @@ use std::ops::RangeInclusive; -use crate::{paint::*, *}; +use crate::*; /// Combined into one function (rather than two) to make it easier /// for the borrow checker. diff --git a/egui/src/widgets/mod.rs b/egui/src/widgets/mod.rs index 6b71e5b1..ef9c8484 100644 --- a/egui/src/widgets/mod.rs +++ b/egui/src/widgets/mod.rs @@ -44,3 +44,31 @@ pub fn reset_button(ui: &mut Ui, value: &mut T) { *value = def; } } + +// ---------------------------------------------------------------------------- + +pub fn stroke_ui(ui: &mut crate::Ui, stroke: &mut epaint::Stroke, text: &str) { + let epaint::Stroke { width, color } = stroke; + ui.horizontal(|ui| { + ui.add(DragValue::f32(width).speed(0.1).range(0.0..=5.0)) + .on_hover_text("Width"); + ui.color_edit_button_srgba(color); + ui.label(text); + + // stroke preview: + let (_id, stroke_rect) = ui.allocate_space(ui.style().spacing.interact_size); + let left = stroke_rect.left_center(); + let right = stroke_rect.right_center(); + ui.painter().line_segment([left, right], (*width, *color)); + }); +} + +pub(crate) fn shadow_ui(ui: &mut Ui, shadow: &mut epaint::Shadow, text: &str) { + let epaint::Shadow { extrusion, color } = shadow; + ui.horizontal(|ui| { + ui.label(text); + ui.add(DragValue::f32(extrusion).speed(1.0).range(0.0..=100.0)) + .on_hover_text("Extrusion"); + ui.color_edit_button_srgba(color); + }); +} diff --git a/egui/src/widgets/slider.rs b/egui/src/widgets/slider.rs index 432f7697..4d84bb7c 100644 --- a/egui/src/widgets/slider.rs +++ b/egui/src/widgets/slider.rs @@ -2,7 +2,7 @@ use std::ops::RangeInclusive; -use crate::{paint::*, widgets::Label, *}; +use crate::{widgets::Label, *}; // ---------------------------------------------------------------------------- diff --git a/egui_demo_lib/src/apps/demo/painting.rs b/egui_demo_lib/src/apps/demo/painting.rs index 73a3fa9e..2e020050 100644 --- a/egui_demo_lib/src/apps/demo/painting.rs +++ b/egui_demo_lib/src/apps/demo/painting.rs @@ -19,7 +19,7 @@ impl Default for Painting { impl Painting { pub fn ui_control(&mut self, ui: &mut Ui) { ui.horizontal(|ui| { - self.stroke.ui(ui, "Stroke"); + egui::stroke_ui(ui, &mut self.stroke, "Stroke"); ui.separator(); if ui.button("Clear Painting").clicked { self.lines.clear(); diff --git a/epaint/Cargo.toml b/epaint/Cargo.toml new file mode 100644 index 00000000..edc31fad --- /dev/null +++ b/epaint/Cargo.toml @@ -0,0 +1,41 @@ +[package] +name = "epaint" +version = "0.7.0" +authors = ["Emil Ernerfeldt "] +description = "Minimal 2D graphics library for GUI work" +edition = "2018" +homepage = "https://github.com/emilk/egui" +license = "MIT OR Apache-2.0" +readme = "README.md" +repository = "https://github.com/emilk/egui" +categories = ["gui", "graphics"] +keywords = ["gui", "graphics"] +include = [ + "**/*.rs", + "Cargo.toml", + "fonts/*.ttf", +] + +[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 epaint in a single thread. About as fast as parking_lot. Panics on multi-threaded use. +parking_lot = { version = "0.11", optional = true } # Using parking_lot over std::sync::Mutex gives 50% speedups in some real-world scenarios. +rusttype = "0.9" +serde = { version = "1", features = ["derive"], optional = true } + +[features] +default = ["multi_threaded", "default_fonts"] +persistence = ["serde", "emath/serde"] + +# If set, epaint will use `include_bytes!` to bundle some fonts. +# If you plan on specifying your own fonts you may disable this feature. +default_fonts = [] + +single_threaded = ["atomic_refcell"] + +# Only needed if you plan to use the same fonts from multiple threads. +multi_threaded = ["parking_lot"] diff --git a/epaint/README.md b/epaint/README.md new file mode 100644 index 00000000..29c15c4e --- /dev/null +++ b/epaint/README.md @@ -0,0 +1,5 @@ +# epaint - Egui Paint Library + +A bare-bones 2D graphics library for turning simple 2D shapes and text into textured triangles. + +Made for [`egui`](https://github.com/emilk/egui/). diff --git a/egui/fonts/NotoEmoji-Regular.ttf b/epaint/fonts/NotoEmoji-Regular.ttf similarity index 100% rename from egui/fonts/NotoEmoji-Regular.ttf rename to epaint/fonts/NotoEmoji-Regular.ttf diff --git a/egui/fonts/OFL.txt b/epaint/fonts/OFL.txt similarity index 100% rename from egui/fonts/OFL.txt rename to epaint/fonts/OFL.txt diff --git a/egui/fonts/ProggyClean.ttf b/epaint/fonts/ProggyClean.ttf similarity index 100% rename from egui/fonts/ProggyClean.ttf rename to epaint/fonts/ProggyClean.ttf diff --git a/egui/fonts/UFL.txt b/epaint/fonts/UFL.txt similarity index 100% rename from egui/fonts/UFL.txt rename to epaint/fonts/UFL.txt diff --git a/egui/fonts/Ubuntu-Light.ttf b/epaint/fonts/Ubuntu-Light.ttf similarity index 100% rename from egui/fonts/Ubuntu-Light.ttf rename to epaint/fonts/Ubuntu-Light.ttf diff --git a/egui/fonts/emoji-icon-font-mit-license.txt b/epaint/fonts/emoji-icon-font-mit-license.txt similarity index 100% rename from egui/fonts/emoji-icon-font-mit-license.txt rename to epaint/fonts/emoji-icon-font-mit-license.txt diff --git a/egui/fonts/emoji-icon-font.ttf b/epaint/fonts/emoji-icon-font.ttf similarity index 100% rename from egui/fonts/emoji-icon-font.ttf rename to epaint/fonts/emoji-icon-font.ttf diff --git a/egui/fonts/list_fonts.py b/epaint/fonts/list_fonts.py similarity index 100% rename from egui/fonts/list_fonts.py rename to epaint/fonts/list_fonts.py diff --git a/egui/src/paint/color.rs b/epaint/src/color.rs similarity index 99% rename from egui/src/paint/color.rs rename to epaint/src/color.rs index 8aaaea74..6976fcb6 100644 --- a/egui/src/paint/color.rs +++ b/epaint/src/color.rs @@ -1,4 +1,4 @@ -use crate::math::clamp; +use emath::clamp; /// This format is used for space-efficient color representation (32 bits). /// diff --git a/egui/src/paint/mod.rs b/epaint/src/lib.rs similarity index 51% rename from egui/src/paint/mod.rs rename to epaint/src/lib.rs index e9b49a7a..058ed089 100644 --- a/egui/src/paint/mod.rs +++ b/epaint/src/lib.rs @@ -1,6 +1,51 @@ //! 2D graphics/rendering. Fonts, textures, color, geometry, tessellation etc. +#![cfg_attr(not(debug_assertions), deny(warnings))] // Forbid warnings in release builds +#![forbid(unsafe_code)] +#![warn( + clippy::all, + clippy::await_holding_lock, + clippy::dbg_macro, + clippy::doc_markdown, + clippy::empty_enum, + clippy::enum_glob_use, + clippy::exit, + clippy::filter_map_next, + clippy::fn_params_excessive_bools, + clippy::if_let_mutex, + clippy::imprecise_flops, + clippy::inefficient_to_string, + clippy::linkedlist, + clippy::lossy_float_literal, + clippy::macro_use_imports, + clippy::match_on_vec_items, + clippy::match_wildcard_for_single_variants, + clippy::mem_forget, + clippy::mismatched_target_os, + clippy::missing_errors_doc, + clippy::missing_safety_doc, + clippy::needless_borrow, + clippy::needless_continue, + clippy::needless_pass_by_value, + clippy::option_option, + clippy::pub_enum_variant_names, + clippy::rest_pat_in_fully_bound_structs, + clippy::todo, + clippy::unimplemented, + clippy::unnested_or_patterns, + clippy::verbose_file_reads, + future_incompatible, + missing_crate_level_docs, + missing_doc_code_examples, + // missing_docs, + nonstandard_style, + rust_2018_idioms, + unused_doc_comments, +)] +#![allow(clippy::manual_range_contains)] + pub mod color; +pub mod mutex; mod shadow; pub mod shape; pub mod stats; @@ -22,11 +67,14 @@ pub use { triangles::{Triangles, Vertex}, }; +pub use ahash; +pub use emath; + /// The UV coordinate of a white region of the texture mesh. /// The default Egui texture has the top-left corner pixel fully white. /// You need need use a clamping texture sampler for this to work /// (so it doesn't do bilinear blending with bottom right corner). -pub const WHITE_UV: crate::Pos2 = crate::pos2(0.0, 0.0); +pub const WHITE_UV: emath::Pos2 = emath::pos2(0.0, 0.0); /// What texture to use in a [`Triangles`] mesh. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -47,7 +95,7 @@ impl Default for TextureId { } pub(crate) struct PaintRect { - pub rect: crate::Rect, + pub rect: emath::Rect, /// How rounded the corners are. Use `0.0` for no rounding. pub corner_radius: f32, pub fill: Color32, diff --git a/egui/src/util/mutex.rs b/epaint/src/mutex.rs similarity index 95% rename from egui/src/util/mutex.rs rename to epaint/src/mutex.rs index e8147917..c189284c 100644 --- a/egui/src/util/mutex.rs +++ b/epaint/src/mutex.rs @@ -1,6 +1,6 @@ //! Helper module that wraps some Mutex types with different implementations. -//! By default, Egui Mutexes will panic when used in a multi-threaded environment. -//! To use the same [`crate::Context`] from different threads, enable the `multi_threaded` feature. +//! +//! When the `single_threaded` feature is on the mutexes will panic when locked from different threads. // ---------------------------------------------------------------------------- diff --git a/egui/src/paint/shadow.rs b/epaint/src/shadow.rs similarity index 92% rename from egui/src/paint/shadow.rs rename to epaint/src/shadow.rs index 436bcb48..52dd10e1 100644 --- a/egui/src/paint/shadow.rs +++ b/epaint/src/shadow.rs @@ -25,12 +25,12 @@ impl Shadow { } } - pub fn tessellate(&self, rect: crate::Rect, corner_radius: f32) -> Triangles { + pub fn tessellate(&self, rect: emath::Rect, corner_radius: f32) -> Triangles { // tessellator.clip_rect = clip_rect; // TODO: culling let Self { extrusion, color } = *self; - use crate::paint::tessellator::*; + use crate::tessellator::*; let rect = PaintRect { rect: rect.expand(0.5 * extrusion), corner_radius: corner_radius + 0.5 * extrusion, diff --git a/egui/src/paint/shape.rs b/epaint/src/shape.rs similarity index 97% rename from egui/src/paint/shape.rs rename to epaint/src/shape.rs index 040d1ca0..300f5c3d 100644 --- a/egui/src/paint/shape.rs +++ b/epaint/src/shape.rs @@ -1,10 +1,8 @@ -use { - super::{ - text::{Fonts, Galley, TextStyle}, - Color32, Triangles, - }, - crate::*, +use crate::{ + text::{Fonts, Galley, TextStyle}, + Color32, Stroke, Triangles, }; +use emath::*; /// A paint primitive such as a circle or a piece of text. /// Coordinates are all screen space points (not physical pixels). diff --git a/egui/src/paint/stats.rs b/epaint/src/stats.rs similarity index 77% rename from egui/src/paint/stats.rs rename to epaint/src/stats.rs index 4f974078..aa94b083 100644 --- a/egui/src/paint/stats.rs +++ b/epaint/src/stats.rs @@ -1,4 +1,4 @@ -use crate::{paint::*, Rect}; +use {crate::*, emath::*}; #[derive(Clone, Copy, PartialEq)] enum ElementSize { @@ -129,24 +129,20 @@ impl AllocInfo { ) } } - - pub fn label(&self, ui: &mut crate::Ui, what: &str) -> crate::Response { - ui.add(crate::Label::new(self.format(what)).multiline(false)) - } } #[derive(Clone, Copy, Default)] pub struct PaintStats { - shapes: AllocInfo, - shape_text: AllocInfo, - shape_path: AllocInfo, - shape_mesh: AllocInfo, - shape_vec: AllocInfo, + pub shapes: AllocInfo, + pub shape_text: AllocInfo, + pub shape_path: AllocInfo, + pub shape_mesh: AllocInfo, + pub shape_vec: AllocInfo, /// Number of separate clip rectangles - jobs: AllocInfo, - vertices: AllocInfo, - indices: AllocInfo, + pub jobs: AllocInfo, + pub vertices: AllocInfo, + pub indices: AllocInfo, } impl PaintStats { @@ -187,7 +183,7 @@ impl PaintStats { } } - pub fn with_paint_jobs(mut self, paint_jobs: &[crate::paint::PaintJob]) -> Self { + pub fn with_paint_jobs(mut self, paint_jobs: &[crate::PaintJob]) -> Self { self.jobs += AllocInfo::from_slice(paint_jobs); for (_, indices) in paint_jobs { self.vertices += AllocInfo::from_slice(&indices.vertices); @@ -207,51 +203,6 @@ impl PaintStats { // } } -impl PaintStats { - pub fn ui(&self, ui: &mut crate::Ui) { - ui.label( - "Egui generates intermediate level shapes like circles and text. \ - These are later tessellated into triangles.", - ); - ui.advance_cursor(10.0); - - ui.style_mut().body_text_style = TextStyle::Monospace; - - let Self { - shapes, - shape_text, - shape_path, - shape_mesh, - shape_vec, - jobs, - vertices, - indices, - } = self; - - ui.label("Intermediate:"); - shapes - .label(ui, "shapes") - .on_hover_text("Boxes, circles, etc"); - shape_text.label(ui, "text"); - shape_path.label(ui, "paths"); - shape_mesh.label(ui, "meshes"); - shape_vec.label(ui, "nested"); - ui.advance_cursor(10.0); - - ui.label("Tessellated:"); - jobs.label(ui, "jobs") - .on_hover_text("Number of separate clip rectangles"); - vertices.label(ui, "vertices"); - indices - .label(ui, "indices") - .on_hover_text("Three 32-bit indices per triangles"); - ui.advance_cursor(10.0); - - // ui.label("Total:"); - // ui.label(self.total().format("")); - } -} - fn megabytes(size: usize) -> String { format!("{:.2} MB", size as f64 / 1e6) } diff --git a/egui/src/paint/stroke.rs b/epaint/src/stroke.rs similarity index 100% rename from egui/src/paint/stroke.rs rename to epaint/src/stroke.rs diff --git a/egui/src/paint/tessellator.rs b/epaint/src/tessellator.rs similarity index 99% rename from egui/src/paint/tessellator.rs rename to epaint/src/tessellator.rs index 76f6897e..4e7d152b 100644 --- a/egui/src/paint/tessellator.rs +++ b/epaint/src/tessellator.rs @@ -5,11 +5,9 @@ #![allow(clippy::identity_op)] -use { - super::{text::Fonts, *}, - crate::math::*, - std::f32::consts::TAU, -}; +use crate::{text::Fonts, *}; +use emath::*; +use std::f32::consts::TAU; /// A clip triangle and some textured triangles. pub type PaintJob = (Rect, Triangles); @@ -133,7 +131,7 @@ impl Path { } } -pub(crate) mod path { +pub mod path { //! Helpers for constructing paths use super::*; diff --git a/egui/src/paint/text/cursor.rs b/epaint/src/text/cursor.rs similarity index 100% rename from egui/src/paint/text/cursor.rs rename to epaint/src/text/cursor.rs diff --git a/egui/src/paint/text/font.rs b/epaint/src/text/font.rs similarity index 99% rename from egui/src/paint/text/font.rs rename to epaint/src/text/font.rs index b0fefbaa..841f9eed 100644 --- a/egui/src/paint/text/font.rs +++ b/epaint/src/text/font.rs @@ -6,13 +6,11 @@ use { }; use crate::{ - math::{vec2, Vec2}, mutex::{Mutex, RwLock}, - paint::{ - text::galley::{Galley, Row}, - TextureAtlas, - }, + text::galley::{Galley, Row}, + TextureAtlas, }; +use emath::{vec2, Vec2}; // ---------------------------------------------------------------------------- diff --git a/egui/src/paint/text/fonts.rs b/epaint/src/text/fonts.rs similarity index 98% rename from egui/src/paint/text/fonts.rs rename to epaint/src/text/fonts.rs index e99a7daf..7b147324 100644 --- a/egui/src/paint/text/fonts.rs +++ b/epaint/src/text/fonts.rs @@ -4,9 +4,11 @@ use std::{ sync::Arc, }; -use super::font::{Font, FontImpl}; -use crate::mutex::Mutex; -use crate::paint::{Texture, TextureAtlas}; +use crate::{ + mutex::Mutex, + text::font::{Font, FontImpl}, + Texture, TextureAtlas, +}; // TODO: rename /// One of a few categories of styles of text, e.g. body, button or heading. @@ -68,7 +70,7 @@ fn rusttype_font_from_font_data(name: &str, data: &FontData) -> rusttype::Font<' /// /// Often you would start with [`FontDefinitions::default()`] and then add/change the contents. /// -/// ``` +/// ``` ignore /// # let mut ctx = egui::CtxRef::default(); /// let mut fonts = egui::FontDefinitions::default(); /// // Large button text: @@ -115,22 +117,22 @@ impl Default for FontDefinitions { // Use size 13 for this. NOTHING ELSE: font_data.insert( "ProggyClean".to_owned(), - std::borrow::Cow::Borrowed(include_bytes!("../../../fonts/ProggyClean.ttf")), + std::borrow::Cow::Borrowed(include_bytes!("../../fonts/ProggyClean.ttf")), ); font_data.insert( "Ubuntu-Light".to_owned(), - std::borrow::Cow::Borrowed(include_bytes!("../../../fonts/Ubuntu-Light.ttf")), + std::borrow::Cow::Borrowed(include_bytes!("../../fonts/Ubuntu-Light.ttf")), ); // Some good looking emojis. Use as first priority: font_data.insert( "NotoEmoji-Regular".to_owned(), - std::borrow::Cow::Borrowed(include_bytes!("../../../fonts/NotoEmoji-Regular.ttf")), + std::borrow::Cow::Borrowed(include_bytes!("../../fonts/NotoEmoji-Regular.ttf")), ); // Bigger emojis, and more. : font_data.insert( "emoji-icon-font".to_owned(), - std::borrow::Cow::Borrowed(include_bytes!("../../../fonts/emoji-icon-font.ttf")), + std::borrow::Cow::Borrowed(include_bytes!("../../fonts/emoji-icon-font.ttf")), ); fonts_for_family.insert( diff --git a/egui/src/paint/text/galley.rs b/epaint/src/text/galley.rs similarity index 99% rename from egui/src/paint/text/galley.rs rename to epaint/src/text/galley.rs index 296ac008..0eb729db 100644 --- a/egui/src/paint/text/galley.rs +++ b/epaint/src/text/galley.rs @@ -20,7 +20,7 @@ //! [`CCursor::prefer_next_row`] etc selects which. use super::cursor::*; -use crate::math::{pos2, NumExt, Rect, Vec2}; +use emath::{pos2, NumExt, Rect, Vec2}; /// A collection of text locked into place. #[derive(Clone, Debug, Default)] @@ -551,7 +551,7 @@ fn test_text_layout() { } } - use crate::paint::*; + use crate::*; let pixels_per_point = 1.0; let fonts = text::Fonts::from_definitions(pixels_per_point, text::FontDefinitions::default()); diff --git a/egui/src/paint/text/mod.rs b/epaint/src/text/mod.rs similarity index 100% rename from egui/src/paint/text/mod.rs rename to epaint/src/text/mod.rs diff --git a/egui/src/paint/texture_atlas.rs b/epaint/src/texture_atlas.rs similarity index 100% rename from egui/src/paint/texture_atlas.rs rename to epaint/src/texture_atlas.rs diff --git a/egui/src/paint/triangles.rs b/epaint/src/triangles.rs similarity index 99% rename from egui/src/paint/triangles.rs rename to epaint/src/triangles.rs index c824fb31..47ae2164 100644 --- a/egui/src/paint/triangles.rs +++ b/epaint/src/triangles.rs @@ -1,5 +1,5 @@ -use super::*; use crate::*; +use emath::*; /// The vertex type. ///