egui/emigui/src/region.rs

481 lines
15 KiB
Rust
Raw Normal View History

2020-04-17 13:29:48 +00:00
use std::{hash::Hash, sync::Arc};
use crate::{font::TextFragment, layout::*, widgets::*, *};
/// 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?
pub struct Region {
pub(crate) ctx: Arc<Context>,
2020-04-17 13:29:48 +00:00
/// Where to put the graphics output of this Region
pub(crate) layer: Layer,
pub(crate) style: Style,
/// Unique ID of this region.
pub(crate) id: Id,
/// Doesn't change.
pub(crate) dir: Direction,
pub(crate) align: Align,
// The `rect` represents where in space the region is
// and its max size (original available_space).
// Note that the size may be infinite in one or both dimensions.
// The widgets will TRY to fit within the rect,
// but may overflow (which you will see in bounding_size).
// It will use the rect (which never changes) as a
// clip rect when painting the contents of the region.
pub(crate) rect: Rect,
2020-04-17 13:29:48 +00:00
/// Where the next widget will be put.
/// Progresses along self.dir.
/// Initially set to rect.min()
pub(crate) cursor: Pos2,
2020-04-17 13:29:48 +00:00
/// Bounding box of children.
/// We keep track of our max-size along the orthogonal to self.dir
/// Initially set to zero.
2020-04-17 13:29:48 +00:00
pub(crate) bounding_size: Vec2,
}
impl Region {
pub fn new(ctx: Arc<Context>, layer: Layer, id: Id, rect: Rect) -> Self {
let style = ctx.style();
Region {
ctx,
layer,
style,
id,
dir: Direction::Vertical,
align: Align::Min,
rect,
cursor: rect.min(),
bounding_size: Vec2::default(),
}
}
2020-04-17 13:29:48 +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!
pub fn add_paint_cmd(&mut self, paint_cmd: PaintCmd) {
2020-04-20 21:33:16 +00:00
self.ctx
.graphics
.lock()
.layer(self.layer)
.push((self.clip_rect(), paint_cmd))
2020-04-17 13:29:48 +00:00
}
pub fn add_paint_cmds(&mut self, mut cmds: Vec<PaintCmd>) {
2020-04-20 21:33:16 +00:00
let clip_rect = self.clip_rect();
self.ctx
.graphics
.lock()
.layer(self.layer)
.extend(cmds.drain(..).map(|cmd| (clip_rect, cmd)));
2020-04-17 13:29:48 +00:00
}
/// Options for this region, and any child regions we may spawn.
pub fn style(&self) -> &Style {
&self.style
}
pub fn ctx(&self) -> &Arc<Context> {
&self.ctx
2020-04-17 13:29:48 +00:00
}
pub fn input(&self) -> &GuiInput {
self.ctx.input()
2020-04-17 13:29:48 +00:00
}
pub fn fonts(&self) -> &Fonts {
&*self.ctx.fonts
2020-04-17 13:29:48 +00:00
}
2020-04-20 21:33:16 +00:00
/// Screen-space rectangle for clipping what we paint in this region.
/// This is used, for instance, to avoid painting outside a window that is smaller
/// than its contents.
pub fn clip_rect(&self) -> Rect {
self.rect
}
pub fn available_width(&self) -> f32 {
self.rect.max().x - self.cursor.x
2020-04-17 13:29:48 +00:00
}
pub fn available_height(&self) -> f32 {
self.rect.max().y - self.cursor.y
2020-04-17 13:29:48 +00:00
}
/// This how much more space we can take up without overflowing our parent.
/// Shrinks as cursor increments.
pub fn available_space(&self) -> Vec2 {
self.rect.max() - self.cursor
2020-04-17 13:29:48 +00:00
}
pub fn direction(&self) -> Direction {
self.dir
}
pub fn cursor(&self) -> Pos2 {
2020-04-17 13:29:48 +00:00
self.cursor
}
pub fn set_align(&mut self, align: Align) {
self.align = align;
}
// ------------------------------------------------------------------------
// Sub-regions:
pub fn foldable<S, F>(&mut self, text: S, add_contents: F) -> GuiResponse
where
S: Into<String>,
F: FnOnce(&mut Region),
{
assert!(
self.dir == Direction::Vertical,
"Horizontal foldable is unimplemented"
);
let text: String = text.into();
let id = self.make_unique_id(&text);
2020-04-17 13:29:48 +00:00
let text_style = TextStyle::Button;
let font = &self.fonts()[text_style];
let (text, text_size) = font.layout_multiline(&text, self.available_width());
2020-04-17 13:29:48 +00:00
let text_cursor = self.cursor + self.style.button_padding;
let interact = self.reserve_space(
vec2(
self.available_width(),
2020-04-17 13:29:48 +00:00
text_size.y + 2.0 * self.style.button_padding.y,
),
Some(id),
);
let open = {
2020-04-17 21:30:01 +00:00
let mut memory = self.ctx.memory.lock();
2020-04-17 13:29:48 +00:00
if interact.clicked {
if memory.open_foldables.contains(&id) {
memory.open_foldables.remove(&id);
} else {
memory.open_foldables.insert(id);
}
}
memory.open_foldables.contains(&id)
};
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: 5.0,
2020-04-19 21:34:34 +00:00
fill_color,
2020-04-19 09:11:41 +00:00
outline: Some(Outline::new(1.0, color::WHITE)),
2020-04-17 13:29:48 +00:00
rect: interact.rect,
});
let (small_icon_rect, _) = self.style.icon_rectangles(&interact.rect);
// Draw a minus:
self.add_paint_cmd(PaintCmd::Line {
points: vec![
pos2(small_icon_rect.min().x, small_icon_rect.center().y),
pos2(small_icon_rect.max().x, small_icon_rect.center().y),
2020-04-17 13:29:48 +00:00
],
color: stroke_color,
width: self.style.line_width,
});
if !open {
// Draw it as a plus:
self.add_paint_cmd(PaintCmd::Line {
points: vec![
pos2(small_icon_rect.center().x, small_icon_rect.min().y),
pos2(small_icon_rect.center().x, small_icon_rect.max().y),
2020-04-17 13:29:48 +00:00
],
color: stroke_color,
width: self.style.line_width,
});
}
self.add_text(
text_cursor + vec2(self.style.start_icon_width, 0.0),
text_style,
text,
None,
);
if open {
let old_id = self.id;
self.id = id;
self.indent(add_contents);
self.id = old_id;
}
self.response(interact)
}
/// Create a child region which is indented to the right
pub fn indent<F>(&mut self, add_contents: F)
where
F: FnOnce(&mut Region),
{
let indent = vec2(self.style.indent, 0.0);
let region_pos = self.cursor + indent;
2020-04-17 13:29:48 +00:00
let mut child_region = Region {
ctx: self.ctx.clone(),
2020-04-17 13:29:48 +00:00
layer: self.layer,
style: self.style,
id: self.id,
dir: self.dir,
align: Align::Min,
rect: Rect::from_min_max(region_pos, self.rect.max()),
cursor: region_pos,
2020-04-17 13:29:48 +00:00
bounding_size: vec2(0.0, 0.0),
};
add_contents(&mut child_region);
let size = child_region.bounding_size;
self.reserve_space_without_padding(indent + size);
}
/// Return a sub-region relative to the parent
pub fn relative_region(&mut self, rect: Rect) -> Region {
let region_pos = self.cursor + rect.min().to_vec2();
2020-04-17 13:29:48 +00:00
Region {
ctx: self.ctx.clone(),
2020-04-17 13:29:48 +00:00
layer: self.layer,
style: self.style,
id: self.id,
dir: self.dir,
align: self.align,
2020-04-20 21:22:50 +00:00
rect: Rect::from_min_size(region_pos, rect.size()),
cursor: region_pos,
2020-04-17 13:29:48 +00:00
bounding_size: vec2(0.0, 0.0),
}
}
/// 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_width() / 2.0 - width / 2.0,
Align::Max => self.available_width() - width,
2020-04-17 13:29:48 +00:00
};
self.relative_region(Rect::from_min_size(
pos2(x, 0.0),
vec2(width, self.available_height()),
2020-04-17 13:29:48 +00:00
))
}
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)
}
pub fn inner_layout<F>(&mut self, dir: Direction, align: Align, add_contents: F)
where
F: FnOnce(&mut Region),
{
let mut child_region = Region {
ctx: self.ctx.clone(),
2020-04-17 13:29:48 +00:00
layer: self.layer,
style: self.style,
id: self.id,
dir,
align,
rect: Rect::from_min_max(self.cursor, self.rect.max()),
2020-04-17 13:29:48 +00:00
cursor: self.cursor,
bounding_size: vec2(0.0, 0.0),
};
add_contents(&mut child_region);
let size = child_region.bounding_size;
self.reserve_space_without_padding(size);
}
/// 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.
///
/// region.columns(2, |columns| {
/// columns[0].add(emigui::widgets::label!("First column"));
/// columns[1].add(emigui::widgets::label!("Second column"));
/// });
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
let padding = self.style.item_spacing.x;
let total_padding = padding * (num_columns as f32 - 1.0);
let column_width = (self.available_width() - total_padding) / (num_columns as f32);
2020-04-17 13:29:48 +00:00
let mut columns: Vec<Region> = (0..num_columns)
.map(|col_idx| {
let pos = self.cursor + vec2((col_idx as f32) * (column_width + padding), 0.0);
Region {
ctx: self.ctx.clone(),
layer: self.layer,
style: self.style,
id: self.make_child_region_id(&("column", col_idx)),
dir: Direction::Vertical,
align: self.align,
rect: Rect::from_min_max(pos, pos2(pos.x + column_width, self.rect.max().y)),
cursor: pos,
bounding_size: vec2(0.0, 0.0),
}
2020-04-17 13:29:48 +00:00
})
.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);
}
self.reserve_space_without_padding(vec2(self.available_width(), max_height));
2020-04-17 13:29:48 +00:00
result
}
// ------------------------------------------------------------------------
pub fn add<W: Widget>(&mut self, widget: W) -> GuiResponse {
widget.add_to(self)
}
2020-04-20 21:42:11 +00:00
// Convenience functions:
pub fn add_label(&mut self, text: impl Into<String>) -> GuiResponse {
self.add(Label::new(text))
}
2020-04-17 13:29:48 +00:00
// ------------------------------------------------------------------------
pub fn reserve_space(&mut self, size: Vec2, interaction_id: Option<Id>) -> InteractInfo {
let pos = self.reserve_space_without_padding(size + self.style.item_spacing);
let rect = Rect::from_min_size(pos, size);
self.ctx.interact(self.layer, rect, interaction_id)
2020-04-17 13:29:48 +00:00
}
/// Reserve this much space and move the cursor.
/// Returns where to put the widget.
pub fn reserve_space_without_padding(&mut self, size: Vec2) -> Pos2 {
2020-04-17 13:29:48 +00:00
let mut pos = self.cursor;
if self.dir == Direction::Horizontal {
pos.y += match self.align {
Align::Min => 0.0,
Align::Center => 0.5 * (self.available_height() - size.y),
Align::Max => self.available_height() - size.y,
2020-04-17 13:29:48 +00:00
};
self.cursor.x += size.x;
self.bounding_size.x += size.x;
self.bounding_size.y = self.bounding_size.y.max(size.y);
} else {
pos.x += match self.align {
Align::Min => 0.0,
Align::Center => 0.5 * (self.available_width() - size.x),
Align::Max => self.available_width() - size.x,
2020-04-17 13:29:48 +00:00
};
self.cursor.y += size.y;
self.bounding_size.y += size.y;
self.bounding_size.x = self.bounding_size.x.max(size.x);
}
pos
}
/// Will warn if the returned id is not guaranteed unique.
/// Use this to generate widget ids for widgets that have persistent state in Memory.
/// If the child_id_source is not unique within this region
/// then an error will be printed at the current cursor position.
pub fn make_unique_id<IdSource>(&self, child_id_source: &IdSource) -> Id
where
IdSource: Hash + std::fmt::Debug,
{
let id = self.id.with(child_id_source);
self.ctx
.register_unique_id(id, child_id_source, self.cursor)
2020-04-17 13:29:48 +00:00
}
/// Make an Id that is unique to this positon.
/// Can be used for widgets that do NOT persist state in Memory
/// but you still need to interact with (e.g. buttons, sliders).
pub fn make_position_id(&self) -> Id {
self.id.with(&Id::from_pos(self.cursor))
2020-04-17 13:29:48 +00:00
}
pub fn make_child_region_id<H: Hash>(&self, child_id: &H) -> Id {
self.id.with(child_id)
}
/// Show some text anywhere in the region.
/// To center the text at the given position, use `align: (Center, Center)`.
/// If you want to draw text floating on top of everything,
/// consider using Context.floating_text instead.
2020-04-17 13:29:48 +00:00
pub fn floating_text(
&mut self,
pos: Pos2,
2020-04-17 13:29:48 +00:00
text: &str,
text_style: TextStyle,
align: (Align, Align),
text_color: Option<Color>,
) -> Vec2 {
let font = &self.fonts()[text_style];
let (text, size) = font.layout_multiline(text, std::f32::INFINITY);
let rect = align_rect(Rect::from_min_size(pos, size), align);
self.add_text(rect.min(), text_style, text, text_color);
size
2020-04-17 13:29:48 +00:00
}
/// Already layed out text.
2020-04-17 13:29:48 +00:00
pub fn add_text(
&mut self,
pos: Pos2,
2020-04-17 13:29:48 +00:00
text_style: TextStyle,
text: Vec<TextFragment>,
color: Option<Color>,
) {
let color = color.unwrap_or_else(|| self.style().text_color());
for fragment in text {
self.add_paint_cmd(PaintCmd::Text {
color,
pos: pos + vec2(0.0, fragment.y_offset),
text: fragment.text,
text_style,
x_offsets: fragment.x_offsets,
});
}
}
pub fn response(&mut self, interact: InteractInfo) -> GuiResponse {
// TODO: unify GuiResponse and InteractInfo. They are the same thing!
GuiResponse {
hovered: interact.hovered,
clicked: interact.clicked,
active: interact.active,
rect: interact.rect,
ctx: self.ctx.clone(),
2020-04-17 13:29:48 +00:00
}
}
}