Browse Source

refactor: move some code around to make place for new Layout

readable-ids
Emil Ernerfeldt 5 years ago
parent
commit
b8675ad67f
  1. 3
      emigui/src/containers.rs
  2. 27
      emigui/src/containers/popup.rs
  3. 67
      emigui/src/layout.rs
  4. 2
      emigui/src/lib.rs
  5. 7
      emigui/src/math.rs
  6. 4
      emigui/src/texture_atlas.rs
  7. 51
      emigui/src/types.rs
  8. 5
      emigui/src/widgets.rs

3
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,
};

27
emigui/src/containers/popup.rs

@ -0,0 +1,27 @@
use std::sync::Arc;
use crate::*;
pub fn show_tooltip(ctx: &Arc<Context>, 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<Context>,
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))
}

67
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<Context>,
}
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<String>) -> &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<Context>, 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<Context>,
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))
}

2
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},

7
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,

4
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)",

51
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<Context>,
}
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<String>) -> &mut Self {
self.tooltip(|popup| {
popup.add(crate::widgets::Label::new(text));
})
}
}
// ----------------------------------------------------------------------------
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]

5
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;

Loading…
Cancel
Save