Skip to content

Commit

Permalink
refactor(world_map): optimize nearest object calculation and move to …
Browse files Browse the repository at this point in the history
…SatellitesState

- Move get_nearest_object method from world_map to SatellitesState
- Use Utc::now() as a parameter to make the method more flexible
- Simplify code by removing unnecessary get_nearest_object function from world_map
- Update mouse event handling to use the new method
  • Loading branch information
ShenMian committed Dec 17, 2024
1 parent 6864324 commit b6f5ae8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
15 changes: 15 additions & 0 deletions src/widgets/satellites.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::time::Instant;

use anyhow::Result;
use chrono::{DateTime, Utc};
use crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
use ratatui::{
buffer::Buffer,
Expand Down Expand Up @@ -46,6 +47,20 @@ impl SatellitesState {
}
}

/// Get the index of the nearest object to the given area coordinates
pub fn get_nearest_object(&self, time: DateTime<Utc>, lon: f64, lat: f64) -> Option<usize> {
self.objects
.iter()
.enumerate()
.min_by_key(|(_, obj)| {
let state = obj.predict(time).unwrap();
let dx = state.longitude() - lon;
let dy = state.latitude() - lat;
((dx * dx + dy * dy) * 1000.0) as i32
})
.map(|(index, _)| index)
}

pub fn scroll_up(&mut self) {
*self.list_state.offset_mut() = self.list_state.offset().saturating_sub(1);
}
Expand Down
24 changes: 6 additions & 18 deletions src/widgets/world_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,38 +144,26 @@ pub async fn handle_mouse_events(event: MouseEvent, app: &mut App) -> Result<()>
// Convert window coordinates to area coordinates
let mouse = Position::new(event.column - inner_area.x, event.row - inner_area.y);

let (lon, lat) = area_to_lon_lat(mouse.x, mouse.y, app.world_map_state.inner_area);
let nearest_object = app
.satellites_state
.get_nearest_object(Utc::now(), lon, lat);
if let MouseEventKind::Down(buttom) = event.kind {
match buttom {
MouseButton::Left => {
app.world_map_state.selected_object = get_nearest_object(app, mouse.x, mouse.y);
app.world_map_state.selected_object = nearest_object;
}
MouseButton::Right => {
app.world_map_state.selected_object = None;
}
_ => {}
}
}
app.world_map_state.hovered_object = get_nearest_object(app, mouse.x, mouse.y);
app.world_map_state.hovered_object = nearest_object;

Ok(())
}

/// Get the index of the nearest object to the given area coordinates
fn get_nearest_object(app: &mut App, x: u16, y: u16) -> Option<usize> {
app.satellites_state
.objects
.iter()
.enumerate()
.min_by_key(|(_, obj)| {
let state = obj.predict(Utc::now()).unwrap();
let (lon, lat) = area_to_lon_lat(x, y, app.world_map_state.inner_area);
let dx = state.longitude() - lon;
let dy = state.latitude() - lat;
((dx * dx + dy * dy) * 1000.0) as i32
})
.map(|(index, _)| index)
}

/// Convert area coordinates to lon/lat coordinates
fn area_to_lon_lat(x: u16, y: u16, area: Rect) -> (f64, f64) {
debug_assert!(x < area.width && y < area.height);
Expand Down

0 comments on commit b6f5ae8

Please sign in to comment.