Skip to content

Commit

Permalink
avoid doubling first point
Browse files Browse the repository at this point in the history
  • Loading branch information
rectalogic committed Feb 14, 2025
1 parent 8d2e67c commit 19243b1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
86 changes: 38 additions & 48 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use bevy::{
ecs::query::{QueryData, QueryEntityError},
prelude::*,
render::view::RenderLayers,
window::PrimaryWindow,
};

pub(super) fn plugin(app: &mut App) {
Expand Down Expand Up @@ -126,62 +125,53 @@ fn start_drawing(
mut undo: ResMut<Undo>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<PointsMaterial>>,
window: Single<&Window, With<PrimaryWindow>>,
camera_query: Query<(&Camera, &RenderLayers, &GlobalTransform, &Interpolated)>,
camera_query: Query<(&RenderLayers, &Interpolated)>,
) {
let interpolation_type = match state.get() {
AppState::Draw(interpolated) => interpolated,
_ => return,
};

if let Some(window_position) = window.cursor_position() {
for (camera, camera_render_layers, camera_transform, camera_interpolation_type) in
&camera_query
{
if camera_interpolation_type != interpolation_type {
continue;
}

let count = match camera_interpolation_type {
Interpolated::Source => {
drawing_count.source += 1;
drawing_count.source
}
for (camera_render_layers, camera_interpolation_type) in &camera_query {
if camera_interpolation_type != interpolation_type {
continue;
}

Interpolated::Target => {
drawing_count.target += 1;
drawing_count.target
}
};
let count = match camera_interpolation_type {
Interpolated::Source => {
drawing_count.source += 1;
drawing_count.source
}

if let Some(world_position) =
window_position_to_world(camera, camera_transform, window_position)
{
let entity = commands
.spawn((
ActiveDrawing,
DrawingNumber(count),
camera_render_layers.clone(),
*camera_interpolation_type,
Mesh2d(meshes.add(Mesh::build(&Points(vec![world_position])))),
Transform::from_xyz(0., 0., count as f32), // use count as Z index
MeshMaterial2d(materials.add(PointsMaterial {
source_settings: PointsSettings {
color: brush.color.into(),
radius: brush.radius,
},
target_settings: PointsSettings {
color: brush.color.into(),
radius: brush.radius,
},
t: 0.0,
})),
))
.id();

undo.add(entity);
Interpolated::Target => {
drawing_count.target += 1;
drawing_count.target
}
}
};

let entity = commands
.spawn((
ActiveDrawing,
DrawingNumber(count),
camera_render_layers.clone(),
*camera_interpolation_type,
Mesh2d(meshes.add(Mesh::build(None))),
Transform::from_xyz(0., 0., count as f32), // use count as Z index
MeshMaterial2d(materials.add(PointsMaterial {
source_settings: PointsSettings {
color: brush.color.into(),
radius: brush.radius,
},
target_settings: PointsSettings {
color: brush.color.into(),
radius: brush.radius,
},
t: 0.0,
})),
))
.id();

undo.add(entity);
}
}

Expand Down
30 changes: 20 additions & 10 deletions src/points/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,33 @@ impl TryFrom<&VertexAttributeValues> for Points {
}

pub(crate) trait PointsMeshBuilder {
fn build(points: &Points) -> Mesh;
fn empty() -> Mesh;
fn build(points: Option<&Points>) -> Mesh;
fn build_interpolated<T>(source: T, target: T) -> Result<Mesh>
where
T: Into<VertexAttributeValues>;
fn to_points(&self) -> Result<(Points, Points), &'static str>;
}

impl PointsMeshBuilder for Mesh {
fn build(points: &Points) -> Mesh {
fn empty() -> Mesh {
Mesh::new(
bevy::render::mesh::PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, points)
}

fn build(points: Option<&Points>) -> Mesh {
let mut mesh = Mesh::empty();
if let Some(points) = points {
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, points);
} else {
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
VertexAttributeValues::Float32x3(Vec::new()),
);
}
mesh
}

fn build_interpolated<T>(source: T, target: T) -> Result<Mesh>
Expand All @@ -145,12 +158,9 @@ impl PointsMeshBuilder for Mesh {
"source and target have different number of vertices"
));
}
Ok(Mesh::new(
bevy::render::mesh::PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, source)
.with_inserted_attribute(ATTRIBUTE_TARGET_POSITION, target))
Ok(Mesh::empty()
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, source)
.with_inserted_attribute(ATTRIBUTE_TARGET_POSITION, target))
}

fn to_points(&self) -> Result<(Points, Points), &'static str> {
Expand Down Expand Up @@ -210,7 +220,7 @@ mod tests {
#[test]
fn test_mesh_builder() {
let points = Points(vec![Vec2::new(1.0, 2.0), Vec2::new(3.0, 4.0)]);
let mesh = Mesh::build(&points);
let mesh = Mesh::build(Some(&points));
let result = mesh.to_points();
assert!(result.is_err());
}
Expand Down

0 comments on commit 19243b1

Please sign in to comment.