egui/emigui/src/layout.rs

127 lines
3.2 KiB
Rust
Raw Normal View History

2020-04-17 21:44:14 +00:00
use std::sync::Arc;
2018-12-27 18:08:43 +00:00
2020-04-17 13:29:48 +00:00
use crate::{widgets::*, *};
2018-12-26 09:46:23 +00:00
2018-12-26 22:08:50 +00:00
// ----------------------------------------------------------------------------
2018-12-26 09:46:23 +00:00
// TODO: rename GuiResponse
2019-01-07 07:54:37 +00:00
pub struct GuiResponse {
2018-12-28 22:53:15 +00:00
/// The mouse is hovering above this
pub hovered: bool,
/// The mouse went got pressed on this thing this frame
pub clicked: bool,
/// The mouse is interacting with this thing (e.g. dragging it)
pub active: bool,
2019-01-14 13:26:02 +00:00
/// The region of the screen we are talking about
pub rect: Rect,
2019-01-06 15:34:01 +00:00
/// Used for showing a popup (if any)
pub ctx: Arc<Context>,
2018-12-28 22:53:15 +00:00
}
2019-01-07 07:54:37 +00:00
impl GuiResponse {
2018-12-28 22:53:15 +00:00
/// Show some stuff if the item was hovered
2019-01-06 15:34:01 +00:00
pub fn tooltip<F>(&mut self, add_contents: F) -> &mut Self
2018-12-28 22:53:15 +00:00
where
2019-01-06 15:34:01 +00:00
F: FnOnce(&mut Region),
2018-12-28 22:53:15 +00:00
{
if self.hovered {
if let Some(mouse_pos) = self.ctx.input().mouse_pos {
let window_pos = mouse_pos + vec2(16.0, 16.0);
show_popup(&self.ctx, window_pos, add_contents);
}
2018-12-28 22:53:15 +00:00
}
self
}
/// Show this text if the item was hovered
2019-01-06 15:34:01 +00:00
pub fn tooltip_text<S: Into<String>>(&mut self, text: S) -> &mut Self {
2018-12-28 22:53:15 +00:00
self.tooltip(|popup| {
2019-01-21 07:48:32 +00:00
popup.add(Label::new(text));
2018-12-28 22:53:15 +00:00
})
}
}
// ----------------------------------------------------------------------------
2018-12-28 09:39:08 +00:00
#[derive(Clone, Copy, Debug, PartialEq)]
2019-01-06 15:34:01 +00:00
pub enum Direction {
2018-12-28 09:39:08 +00:00
Horizontal,
Vertical,
}
impl Default for Direction {
fn default() -> Direction {
Direction::Vertical
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Align {
/// Left/Top
Min,
/// Note: requires a bounded/known available_width.
Center,
/// Right/Bottom
/// Note: requires a bounded/known available_width.
Max,
}
impl Default for Align {
fn default() -> Align {
Align::Min
}
}
2018-12-28 09:39:08 +00:00
// ----------------------------------------------------------------------------
2019-01-07 07:54:37 +00:00
/// Show a pop-over window
pub fn show_popup<F>(ctx: &Arc<Context>, window_pos: Pos2, add_contents: F)
2019-01-07 07:54:37 +00:00
where
F: FnOnce(&mut Region),
{
let layer = Layer::Popup;
2020-04-17 21:30:01 +00:00
let where_to_put_background = ctx.graphics.lock().layer(layer).len();
2019-01-07 07:54:37 +00:00
let style = ctx.style();
let window_padding = style.window_padding;
2019-01-07 07:54:37 +00:00
let mut contents_region = Region {
ctx: ctx.clone(),
layer,
style,
2020-04-17 21:44:14 +00:00
id: Id::popup(),
2019-01-07 07:54:37 +00:00
dir: Direction::Vertical,
align: Align::Min,
2019-01-07 07:54:37 +00:00
cursor: window_pos + window_padding,
bounding_size: vec2(0.0, 0.0),
available_space: vec2(ctx.input.screen_size.x.min(350.0), std::f32::INFINITY), // TODO: popup/tooltip width
2019-01-07 07:54:37 +00:00
};
add_contents(&mut contents_region);
// Now insert popup background:
2019-01-07 07:54:37 +00:00
// TODO: handle the last item_spacing in a nicer way
let inner_size = contents_region.bounding_size - style.item_spacing;
2019-01-07 07:54:37 +00:00
let outer_size = inner_size + 2.0 * window_padding;
let rect = Rect::from_min_size(window_pos, outer_size);
2020-04-17 21:30:01 +00:00
let mut graphics = ctx.graphics.lock();
graphics.layer(layer).insert(
where_to_put_background,
PaintCmd::Rect {
corner_radius: 5.0,
fill_color: Some(style.background_fill_color()),
2020-04-19 09:11:41 +00:00
outline: Some(Outline::new(1.0, color::WHITE)),
rect,
},
);
2019-01-06 15:34:01 +00:00
}