Skip to content

Commit

Permalink
refactor: apply clippy suggestions
Browse files Browse the repository at this point in the history
Just overall refactors around the board, over 20 Clippy-suggested changes so that CI can finally accept my code.
  • Loading branch information
DakshG07 committed Sep 4, 2023
1 parent bfbff5a commit 0a1e4d6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 88 deletions.
10 changes: 5 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ impl ComplexAngle {
// Further limit range from -90 to 90
// Since these angles are still bidirectional, it's fine to add/subtract 180
if res_angle > 90 {
res_angle = res_angle - 180;
res_angle -= 180;
} else if res_angle < -90 {
res_angle = res_angle + 180;
res_angle += 180;
}
Self {
angle: res_angle,
Expand Down Expand Up @@ -107,7 +107,7 @@ impl ComplexAngle {
}
// If directions differ, subtract 180 from the result
// And we normalize the angle for good measure
return Self::normalize((self.angle - angle.angle) - 180);
Self::normalize((self.angle - angle.angle) - 180)
}
}

Expand Down Expand Up @@ -185,7 +185,7 @@ impl PathyApp {
let mut distance: f32 =
// f32::sqrt((pos.x - prev.x).powi(2) + (pos.x - prev.x).powi(2)).round() as i32;
cx / angle.to_radians().cos();
if angle <= 1.0 && angle >= -1.0 {
if (-1.0..=1.0).contains(&angle) {
// It's basically straight
angle = 0.0;
}
Expand Down Expand Up @@ -232,7 +232,7 @@ impl PathyApp {
.as_slice()
.concat()
}
pub fn generate(processes: &Vec<Process>) -> String {
pub fn generate(processes: &[Process]) -> String {
let mut result = String::from("// The following code was generated by Pathy:");
processes.iter().for_each(|process| match *process {
Process::Turn(angle) => {
Expand Down
152 changes: 69 additions & 83 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl eframe::App for PathyApp {
ui.heading("Settings");

// Size settings
let response = ui.add_enabled_ui(path.len() == 0, |ui| {
let response = ui.add_enabled_ui(path.is_empty(), |ui| {
ui.label("Field Dimensions:");
ui.horizontal(|ui| {
ui.add(egui::DragValue::new(height));
Expand All @@ -90,7 +90,7 @@ impl eframe::App for PathyApp {
});
ui.add(egui::Slider::new(scale, 0.0..=1000.0).text("Scale"));
});
if path.len() != 0 {
if !path.is_empty() {
response.response.on_hover_text_at_pointer(
"Size settings may not be changed once you've created a path.",
);
Expand Down Expand Up @@ -155,7 +155,7 @@ impl eframe::App for PathyApp {
// Order here is important, as the ui button is only rendered if the first
// condition is true. Otherwise, there's no point in evaluating the second
// condition, thus not rendering the button.
if processed.len() != 0 && ui.button("Generate").clicked() {
if !processed.is_empty() && ui.button("Generate").clicked() {
*result = Some(Self::generate(processed));
}
if *mode != CursorMode::Default && ui.button("Finish").clicked() {
Expand All @@ -178,15 +178,14 @@ impl eframe::App for PathyApp {
Stroke::new(5.0, Color32::WHITE),
);
// Check for dropped files
ctx.input(|i| match i.raw.dropped_files.last() {
Some(file) => match file.clone().bytes {
Some(bytes) => match RetainedImage::from_image_bytes("", &*bytes) {
Ok(image) => *overlay = Some(image),
Err(_) => (),
},
None => (),
},
None => (),
ctx.input(|i| {
if let Some(file) = i.raw.dropped_files.last() {
if let Some(bytes) = file.clone().bytes {
if let Ok(image) = RetainedImage::from_image_bytes("", &bytes) {
*overlay = Some(image);
}
}
}
});
// Render image
match overlay {
Expand Down Expand Up @@ -215,12 +214,9 @@ impl eframe::App for PathyApp {
y: response.rect.min.y + pos.y,
};
// Render lines
match prev {
Some(prev_pos) => {
ui.painter()
.line_segment([prev_pos, screen_pos], yellow_line);
}
None => (),
if let Some(prev_pos) = prev {
ui.painter()
.line_segment([prev_pos, screen_pos], yellow_line);
};
prev = Some(screen_pos);
});
Expand Down Expand Up @@ -248,51 +244,42 @@ impl eframe::App for PathyApp {
CursorMode::Default => (), // No tooltip
CursorMode::Create => {
// Get pointer position
match hovered {
if let Some(pos) = hovered {
// Put circle under cursor
Some(pos) => {
ui.painter().circle_filled(pos, 5.0, Color32::YELLOW);
// Render line preview
match prev {
Some(prev_pos) => {
ui.painter().line_segment([prev_pos, pos], yellow_line)
}
None => (),
}
ui.painter().circle_filled(pos, 5.0, Color32::YELLOW);
// Render line preview
if let Some(prev_pos) = prev {
ui.painter().line_segment([prev_pos, pos], yellow_line);
}
None => (),
}
}
CursorMode::Edit | CursorMode::Delete | CursorMode::Trim => {
// Get pointer position
match hovered {
Some(hover_pos) => {
// Find the nearest point. We just add the x and y differences without
// squaring them, since we don't need the actual distance, just
// something we can compare (and works 99% of the time).
let mut distance = f32::MAX;
hovered = Some(path.iter().enumerate().fold(
hover_pos,
|old_pos, (idx, pos)| {
let screen_pos = Pos2 {
x: response.rect.min.x + pos.x,
y: response.rect.min.y + pos.y,
};
let dis = f32::abs(hover_pos.x - screen_pos.x)
+ f32::abs(hover_pos.y - screen_pos.y);
if dis < distance {
distance = dis;
sl_idx = Some(idx);
return screen_pos;
}
old_pos
},
));
// Render closest point red
ui.painter()
.circle_filled(hovered.unwrap(), 5.1, Color32::RED);
}
None => (),
if let Some(hover_pos) = hovered {
// Find the nearest point. We just add the x and y differences without
// squaring them, since we don't need the actual distance, just
// something we can compare (and works 99% of the time).
let mut distance = f32::MAX;
hovered = Some(path.iter().enumerate().fold(
hover_pos,
|old_pos, (idx, pos)| {
let screen_pos = Pos2 {
x: response.rect.min.x + pos.x,
y: response.rect.min.y + pos.y,
};
let dis = f32::abs(hover_pos.x - screen_pos.x)
+ f32::abs(hover_pos.y - screen_pos.y);
if dis < distance {
distance = dis;
sl_idx = Some(idx);
return screen_pos;
}
old_pos
},
));
// Render closest point red
ui.painter()
.circle_filled(hovered.unwrap(), 5.1, Color32::RED);
}
}
}
Expand All @@ -305,46 +292,45 @@ impl eframe::App for PathyApp {
// Default does nothing, and Edit uses drags
CursorMode::Default | CursorMode::Edit => (),
// Add cursor position to list
CursorMode::Create => match ctx.pointer_interact_pos() {
Some(pos) => path.push(Pos2 {
x: pos.x - response.rect.min.x,
y: pos.y - response.rect.min.y,
}),
None => (),
},
CursorMode::Create => {
if let Some(pos) = ctx.pointer_interact_pos() {
path.push(Pos2 {
x: pos.x - response.rect.min.x,
y: pos.y - response.rect.min.y,
});
}
}
// Delete cursor position (slices vector)
CursorMode::Delete => match sl_idx {
Some(idx) => drop(path.remove(idx)), // Deletes the elements
None => (),
},
CursorMode::Trim => match sl_idx {
Some(idx) => drop(path.drain(idx..)), // Deletes elements from idx
None => (),
},
CursorMode::Delete => {
if let Some(idx) = sl_idx {
path.remove(idx); // Deletes the elements
}
}
CursorMode::Trim => {
if let Some(idx) = sl_idx {
path.drain(idx..); // Deletes elements from idx
}
}
}
}
// Handle drags - Edit mode only
if *mode == CursorMode::Edit && path.len() > 0 {
if *mode == CursorMode::Edit && !path.is_empty() {
// Set selected at drag start
if response.drag_started() {
// Drag started, set current index as selected.
// This is to prevent, say, dragging over another point from stealing focus from
// the currently selected point.
match sl_idx {
Some(idx) => *selected = idx,
None => (),
if let Some(idx) = sl_idx {
*selected = idx;
}
}
// Move the selected point
if response.dragged() {
match ctx.pointer_interact_pos() {
Some(pos) => {
path[*selected] = Pos2 {
x: pos.x - response.rect.min.x,
y: pos.y - response.rect.min.y,
}
if let Some(pos) = ctx.pointer_interact_pos() {
path[*selected] = Pos2 {
x: pos.x - response.rect.min.x,
y: pos.y - response.rect.min.y,
}
None => (),
}
}
}
Expand Down

0 comments on commit 0a1e4d6

Please sign in to comment.