Browse Source

WIP: Working clip rects for glium

clip_rect
Emil Ernerfeldt 5 years ago
parent
commit
46f123eeb6
  1. 16
      emigui/src/math.rs
  2. 44
      emigui/src/region.rs
  3. 2
      emigui/src/types.rs
  4. 19
      emigui_glium/src/painter.rs

16
emigui/src/math.rs

@ -63,6 +63,10 @@ impl Vec2 {
vec2(self.x.ceil(), self.y.ceil()) 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 { pub fn min(self, other: Vec2) -> Self {
vec2(self.x.min(other.x), self.y.min(other.y)) vec2(self.x.min(other.x), self.y.min(other.y))
} }
@ -195,6 +199,10 @@ impl Pos2 {
pub fn ceil(self) -> Self { pub fn ceil(self) -> Self {
pos2(self.x.ceil(), self.y.ceil()) 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<Vec2> for Pos2 { impl std::ops::AddAssign<Vec2> for Pos2 {
@ -342,6 +350,14 @@ impl Rect {
self.max.y - self.min.y 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): // Convenience functions (assumes origin is towards left top):
pub fn left_top(&self) -> Pos2 { pub fn left_top(&self) -> Pos2 {

44
emigui/src/region.rs

@ -6,39 +6,49 @@ use crate::{font::TextFragment, layout::*, widgets::*, *};
/// with a type of layout (horizontal or vertical). /// with a type of layout (horizontal or vertical).
/// TODO: make Region a trait so we can have type-safe HorizontalRegion etc? /// TODO: make Region a trait so we can have type-safe HorizontalRegion etc?
pub struct Region { pub struct Region {
// TODO: remove pub(crate) from all members.
//
/// How we access input, output and memory
pub(crate) ctx: Arc<Context>, pub(crate) ctx: Arc<Context>,
/// Where to put the graphics output of this Region /// Where to put the graphics output of this Region
pub(crate) layer: Layer, pub(crate) layer: Layer,
pub(crate) style: Style, /// ID of this region.
/// Generated based on id of parent region together with
/// Unique ID of this region. /// another source of child identity (e.g. window title).
/// Acts like a namespace for child regions.
/// Hopefully unique.
pub(crate) id: Id, 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. /// Doesn't change.
pub(crate) dir: Direction, pub(crate) dir: Direction,
pub(crate) align: Align, 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. /// Where the next widget will be put.
/// Progresses along self.dir. /// Progresses along self.dir.
/// Initially set to rect.min() /// Initially set to rect.min()
pub(crate) cursor: Pos2, 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 { impl Region {

2
emigui/src/types.rs

@ -120,10 +120,10 @@ pub enum PaintCmd {
outline: Option<Outline>, outline: Option<Outline>,
}, },
Rect { Rect {
rect: Rect,
corner_radius: f32, corner_radius: f32,
fill_color: Option<Color>, fill_color: Option<Color>,
outline: Option<Outline>, outline: Option<Outline>,
rect: Rect,
}, },
/// Paint a single line of text /// Paint a single line of text
Text { Text {

19
emigui_glium/src/painter.rs

@ -60,8 +60,8 @@ impl Painter {
void main() { void main() {
if (v_pos.x < v_clip_rect.x) { discard; } if (v_pos.x < v_clip_rect.x) { discard; }
if (v_pos.y < v_clip_rect.y) { discard; } if (v_pos.y < v_clip_rect.y) { discard; }
// if (v_pos.x < v_clip_rect.z) { discard; } // TODO if (v_pos.x > v_clip_rect.z) { discard; }
// if (v_pos.y > v_clip_rect.w) { discard; } // TODO if (v_pos.y > v_clip_rect.w) { discard; }
f_color = v_color; f_color = v_color;
f_color.rgb = linear_from_srgb(f_color.rgb); f_color.rgb = linear_from_srgb(f_color.rgb);
f_color.a *= texture(u_sampler, v_tc).r; 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, 1.0 - 2.0 * a_pos.y / u_screen_size.y,
0.0, 0.0,
1.0); 1.0);
v_pos = a_pos;
v_color = a_color / 255.0; v_color = a_color / 255.0;
v_tc = a_tc / u_tex_size; v_tc = a_tc / u_tex_size;
v_clip_rect = u_clip_rect;
} }
", ",
@ -106,6 +108,10 @@ impl Painter {
} }
void main() { 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 = v_color;
gl_FragColor.rgb = linear_from_srgb(gl_FragColor.rgb); gl_FragColor.rgb = linear_from_srgb(gl_FragColor.rgb);
gl_FragColor.a *= texture2D(u_sampler, v_tc).r; 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, 1.0 - 2.0 * a_pos.y / u_screen_size.y,
0.0, 0.0,
1.0); 1.0);
v_pos = a_pos;
v_color = a_color / 255.0; v_color = a_color / 255.0;
v_tc = a_tc / u_tex_size; v_tc = a_tc / u_tex_size;
v_clip_rect = u_clip_rect;
} }
", ",
@ -150,6 +158,10 @@ impl Painter {
} }
void main() { 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 = v_color;
gl_FragColor.rgb = linear_from_srgb(gl_FragColor.rgb); gl_FragColor.rgb = linear_from_srgb(gl_FragColor.rgb);
gl_FragColor.a *= texture2D(u_sampler, v_tc).r; 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 height_points = height_pixels as f32 / pixels_per_point;
let uniforms = uniform! { let uniforms = uniform! {
u_clip_rect_min: [clip_rect.min().x, clip_rect.min().y], u_clip_rect: [clip_rect.min().x, clip_rect.min().y, clip_rect.max().x, clip_rect.max().y],
u_clip_rect_max: [clip_rect.max().x, clip_rect.max().y],
u_screen_size: [width_points, height_points], u_screen_size: [width_points, height_points],
u_tex_size: [texture.width as f32, texture.height as f32], u_tex_size: [texture.width as f32, texture.height as f32],
u_sampler: &self.texture, u_sampler: &self.texture,

Loading…
Cancel
Save