#[repr(C)]pub struct Rect {
pub min: Pos2,
pub max: Pos2,
}Expand description
A rectangular region of space.
Usually a Rect has a positive (or zero) size,
and then Self::min <= Self::max.
In these cases Self::min is the left-top corner
and Self::max is the right-bottom corner.
A rectangle is allowed to have a negative size, which happens when the order
of min and max are swapped. These are usually a sign of an error.
Normally the unit is points (logical pixels) in screen space coordinates.
Fields§
§min: Pos2One of the corners of the rectangle, usually the left top one.
max: Pos2The other corner, opposing Self::min. Usually the right bottom one.
Implementations§
Source§impl Rect
impl Rect
Sourcepub const EVERYTHING: Rect
pub const EVERYTHING: Rect
Infinite rectangle that contains every point.
Sourcepub const NOTHING: Rect
pub const NOTHING: Rect
The inverse of Self::EVERYTHING: stretches from positive infinity to negative infinity.
Contains no points.
This is useful as the seed for bounding boxes.
§Example:
let mut rect = Rect::NOTHING;
assert!(rect.size() == Vec2::splat(-f32::INFINITY));
assert!(rect.contains(pos2(0.0, 0.0)) == false);
rect.extend_with(pos2(2.0, 1.0));
rect.extend_with(pos2(0.0, 3.0));
assert_eq!(rect, Rect::from_min_max(pos2(0.0, 1.0), pos2(2.0, 3.0)))pub const fn from_min_max(min: Pos2, max: Pos2) -> Rect
Sourcepub fn from_min_size(min: Pos2, size: Vec2) -> Rect
pub fn from_min_size(min: Pos2, size: Vec2) -> Rect
left-top corner plus a size (stretching right-down).
Examples found in repository?
43fn main() {
44 // initialize GLFW3 with OpenGL ES 3.0
45 let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
46 glfw.window_hint(glfw::WindowHint::ContextCreationApi(
47 glfw::ContextCreationApi::Egl,
48 ));
49 glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::OpenGlEs));
50 glfw.window_hint(glfw::WindowHint::ContextVersion(3, 0));
51 glfw.window_hint(glfw::WindowHint::OpenGlProfile(
52 glfw::OpenGlProfileHint::Core,
53 ));
54 glfw.window_hint(glfw::WindowHint::DoubleBuffer(true));
55 glfw.window_hint(glfw::WindowHint::Resizable(true));
56 glfw.window_hint(glfw::WindowHint::Floating(true));
57
58 let (mut window, events) = glfw
59 .create_window(1024, 900, "Egui Test", glfw::WindowMode::Windowed)
60 .expect("Failed to create GLFW window.");
61
62 window.set_char_polling(true);
63 window.set_cursor_pos_polling(true);
64 window.set_key_polling(true);
65 window.set_mouse_button_polling(true);
66 window.make_current();
67 glfw.set_swap_interval(glfw::SwapInterval::Sync(1));
68
69 let mut driver = renderer::get_driver();
70
71 // initialize EGUI
72 let mut egui_ctx = egui::Context::default();
73 let mut painter = ui::Painter::new(&mut driver, &mut window, 1024, 900);
74
75 let (width, height) = window.get_framebuffer_size();
76 let native_pixels_per_point = window.get_content_scale().0;
77
78 println!("pixels per point: {}", native_pixels_per_point);
79 let mut egui_input_state = ui::EguiInputState::new(egui::RawInput {
80 screen_rect: Some(egui::Rect::from_min_size(
81 egui::Pos2::new(0f32, 0f32),
82 egui::vec2(width as f32, height as f32) / native_pixels_per_point,
83 )),
84 pixels_per_point: Some(native_pixels_per_point),
85 ..Default::default()
86 });
87
88 let start_time = std::time::Instant::now();
89 let mut quit = false;
90 while !window.should_close() {
91 let (width, height) = window.get_framebuffer_size();
92 painter.set_canvas_size(width as u32, height as u32);
93 let native_pixels_per_point = window.get_content_scale().0;
94 egui_input_state.egui_input.screen_rect = Some(egui::Rect::from_min_size(
95 egui::Pos2::new(0f32, 0f32),
96 egui::vec2(width as f32, height as f32) / native_pixels_per_point,
97 ));
98 egui_input_state.egui_input.time = Some(start_time.elapsed().as_secs_f64());
99
100 let egui_output = egui_ctx.run(egui_input_state.egui_input.take(), |egui_ctx| {
101 egui::SidePanel::left("Test").show(&egui_ctx, |ui| {
102 if ui.button("click me!").clicked() {
103 println!("Clicked")
104 }
105 });
106 });
107
108 //Handle cut, copy text from egui
109 if !egui_output.platform_output.copied_text.is_empty() {
110 ui::copy_to_clipboard(
111 &mut egui_input_state,
112 egui_output.platform_output.copied_text,
113 );
114 }
115
116 let paint_jobs = egui_ctx.tessellate(egui_output.shapes);
117 let mut pass = Pass::new(
118 width as usize,
119 height as usize,
120 None,
121 [
122 ColorPassAction::Clear(color4b(0x7F, 0x7F, 0x7F, 0xFF)),
123 ColorPassAction::Previous,
124 ColorPassAction::Previous,
125 ColorPassAction::Previous,
126 ],
127 DepthPassAction::Clear(1.0),
128 );
129
130 //Note: passing a bg_color to paint_jobs will clear any previously drawn stuff.
131 //Use this only if egui is being used for all drawing and you aren't mixing your own Open GL
132 //drawing calls with it.
133 //Since we are custom drawing an OpenGL Triangle we don't need egui to clear the background.
134 painter.paint_jobs(
135 &mut pass,
136 paint_jobs,
137 &egui_output.textures_delta,
138 native_pixels_per_point,
139 );
140
141 driver.render_pass(&mut pass);
142 window.swap_buffers();
143
144 glfw.poll_events();
145 for (_, event) in glfw::flush_messages(&events) {
146 match event {
147 glfw::WindowEvent::Key(glfw::Key::Escape, _, _, _) | glfw::WindowEvent::Close => {
148 quit = true
149 }
150 _ => neocogi::ui::handle_event(event, &mut egui_input_state),
151 }
152 }
153
154 if quit {
155 window.set_should_close(true)
156 }
157 }
158}More examples
46fn main() {
47 // initialize GLFW3 with OpenGL ES 3.0
48 let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
49 glfw.window_hint(glfw::WindowHint::ContextCreationApi(
50 glfw::ContextCreationApi::Egl,
51 ));
52 glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::OpenGlEs));
53 glfw.window_hint(glfw::WindowHint::ContextVersion(3, 0));
54 glfw.window_hint(glfw::WindowHint::OpenGlProfile(
55 glfw::OpenGlProfileHint::Core,
56 ));
57 glfw.window_hint(glfw::WindowHint::DoubleBuffer(true));
58 glfw.window_hint(glfw::WindowHint::Resizable(true));
59 glfw.window_hint(glfw::WindowHint::Floating(true));
60
61 let (mut window, events) = glfw
62 .create_window(1024, 900, "Grid", glfw::WindowMode::Windowed)
63 .expect("Failed to create GLFW window.");
64
65 window.set_char_polling(true);
66 window.set_cursor_pos_polling(true);
67 window.set_key_polling(true);
68 window.set_mouse_button_polling(true);
69 window.make_current();
70 glfw.set_swap_interval(glfw::SwapInterval::Sync(1));
71
72 let mut driver = renderer::get_driver();
73 // utility mesh renderer
74 let mut um_renderer = UMRenderer::new(&mut driver, 65536);
75
76 // initialize EGUI
77 let mut egui_ctx = egui::Context::default();
78 let mut painter = ui::Painter::new(&mut driver, &mut window, 1024, 900);
79
80 let (width, height) = window.get_framebuffer_size();
81 let native_pixels_per_point = window.get_content_scale().0;
82
83 println!("pixels per point: {}", native_pixels_per_point);
84 let mut egui_input_state = ui::EguiInputState::new(egui::RawInput {
85 screen_rect: Some(egui::Rect::from_min_size(
86 egui::Pos2::new(0f32, 0f32),
87 egui::vec2(width as f32, height as f32) / native_pixels_per_point,
88 )),
89 pixels_per_point: Some(native_pixels_per_point),
90 ..Default::default()
91 });
92
93 let start_time = std::time::Instant::now();
94 let camera = Camera::new(
95 Vec3f::zero(),
96 20.0,
97 Quatf::of_axis_angle(&Vec3f::new(-1.0, 0.0, 0.0), std::f32::consts::PI / 4.0),
98 std::f32::consts::PI / 4.0,
99 1.0,
100 0.1,
101 100.0,
102 );
103 let mut view3d = View3D::new(
104 camera,
105 Dimensioni::new(1024, 900),
106 Box3f::new(
107 &Vec3f::new(-20.0, -20.0, -20.0),
108 &Vec3f::new(20.0, 20.0, 20.0),
109 ),
110 );
111
112 let mut quit = false;
113 while !window.should_close() {
114 let (width, height) = window.get_framebuffer_size();
115 painter.set_canvas_size(width as u32, height as u32);
116 let native_pixels_per_point = window.get_content_scale().0;
117 egui_input_state.egui_input.screen_rect = Some(egui::Rect::from_min_size(
118 egui::Pos2::new(0f32, 0f32),
119 egui::vec2(width as f32, height as f32) / native_pixels_per_point,
120 ));
121 egui_input_state.egui_input.time = Some(start_time.elapsed().as_secs_f64());
122
123 let egui_output = egui_ctx.run(egui_input_state.egui_input.take(), |egui_ctx| {
124 egui::SidePanel::left("Test").show(&egui_ctx, |ui| {
125 ui.label("Navigation Mode:");
126
127 ui.horizontal(|ui| {
128 let mut nav_mode = view3d.get_navigation_mode();
129 ui.selectable_value(&mut nav_mode, NavigationMode::Pan, "Pan");
130 ui.selectable_value(&mut nav_mode, NavigationMode::Orbit, "Orbit");
131
132 view3d.set_navigation_mode(nav_mode);
133 });
134 });
135 });
136
137 //Handle cut, copy text from egui
138 if !egui_output.platform_output.copied_text.is_empty() {
139 ui::copy_to_clipboard(
140 &mut egui_input_state,
141 egui_output.platform_output.copied_text,
142 );
143 }
144
145 let paint_jobs = egui_ctx.tessellate(egui_output.shapes);
146 let mut pass = Pass::new(
147 width as usize,
148 height as usize,
149 None,
150 [
151 ColorPassAction::Clear(color4b(0x7F, 0x7F, 0x7F, 0xFF)),
152 ColorPassAction::Previous,
153 ColorPassAction::Previous,
154 ColorPassAction::Previous,
155 ],
156 DepthPassAction::Clear(1.0),
157 );
158
159 let grid = UMNode::grid_xz(&Vec3f::new(0.0, 0.0, 0.0), 10.0, 20);
160 let axis = UMNode::basis_cone(
161 &Vec3f::zero(),
162 &Vec3f::new(2.0, 0.0, 0.0),
163 &Vec3f::new(0.0, 2.0, 0.0),
164 &Vec3f::new(0.0, 0.0, 2.0),
165 );
166
167 let nodes = UMNode::Assembly(vec![grid, axis]);
168 um_renderer.draw_node(&mut pass, &view3d.pvm(), &nodes);
169
170 //Note: passing a bg_color to paint_jobs will clear any previously drawn stuff.
171 //Use this only if egui is being used for all drawing and you aren't mixing your own Open GL
172 //drawing calls with it.
173 //Since we are custom drawing an OpenGL Triangle we don't need egui to clear the background.
174 painter.paint_jobs(
175 &mut pass,
176 paint_jobs,
177 &egui_output.textures_delta,
178 native_pixels_per_point,
179 );
180
181 driver.render_pass(&mut pass);
182 window.swap_buffers();
183
184 let (xpos, ypos) = window.get_cursor_pos();
185
186 let rel_x = ((xpos as f32) / (width as f32)) * 2.0 - 1.0;
187 let rel_y = -(((ypos as f32) / (height as f32)) * 2.0 - 1.0);
188
189 let pointer_button =
190 if window.get_mouse_button(glfw::MouseButtonLeft) == glfw::Action::Press {
191 pointer::ButtonState::Pressed(1.0)
192 } else {
193 pointer::ButtonState::Released
194 };
195 let pointer_pos = Vec2f::new(rel_x, rel_y);
196
197 view3d.set_dimension(Dimensioni::new(width as i32, height as i32));
198 view3d.set_pointer(pointer_pos, pointer_button);
199
200 glfw.poll_events();
201 for (_, event) in glfw::flush_messages(&events) {
202 match event {
203 glfw::WindowEvent::Key(glfw::Key::Escape, _, _, _) | glfw::WindowEvent::Close => {
204 quit = true
205 }
206 _ => neocogi::ui::handle_event(event, &mut egui_input_state),
207 }
208 }
209
210 if quit {
211 window.set_should_close(true)
212 }
213 }
214}pub fn from_center_size(center: Pos2, size: Vec2) -> Rect
pub fn from_x_y_ranges( x_range: RangeInclusive<f32>, y_range: RangeInclusive<f32>, ) -> Rect
Sourcepub fn from_two_pos(a: Pos2, b: Pos2) -> Rect
pub fn from_two_pos(a: Pos2, b: Pos2) -> Rect
Returns the bounding rectangle of the two points.
Sourcepub fn from_points(points: &[Pos2]) -> Rect
pub fn from_points(points: &[Pos2]) -> Rect
Bounding-box around the points.
Sourcepub fn everything_right_of(left_x: f32) -> Rect
pub fn everything_right_of(left_x: f32) -> Rect
A Rect that contains every point to the right of the given X coordinate.
Sourcepub fn everything_left_of(right_x: f32) -> Rect
pub fn everything_left_of(right_x: f32) -> Rect
A Rect that contains every point to the left of the given X coordinate.
Sourcepub fn everything_below(top_y: f32) -> Rect
pub fn everything_below(top_y: f32) -> Rect
A Rect that contains every point below a certain y coordinate
Sourcepub fn everything_above(bottom_y: f32) -> Rect
pub fn everything_above(bottom_y: f32) -> Rect
A Rect that contains every point above a certain y coordinate
Sourcepub fn expand(self, amnt: f32) -> Rect
pub fn expand(self, amnt: f32) -> Rect
Expand by this much in each direction, keeping the center
Sourcepub fn expand2(self, amnt: Vec2) -> Rect
pub fn expand2(self, amnt: Vec2) -> Rect
Expand by this much in each direction, keeping the center
Sourcepub fn shrink(self, amnt: f32) -> Rect
pub fn shrink(self, amnt: f32) -> Rect
Shrink by this much in each direction, keeping the center
Sourcepub fn shrink2(self, amnt: Vec2) -> Rect
pub fn shrink2(self, amnt: Vec2) -> Rect
Shrink by this much in each direction, keeping the center
pub fn translate(self, amnt: Vec2) -> Rect
pub fn intersects(self, other: Rect) -> bool
Sourcepub fn set_height(&mut self, h: f32)
pub fn set_height(&mut self, h: f32)
keep min
Sourcepub fn set_center(&mut self, center: Pos2)
pub fn set_center(&mut self, center: Pos2)
Keep size
pub fn contains(&self, p: Pos2) -> bool
pub fn contains_rect(&self, other: Rect) -> bool
Sourcepub fn clamp(&self, p: Pos2) -> Pos2
pub fn clamp(&self, p: Pos2) -> Pos2
Return the given points clamped to be inside the rectangle
Panics if Self::is_negative.
pub fn extend_with(&mut self, p: Pos2)
Sourcepub fn extend_with_x(&mut self, x: f32)
pub fn extend_with_x(&mut self, x: f32)
Expand to include the given x coordinate
Sourcepub fn extend_with_y(&mut self, y: f32)
pub fn extend_with_y(&mut self, y: f32)
Expand to include the given y coordinate
Sourcepub fn union(self, other: Rect) -> Rect
pub fn union(self, other: Rect) -> Rect
The union of two bounding rectangle, i.e. the minimum Rect
that contains both input rectangles.
Sourcepub fn intersect(self, other: Rect) -> Rect
pub fn intersect(self, other: Rect) -> Rect
The intersection of two Rect, i.e. the area covered by both.
pub fn center(&self) -> Pos2
pub fn size(&self) -> Vec2
pub fn width(&self) -> f32
pub fn height(&self) -> f32
Sourcepub fn aspect_ratio(&self) -> f32
pub fn aspect_ratio(&self) -> f32
Width / height
aspect_ratio < 1: portrait / highaspect_ratio = 1: squareaspect_ratio > 1: landscape / wide
Sourcepub fn square_proportions(&self) -> Vec2
pub fn square_proportions(&self) -> Vec2
[2, 1] for wide screen, and [1, 2] for portrait, etc.
At least one dimension = 1, the other >= 1
Returns the proportions required to letter-box a square view area.
pub fn area(&self) -> f32
Sourcepub fn distance_to_pos(&self, pos: Pos2) -> f32
pub fn distance_to_pos(&self, pos: Pos2) -> f32
The distance from the rect to the position.
The distance is zero when the position is in the interior of the rectangle.
Sourcepub fn distance_sq_to_pos(&self, pos: Pos2) -> f32
pub fn distance_sq_to_pos(&self, pos: Pos2) -> f32
The distance from the rect to the position, squared.
The distance is zero when the position is in the interior of the rectangle.
Sourcepub fn signed_distance_to_pos(&self, pos: Pos2) -> f32
pub fn signed_distance_to_pos(&self, pos: Pos2) -> f32
Signed distance to the edge of the box.
Negative inside the box.
pub fn x_range(&self) -> RangeInclusive<f32>
pub fn y_range(&self) -> RangeInclusive<f32>
pub fn bottom_up_range(&self) -> RangeInclusive<f32>
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
width < 0 || height < 0
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
width > 0 && height > 0
Source§impl Rect
§Convenience functions (assumes origin is towards left top):
impl Rect
§Convenience functions (assumes origin is towards left top):
Sourcepub fn bottom_mut(&mut self) -> &mut f32
pub fn bottom_mut(&mut self) -> &mut f32
max.y
Sourcepub fn set_bottom(&mut self, y: f32)
pub fn set_bottom(&mut self, y: f32)
max.y