diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 2616f03d..6eb7b262 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -63,6 +63,10 @@ impl Vec2 { vec2(self.x.ceil(), self.y.ceil()) } + pub fn is_finite(&self) -> bool { + self.x.is_finite() && self.y.is_finite() + } + pub fn min(self, other: Vec2) -> Self { vec2(self.x.min(other.x), self.y.min(other.y)) } @@ -195,6 +199,10 @@ impl Pos2 { pub fn ceil(self) -> Self { pos2(self.x.ceil(), self.y.ceil()) } + + pub fn is_finite(&self) -> bool { + self.x.is_finite() && self.y.is_finite() + } } impl std::ops::AddAssign for Pos2 { @@ -342,6 +350,14 @@ impl Rect { self.max.y - self.min.y } + pub fn is_empty(&self) -> bool { + self.max.x < self.min.x || self.max.y < self.min.y + } + + pub fn is_finite(&self) -> bool { + self.min.is_finite() && self.max.is_finite() + } + // Convenience functions (assumes origin is towards left top): pub fn left_top(&self) -> Pos2 { diff --git a/emigui/src/region.rs b/emigui/src/region.rs index 0b5006d5..4316e8c9 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -6,39 +6,49 @@ use crate::{font::TextFragment, layout::*, widgets::*, *}; /// with a type of layout (horizontal or vertical). /// TODO: make Region a trait so we can have type-safe HorizontalRegion etc? pub struct Region { + // TODO: remove pub(crate) from all members. + // + /// How we access input, output and memory pub(crate) ctx: Arc, /// Where to put the graphics output of this Region pub(crate) layer: Layer, - pub(crate) style: Style, - - /// Unique ID of this region. + /// ID of this region. + /// Generated based on id of parent region together with + /// another source of child identity (e.g. window title). + /// Acts like a namespace for child regions. + /// Hopefully unique. pub(crate) id: Id, + /// 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, // TODO: rename desired_rect + + /// Bounding box of children. + /// We keep track of our max-size along the orthogonal to self.dir + /// Initially set to zero. + /// TODO: make into `child_bounds: Rect` + pub(crate) bounding_size: Vec2, + + /// Overide default style in this region + pub(crate) style: Style, + + // Layout stuff follows. TODO: move to own type and abstract. /// 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, - /// Where the next widget will be put. /// Progresses along self.dir. /// Initially set to rect.min() pub(crate) cursor: Pos2, - - /// Bounding box of children. - /// We keep track of our max-size along the orthogonal to self.dir - /// Initially set to zero. - pub(crate) bounding_size: Vec2, } impl Region { diff --git a/emigui/src/types.rs b/emigui/src/types.rs index f1f16562..ce79be4e 100644 --- a/emigui/src/types.rs +++ b/emigui/src/types.rs @@ -120,10 +120,10 @@ pub enum PaintCmd { outline: Option, }, Rect { + rect: Rect, corner_radius: f32, fill_color: Option, outline: Option, - rect: Rect, }, /// Paint a single line of text Text { diff --git a/emigui_glium/src/painter.rs b/emigui_glium/src/painter.rs index b6b182e5..7e467d6a 100644 --- a/emigui_glium/src/painter.rs +++ b/emigui_glium/src/painter.rs @@ -60,8 +60,8 @@ impl Painter { void main() { if (v_pos.x < v_clip_rect.x) { discard; } if (v_pos.y < v_clip_rect.y) { discard; } - // if (v_pos.x < v_clip_rect.z) { discard; } // TODO - // if (v_pos.y > v_clip_rect.w) { discard; } // TODO + if (v_pos.x > v_clip_rect.z) { discard; } + if (v_pos.y > v_clip_rect.w) { discard; } f_color = v_color; f_color.rgb = linear_from_srgb(f_color.rgb); f_color.a *= texture(u_sampler, v_tc).r; @@ -86,8 +86,10 @@ impl Painter { 1.0 - 2.0 * a_pos.y / u_screen_size.y, 0.0, 1.0); + v_pos = a_pos; v_color = a_color / 255.0; v_tc = a_tc / u_tex_size; + v_clip_rect = u_clip_rect; } ", @@ -106,6 +108,10 @@ impl Painter { } void main() { + if (v_pos.x < v_clip_rect.x) { discard; } + if (v_pos.y < v_clip_rect.y) { discard; } + if (v_pos.x > v_clip_rect.z) { discard; } + if (v_pos.y > v_clip_rect.w) { discard; } gl_FragColor = v_color; gl_FragColor.rgb = linear_from_srgb(gl_FragColor.rgb); gl_FragColor.a *= texture2D(u_sampler, v_tc).r; @@ -130,8 +136,10 @@ impl Painter { 1.0 - 2.0 * a_pos.y / u_screen_size.y, 0.0, 1.0); + v_pos = a_pos; v_color = a_color / 255.0; v_tc = a_tc / u_tex_size; + v_clip_rect = u_clip_rect; } ", @@ -150,6 +158,10 @@ impl Painter { } void main() { + if (v_pos.x < v_clip_rect.x) { discard; } + if (v_pos.y < v_clip_rect.y) { discard; } + if (v_pos.x > v_clip_rect.z) { discard; } + if (v_pos.y > v_clip_rect.w) { discard; } gl_FragColor = v_color; gl_FragColor.rgb = linear_from_srgb(gl_FragColor.rgb); gl_FragColor.a *= texture2D(u_sampler, v_tc).r; @@ -247,8 +259,7 @@ impl Painter { let height_points = height_pixels as f32 / pixels_per_point; let uniforms = uniform! { - u_clip_rect_min: [clip_rect.min().x, clip_rect.min().y], - u_clip_rect_max: [clip_rect.max().x, clip_rect.max().y], + u_clip_rect: [clip_rect.min().x, clip_rect.min().y, clip_rect.max().x, clip_rect.max().y], u_screen_size: [width_points, height_points], u_tex_size: [texture.width as f32, texture.height as f32], u_sampler: &self.texture,