Skip to content

Commit

Permalink
Merge pull request #67 from AryanpurTech/Objects-chained-setters
Browse files Browse the repository at this point in the history
feat: Objects now return reference for chained setters
  • Loading branch information
ElhamAryanpur authored Jul 26, 2024
2 parents 7421479 + 6a083aa commit 3cc6886
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 57 deletions.
3 changes: 1 addition & 2 deletions examples/shapes/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 |_, _, _, _, _, _| {})
Expand Down
34 changes: 12 additions & 22 deletions examples/utils/render_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");
Expand Down
8 changes: 1 addition & 7 deletions examples/utils/resource_sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
46 changes: 29 additions & 17 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -177,6 +179,7 @@ impl Object {
self.inverse_matrices();

self.changed = true;
self
}

/// Resizes an object in pixels which are relative to the window
Expand All @@ -186,7 +189,7 @@ impl Object {
height: f32,
depth: f32,
window_size: winit::dpi::PhysicalSize<u32>,
) {
) -> &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);
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions src/utils/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,10 @@ impl CameraContainer {
&mut self,
renderer: &mut Renderer,
) -> eyre::Result<crate::UniformBuffers> {
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 {
Expand Down

0 comments on commit 3cc6886

Please sign in to comment.