From 6a083aa18ef4d4f0ef70d3aa529ba8ce3a554437 Mon Sep 17 00:00:00 2001 From: Elham Aryanpur Date: Fri, 26 Jul 2024 19:49:29 +0300 Subject: [PATCH] feat: Objects now return reference for chained setters --- examples/shapes/cube.rs | 3 +- examples/shapes/square.rs | 2 +- examples/utils/render_order.rs | 34 ++++++++-------------- examples/utils/resource_sharing.rs | 8 +----- src/objects.rs | 46 +++++++++++++++++++----------- src/render.rs | 6 +--- src/utils/camera.rs | 5 ++-- 7 files changed, 47 insertions(+), 57 deletions(-) diff --git a/examples/shapes/cube.rs b/examples/shapes/cube.rs index b815bc3..9325b26 100644 --- a/examples/shapes/cube.rs +++ b/examples/shapes/cube.rs @@ -14,8 +14,7 @@ fn main() { .objects .get_mut("Cube") .unwrap() - .set_color(0f32, 0f32, 1f32, 1f32) - .unwrap(); + .set_color(0f32, 0f32, 1f32, 1f32); let radius = 5f32; let start = std::time::SystemTime::now(); diff --git a/examples/shapes/square.rs b/examples/shapes/square.rs index 2f1d9f3..c791121 100644 --- a/examples/shapes/square.rs +++ b/examples/shapes/square.rs @@ -50,7 +50,7 @@ pub fn square(name: impl StringBuffer, engine: &mut Engine) -> eyre::Result<()> fn main() { let mut engine = Engine::new().expect("win"); - let _ = square("Square", &mut engine).unwrap(); + square("Square", &mut engine).unwrap(); engine .update_loop(move |_, _, _, _, _, _| {}) diff --git a/examples/utils/render_order.rs b/examples/utils/render_order.rs index 5a89bdd..a65ee9e 100644 --- a/examples/utils/render_order.rs +++ b/examples/utils/render_order.rs @@ -30,32 +30,22 @@ fn main() { .expect("failed to create square"); // Get layer 1 object - let layer1 = engine + engine .objects .get_mut("layer1") - .expect("failed to gete object"); - // set a color to differentiate it - layer1 - .set_color(1f32, 0.5, 0f32, 1f32) - .expect("failed to set color"); - // move it to left a bit - layer1.set_position(-0.5, 0f32, 0f32); - // set render order to 0th - layer1.set_render_order(0).unwrap(); + .expect("failed to gete object") + .set_color(1f32, 0.5, 0f32, 1f32) // set a color to differentiate it + .set_position(-0.5, 0f32, 0f32) // move it to left a bit + .set_render_order(0); // set render order to 0th // Get layer 2 object - let layer2 = engine + engine .objects .get_mut("layer2") - .expect("failed to gete object"); - // set a color to differentiate it - layer2 - .set_color(0f32, 0f32, 1f32, 1f32) - .expect("failed to set color"); - // move it to right a bit - layer2.set_position(0.5, 0f32, 0f32); - // set render order to 1st - layer2.set_render_order(1).unwrap(); + .expect("failed to gete object") + .set_color(0f32, 0f32, 1f32, 1f32) // set a color to differentiate it + .set_position(0.5, 0f32, 0f32) // move it to right a bit + .set_render_order(1); // set render order to 1st // get a timer for order change let start = std::time::SystemTime::now(); @@ -68,10 +58,10 @@ fn main() { // on ever 2 seconds change order if start.elapsed().unwrap().as_secs() % 2 == 0 { - target.set_render_order(2).unwrap(); + target.set_render_order(2); } else { // change back to default - target.set_render_order(0).unwrap(); + target.set_render_order(0); } }) .expect("Error during update loop"); diff --git a/examples/utils/resource_sharing.rs b/examples/utils/resource_sharing.rs index 2b37a34..679d543 100644 --- a/examples/utils/resource_sharing.rs +++ b/examples/utils/resource_sharing.rs @@ -36,13 +36,7 @@ fn main() { .get_mut("main") .unwrap() .set_texture(texture) - .expect("Error during inserting texture to the main square"); - // set position to make it visible - engine - .objects - .get_mut("main") - .expect("Error during setting the position of the main square") - .set_position(-1.5f32, 0f32, 0f32); + .set_position(-1.5f32, 0f32, 0f32); // set position to make it visible // create another object where you want to get resources shared with square( diff --git a/src/objects.rs b/src/objects.rs index bf3bf7c..e35b3d0 100644 --- a/src/objects.rs +++ b/src/objects.rs @@ -161,12 +161,14 @@ impl ObjectStorage { impl Object { /// Sets the name of the object - pub fn set_name(&mut self, name: impl StringBuffer) { + pub fn set_name(&mut self, name: impl StringBuffer) -> &mut Self { self.name = name.as_arc(); + + self } /// Scales an object. e.g. 2.0 doubles the size and 0.5 halves - pub fn set_scale(&mut self, x: f32, y: f32, z: f32) { + pub fn set_scale(&mut self, x: f32, y: f32, z: f32) -> &mut Self { self.size.x *= x; self.size.y *= y; self.size.z *= z; @@ -177,6 +179,7 @@ impl Object { self.inverse_matrices(); self.changed = true; + self } /// Resizes an object in pixels which are relative to the window @@ -186,7 +189,7 @@ impl Object { height: f32, depth: f32, window_size: winit::dpi::PhysicalSize, - ) { + ) -> &mut Self { let difference_in_width = if self.size.x != 0.0 && width != 0.0 { let a = pixel_to_cartesian(width, window_size.width); let b = pixel_to_cartesian(self.size.x, window_size.width); @@ -227,10 +230,11 @@ impl Object { difference_in_height, difference_in_depth, ); + self } /// Rotates the object in the axis you specify - pub fn set_rotatation(&mut self, angle: f32, axis: RotateAxis) { + pub fn set_rotatation(&mut self, angle: f32, axis: RotateAxis) -> &mut Self { // The reason for using different transformation matrix is because // of alteration of translation that happens due to rotation. The // solution suggested by https://github.com/tksuoran fixed this through @@ -256,10 +260,11 @@ impl Object { self.inverse_matrices(); self.changed = true; + self } /// Moves the object by the amount you specify in the axis you specify - pub fn set_translation(&mut self, x: f32, y: f32, z: f32) { + pub fn set_translation(&mut self, x: f32, y: f32, z: f32) -> &mut Self { self.position.x -= x; self.position.y -= y; self.position.z -= z; @@ -270,10 +275,11 @@ impl Object { self.inverse_matrices(); self.changed = true; + self } /// Sets the position of the object in 3D space relative to the window - pub fn set_position(&mut self, x: f32, y: f32, z: f32) { + pub fn set_position(&mut self, x: f32, y: f32, z: f32) -> &mut Self { self.set_translation( (self.position.x - x) * -1f32, (self.position.y - y) * -1f32, @@ -283,32 +289,33 @@ impl Object { self.position.x = x; self.position.y = y; self.position.z = z; + self } /// Changes the color of the object. If textures exist, the color of textures will change - pub fn set_color(&mut self, red: f32, green: f32, blue: f32, alpha: f32) -> eyre::Result<()> { + pub fn set_color(&mut self, red: f32, green: f32, blue: f32, alpha: f32) -> &mut Self { self.color = Array4 { data: [red, green, blue, alpha], }; self.changed = true; - Ok(()) + self } /// Changes the render order of the Object. /// /// Objects with higher number get rendered later and appear "on top" when occupying the same space - pub fn set_render_order(&mut self, render_order: usize) -> eyre::Result<()> { + pub fn set_render_order(&mut self, render_order: usize) -> &mut Self { self.render_order = render_order; - Ok(()) + self } /// Replaces the object's texture with provided one - pub fn set_texture(&mut self, texture: Textures) -> eyre::Result<()> { + pub fn set_texture(&mut self, texture: Textures) -> &mut Self { self.pipeline.texture = PipelineData::Data(texture); self.changed = true; - Ok(()) + self } /// This will flag object as changed and altered, leading to rebuilding parts, or entirety on next frame. @@ -479,30 +486,35 @@ impl Object { // ============================= FOR COPY OF PIPELINES ============================= /// References another object's vertices - pub fn reference_vertices(&mut self, object_id: impl StringBuffer) { + pub fn reference_vertices(&mut self, object_id: impl StringBuffer) -> &mut Self { self.pipeline.vertex_buffer = PipelineData::Copy(object_id.as_string()); + self } /// References another object's shader - pub fn reference_shader(&mut self, object_id: impl StringBuffer) { + pub fn reference_shader(&mut self, object_id: impl StringBuffer) -> &mut Self { self.pipeline.shader = PipelineData::Copy(object_id.as_string()); + self } /// References another object's texture - pub fn reference_texture(&mut self, object_id: impl StringBuffer) { + pub fn reference_texture(&mut self, object_id: impl StringBuffer) -> &mut Self { self.pipeline.texture = PipelineData::Copy(object_id.as_string()); + self } /// References another object's uniform buffer - pub fn reference_uniform_buffer(&mut self, object_id: impl StringBuffer) { + pub fn reference_uniform_buffer(&mut self, object_id: impl StringBuffer) -> &mut Self { self.pipeline.uniform = PipelineData::Copy(object_id.as_string()); + self } // ============================= Instances ============================= /// Add an instance to the object - pub fn add_instance(&mut self, instance: Instance) { + pub fn add_instance(&mut self, instance: Instance) -> &mut Self { self.instances.push(instance); self.changed = true; + self } } diff --git a/src/render.rs b/src/render.rs index 5bf844a..ca9b8a8 100644 --- a/src/render.rs +++ b/src/render.rs @@ -306,11 +306,7 @@ impl Renderer { &[], ); } else { - render_pass.set_bind_group( - 1, - &camera.get("main".into()).unwrap().uniform_data, - &[], - ); + render_pass.set_bind_group(1, &camera.get("main").unwrap().uniform_data, &[]); } if i.is_visible { diff --git a/src/utils/camera.rs b/src/utils/camera.rs index c04d721..0912434 100644 --- a/src/utils/camera.rs +++ b/src/utils/camera.rs @@ -298,11 +298,10 @@ impl CameraContainer { &mut self, renderer: &mut Renderer, ) -> eyre::Result { - Ok(self - .cameras + self.cameras .get_mut("main") .unwrap() - .update_view_projection_and_return(renderer)?) + .update_view_projection_and_return(renderer) } /// Builds a view matrix for camera projection pub fn build_view_matrix(&self) -> nalgebra_glm::Mat4 {