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
|
|
|
|
2019-01-16 15:28:43 +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,
|
|
|
|
|
2020-04-19 09:13:24 +00:00
|
|
|
/// The mouse clicked this thing this frame
|
2018-12-28 22:53:15 +00:00
|
|
|
pub clicked: bool,
|
|
|
|
|
|
|
|
/// The mouse is interacting with this thing (e.g. dragging it)
|
|
|
|
pub active: bool,
|
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
/// The area of the screen we are talking about
|
2019-01-14 13:26:02 +00:00
|
|
|
pub rect: Rect,
|
|
|
|
|
2020-04-19 09:13:24 +00:00
|
|
|
/// Used for optionally showing a tooltip
|
2020-04-17 13:33:52 +00:00
|
|
|
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
|
2020-05-08 20:42:31 +00:00
|
|
|
pub fn tooltip(&mut self, add_contents: impl FnOnce(&mut Ui)) -> &mut Self {
|
2018-12-28 22:53:15 +00:00
|
|
|
if self.hovered {
|
2020-04-17 13:33:52 +00:00
|
|
|
if let Some(mouse_pos) = self.ctx.input().mouse_pos {
|
2019-02-10 14:30:48 +00:00
|
|
|
let window_pos = mouse_pos + vec2(16.0, 16.0);
|
2020-04-17 13:33:52 +00:00
|
|
|
show_popup(&self.ctx, window_pos, add_contents);
|
2019-02-10 14:30:48 +00:00
|
|
|
}
|
2018-12-28 22:53:15 +00:00
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Show this text if the item was hovered
|
2020-04-24 16:47:14 +00:00
|
|
|
pub fn tooltip_text(&mut self, text: impl Into<String>) -> &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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 13:54:06 +00:00
|
|
|
#[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,
|
2020-04-20 21:33:16 +00:00
|
|
|
// TODO: Justified
|
2019-01-14 13:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Align {
|
|
|
|
fn default() -> Align {
|
|
|
|
Align::Min
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:12:00 +00:00
|
|
|
pub fn align_rect(rect: Rect, align: (Align, Align)) -> Rect {
|
2020-04-19 09:13:24 +00:00
|
|
|
let x = match align.0 {
|
2020-04-22 17:38:38 +00:00
|
|
|
Align::Min => rect.left(),
|
2020-04-22 22:17:37 +00:00
|
|
|
Align::Center => rect.left() - 0.5 * rect.width(),
|
|
|
|
Align::Max => rect.left() - rect.width(),
|
2020-04-19 09:13:24 +00:00
|
|
|
};
|
|
|
|
let y = match align.1 {
|
2020-04-22 17:38:38 +00:00
|
|
|
Align::Min => rect.top(),
|
2020-04-22 22:17:37 +00:00
|
|
|
Align::Center => rect.top() - 0.5 * rect.height(),
|
|
|
|
Align::Max => rect.top() - rect.height(),
|
2020-04-19 09:13:24 +00:00
|
|
|
};
|
|
|
|
Rect::from_min_size(pos2(x, y), rect.size())
|
|
|
|
}
|
|
|
|
|
2018-12-28 09:39:08 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-04-19 22:48:54 +00:00
|
|
|
// TODO: move show_popup, and expand its features (default size, autosize, etc)
|
2019-01-07 07:54:37 +00:00
|
|
|
/// Show a pop-over window
|
2020-05-08 20:42:31 +00:00
|
|
|
pub fn show_popup(ctx: &Arc<Context>, window_pos: Pos2, add_contents: impl FnOnce(&mut Ui)) {
|
2020-04-17 12:26:36 +00:00
|
|
|
let layer = Layer::Popup;
|
2020-05-04 19:35:16 +00:00
|
|
|
let where_to_put_background = ctx.graphics().layer(layer).len();
|
2019-01-07 07:54:37 +00:00
|
|
|
|
2020-04-17 13:33:52 +00:00
|
|
|
let style = ctx.style();
|
2019-03-11 14:39:54 +00:00
|
|
|
let window_padding = style.window_padding;
|
2019-01-07 07:54:37 +00:00
|
|
|
|
2020-05-04 19:35:16 +00:00
|
|
|
let size = vec2(ctx.input().screen_size.x.min(350.0), f32::INFINITY); // TODO: popup/tooltip width
|
2020-04-19 22:48:54 +00:00
|
|
|
let inner_rect = Rect::from_min_size(window_pos + window_padding, size);
|
2020-05-08 20:42:31 +00:00
|
|
|
let mut contents_ui = Ui::new(ctx.clone(), layer, Id::popup(), inner_rect);
|
2019-01-07 07:54:37 +00:00
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
add_contents(&mut contents_ui);
|
2020-04-17 12:26:36 +00:00
|
|
|
|
|
|
|
// Now insert popup background:
|
2019-01-07 07:54:37 +00:00
|
|
|
|
2020-05-08 20:42:31 +00:00
|
|
|
let inner_size = contents_ui.bounding_size();
|
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-05-04 19:35:16 +00:00
|
|
|
let mut graphics = ctx.graphics();
|
2020-04-17 21:30:01 +00:00
|
|
|
graphics.layer(layer).insert(
|
2020-04-17 12:26:36 +00:00
|
|
|
where_to_put_background,
|
2020-04-20 21:33:16 +00:00
|
|
|
(
|
|
|
|
Rect::everything(),
|
|
|
|
PaintCmd::Rect {
|
|
|
|
corner_radius: 5.0,
|
|
|
|
fill_color: Some(style.background_fill_color()),
|
|
|
|
outline: Some(Outline::new(1.0, color::WHITE)),
|
|
|
|
rect,
|
|
|
|
},
|
|
|
|
),
|
2020-04-17 12:26:36 +00:00
|
|
|
);
|
2019-01-06 15:34:01 +00:00
|
|
|
}
|