Unify child region creation

This commit is contained in:
Emil Ernerfeldt 2020-04-22 19:02:43 +02:00
parent c604574e52
commit 2a4828670e
3 changed files with 27 additions and 79 deletions

View file

@ -1,6 +1,6 @@
use std::sync::Arc;
use crate::{layout, mesher::*, widgets::*, *};
use crate::{mesher::*, widgets::*, *};
#[derive(Clone, Copy, Default)]
struct Stats {
@ -48,19 +48,8 @@ impl Emigui {
/// A region for the entire screen, behind any windows.
pub fn background_region(&mut self) -> Region {
let child_rect = Rect::from_min_size(Default::default(), self.ctx.input.screen_size);
Region {
ctx: self.ctx.clone(),
id: Id::background(),
layer: Layer::Background,
clip_rect: child_rect,
desired_rect: child_rect,
cursor: Default::default(),
bounding_size: Default::default(),
style: self.ctx.style(),
dir: layout::Direction::Vertical,
align: layout::Align::Center,
}
let rect = Rect::from_min_size(Default::default(), self.ctx.input.screen_size);
Region::new(self.ctx.clone(), Layer::Background, Id::background(), rect)
}
pub fn paint(&mut self) -> PaintBatches {

View file

@ -70,6 +70,21 @@ impl Region {
}
}
pub fn child_region(&self, child_rect: Rect) -> Self {
Region {
ctx: self.ctx.clone(),
layer: self.layer,
style: self.style,
id: self.id,
clip_rect: self.clip_rect.intersect(child_rect),
desired_rect: child_rect,
cursor: child_rect.min(),
bounding_size: vec2(0.0, 0.0),
dir: self.dir,
align: self.align,
}
}
/// 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!
@ -155,16 +170,9 @@ impl Region {
let indent = vec2(self.style.indent, 0.0);
let child_rect = Rect::from_min_max(self.cursor + indent, self.desired_rect.max());
let mut child_region = Region {
ctx: self.ctx.clone(),
id,
layer: self.layer,
style: self.style,
clip_rect: self.clip_rect.intersect(child_rect),
desired_rect: child_rect,
bounding_size: vec2(0.0, 0.0),
cursor: child_rect.min(),
dir: self.dir,
align: Align::Min,
..self.child_region(child_rect)
};
add_contents(&mut child_region);
let size = child_region.bounding_size;
@ -201,70 +209,27 @@ impl Region {
Align::Center => self.available_width() / 2.0 - width / 2.0,
Align::Max => self.available_width() - width,
};
self.relative_region(Rect::from_min_size(
pos2(x, 0.0),
self.child_region(Rect::from_min_size(
self.cursor + vec2(x, 0.0),
vec2(width, self.available_height()),
))
}
/// Return a sub-region relative to the cursor
pub fn relative_region(&mut self, rect: Rect) -> Region {
let region_pos = self.cursor + rect.min().to_vec2();
let child_rect = Rect::from_min_size(region_pos, rect.size());
Region {
ctx: self.ctx.clone(),
layer: self.layer,
style: self.style,
id: self.id,
dir: self.dir,
align: self.align,
clip_rect: self.clip_rect.intersect(child_rect),
desired_rect: child_rect,
cursor: child_rect.min(),
bounding_size: vec2(0.0, 0.0),
}
}
pub fn inner_layout<F>(&mut self, dir: Direction, align: Align, add_contents: F)
where
F: FnOnce(&mut Region),
{
let child_rect = Rect::from_min_max(self.cursor, self.desired_rect.max());
let mut child_region = Region {
ctx: self.ctx.clone(),
layer: self.layer,
style: self.style,
id: self.id,
clip_rect: self.clip_rect.intersect(child_rect),
desired_rect: child_rect,
cursor: self.cursor,
bounding_size: vec2(0.0, 0.0),
dir,
align,
..self.child_region(child_rect)
};
add_contents(&mut child_region);
let size = child_region.bounding_size;
self.reserve_space_without_padding(size);
}
/// Create a child region from current cursor with the given dimensions.
/// Does NOT move the cursor.
pub fn create_child_region(&self, size: Vec2) -> Self {
let child_rect = Rect::from_min_size(self.cursor, size);
Region {
ctx: self.ctx.clone(),
layer: self.layer,
style: self.style,
id: self.id,
clip_rect: self.clip_rect.intersect(child_rect),
desired_rect: child_rect,
cursor: self.cursor,
bounding_size: vec2(0.0, 0.0),
dir: self.dir,
align: self.align,
}
}
/// Start a region with horizontal layout
pub fn horizontal<F>(&mut self, align: Align, add_contents: F)
where
@ -301,18 +266,11 @@ impl Region {
let pos = self.cursor + vec2((col_idx as f32) * (column_width + padding), 0.0);
let child_rect =
Rect::from_min_max(pos, pos2(pos.x + column_width, self.desired_rect.max().y));
Region {
ctx: self.ctx.clone(),
id: self.make_child_region_id(&("column", col_idx)),
layer: self.layer,
style: self.style,
clip_rect: self.clip_rect.intersect(child_rect),
desired_rect: child_rect,
bounding_size: vec2(0.0, 0.0),
cursor: child_rect.min(),
Region {
id: self.make_child_region_id(&("column", col_idx)),
dir: Direction::Vertical,
align: self.align,
..self.child_region(child_rect)
}
})
.collect();

View file

@ -51,7 +51,8 @@ impl ScrollArea {
let inner_size = outer_size - vec2(scroll_bar_width, 0.0);
let inner_rect = Rect::from_min_size(outer_region.cursor, inner_size);
let mut content_region = outer_region.create_child_region(inner_size);
let mut content_region =
outer_region.child_region(Rect::from_min_size(outer_region.cursor(), inner_size));
content_region.cursor -= state.offset;
content_region.desired_rect = content_region.desired_rect.translate(-state.offset);
add_contents(&mut content_region);