From b8675ad67f6ce4e578d970f3e1827c50ff35139d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 13 May 2020 20:16:59 +0200 Subject: [PATCH] refactor: move some code around to make place for new Layout --- emigui/src/containers.rs | 3 +- emigui/src/containers/popup.rs | 27 ++++++++++++++ emigui/src/layout.rs | 67 +--------------------------------- emigui/src/lib.rs | 2 +- emigui/src/math.rs | 7 ++++ emigui/src/texture_atlas.rs | 4 +- emigui/src/types.rs | 51 ++++++++++++++++++++++++++ emigui/src/widgets.rs | 5 +-- 8 files changed, 93 insertions(+), 73 deletions(-) create mode 100644 emigui/src/containers/popup.rs diff --git a/emigui/src/containers.rs b/emigui/src/containers.rs index 6e685b93..cd9c5fab 100644 --- a/emigui/src/containers.rs +++ b/emigui/src/containers.rs @@ -2,12 +2,13 @@ pub mod area; pub mod collapsing_header; pub mod frame; pub mod menu; +pub mod popup; pub mod resize; pub mod scroll_area; pub mod window; pub use { - area::Area, collapsing_header::CollapsingHeader, frame::Frame, resize::Resize, + area::Area, collapsing_header::CollapsingHeader, frame::Frame, popup::*, resize::Resize, scroll_area::ScrollArea, window::Window, }; diff --git a/emigui/src/containers/popup.rs b/emigui/src/containers/popup.rs new file mode 100644 index 00000000..eaec9d90 --- /dev/null +++ b/emigui/src/containers/popup.rs @@ -0,0 +1,27 @@ +use std::sync::Arc; + +use crate::*; + +pub fn show_tooltip(ctx: &Arc, add_contents: impl FnOnce(&mut Ui)) { + if let Some(mouse_pos) = ctx.input().mouse_pos { + // TODO: default size + let id = Id::tooltip(); + let window_pos = mouse_pos + vec2(16.0, 16.0); + show_popup(ctx, id, window_pos, add_contents); + } +} + +/// Show a pop-over window +pub fn show_popup( + ctx: &Arc, + id: Id, + window_pos: Pos2, + add_contents: impl FnOnce(&mut Ui), +) -> InteractInfo { + use containers::*; + Area::new(id) + .order(Order::Foreground) + .fixed_pos(window_pos) + .interactable(false) + .show(ctx, |ui| Frame::popup(&ctx.style()).show(ui, add_contents)) +} diff --git a/emigui/src/layout.rs b/emigui/src/layout.rs index 04915d94..7ba26bf2 100644 --- a/emigui/src/layout.rs +++ b/emigui/src/layout.rs @@ -1,45 +1,6 @@ -use std::sync::Arc; - use serde_derive::{Deserialize, Serialize}; -use crate::{widgets::*, *}; - -// ---------------------------------------------------------------------------- - -// TODO: rename GuiResponse -pub struct GuiResponse { - /// The mouse is hovering above this - pub hovered: bool, - - /// The mouse clicked this thing this frame - pub clicked: bool, - - /// The mouse is interacting with this thing (e.g. dragging it) - pub active: bool, - - /// The area of the screen we are talking about - pub rect: Rect, - - /// Used for optionally showing a tooltip - pub ctx: Arc, -} - -impl GuiResponse { - /// Show some stuff if the item was hovered - pub fn tooltip(&mut self, add_contents: impl FnOnce(&mut Ui)) -> &mut Self { - if self.hovered { - show_tooltip(&self.ctx, add_contents); - } - self - } - - /// Show this text if the item was hovered - pub fn tooltip_text(&mut self, text: impl Into) -> &mut Self { - self.tooltip(|popup| { - popup.add(Label::new(text)); - }) - } -} +use crate::math::*; // ---------------------------------------------------------------------------- @@ -94,29 +55,3 @@ pub fn align_rect(rect: Rect, align: (Align, Align)) -> Rect { }; Rect::from_min_size(pos2(x, y), rect.size()) } - -// ---------------------------------------------------------------------------- - -pub fn show_tooltip(ctx: &Arc, add_contents: impl FnOnce(&mut Ui)) { - if let Some(mouse_pos) = ctx.input().mouse_pos { - // TODO: default size - let id = Id::tooltip(); - let window_pos = mouse_pos + vec2(16.0, 16.0); - show_popup(ctx, id, window_pos, add_contents); - } -} - -/// Show a pop-over window -pub fn show_popup( - ctx: &Arc, - id: Id, - window_pos: Pos2, - add_contents: impl FnOnce(&mut Ui), -) -> InteractInfo { - use containers::*; - Area::new(id) - .order(Order::Foreground) - .fixed_pos(window_pos) - .interactable(false) - .show(ctx, |ui| Frame::popup(&ctx.style()).show(ui, add_contents)) -} diff --git a/emigui/src/lib.rs b/emigui/src/lib.rs index fdd511b6..195f18ef 100644 --- a/emigui/src/lib.rs +++ b/emigui/src/lib.rs @@ -49,7 +49,7 @@ pub use { id::Id, input::*, layers::*, - layout::{Align, GuiResponse}, + layout::Align, math::*, memory::Memory, mesher::{Mesh, PaintBatches, Vertex}, diff --git a/emigui/src/math.rs b/emigui/src/math.rs index c587cd6b..4ef7a385 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -432,6 +432,13 @@ impl Rect { self.max = self.max.max(p); } + pub fn union(self, other: Rect) -> Rect { + Rect { + min: self.min.min(other.min), + max: self.max.max(other.max), + } + } + pub fn center(&self) -> Pos2 { Pos2 { x: self.min.x + self.size().x / 2.0, diff --git a/emigui/src/texture_atlas.rs b/emigui/src/texture_atlas.rs index 22e37bfa..14b7195c 100644 --- a/emigui/src/texture_atlas.rs +++ b/emigui/src/texture_atlas.rs @@ -92,7 +92,9 @@ impl TextureAtlas { impl Texture { pub fn ui(&self, ui: &mut crate::Ui) { - use crate::{color::WHITE, label, layout::show_tooltip, math::*, Mesh, PaintCmd, Vertex}; + use crate::{ + color::WHITE, containers::show_tooltip, label, math::*, Mesh, PaintCmd, Vertex, + }; ui.add(label!( "Texture size: {} x {} (hover to zoom)", diff --git a/emigui/src/types.rs b/emigui/src/types.rs index 34cf6ab9..43b869d9 100644 --- a/emigui/src/types.rs +++ b/emigui/src/types.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use serde_derive::{Deserialize, Serialize}; use crate::{ @@ -5,6 +7,7 @@ use crate::{ fonts::TextStyle, math::{Pos2, Rect}, mesher::{Mesh, Path}, + Context, Ui, }; // ---------------------------------------------------------------------------- @@ -53,6 +56,54 @@ pub struct InteractInfo { pub rect: Rect, } +impl InteractInfo { + pub fn union(self, other: Self) -> Self { + Self { + hovered: self.hovered || other.hovered, + clicked: self.clicked || other.clicked, + active: self.active || other.active, + rect: self.rect.union(other.rect), + } + } +} + +// ---------------------------------------------------------------------------- + +// TODO: rename GuiResponse +pub struct GuiResponse { + /// The mouse is hovering above this + pub hovered: bool, + + /// The mouse clicked this thing this frame + pub clicked: bool, + + /// The mouse is interacting with this thing (e.g. dragging it) + pub active: bool, + + /// The area of the screen we are talking about + pub rect: Rect, + + /// Used for optionally showing a tooltip + pub ctx: Arc, +} + +impl GuiResponse { + /// Show some stuff if the item was hovered + pub fn tooltip(&mut self, add_contents: impl FnOnce(&mut Ui)) -> &mut Self { + if self.hovered { + crate::containers::show_tooltip(&self.ctx, add_contents); + } + self + } + + /// Show this text if the item was hovered + pub fn tooltip_text(&mut self, text: impl Into) -> &mut Self { + self.tooltip(|popup| { + popup.add(crate::widgets::Label::new(text)); + }) + } +} + // ---------------------------------------------------------------------------- #[derive(Clone, Copy, Debug, Deserialize, Serialize)] diff --git a/emigui/src/widgets.rs b/emigui/src/widgets.rs index 1bd3bba4..4509c827 100644 --- a/emigui/src/widgets.rs +++ b/emigui/src/widgets.rs @@ -1,9 +1,6 @@ #![allow(clippy::new_without_default)] -use crate::{ - layout::{Direction, GuiResponse}, - *, -}; +use crate::{layout::Direction, GuiResponse, *}; mod slider; mod text_edit;