diff --git a/emigui/README.md b/emigui/README.md index a6e00051..37fcc354 100644 --- a/emigui/README.md +++ b/emigui/README.md @@ -88,10 +88,7 @@ Add extremely quick animations for some things, maybe 2-3 frames. For instance: * [ ] Solve which parts of Context are behind a mutex * [ ] All of Context behind one mutex? * [ } Break up Context into Input, State, Output ? -* [ ] Rename Region to something shorter? - * `region: &Region` `region.add(...)` :/ - * `gui: &Gui` `gui.add(...)` :) - * `ui: &Ui` `ui.add(...)` :) +* [x] Rename Region to Ui * [ ] Maybe find a shorter name for the library like `egui`? ### Global widget search diff --git a/emigui/src/containers.rs b/emigui/src/containers.rs index 9071f21a..05ffb243 100644 --- a/emigui/src/containers.rs +++ b/emigui/src/containers.rs @@ -12,10 +12,10 @@ pub use { // TODO // pub trait Container { -// fn show(self, region: &mut Region, add_contents: impl FnOnce(&mut Region)); +// fn show(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)); // } // pub trait Container { -// fn begin(&mut self, parent: &mut Region) -> Region; -// fn end(self, parent: &mut Region, content: Region); +// fn begin(&mut self, parent: &mut Ui) -> Ui; +// fn end(self, parent: &mut Ui, content: Ui); // } diff --git a/emigui/src/containers/collapsing_header.rs b/emigui/src/containers/collapsing_header.rs index 5da11cc4..68b8601e 100644 --- a/emigui/src/containers/collapsing_header.rs +++ b/emigui/src/containers/collapsing_header.rs @@ -8,7 +8,7 @@ pub(crate) struct State { #[serde(skip)] // Times are relative, and we don't want to continue animations anyway toggle_time: f64, - /// Height open the region when open. Used for animations + /// Height of the region when open. Used for animations open_height: Option, } @@ -44,9 +44,9 @@ impl CollapsingHeader { } impl CollapsingHeader { - pub fn show(self, region: &mut Region, add_contents: impl FnOnce(&mut Region)) -> GuiResponse { + pub fn show(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) -> GuiResponse { assert!( - region.direction() == Direction::Vertical, + ui.direction() == Direction::Vertical, "Horizontal collapsing is unimplemented" ); let Self { @@ -57,63 +57,63 @@ impl CollapsingHeader { // TODO: horizontal layout, with icon and text as labels. Inser background behind using Frame. let title = &label.text; // TODO: not this - let id = region.make_unique_id(title); - let text_pos = region.cursor() + vec2(region.style().indent, 0.0); - let (title, text_size) = label.layout(text_pos, region); + let id = ui.make_unique_id(title); + let text_pos = ui.cursor() + vec2(ui.style().indent, 0.0); + let (title, text_size) = label.layout(text_pos, ui); let text_max_x = text_pos.x + text_size.x; - let desired_width = region.available_width().max(text_max_x - region.cursor().x); + let desired_width = ui.available_width().max(text_max_x - ui.cursor().x); - let interact = region.reserve_space( + let interact = ui.reserve_space( vec2( desired_width, - text_size.y + 2.0 * region.style().button_padding.y, + text_size.y + 2.0 * ui.style().button_padding.y, ), Some(id), ); let text_pos = pos2(text_pos.x, interact.rect.center().y - text_size.y / 2.0); let mut state = { - let mut memory = region.memory(); + let mut memory = ui.memory(); let mut state = memory.collapsing_headers.entry(id).or_insert(State { open: default_open, ..Default::default() }); if interact.clicked { state.open = !state.open; - state.toggle_time = region.input().time; + state.toggle_time = ui.input().time; } *state }; - let where_to_put_background = region.paint_list_len(); + let where_to_put_background = ui.paint_list_len(); - paint_icon(region, &state, &interact); + paint_icon(ui, &state, &interact); - region.add_text( + ui.add_text( text_pos, label.text_style, title, - Some(region.style().interact_stroke_color(&interact)), + Some(ui.style().interact_stroke_color(&interact)), ); - region.insert_paint_cmd( + ui.insert_paint_cmd( where_to_put_background, PaintCmd::Rect { - corner_radius: region.style().interact_corner_radius(&interact), - fill_color: region.style().interact_fill_color(&interact), - outline: region.style().interact_outline(&interact), + corner_radius: ui.style().interact_corner_radius(&interact), + fill_color: ui.style().interact_fill_color(&interact), + outline: ui.style().interact_outline(&interact), rect: interact.rect, }, ); - region.expand_to_include_child(interact.rect); // TODO: remove, just a test + ui.expand_to_include_child(interact.rect); // TODO: remove, just a test - let animation_time = region.style().animation_time; - let time_since_toggle = (region.input().time - state.toggle_time) as f32; - let time_since_toggle = time_since_toggle + region.input().dt; // Instant feedback + let animation_time = ui.style().animation_time; + let time_since_toggle = (ui.input().time - state.toggle_time) as f32; + let time_since_toggle = time_since_toggle + ui.input().dt; // Instant feedback let animate = time_since_toggle < animation_time; if animate { - region.indent(id, |child_region| { + ui.indent(id, |child_ui| { let max_height = if state.open { let full_height = state.open_height.unwrap_or(1000.0); remap(time_since_toggle, 0.0..=animation_time, 0.0..=full_height) @@ -122,42 +122,42 @@ impl CollapsingHeader { remap_clamp(time_since_toggle, 0.0..=animation_time, full_height..=0.0) }; - let mut clip_rect = child_region.clip_rect(); - clip_rect.max.y = clip_rect.max.y.min(child_region.cursor().y + max_height); - child_region.set_clip_rect(clip_rect); + let mut clip_rect = child_ui.clip_rect(); + clip_rect.max.y = clip_rect.max.y.min(child_ui.cursor().y + max_height); + child_ui.set_clip_rect(clip_rect); - let top_left = child_region.top_left(); - add_contents(child_region); + let top_left = child_ui.top_left(); + add_contents(child_ui); - state.open_height = Some(child_region.bounding_size().y); + state.open_height = Some(child_ui.bounding_size().y); // Pretend children took up less space: - let mut child_bounds = child_region.child_bounds(); + let mut child_bounds = child_ui.child_bounds(); child_bounds.max.y = child_bounds.max.y.min(top_left.y + max_height); - child_region.force_set_child_bounds(child_bounds); + child_ui.force_set_child_bounds(child_bounds); }); } else if state.open { - let full_size = region.indent(id, add_contents).rect.size(); + let full_size = ui.indent(id, add_contents).rect.size(); state.open_height = Some(full_size.y); } - region.memory().collapsing_headers.insert(id, state); - region.response(interact) + ui.memory().collapsing_headers.insert(id, state); + ui.response(interact) } } -fn paint_icon(region: &mut Region, state: &State, interact: &InteractInfo) { - let stroke_color = region.style().interact_stroke_color(interact); - let stroke_width = region.style().interact_stroke_width(interact); +fn paint_icon(ui: &mut Ui, state: &State, interact: &InteractInfo) { + let stroke_color = ui.style().interact_stroke_color(interact); + let stroke_width = ui.style().interact_stroke_width(interact); - let (mut small_icon_rect, _) = region.style().icon_rectangles(interact.rect); + let (mut small_icon_rect, _) = ui.style().icon_rectangles(interact.rect); small_icon_rect.set_center(pos2( - interact.rect.left() + region.style().indent / 2.0, + interact.rect.left() + ui.style().indent / 2.0, interact.rect.center().y, )); // Draw a minus: - region.add_paint_cmd(PaintCmd::Line { + ui.add_paint_cmd(PaintCmd::Line { points: vec![ pos2(small_icon_rect.left(), small_icon_rect.center().y), pos2(small_icon_rect.right(), small_icon_rect.center().y), @@ -168,7 +168,7 @@ fn paint_icon(region: &mut Region, state: &State, interact: &InteractInfo) { if !state.open { // Draw it as a plus: - region.add_paint_cmd(PaintCmd::Line { + ui.add_paint_cmd(PaintCmd::Line { points: vec![ pos2(small_icon_rect.center().x, small_icon_rect.top()), pos2(small_icon_rect.center().x, small_icon_rect.bottom()), diff --git a/emigui/src/containers/floating.rs b/emigui/src/containers/floating.rs index b7dd7c5d..b2048800 100644 --- a/emigui/src/containers/floating.rs +++ b/emigui/src/containers/floating.rs @@ -1,4 +1,4 @@ -//! A Floating is a region that has no parent, it floats on the background. +//! A Floating is an Ui that has no parent, it floats on the background. //! It is potentioally movable. //! It has no frame or own size. //! It is the foundation for a window @@ -49,7 +49,7 @@ impl Floating { } impl Floating { - pub fn show(self, ctx: &Arc, add_contents: impl FnOnce(&mut Region)) { + pub fn show(self, ctx: &Arc, add_contents: impl FnOnce(&mut Ui)) { let default_pos = self.default_pos.unwrap_or_else(|| pos2(100.0, 100.0)); // TODO let id = ctx.register_unique_id(self.id, "Floating", default_pos); let layer = Layer::Window(id); @@ -67,14 +67,14 @@ impl Floating { }; state.pos = state.pos.round(); - let mut region = Region::new( + let mut ui = Ui::new( ctx.clone(), layer, id, Rect::from_min_size(state.pos, Vec2::infinity()), ); - add_contents(&mut region); - state.size = region.bounding_size().ceil(); + add_contents(&mut ui); + state.size = ui.bounding_size().ceil(); let rect = Rect::from_min_size(state.pos, state.size); let clip_rect = Rect::everything(); diff --git a/emigui/src/containers/frame.rs b/emigui/src/containers/frame.rs index 8725cb51..93ec5cc7 100644 --- a/emigui/src/containers/frame.rs +++ b/emigui/src/containers/frame.rs @@ -15,26 +15,26 @@ impl Frame { } impl Frame { - pub fn show(self, region: &mut Region, add_contents: impl FnOnce(&mut Region)) { - let style = region.style(); + pub fn show(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) { + let style = ui.style(); let margin = self.margin.unwrap_or_default(); - let outer_pos = region.cursor(); + let outer_pos = ui.cursor(); let inner_rect = - Rect::from_min_size(outer_pos + margin, region.available_space() - 2.0 * margin); - let where_to_put_background = region.paint_list_len(); + Rect::from_min_size(outer_pos + margin, ui.available_space() - 2.0 * margin); + let where_to_put_background = ui.paint_list_len(); - let mut child_region = region.child_region(inner_rect); - add_contents(&mut child_region); + let mut child_ui = ui.child_ui(inner_rect); + add_contents(&mut child_ui); - let inner_size = child_region.bounding_size(); + let inner_size = child_ui.bounding_size(); let inner_size = inner_size.ceil(); // TODO: round to pixel let outer_rect = Rect::from_min_size(outer_pos, margin + inner_size + margin); let corner_radius = style.window.corner_radius; let fill_color = style.background_fill_color(); - region.insert_paint_cmd( + ui.insert_paint_cmd( where_to_put_background, PaintCmd::Rect { corner_radius, @@ -44,7 +44,7 @@ impl Frame { }, ); - region.expand_to_include_child(child_region.child_bounds().expand2(margin)); + ui.expand_to_include_child(child_ui.child_bounds().expand2(margin)); // TODO: move up cursor? } } diff --git a/emigui/src/containers/resize.rs b/emigui/src/containers/resize.rs index 5d402de6..8348c7e2 100644 --- a/emigui/src/containers/resize.rs +++ b/emigui/src/containers/resize.rs @@ -13,7 +13,7 @@ pub struct Resize { /// If false, we are no enabled resizable: bool, - // Will still try to stay within parent region bounds + // Will still try to stay within parent ui bounds min_size: Vec2, max_size: Vec2, @@ -132,17 +132,17 @@ impl Resize { // TODO: a common trait for Things that follow this pattern impl Resize { - pub fn show(mut self, region: &mut Region, add_contents: impl FnOnce(&mut Region)) { + pub fn show(mut self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) { if !self.resizable { - return add_contents(region); + return add_contents(ui); } - let id = region.make_child_id("scroll"); - self.min_size = self.min_size.min(region.available_space()); - self.max_size = self.max_size.min(region.available_space()); + let id = ui.make_child_id("scroll"); + self.min_size = self.min_size.min(ui.available_space()); + self.max_size = self.max_size.min(ui.available_space()); self.max_size = self.max_size.max(self.min_size); - let (is_new, mut state) = match region.memory().resize.get(&id) { + let (is_new, mut state) = match ui.memory().resize.get(&id) { Some(state) => (false, *state), None => { let default_size = self.default_size.clamp(self.min_size..=self.max_size); @@ -153,7 +153,7 @@ impl Resize { state.size = state.size.clamp(self.min_size..=self.max_size); let last_frame_size = state.size; - let position = region.cursor(); + let position = ui.cursor(); // Resize-corner: let corner_size = Vec2::splat(16.0); // TODO: style @@ -161,10 +161,10 @@ impl Resize { position + state.size + self.handle_offset - corner_size, corner_size, ); - let corner_interact = region.interact_rect(corner_rect, id.with("corner")); + let corner_interact = ui.interact_rect(corner_rect, id.with("corner")); if corner_interact.active { - if let Some(mouse_pos) = region.input().mouse_pos { + if let Some(mouse_pos) = ui.input().mouse_pos { // This is the desired size. We may not be able to achieve it. state.size = @@ -178,11 +178,11 @@ impl Resize { // ------------------------------ - let inner_rect = Rect::from_min_size(region.cursor(), state.size); + let inner_rect = Rect::from_min_size(ui.cursor(), state.size); let desired_size = { - let mut content_clip_rect = region + let mut content_clip_rect = ui .clip_rect() - .intersect(inner_rect.expand(region.style().clip_rect_margin)); + .intersect(inner_rect.expand(ui.style().clip_rect_margin)); // If we pull the resize handle to shrink, we want to TRY to shink it. // After laying out the contents, we might be much bigger. @@ -192,12 +192,12 @@ impl Resize { content_clip_rect.max = content_clip_rect .max .max(content_clip_rect.min + last_frame_size) - .min(region.clip_rect().max); // Respect parent region + .min(ui.clip_rect().max); // Respect parent region - let mut contents_region = region.child_region(inner_rect); - contents_region.set_clip_rect(content_clip_rect); - add_contents(&mut contents_region); - contents_region.bounding_size() + let mut contents_ui = ui.child_ui(inner_rect); + contents_ui.set_clip_rect(content_clip_rect); + add_contents(&mut contents_ui); + contents_ui.bounding_size() }; let desired_size = desired_size.ceil(); // Avoid rounding errors in math @@ -220,29 +220,29 @@ impl Resize { // state.size = state.size.clamp(self.min_size..=self.max_size); state.size = state.size.round(); // TODO: round to pixels - region.reserve_space(state.size, None); + ui.reserve_space(state.size, None); // ------------------------------ - paint_resize_corner(region, &corner_interact); + paint_resize_corner(ui, &corner_interact); if corner_interact.hovered || corner_interact.active { - region.ctx().output().cursor_icon = CursorIcon::ResizeNwSe; + ui.ctx().output().cursor_icon = CursorIcon::ResizeNwSe; } - region.memory().resize.insert(id, state); + ui.memory().resize.insert(id, state); } } -fn paint_resize_corner(region: &mut Region, interact: &InteractInfo) { - let color = region.style().interact_stroke_color(interact); - let width = region.style().interact_stroke_width(interact); +fn paint_resize_corner(ui: &mut Ui, interact: &InteractInfo) { + let color = ui.style().interact_stroke_color(interact); + let width = ui.style().interact_stroke_width(interact); let corner = interact.rect.right_bottom().round(); // TODO: round to pixels let mut w = 2.0; while w < 12.0 { - region.add_paint_cmd(PaintCmd::line_segment( + ui.add_paint_cmd(PaintCmd::line_segment( (pos2(corner.x - w, corner.y), pos2(corner.x, corner.y - w)), color, width, diff --git a/emigui/src/containers/scroll_area.rs b/emigui/src/containers/scroll_area.rs index cce4dd06..ef443391 100644 --- a/emigui/src/containers/scroll_area.rs +++ b/emigui/src/containers/scroll_area.rs @@ -45,10 +45,10 @@ impl ScrollArea { } impl ScrollArea { - pub fn show(self, outer_region: &mut Region, add_contents: impl FnOnce(&mut Region)) { - let ctx = outer_region.ctx().clone(); + pub fn show(self, outer_ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) { + let ctx = outer_ui.ctx().clone(); - let scroll_area_id = outer_region.make_child_id("scroll_area"); + let scroll_area_id = outer_ui.make_child_id("scroll_area"); let mut state = ctx .memory() .scroll_areas @@ -69,23 +69,23 @@ impl ScrollArea { }; let outer_size = vec2( - outer_region.available_width(), - outer_region.available_height().min(self.max_height), + outer_ui.available_width(), + outer_ui.available_height().min(self.max_height), ); let inner_size = outer_size - vec2(current_scroll_bar_width, 0.0); - let inner_rect = Rect::from_min_size(outer_region.cursor(), inner_size); + let inner_rect = Rect::from_min_size(outer_ui.cursor(), inner_size); - let mut content_region = outer_region.child_region(Rect::from_min_size( - outer_region.cursor() - state.offset, + let mut content_ui = outer_ui.child_ui(Rect::from_min_size( + outer_ui.cursor() - state.offset, vec2(inner_size.x, f32::INFINITY), )); - let mut content_clip_rect = outer_region.clip_rect().intersect(inner_rect); - content_clip_rect.max.x = outer_region.clip_rect().max.x - current_scroll_bar_width; // Nice handling of forced resizing beyond the possible - content_region.set_clip_rect(content_clip_rect); + let mut content_clip_rect = outer_ui.clip_rect().intersect(inner_rect); + content_clip_rect.max.x = outer_ui.clip_rect().max.x - current_scroll_bar_width; // Nice handling of forced resizing beyond the possible + content_ui.set_clip_rect(content_clip_rect); - add_contents(&mut content_region); - let content_size = content_region.bounding_size(); + add_contents(&mut content_ui); + let content_size = content_ui.bounding_size(); let inner_rect = Rect::from_min_size( inner_rect.min, @@ -100,14 +100,14 @@ impl ScrollArea { inner_rect.size() + vec2(current_scroll_bar_width, 0.0), ); - let content_interact = outer_region.interact_rect(inner_rect, scroll_area_id.with("area")); + let content_interact = outer_ui.interact_rect(inner_rect, scroll_area_id.with("area")); if content_interact.active { // Dragging scroll area to scroll: state.offset.y -= ctx.input().mouse_move.y; } // TODO: check that nothing else is being inteacted with - if outer_region.contains_mouse(outer_rect) && ctx.memory().active_id.is_none() { + if outer_ui.contains_mouse(outer_rect) && ctx.memory().active_id.is_none() { state.offset.y -= ctx.input().scroll_delta.y; } @@ -134,7 +134,7 @@ impl ScrollArea { // intentionally use same id for inside and outside of handle let interact_id = scroll_area_id.with("vertical"); - let handle_interact = outer_region.interact_rect(handle_rect, interact_id); + let handle_interact = outer_ui.interact_rect(handle_rect, interact_id); if let Some(mouse_pos) = ctx.input().mouse_pos { if handle_interact.active { @@ -144,8 +144,7 @@ impl ScrollArea { } } else { // Check for mouse down outside handle: - let scroll_bg_interact = - outer_region.interact_rect(outer_scroll_rect, interact_id); + let scroll_bg_interact = outer_ui.interact_rect(outer_scroll_rect, interact_id); if scroll_bg_interact.active { // Center scroll at mouse pos: @@ -164,18 +163,18 @@ impl ScrollArea { pos2(right, from_content(state.offset.y + inner_rect.height())), ); - let style = outer_region.style(); + let style = outer_ui.style(); let handle_fill_color = style.interact_fill_color(&handle_interact); let handle_outline = style.interact_outline(&handle_interact); - outer_region.add_paint_cmd(PaintCmd::Rect { + outer_ui.add_paint_cmd(PaintCmd::Rect { rect: outer_scroll_rect, corner_radius, fill_color: Some(color::gray(0, 196)), // TODO style outline: None, }); - outer_region.add_paint_cmd(PaintCmd::Rect { + outer_ui.add_paint_cmd(PaintCmd::Rect { rect: handle_rect.expand(-2.0), corner_radius, fill_color: handle_fill_color, @@ -189,15 +188,12 @@ impl ScrollArea { // content_size.y.min(inner_rect.size().y), // respect vertical height. // ); let size = outer_rect.size(); - outer_region.reserve_space(size, None); + outer_ui.reserve_space(size, None); state.offset.y = state.offset.y.min(content_size.y - inner_rect.height()); state.offset.y = state.offset.y.max(0.0); state.show_scroll = show_scroll_this_frame; - outer_region - .memory() - .scroll_areas - .insert(scroll_area_id, state); + outer_ui.memory().scroll_areas.insert(scroll_area_id, state); } } diff --git a/emigui/src/containers/window.rs b/emigui/src/containers/window.rs index 9a88c779..58d3f7e9 100644 --- a/emigui/src/containers/window.rs +++ b/emigui/src/containers/window.rs @@ -85,7 +85,7 @@ impl Window { } impl Window { - pub fn show(self, ctx: &Arc, add_contents: impl FnOnce(&mut Region)) { + pub fn show(self, ctx: &Arc, add_contents: impl FnOnce(&mut Ui)) { let Window { title_label, floating, @@ -96,12 +96,12 @@ impl Window { frame.margin = Some(frame.margin.unwrap_or_else(|| ctx.style().window_padding)); // TODO: easier way to compose these - floating.show(ctx, |region| { - frame.show(region, |region| { - resize.show(region, |region| { - region.add(title_label); - region.add(Separator::new().line_width(1.0)); // TODO: nicer way to split window title from contents - scroll.show(region, |region| add_contents(region)) + floating.show(ctx, |ui| { + frame.show(ui, |ui| { + resize.show(ui, |ui| { + ui.add(title_label); + ui.add(Separator::new().line_width(1.0)); // TODO: nicer way to split window title from contents + scroll.show(ui, |ui| add_contents(ui)) }) }) }) diff --git a/emigui/src/context.rs b/emigui/src/context.rs index 6cfd1dfd..01dc2f87 100644 --- a/emigui/src/context.rs +++ b/emigui/src/context.rs @@ -13,11 +13,11 @@ struct PaintStats { } /// Contains the input, style and output of all GUI commands. -/// Regions keep an Arc pointer to this. -/// This allows us to create several child regions at once, +/// `Ui`:s keep an Arc pointer to this. +/// This allows us to create several child `Ui`:s at once, /// all working against the same shared Context. pub struct Context { - /// The default style for new regions + /// The default style for new `Ui`:s style: Mutex