Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Implement the Center pivot point for lines #214

Merged
merged 1 commit into from
Oct 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/core/components/shapes/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Range;
use wgpu::{util::BufferInitDescriptor, PrimitiveTopology};

use crate::{
core::components::{material::Material, maths::coordinates::Coordinates},
core::components::{material::Material, maths::{coordinates::Coordinates, Pivot}},
rendering::{gl_representations::TexturedGlVertex, Renderable2D},
};

Expand All @@ -18,9 +18,23 @@ pub struct Line {
impl Line {
/// Creates a new line using `vertices`.
pub fn new(vertices: [Coordinates; 2]) -> Self {
Line::new_with_offset(vertices, Coordinates::new(0., 0.))
}

/// Sets the pivot point of the line and returns it
pub fn pivot(self, pivot: Pivot) -> Self {
let offset = match pivot {
Pivot::TopLeft => Coordinates::new(0., 0.),
Pivot::Center => Coordinates::new(-(self.vertices[1].x - self.vertices[0].x).abs()/2., -(self.vertices[1].y - self.vertices[0].y).abs()/2.),
};
Line::new_with_offset(self.vertices, offset)
}

/// Creates a new line using `vertices`.
pub fn new_with_offset(vertices: [Coordinates; 2], offset: Coordinates) -> Self {
let contents = [
TexturedGlVertex::from((&vertices[0], &Coordinates::new(0., 0.))),
TexturedGlVertex::from((&vertices[1], &Coordinates::new(0., 0.))),
TexturedGlVertex::from((&Coordinates::new(&vertices[0].x + offset.x, &vertices[0].y + offset.y), &Coordinates::new(0., 0.))),
TexturedGlVertex::from((&Coordinates::new(&vertices[1].x + offset.x, &vertices[1].y + offset.y), &Coordinates::new(0., 0.))),
];
Self { vertices, contents }
}
Expand Down