mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-10-07 06:14:07 +00:00
progress
This commit is contained in:
@@ -3,9 +3,11 @@ mod edit_waypoint_chunks;
|
||||
mod insert_waypoints;
|
||||
mod produce;
|
||||
mod update_selected;
|
||||
mod update_waypoint;
|
||||
|
||||
pub use copy::*;
|
||||
pub use edit_waypoint_chunks::*;
|
||||
pub use insert_waypoints::*;
|
||||
pub use produce::*;
|
||||
pub use update_selected::*;
|
||||
pub use update_waypoint::*;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{CommandError, FileId, State, Waypoint, WaypointId, edit_waypoint_chunks};
|
||||
|
||||
/// Changes one waypoint of a file with `f`, whatever is selected. Nothing to do if the file or
|
||||
/// the waypoint does not exist.
|
||||
pub fn update_waypoint(
|
||||
state: &mut State,
|
||||
file_id: FileId,
|
||||
waypoint_id: WaypointId,
|
||||
f: impl Fn(&mut Waypoint),
|
||||
) -> Result<(), CommandError> {
|
||||
let file = state.files.get(&file_id).ok_or(CommandError::NothingToDo)?;
|
||||
let mut file = (**file).clone();
|
||||
let changed = edit_waypoint_chunks(
|
||||
&mut file,
|
||||
|wpt| wpt.id == waypoint_id,
|
||||
|wpts| {
|
||||
wpts.iter_mut()
|
||||
.filter(|wpt| wpt.id == waypoint_id)
|
||||
.for_each(&f);
|
||||
true
|
||||
},
|
||||
);
|
||||
if !changed {
|
||||
return Err(CommandError::NothingToDo);
|
||||
}
|
||||
state.files.insert(file_id, Rc::new(file));
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
use crate::{
|
||||
Apply, CommandError, Edit, Editor, State, Waypoint, set_waypoint_fields, update_selected,
|
||||
};
|
||||
use crate::{Apply, CommandError, FileId, State, WaypointId, set_waypoint_fields, update_waypoint};
|
||||
|
||||
/// Changes the selected waypoints (what the form of the waypoint tool does): their name,
|
||||
/// description, icon, link, position and elevation. The strings that are empty remove the field.
|
||||
/// Changes a waypoint of a file, whatever is selected (what the form of the waypoint tool does):
|
||||
/// its name, description, icon, link, position and elevation. The strings that are empty remove
|
||||
/// the field.
|
||||
#[derive(Debug)]
|
||||
pub struct EditWaypoint<'a> {
|
||||
pub file_id: FileId,
|
||||
pub waypoint_id: WaypointId,
|
||||
pub lng: f64,
|
||||
pub lat: f64,
|
||||
pub ele: f64,
|
||||
@@ -16,38 +17,32 @@ pub struct EditWaypoint<'a> {
|
||||
}
|
||||
|
||||
impl Apply for EditWaypoint<'_> {
|
||||
fn apply(mut self, state: &mut State) -> Result<(), CommandError> {
|
||||
update_selected(state, &mut self);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Editor for EditWaypoint<'_> {
|
||||
fn waypoint(&mut self, waypoint: &mut Waypoint) -> Edit {
|
||||
set_waypoint_fields(
|
||||
waypoint,
|
||||
(self.lng, self.lat, self.ele),
|
||||
self.name,
|
||||
self.desc,
|
||||
self.icon,
|
||||
self.link,
|
||||
);
|
||||
Edit::Changed
|
||||
fn apply(self, state: &mut State) -> Result<(), CommandError> {
|
||||
update_waypoint(state, self.file_id, self.waypoint_id, |wpt| {
|
||||
set_waypoint_fields(
|
||||
wpt,
|
||||
(self.lng, self.lat, self.ele),
|
||||
self.name,
|
||||
self.desc,
|
||||
self.icon,
|
||||
self.link,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{collections::HashSet, rc::Rc};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{
|
||||
File, Link, Selection, WaypointChunk, engine::command::fixture::Fixture, waypoint_ids,
|
||||
File, Link, Waypoint, WaypointChunk, engine::command::fixture::Fixture, waypoint_ids,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_edit_the_selected_waypoint() {
|
||||
fn test_edit_the_given_waypoint() {
|
||||
let mut fx = Fixture::default();
|
||||
let mut file = File::default();
|
||||
file.wpt.push(Rc::new(WaypointChunk {
|
||||
@@ -73,12 +68,10 @@ mod tests {
|
||||
let id = file.id;
|
||||
let ids: Vec<_> = waypoint_ids(&file).collect();
|
||||
fx.files.insert(id, Rc::new(file));
|
||||
fx.selection = Selection::Waypoint {
|
||||
file_id: id,
|
||||
wpt_ids: HashSet::from([ids[0]]),
|
||||
};
|
||||
|
||||
EditWaypoint {
|
||||
file_id: id,
|
||||
waypoint_id: ids[0],
|
||||
lng: 4.5,
|
||||
lat: 50.5,
|
||||
ele: 12.0,
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{Apply, CommandError, FileId, LngLat, State, WaypointId, edit_waypoint_chunks};
|
||||
use crate::{Apply, CommandError, FileId, LngLat, State, WaypointId, update_waypoint};
|
||||
|
||||
/// Moves a waypoint of a file, whatever is selected (what a drag does): only its position and
|
||||
/// elevation change.
|
||||
@@ -15,30 +13,13 @@ pub struct MoveWaypoint {
|
||||
|
||||
impl Apply for MoveWaypoint {
|
||||
fn apply(self, state: &mut State) -> Result<(), CommandError> {
|
||||
let file = state
|
||||
.files
|
||||
.get(&self.file_id)
|
||||
.ok_or(CommandError::NothingToDo)?;
|
||||
let mut file = (**file).clone();
|
||||
let changed = edit_waypoint_chunks(
|
||||
&mut file,
|
||||
|wpt| wpt.id == self.waypoint_id,
|
||||
|wpts| {
|
||||
for wpt in wpts.iter_mut().filter(|wpt| wpt.id == self.waypoint_id) {
|
||||
wpt.coordinates = LngLat {
|
||||
lng: self.lng,
|
||||
lat: self.lat,
|
||||
};
|
||||
wpt.ele = self.ele;
|
||||
}
|
||||
true
|
||||
},
|
||||
);
|
||||
if !changed {
|
||||
return Err(CommandError::NothingToDo);
|
||||
}
|
||||
state.files.insert(self.file_id, Rc::new(file));
|
||||
Ok(())
|
||||
update_waypoint(state, self.file_id, self.waypoint_id, |wpt| {
|
||||
wpt.coordinates = LngLat {
|
||||
lng: self.lng,
|
||||
lat: self.lat,
|
||||
};
|
||||
wpt.ele = self.ele;
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1188,18 +1188,11 @@ mod tests {
|
||||
// the coordinates buffer follows
|
||||
assert_eq!(engine.waypoint_coordinates(&file), &[5.0, 51.0]);
|
||||
|
||||
// editing applies to the selected waypoints
|
||||
select_elements(
|
||||
&mut engine,
|
||||
Selection::Waypoint {
|
||||
file_id: file,
|
||||
wpt_ids: [id].into(),
|
||||
},
|
||||
SelectMode::Replace,
|
||||
);
|
||||
assert!(edit(
|
||||
&mut engine,
|
||||
Command::EditWaypoint(EditWaypoint {
|
||||
file_id: file,
|
||||
waypoint_id: id,
|
||||
lng: 6.0,
|
||||
lat: 52.0,
|
||||
ele: 30.0,
|
||||
|
||||
Reference in New Issue
Block a user