mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-10-17 08:42:28 +00:00
progress
This commit is contained in:
1 parent
c6ad088947
commit
17533586ef
6 files changed
+404
-27
No files matched your search
@@ -0,0 +1,106 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use crate::{FileId, LngLatBounds};
|
||||
|
||||
/// A user action, decoded from the WASM boundary.
|
||||
///
|
||||
/// Bulk data (coordinates, file bytes) is borrowed straight from the wasm-bindgen argument
|
||||
/// buffers, so a command is built without copying; the engine only copies what it keeps.
|
||||
pub enum Command<'a> {
|
||||
// File commands
|
||||
New {
|
||||
name: &'a str,
|
||||
},
|
||||
Load {
|
||||
data: &'a [u8],
|
||||
},
|
||||
Duplicate,
|
||||
Delete,
|
||||
DeleteAll,
|
||||
// Edit commands
|
||||
Metadata {
|
||||
name: &'a str,
|
||||
desc: &'a str,
|
||||
},
|
||||
Style {
|
||||
color: Option<&'a str>,
|
||||
opacity: Option<f64>,
|
||||
width: Option<f64>,
|
||||
},
|
||||
NewTrack,
|
||||
NewTrackSegment,
|
||||
// Tools
|
||||
Reverse,
|
||||
Append {
|
||||
lng: &'a [f64],
|
||||
lat: &'a [f64],
|
||||
ele: &'a [f64],
|
||||
},
|
||||
Replace {
|
||||
start: u32,
|
||||
end: u32,
|
||||
lng: &'a [f64],
|
||||
lat: &'a [f64],
|
||||
ele: &'a [f64],
|
||||
},
|
||||
NewWaypoint {
|
||||
lng: f64,
|
||||
lat: f64,
|
||||
ele: f64,
|
||||
name: &'a str,
|
||||
desc: &'a str,
|
||||
icon: &'a str,
|
||||
link: &'a str,
|
||||
},
|
||||
MoveWaypoint {
|
||||
lng: f64,
|
||||
lat: f64,
|
||||
ele: f64,
|
||||
},
|
||||
Crop {
|
||||
start: u32,
|
||||
end: u32,
|
||||
},
|
||||
Split {
|
||||
at: u32,
|
||||
},
|
||||
Time,
|
||||
Merge {
|
||||
type_: MergeType,
|
||||
},
|
||||
Extract,
|
||||
Elevation {
|
||||
ele: &'a [f64],
|
||||
},
|
||||
Clean {
|
||||
bounds: LngLatBounds,
|
||||
type_: CleanType,
|
||||
trkpt: bool,
|
||||
wpt: bool,
|
||||
},
|
||||
// Undo-redo
|
||||
Undo,
|
||||
Redo,
|
||||
// Selection
|
||||
Select {
|
||||
file_ids: Vec<FileId>,
|
||||
},
|
||||
AddSelect {
|
||||
file_ids: Vec<FileId>,
|
||||
},
|
||||
SelectAll,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum MergeType {
|
||||
Connect,
|
||||
Group,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum CleanType {
|
||||
Inside,
|
||||
Outside,
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
mod command;
|
||||
|
||||
pub use command::*;
|
||||
+45
-27
@@ -4,36 +4,22 @@ use std::rc::Rc;
|
||||
|
||||
use js_sys::{Float64Array, Function, Int32Array};
|
||||
use uuid::Uuid;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use crate::{File, FileId, Selection, Stack, StackEntry, StatisticsBuffer, StatisticsCache, parse};
|
||||
use crate::{
|
||||
Command, File, FileId, Selection, Stack, StackEntry, StatisticsBuffer, StatisticsCache, parse,
|
||||
};
|
||||
|
||||
extern crate console_error_panic_hook;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[wasm_bindgen]
|
||||
pub struct Engine {
|
||||
stack: Stack,
|
||||
selection: Selection,
|
||||
statistics_cache: StatisticsCache,
|
||||
statistics_buffer: StatisticsBuffer,
|
||||
callback: Option<Function>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl Engine {
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(callback: Function) -> Self {
|
||||
console_error_panic_hook::set_once();
|
||||
Self {
|
||||
stack: Default::default(),
|
||||
selection: Default::default(),
|
||||
statistics_cache: Default::default(),
|
||||
statistics_buffer: Default::default(),
|
||||
callback: Some(callback),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn total_distance(&self) -> Float64Array {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.total_distance) }
|
||||
}
|
||||
@@ -74,16 +60,16 @@ impl Engine {
|
||||
unsafe { Float64Array::view(&self.statistics_buffer.slope_segment_distance) }
|
||||
}
|
||||
|
||||
pub fn create_file(&mut self, name: &str) {
|
||||
pub fn create_file(&mut self, name: &str) -> bool {
|
||||
self.event_loop(|entry| {
|
||||
let mut file = File::default();
|
||||
file.info.name = name.to_string();
|
||||
entry.insert(file.id, Rc::new(file));
|
||||
true
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
pub fn load_file(&mut self, data: &[u8]) {
|
||||
pub fn load_file(&mut self, data: &[u8]) -> bool {
|
||||
self.event_loop(|entry| {
|
||||
if let Ok(file) = parse(data) {
|
||||
entry.insert(file.id, Rc::new(file));
|
||||
@@ -91,10 +77,10 @@ impl Engine {
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
pub fn delete_file(&mut self, id: &[u8]) {
|
||||
pub fn delete_file(&mut self, id: &[u8]) -> bool {
|
||||
self.event_loop(|entry| {
|
||||
if let Ok(id) = Uuid::from_slice(id) {
|
||||
let id = FileId(id);
|
||||
@@ -102,10 +88,42 @@ impl Engine {
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
fn event_loop<F>(&mut self, f: F)
|
||||
/// Single entry point for every user action coming from the frontend.
|
||||
pub fn execute(&mut self, command: Command) -> bool {
|
||||
match command {
|
||||
Command::New { name } => self.create_file(name),
|
||||
Command::Load { data } => self.load_file(data),
|
||||
Command::Delete
|
||||
| Command::DeleteAll
|
||||
| Command::Duplicate
|
||||
| Command::Metadata { .. }
|
||||
| Command::Style { .. }
|
||||
| Command::NewTrack
|
||||
| Command::NewTrackSegment
|
||||
| Command::Reverse
|
||||
| Command::Append { .. }
|
||||
| Command::Replace { .. }
|
||||
| Command::NewWaypoint { .. }
|
||||
| Command::MoveWaypoint { .. }
|
||||
| Command::Crop { .. }
|
||||
| Command::Split { .. }
|
||||
| Command::Time
|
||||
| Command::Merge { .. }
|
||||
| Command::Extract
|
||||
| Command::Elevation { .. }
|
||||
| Command::Clean { .. }
|
||||
| Command::Undo
|
||||
| Command::Redo
|
||||
| Command::Select { .. }
|
||||
| Command::AddSelect { .. }
|
||||
| Command::SelectAll => todo!("command not implemented yet"),
|
||||
}
|
||||
}
|
||||
|
||||
fn event_loop<F>(&mut self, f: F) -> bool
|
||||
where
|
||||
F: FnOnce(&mut StackEntry) -> bool,
|
||||
{
|
||||
@@ -117,9 +135,9 @@ impl Engine {
|
||||
.statistics_cache
|
||||
.get(self.stack.current(), &self.selection),
|
||||
);
|
||||
if let Some(c) = &self.callback {
|
||||
let _ = c.call0(&JsValue::NULL);
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
mod command;
|
||||
mod derived;
|
||||
mod engine;
|
||||
mod selection;
|
||||
mod state;
|
||||
|
||||
pub use command::*;
|
||||
pub use derived::*;
|
||||
pub use engine::*;
|
||||
pub use selection::*;
|
||||
|
||||
Reference in new issue
Block a user