egui/emigui/src/layout.rs

697 lines
20 KiB
Rust
Raw Normal View History

2019-01-07 07:54:37 +00:00
use std::{
collections::HashSet,
hash::Hash,
sync::{Arc, Mutex},
};
2018-12-27 18:08:43 +00:00
use crate::{
2019-04-25 16:07:36 +00:00
color::{self, Color},
2019-01-12 23:55:56 +00:00
font::TextFragment,
fonts::{Fonts, TextStyle},
math::*,
style::Style,
types::*,
2019-01-21 07:48:32 +00:00
widgets::{Label, Widget},
};
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)
2019-01-07 07:54:37 +00:00
data: Arc<Data>,
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.data.input().mouse_pos {
let window_pos = mouse_pos + vec2(16.0, 16.0);
show_popup(&self.data, 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-27 18:08:43 +00:00
#[derive(Clone, Debug, Default)]
2019-01-06 15:34:01 +00:00
pub struct Memory {
2018-12-26 14:28:38 +00:00
/// The widget being interacted with (e.g. dragged, in case of a slider).
2018-12-28 09:39:08 +00:00
active_id: Option<Id>,
2018-12-27 18:08:43 +00:00
/// Which foldable regions are open.
open_foldables: HashSet<Id>,
2018-12-26 09:46:23 +00:00
}
2018-12-26 22:08:50 +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
// ----------------------------------------------------------------------------
pub type Id = u64;
pub fn make_id<H: Hash>(source: &H) -> Id {
use std::hash::Hasher;
let mut hasher = std::collections::hash_map::DefaultHasher::new();
source.hash(&mut hasher);
hasher.finish()
}
2018-12-26 22:08:50 +00:00
2019-01-07 07:54:37 +00:00
// ----------------------------------------------------------------------------
/// TODO: improve this
#[derive(Clone, Default)]
pub struct GraphicLayers {
2019-03-11 14:59:49 +00:00
pub(crate) graphics: Vec<PaintCmd>,
pub(crate) hovering_graphics: Vec<PaintCmd>,
2019-01-07 07:54:37 +00:00
}
impl GraphicLayers {
2019-03-11 14:59:49 +00:00
pub fn drain(&mut self) -> impl ExactSizeIterator<Item = PaintCmd> {
2019-01-07 07:54:37 +00:00
// TODO: there must be a nicer way to do this?
let mut all_commands: Vec<_> = self.graphics.drain(..).collect();
all_commands.extend(self.hovering_graphics.drain(..));
all_commands.into_iter()
}
}
// ----------------------------------------------------------------------------
2019-01-06 15:34:01 +00:00
// TODO: give a better name.
/// Contains the input, style and output of all GUI commands.
2019-01-06 15:34:01 +00:00
pub struct Data {
/// The default style for new regions
pub(crate) style: Mutex<Style>,
2019-01-12 23:55:56 +00:00
pub(crate) fonts: Arc<Fonts>,
2019-01-06 15:34:01 +00:00
pub(crate) input: GuiInput,
2019-01-07 07:54:37 +00:00
pub(crate) memory: Mutex<Memory>,
pub(crate) graphics: Mutex<GraphicLayers>,
}
impl Clone for Data {
fn clone(&self) -> Self {
Data {
style: Mutex::new(self.style()),
2019-01-12 23:55:56 +00:00
fonts: self.fonts.clone(),
input: self.input,
2019-01-07 07:54:37 +00:00
memory: Mutex::new(self.memory.lock().unwrap().clone()),
graphics: Mutex::new(self.graphics.lock().unwrap().clone()),
}
}
2018-12-26 14:28:38 +00:00
}
2018-12-26 09:46:23 +00:00
2019-01-06 15:34:01 +00:00
impl Data {
2019-01-19 16:10:28 +00:00
pub fn new(pixels_per_point: f32) -> Data {
2019-01-06 15:34:01 +00:00
Data {
style: Default::default(),
2019-01-19 16:10:28 +00:00
fonts: Arc::new(Fonts::new(pixels_per_point)),
2019-01-05 14:28:07 +00:00
input: Default::default(),
memory: Default::default(),
graphics: Default::default(),
}
}
2018-12-26 09:46:23 +00:00
pub fn input(&self) -> &GuiInput {
&self.input
}
pub fn style(&self) -> Style {
*self.style.lock().unwrap()
2018-12-28 22:29:24 +00:00
}
pub fn set_style(&self, style: Style) {
*self.style.lock().unwrap() = style;
2018-12-26 09:46:23 +00:00
}
2018-12-28 09:39:08 +00:00
// TODO: move
2018-12-28 22:29:24 +00:00
pub fn new_frame(&mut self, gui_input: GuiInput) {
2018-12-28 09:39:08 +00:00
self.input = gui_input;
if !gui_input.mouse_down || gui_input.mouse_pos.is_none() {
2019-01-07 07:54:37 +00:00
self.memory.lock().unwrap().active_id = None;
2018-12-28 09:39:08 +00:00
}
}
/// Is the user interacting with anything?
pub fn any_active(&self) -> bool {
self.memory.lock().unwrap().active_id.is_some()
}
2019-01-07 07:54:37 +00:00
}
2018-12-28 09:39:08 +00:00
2020-04-12 10:07:51 +00:00
impl Data {
pub fn style_ui(&self, region: &mut Region) {
let mut style = self.style();
style.ui(region);
self.set_style(style);
}
}
2019-01-07 07:54:37 +00:00
/// Show a pop-over window
pub fn show_popup<F>(data: &Arc<Data>, window_pos: Vec2, add_contents: F)
where
F: FnOnce(&mut Region),
{
// TODO: nicer way to do layering!
let num_graphics_before = data.graphics.lock().unwrap().graphics.len();
let style = data.style();
let window_padding = style.window_padding;
2019-01-07 07:54:37 +00:00
let mut popup_region = Region {
data: data.clone(),
style,
2019-01-07 07:54:37 +00:00
id: Default::default(),
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),
2019-01-19 16:09:00 +00:00
available_space: vec2(data.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 popup_region);
// TODO: handle the last item_spacing in a nicer way
let inner_size = popup_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);
let mut graphics = data.graphics.lock().unwrap();
let popup_graphics = graphics.graphics.split_off(num_graphics_before);
2019-03-11 14:59:49 +00:00
graphics.hovering_graphics.push(PaintCmd::Rect {
corner_radius: 5.0,
fill_color: Some(style.background_fill_color()),
outline: Some(Outline {
2019-04-25 16:07:36 +00:00
color: color::gray(255, 255), // TODO
2019-03-11 14:59:49 +00:00
width: 1.0,
}),
rect,
});
2019-01-07 07:54:37 +00:00
graphics.hovering_graphics.extend(popup_graphics);
2019-01-06 15:34:01 +00:00
}
// ----------------------------------------------------------------------------
/// Represents a region of the screen
/// with a type of layout (horizontal or vertical).
/// TODO: make Region a trait so we can have type-safe HorizontalRegion etc?
2019-01-07 07:54:37 +00:00
pub struct Region {
pub(crate) data: Arc<Data>,
2019-01-06 15:34:01 +00:00
pub(crate) style: Style,
2019-01-06 15:34:01 +00:00
/// Unique ID of this region.
pub(crate) id: Id,
/// Doesn't change.
pub(crate) dir: Direction,
pub(crate) align: Align,
2019-01-06 15:34:01 +00:00
/// Changes only along self.dir
pub(crate) cursor: Vec2,
/// Bounding box children.
2019-01-06 15:34:01 +00:00
/// We keep track of our max-size along the orthogonal to self.dir
pub(crate) bounding_size: Vec2,
/// This how much space we can take up without overflowing our parent.
/// Shrinks as cursor increments.
pub(crate) available_space: Vec2,
2019-01-06 15:34:01 +00:00
}
2019-01-07 07:54:37 +00:00
impl Region {
2019-01-06 15:34:01 +00:00
/// It is up to the caller to make sure there is room for this.
/// Can be used for free painting.
/// NOTE: all coordinates are screen coordinates!
2019-03-11 14:59:49 +00:00
pub fn add_paint_cmd(&mut self, paint_cmd: PaintCmd) {
self.data.graphics.lock().unwrap().graphics.push(paint_cmd)
2019-01-06 15:34:01 +00:00
}
2019-03-11 14:59:49 +00:00
pub fn add_paint_cmds(&mut self, mut cmds: Vec<PaintCmd>) {
self.data
.graphics
.lock()
.unwrap()
.graphics
.append(&mut cmds)
}
/// Options for this region, and any child regions we may spawn.
pub fn style(&self) -> &Style {
&self.style
2019-01-06 15:34:01 +00:00
}
2019-01-17 23:34:01 +00:00
pub fn data(&self) -> &Arc<Data> {
&self.data
}
2019-01-06 15:34:01 +00:00
pub fn input(&self) -> &GuiInput {
self.data.input()
}
2018-12-26 09:46:23 +00:00
2019-01-12 23:55:56 +00:00
pub fn fonts(&self) -> &Fonts {
&*self.data.fonts
}
pub fn width(&self) -> f32 {
self.available_space.x
}
2019-03-16 11:56:00 +00:00
pub fn height(&self) -> f32 {
self.available_space.y
}
2019-11-02 08:50:49 +00:00
pub fn size(&self) -> Vec2 {
self.available_space
}
2019-01-13 18:15:11 +00:00
pub fn direction(&self) -> Direction {
self.dir
}
2019-01-19 16:10:28 +00:00
pub fn cursor(&self) -> Vec2 {
self.cursor
}
2019-04-25 16:07:36 +00:00
pub fn set_align(&mut self, align: Align) {
self.align = align;
}
2018-12-27 18:08:43 +00:00
// ------------------------------------------------------------------------
// Sub-regions:
2018-12-27 18:08:43 +00:00
2018-12-28 22:53:15 +00:00
pub fn foldable<S, F>(&mut self, text: S, add_contents: F) -> GuiResponse
2018-12-27 18:08:43 +00:00
where
S: Into<String>,
2019-01-06 15:34:01 +00:00
F: FnOnce(&mut Region),
2018-12-27 18:08:43 +00:00
{
2018-12-28 09:39:08 +00:00
assert!(
2019-01-06 15:34:01 +00:00
self.dir == Direction::Vertical,
2018-12-28 09:39:08 +00:00
"Horizontal foldable is unimplemented"
);
2018-12-27 22:26:05 +00:00
let text: String = text.into();
2019-01-07 07:54:37 +00:00
let id = self.make_child_id(&text);
2019-01-17 23:34:01 +00:00
let text_style = TextStyle::Button;
2019-01-12 23:55:56 +00:00
let font = &self.fonts()[text_style];
let (text, text_size) = font.layout_multiline(&text, self.width());
2019-03-11 14:59:49 +00:00
let text_cursor = self.cursor + self.style.button_padding;
2019-01-14 13:26:02 +00:00
let interact = self.reserve_space(
2018-12-27 22:26:05 +00:00
vec2(
self.available_space.x,
2019-03-11 14:59:49 +00:00
text_size.y + 2.0 * self.style.button_padding.y,
2018-12-27 22:26:05 +00:00
),
2018-12-28 22:53:15 +00:00
Some(id),
2018-12-27 18:35:02 +00:00
);
2018-12-27 18:08:43 +00:00
2019-01-07 07:54:37 +00:00
let open = {
let mut memory = self.data.memory.lock().unwrap();
if interact.clicked {
if memory.open_foldables.contains(&id) {
memory.open_foldables.remove(&id);
} else {
memory.open_foldables.insert(id);
}
2018-12-27 18:08:43 +00:00
}
2019-01-07 07:54:37 +00:00
memory.open_foldables.contains(&id)
};
2018-12-27 18:08:43 +00:00
2019-03-11 14:59:49 +00:00
let fill_color = self.style.interact_fill_color(&interact);
let stroke_color = self.style.interact_stroke_color(&interact);
self.add_paint_cmd(PaintCmd::Rect {
corner_radius: 3.0,
fill_color: Some(fill_color),
outline: None,
rect: interact.rect,
});
let (small_icon_rect, _) = self.style.icon_rectangles(&interact.rect);
// Draw a minus:
self.add_paint_cmd(PaintCmd::Line {
points: vec![
vec2(small_icon_rect.min().x, small_icon_rect.center().y),
vec2(small_icon_rect.max().x, small_icon_rect.center().y),
],
color: stroke_color,
width: self.style.line_width,
});
if !open {
// Draw it as a plus:
self.add_paint_cmd(PaintCmd::Line {
points: vec![
vec2(small_icon_rect.center().x, small_icon_rect.min().y),
vec2(small_icon_rect.center().x, small_icon_rect.max().y),
],
color: stroke_color,
width: self.style.line_width,
});
}
2019-01-06 15:34:01 +00:00
self.add_text(
2019-03-11 14:59:49 +00:00
text_cursor + vec2(self.style.start_icon_width, 0.0),
2019-01-12 23:55:56 +00:00
text_style,
2019-01-06 15:34:01 +00:00
text,
None,
2019-01-06 15:34:01 +00:00
);
2018-12-27 18:08:43 +00:00
if open {
2018-12-27 22:26:05 +00:00
let old_id = self.id;
self.id = id;
self.indent(add_contents);
2018-12-27 22:26:05 +00:00
self.id = old_id;
2018-12-27 18:08:43 +00:00
}
2018-12-28 22:53:15 +00:00
self.response(interact)
2018-12-27 18:08:43 +00:00
}
/// Create a child region which is indented to the right
pub fn indent<F>(&mut self, add_contents: F)
where
F: FnOnce(&mut Region),
{
2019-03-11 14:59:49 +00:00
let indent = vec2(self.style.indent, 0.0);
let mut child_region = Region {
2019-01-07 07:54:37 +00:00
data: self.data.clone(),
style: self.style,
id: self.id,
dir: self.dir,
align: Align::Min,
cursor: self.cursor + indent,
bounding_size: vec2(0.0, 0.0),
available_space: self.available_space - indent,
};
add_contents(&mut child_region);
let size = child_region.bounding_size;
2019-01-14 13:26:02 +00:00
self.reserve_space_without_padding(indent + size);
}
2019-04-25 16:07:36 +00:00
/// Return a sub-region relative to the parent
pub fn relative_region(&mut self, rect: Rect) -> Region {
2019-03-12 21:59:55 +00:00
Region {
data: self.data.clone(),
style: self.style,
id: self.id,
dir: self.dir,
2019-04-25 16:07:36 +00:00
cursor: self.cursor + rect.min(),
align: self.align,
2019-03-12 21:59:55 +00:00
bounding_size: vec2(0.0, 0.0),
2019-04-25 16:07:36 +00:00
available_space: rect.size(),
2019-03-12 21:59:55 +00:00
}
}
2019-04-25 16:07:36 +00:00
/// A column region with a given width.
pub fn column(&mut self, column_position: Align, width: f32) -> Region {
let x = match column_position {
Align::Min => 0.0,
Align::Center => self.available_space.x / 2.0 - width / 2.0,
Align::Max => self.available_space.x - width,
};
self.relative_region(Rect::from_min_size(
vec2(x, 0.0),
vec2(width, self.available_space.y),
))
}
pub fn left_column(&mut self, width: f32) -> Region {
self.column(Align::Min, width)
}
pub fn centered_column(&mut self, width: f32) -> Region {
self.column(Align::Center, width)
}
pub fn right_column(&mut self, width: f32) -> Region {
self.column(Align::Max, width)
}
2019-02-10 21:06:57 +00:00
pub fn inner_layout<F>(&mut self, dir: Direction, align: Align, add_contents: F)
2018-12-28 09:39:08 +00:00
where
2019-01-06 15:34:01 +00:00
F: FnOnce(&mut Region),
2018-12-28 22:53:15 +00:00
{
let mut child_region = Region {
2019-01-07 07:54:37 +00:00
data: self.data.clone(),
style: self.style,
2018-12-28 22:29:24 +00:00
id: self.id,
2019-02-10 21:06:57 +00:00
dir,
align,
2019-01-06 15:34:01 +00:00
cursor: self.cursor,
bounding_size: vec2(0.0, 0.0),
available_space: self.available_space,
2018-12-28 22:29:24 +00:00
};
add_contents(&mut child_region);
let size = child_region.bounding_size;
2019-01-14 13:26:02 +00:00
self.reserve_space_without_padding(size);
2018-12-28 22:29:24 +00:00
}
2019-02-10 21:06:57 +00:00
/// Start a region with horizontal layout
pub fn horizontal<F>(&mut self, align: Align, add_contents: F)
where
F: FnOnce(&mut Region),
{
self.inner_layout(Direction::Horizontal, align, add_contents)
}
/// Start a region with vertical layout
pub fn vertical<F>(&mut self, align: Align, add_contents: F)
where
F: FnOnce(&mut Region),
{
self.inner_layout(Direction::Vertical, align, add_contents)
}
/// Temporarily split split a vertical layout into several columns.
2019-01-07 07:54:37 +00:00
///
2019-01-17 17:03:39 +00:00
/// region.columns(2, |columns| {
2019-01-21 07:48:32 +00:00
/// columns[0].add(emigui::widgets::label!("First column"));
/// columns[1].add(emigui::widgets::label!("Second column"));
2019-01-17 17:03:39 +00:00
/// });
2019-01-07 07:54:37 +00:00
pub fn columns<F, R>(&mut self, num_columns: usize, add_contents: F) -> R
where
F: FnOnce(&mut [Region]) -> R,
{
// TODO: ensure there is space
2019-03-11 14:59:49 +00:00
let padding = self.style.item_spacing.x;
2019-01-07 07:54:37 +00:00
let total_padding = padding * (num_columns as f32 - 1.0);
let column_width = (self.available_space.x - total_padding) / (num_columns as f32);
let mut columns: Vec<Region> = (0..num_columns)
.map(|col_idx| Region {
data: self.data.clone(),
style: self.style,
2019-01-07 07:54:37 +00:00
id: self.make_child_id(&("column", col_idx)),
dir: Direction::Vertical,
align: self.align,
2019-01-07 07:54:37 +00:00
cursor: self.cursor + vec2((col_idx as f32) * (column_width + padding), 0.0),
bounding_size: vec2(0.0, 0.0),
available_space: vec2(column_width, self.available_space.y),
})
.collect();
let result = add_contents(&mut columns[..]);
let mut max_height = 0.0;
for region in columns {
let size = region.bounding_size;
max_height = size.y.max(max_height);
}
2019-01-14 13:26:02 +00:00
self.reserve_space_without_padding(vec2(self.available_space.x, max_height));
2019-01-07 07:54:37 +00:00
result
}
2018-12-26 09:46:23 +00:00
// ------------------------------------------------------------------------
2018-12-26 14:28:38 +00:00
pub fn add<W: Widget>(&mut self, widget: W) -> GuiResponse {
widget.add_to(self)
}
// ------------------------------------------------------------------------
2019-01-14 13:26:02 +00:00
pub fn reserve_space(&mut self, size: Vec2, interaction_id: Option<Id>) -> InteractInfo {
2019-03-11 14:59:49 +00:00
let pos = self.reserve_space_without_padding(size + self.style.item_spacing);
let rect = Rect::from_min_size(pos, size);
let mut memory = self.data.memory.lock().unwrap();
let is_something_else_active =
memory.active_id.is_some() && memory.active_id != interaction_id;
2019-01-14 13:26:02 +00:00
let hovered = if let Some(mouse_pos) = self.input().mouse_pos {
!is_something_else_active && rect.contains(mouse_pos)
} else {
false
};
2018-12-28 22:53:15 +00:00
let active = if interaction_id.is_some() {
2019-03-16 11:56:00 +00:00
if hovered && self.input().mouse_clicked {
2019-01-07 07:54:37 +00:00
memory.active_id = interaction_id;
2018-12-28 22:53:15 +00:00
}
2019-01-07 07:54:37 +00:00
memory.active_id == interaction_id
2018-12-28 22:53:15 +00:00
} else {
false
};
2018-12-26 16:01:46 +00:00
2019-03-16 11:56:00 +00:00
let clicked = hovered && self.input().mouse_released;
2019-01-14 13:26:02 +00:00
InteractInfo {
rect,
2018-12-26 16:01:46 +00:00
hovered,
clicked,
active,
2019-01-14 13:26:02 +00:00
}
2018-12-26 16:01:46 +00:00
}
2019-01-06 15:34:01 +00:00
/// Reserve this much space and move the cursor.
pub fn reserve_space_without_padding(&mut self, size: Vec2) -> Vec2 {
let mut pos = self.cursor;
2019-01-06 15:34:01 +00:00
if self.dir == Direction::Horizontal {
pos.y += match self.align {
Align::Min => 0.0,
Align::Center => 0.5 * (self.available_space.y - size.y),
Align::Max => self.available_space.y - size.y,
};
2019-01-06 15:34:01 +00:00
self.cursor.x += size.x;
self.available_space.x -= size.x;
self.bounding_size.x += size.x;
self.bounding_size.y = self.bounding_size.y.max(size.y);
2019-01-06 15:34:01 +00:00
} else {
pos.x += match self.align {
Align::Min => 0.0,
Align::Center => 0.5 * (self.available_space.x - size.x),
Align::Max => self.available_space.x - size.x,
};
2019-01-06 15:34:01 +00:00
self.cursor.y += size.y;
self.available_space.y -= size.x;
self.bounding_size.y += size.y;
self.bounding_size.x = self.bounding_size.x.max(size.x);
2019-01-06 15:34:01 +00:00
}
pos
2019-01-06 15:34:01 +00:00
}
pub fn make_child_id<H: Hash>(&self, child_id: &H) -> Id {
2018-12-26 14:28:38 +00:00
use std::hash::Hasher;
let mut hasher = std::collections::hash_map::DefaultHasher::new();
2018-12-27 22:26:05 +00:00
hasher.write_u64(self.id);
2019-01-07 07:54:37 +00:00
child_id.hash(&mut hasher);
2018-12-26 14:28:38 +00:00
hasher.finish()
}
pub fn combined_id(&self, child_id: Option<Id>) -> Option<Id> {
child_id.map(|child_id| {
use std::hash::Hasher;
let mut hasher = std::collections::hash_map::DefaultHasher::new();
hasher.write_u64(self.id);
child_id.hash(&mut hasher);
hasher.finish()
})
}
2019-02-10 19:56:59 +00:00
// Helper function
pub fn floating_text(
&mut self,
pos: Vec2,
text: &str,
text_style: TextStyle,
align: (Align, Align),
text_color: Option<Color>,
2019-03-16 14:14:22 +00:00
) -> Vec2 {
2019-02-10 19:56:59 +00:00
let font = &self.fonts()[text_style];
let (text, text_size) = font.layout_multiline(text, std::f32::INFINITY);
let x = match align.0 {
Align::Min => pos.x,
Align::Center => pos.x - 0.5 * text_size.x,
Align::Max => pos.x - text_size.x,
};
let y = match align.1 {
Align::Min => pos.y,
Align::Center => pos.y - 0.5 * text_size.y,
Align::Max => pos.y - text_size.y,
};
self.add_text(vec2(x, y), text_style, text, text_color);
2019-03-16 14:14:22 +00:00
text_size
2019-02-10 19:56:59 +00:00
}
pub fn add_text(
&mut self,
pos: Vec2,
text_style: TextStyle,
text: Vec<TextFragment>,
color: Option<Color>,
) {
2019-03-11 14:59:49 +00:00
let color = color.unwrap_or_else(|| self.style().text_color());
2018-12-27 22:26:05 +00:00
for fragment in text {
2019-03-11 14:59:49 +00:00
self.add_paint_cmd(PaintCmd::Text {
color,
2019-01-05 14:28:07 +00:00
pos: pos + vec2(0.0, fragment.y_offset),
2018-12-27 22:26:05 +00:00
text: fragment.text,
text_style,
2019-01-05 14:28:07 +00:00
x_offsets: fragment.x_offsets,
2018-12-27 22:26:05 +00:00
});
}
2018-12-26 14:28:38 +00:00
}
2018-12-28 22:53:15 +00:00
pub fn response(&mut self, interact: InteractInfo) -> GuiResponse {
2019-01-14 13:26:02 +00:00
// TODO: unify GuiResponse and InteractInfo. They are the same thing!
2018-12-28 22:53:15 +00:00
GuiResponse {
hovered: interact.hovered,
clicked: interact.clicked,
active: interact.active,
2019-01-14 13:26:02 +00:00
rect: interact.rect,
2019-01-07 07:54:37 +00:00
data: self.data.clone(),
2018-12-28 22:53:15 +00:00
}
}
2018-12-26 09:46:23 +00:00
}