Skip to content

Commit

Permalink
fix: grid visibility and too many draws + added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CalliEve committed Jun 29, 2024
1 parent 5bb3cc9 commit 5d4bd99
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 16 deletions.
105 changes: 105 additions & 0 deletions src/algorithm/a_star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct AStarState {

impl AStarState {
/// Get the path that led to this state.
///
/// Note: This method excludes the starting node of the path and the node of
/// the current state.
fn to_path(&self) -> Vec<GridNode> {
let mut path = Vec::new();

Expand Down Expand Up @@ -108,3 +111,105 @@ pub fn run_a_star(from: GridNode, to: GridNode) -> Vec<GridNode> {

last.to_path()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_to_path() {
let states = AStarState {
path_length: 3,
cost: 0.0,
node: (3, 3).into(),
parent: Some(Box::new(AStarState {
cost: 0.0,
node: (2, 2).into(),
path_length: 2,
parent: Some(Box::new(AStarState {
cost: 0.0,
node: (1, 1).into(),
path_length: 1,
parent: Some(Box::new(AStarState {
cost: 0.0,
node: (0, 0).into(),
path_length: 0,
parent: None,
})),
})),
})),
};

let path = states.to_path();

let expected = vec![(1, 1), (2, 2)];

assert_eq!(path, expected);
}

#[test]
fn test_a_star() {
// down
assert_eq!(
run_a_star((1, 1).into(), (1, 5).into()),
vec![(1, 2), (1, 3), (1, 4)]
);

// down diag left
assert_eq!(
run_a_star((5, 1).into(), (1, 5).into()),
vec![(4, 2), (3, 3), (2, 4)]
);

// left
assert_eq!(
run_a_star((5, 1).into(), (1, 1).into()),
vec![(4, 1), (3, 1), (2, 1)]
);

// up diag left
assert_eq!(
run_a_star((5, 5).into(), (1, 1).into()),
vec![(4, 4), (3, 3), (2, 2)]
);

// up
assert_eq!(
run_a_star((1, 5).into(), (1, 1).into()),
vec![(1, 4), (1, 3), (1, 2)]
);

// up diag right
assert_eq!(
run_a_star((1, 5).into(), (5, 1).into()),
vec![(2, 4), (3, 3), (4, 2)]
);

// right
assert_eq!(
run_a_star((1, 1).into(), (5, 1).into()),
vec![(2, 1), (3, 1), (4, 1)]
);

// down diag right
assert_eq!(
run_a_star((1, 1).into(), (5, 5).into()),
vec![(2, 2), (3, 3), (4, 4)]
);

// long with corner
assert_eq!(
run_a_star((1, 1).into(), (10, 5).into()),
vec![
(2, 2),
(3, 3),
(4, 4),
(5, 5),
(6, 5),
(7, 5),
(8, 5),
(9, 5)
]
);
}
}
2 changes: 1 addition & 1 deletion src/algorithm/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use web_sys::CanvasRenderingContext2d;
/// grid is in the background.
pub fn draw_grid(canvas: &CanvasRenderingContext2d, size: (u32, u32), square_size: u32) {
canvas.begin_path();
canvas.set_line_width(1.0);
canvas.set_line_width(0.3);
canvas.set_stroke_style(&JsValue::from_str("grey"));

draw_vertical_lines(
Expand Down
15 changes: 10 additions & 5 deletions src/components/molecules/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn on_mouse_up(map_state: &mut MapState) {
let Some(selected) = map_state
.get_selected_station()
.cloned()
.map(|s| s.deselect())
.map(SelectedStation::deselect)
else {
return;
};
Expand All @@ -208,13 +208,17 @@ fn on_mouse_up(map_state: &mut MapState) {
/// Listener for the [mousemove] event on the canvas.
///
/// [mousemove]: https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event
fn on_mouse_move(map_state: &mut MapState, ev: &UiEvent) {
fn on_mouse_move(map_state_signal: &RwSignal<MapState>, ev: &UiEvent) {
let mut map_state = map_state_signal.get();
let canvas_pos = canvas_click_pos(map_state.get_size(), ev);
let mouse_pos = GridNode::from_canvas_pos(canvas_pos, map_state.get_square_size());

// Handle move of selected line
if let Some(selected) = map_state.get_mut_selected_line() {
selected.set_current_hover(mouse_pos);
if selected.get_current_hover() != mouse_pos {
selected.set_current_hover(mouse_pos);
map_state_signal.set(map_state);
}
return;
}

Expand All @@ -232,6 +236,7 @@ fn on_mouse_move(map_state: &mut MapState, ev: &UiEvent) {

selected.update_pos(mouse_pos);
map_state.set_selected_station(selected);
map_state_signal.set(map_state);
}

/// Listener for the [mouseout] event on the canvas.
Expand Down Expand Up @@ -309,11 +314,11 @@ pub fn Canvas() -> impl IntoView {
_ref=canvas_ref
on:mousedown=move |ev| map_state.update(|state| on_mouse_down(state, ev.as_ref()))
on:mouseup=move |_| map_state.update(on_mouse_up)
on:mousemove=move |ev| map_state.update(|state| on_mouse_move(state, ev.as_ref()))
on:mousemove=move |ev| on_mouse_move(&map_state, ev.as_ref())
on:mouseout=move |_| map_state.update(on_mouse_out)
on:touchstart=move |ev| map_state.update(|state| on_mouse_down(state, ev.as_ref()))
on:touchend=move |_| map_state.update(on_mouse_up)
on:touchmove=move |ev| map_state.update(|state| on_mouse_move(state, ev.as_ref()))
on:touchmove=move |ev| on_mouse_move(&map_state, ev.as_ref())
on:touchcancel=move |_| map_state.update(on_mouse_out)
on:wheel=move |ev| map_state.update(|state| on_scroll(state, ev.delta_y()))
id="canvas"
Expand Down
9 changes: 9 additions & 0 deletions src/components/state/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ impl MapState {
self.selected_line = None;
}

/// Returns if anything has been selected.
pub fn has_any_selected(&self) -> bool {
self.selected_station
.is_some()
|| self
.selected_line
.is_some()
}

/// A getter method for the canvas size.
pub fn get_size(&self) -> (u32, u32) {
self.size
Expand Down
4 changes: 2 additions & 2 deletions src/models/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Line {
.map(|s| s.0);
break;
}
before = Some(&line_station.0)
before = Some(&line_station.0);
}

if found {
Expand Down Expand Up @@ -170,7 +170,7 @@ impl Drawable for Line {
fn draw(&self, canvas: &CanvasRenderingContext2d, square_size: u32) {
let stations = self.get_stations();

canvas.set_line_width(2.0);
canvas.set_line_width(3.0);
canvas.set_global_alpha(1.0);
canvas.set_stroke_style(&JsValue::from_str(&format!(
"rgb({} {} {})",
Expand Down
7 changes: 4 additions & 3 deletions src/models/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ impl Map {

impl Drawable for Map {
fn draw(&self, canvas: &web_sys::CanvasRenderingContext2d, square_size: u32) {
for station in self.get_stations() {
station.draw(canvas, square_size);
}
leptos::logging::log!("redrawing");
for line in &self.lines {
line.draw(canvas, square_size);
}
for station in self.get_stations() {
station.draw(canvas, square_size);
}
}
}
4 changes: 2 additions & 2 deletions src/models/selected_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Drawable for SelectedLine {
),
canvas,
square_size,
)
);
};
let draw_after = |after: &Station| {
draw_edge(
Expand All @@ -135,7 +135,7 @@ impl Drawable for SelectedLine {
),
canvas,
square_size,
)
);
};

match self.get_before_after() {
Expand Down
6 changes: 3 additions & 3 deletions src/models/selected_station.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Drawable for SelectedStation {
self.station
.draw(canvas, square_size);

canvas.set_line_width(2.0);
canvas.set_line_width(3.0);
canvas.set_stroke_style(&JsValue::from_str("black"));
canvas.set_global_alpha(0.5);
canvas.begin_path();
Expand All @@ -112,7 +112,7 @@ impl Drawable for SelectedStation {
),
canvas,
square_size,
)
);
}

for after in self
Expand All @@ -130,7 +130,7 @@ impl Drawable for SelectedStation {
),
canvas,
square_size,
)
);
}

canvas.stroke();
Expand Down

0 comments on commit 5d4bd99

Please sign in to comment.