Unify child region creation
This commit is contained in:
parent
c604574e52
commit
2a4828670e
3 changed files with 27 additions and 79 deletions
|
@ -1,6 +1,6 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::{layout, mesher::*, widgets::*, *};
|
use crate::{mesher::*, widgets::*, *};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Default)]
|
#[derive(Clone, Copy, Default)]
|
||||||
struct Stats {
|
struct Stats {
|
||||||
|
@ -48,19 +48,8 @@ impl Emigui {
|
||||||
|
|
||||||
/// A region for the entire screen, behind any windows.
|
/// A region for the entire screen, behind any windows.
|
||||||
pub fn background_region(&mut self) -> Region {
|
pub fn background_region(&mut self) -> Region {
|
||||||
let child_rect = Rect::from_min_size(Default::default(), self.ctx.input.screen_size);
|
let rect = Rect::from_min_size(Default::default(), self.ctx.input.screen_size);
|
||||||
Region {
|
Region::new(self.ctx.clone(), Layer::Background, Id::background(), rect)
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn paint(&mut self) -> PaintBatches {
|
pub fn paint(&mut self) -> PaintBatches {
|
||||||
|
|
|
@ -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.
|
/// It is up to the caller to make sure there is room for this.
|
||||||
/// Can be used for free painting.
|
/// Can be used for free painting.
|
||||||
/// NOTE: all coordinates are screen coordinates!
|
/// NOTE: all coordinates are screen coordinates!
|
||||||
|
@ -155,16 +170,9 @@ impl Region {
|
||||||
let indent = vec2(self.style.indent, 0.0);
|
let indent = vec2(self.style.indent, 0.0);
|
||||||
let child_rect = Rect::from_min_max(self.cursor + indent, self.desired_rect.max());
|
let child_rect = Rect::from_min_max(self.cursor + indent, self.desired_rect.max());
|
||||||
let mut child_region = Region {
|
let mut child_region = Region {
|
||||||
ctx: self.ctx.clone(),
|
|
||||||
id,
|
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,
|
align: Align::Min,
|
||||||
|
..self.child_region(child_rect)
|
||||||
};
|
};
|
||||||
add_contents(&mut child_region);
|
add_contents(&mut child_region);
|
||||||
let size = child_region.bounding_size;
|
let size = child_region.bounding_size;
|
||||||
|
@ -201,70 +209,27 @@ impl Region {
|
||||||
Align::Center => self.available_width() / 2.0 - width / 2.0,
|
Align::Center => self.available_width() / 2.0 - width / 2.0,
|
||||||
Align::Max => self.available_width() - width,
|
Align::Max => self.available_width() - width,
|
||||||
};
|
};
|
||||||
self.relative_region(Rect::from_min_size(
|
self.child_region(Rect::from_min_size(
|
||||||
pos2(x, 0.0),
|
self.cursor + vec2(x, 0.0),
|
||||||
vec2(width, self.available_height()),
|
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)
|
pub fn inner_layout<F>(&mut self, dir: Direction, align: Align, add_contents: F)
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Region),
|
F: FnOnce(&mut Region),
|
||||||
{
|
{
|
||||||
let child_rect = Rect::from_min_max(self.cursor, self.desired_rect.max());
|
let child_rect = Rect::from_min_max(self.cursor, self.desired_rect.max());
|
||||||
let mut child_region = Region {
|
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,
|
dir,
|
||||||
align,
|
align,
|
||||||
|
..self.child_region(child_rect)
|
||||||
};
|
};
|
||||||
add_contents(&mut child_region);
|
add_contents(&mut child_region);
|
||||||
let size = child_region.bounding_size;
|
let size = child_region.bounding_size;
|
||||||
self.reserve_space_without_padding(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
|
/// Start a region with horizontal layout
|
||||||
pub fn horizontal<F>(&mut self, align: Align, add_contents: F)
|
pub fn horizontal<F>(&mut self, align: Align, add_contents: F)
|
||||||
where
|
where
|
||||||
|
@ -301,18 +266,11 @@ impl Region {
|
||||||
let pos = self.cursor + vec2((col_idx as f32) * (column_width + padding), 0.0);
|
let pos = self.cursor + vec2((col_idx as f32) * (column_width + padding), 0.0);
|
||||||
let child_rect =
|
let child_rect =
|
||||||
Rect::from_min_max(pos, pos2(pos.x + column_width, self.desired_rect.max().y));
|
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),
|
Region {
|
||||||
desired_rect: child_rect,
|
id: self.make_child_region_id(&("column", col_idx)),
|
||||||
bounding_size: vec2(0.0, 0.0),
|
|
||||||
cursor: child_rect.min(),
|
|
||||||
dir: Direction::Vertical,
|
dir: Direction::Vertical,
|
||||||
align: self.align,
|
..self.child_region(child_rect)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
@ -51,7 +51,8 @@ impl ScrollArea {
|
||||||
let inner_size = outer_size - vec2(scroll_bar_width, 0.0);
|
let inner_size = outer_size - vec2(scroll_bar_width, 0.0);
|
||||||
let inner_rect = Rect::from_min_size(outer_region.cursor, inner_size);
|
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.cursor -= state.offset;
|
||||||
content_region.desired_rect = content_region.desired_rect.translate(-state.offset);
|
content_region.desired_rect = content_region.desired_rect.translate(-state.offset);
|
||||||
add_contents(&mut content_region);
|
add_contents(&mut content_region);
|
||||||
|
|
Loading…
Reference in a new issue