| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- extern crate cgmath;
- #[macro_use]
- extern crate clap;
- extern crate directories;
- extern crate env_logger;
- extern crate glutin;
- extern crate image;
- #[macro_use]
- extern crate lazy_static;
- extern crate linked_hash_map;
- #[macro_use]
- extern crate log;
- extern crate num_cpus;
- extern crate osmpbf;
- extern crate regex;
- extern crate reqwest;
- extern crate scoped_threadpool;
- extern crate toml;
-
- pub mod args;
- pub mod buffer;
- pub mod config;
- #[macro_use]
- pub mod context;
- pub mod coord;
- pub mod ortho_tile_layer;
- pub mod map_view;
- pub mod map_view_gl;
- pub mod marker_layer;
- pub mod mercator_view;
- pub mod orthografic_view;
- pub mod program;
- pub mod search;
- pub mod session;
- pub mod texture;
- pub mod tile;
- pub mod tile_atlas;
- pub mod tile_cache;
- pub mod tile_layer;
- pub mod tile_loader;
- pub mod tile_source;
- pub mod url_template;
- pub mod vertex_attrib;
-
- use coord::{LatLonDeg, ScreenCoord};
- use glutin::{ControlFlow, ElementState, Event, GlContext, MouseButton, MouseScrollDelta, VirtualKeyCode, WindowEvent};
- use map_view_gl::MapViewGl;
- use std::error::Error;
- use std::sync::mpsc;
- use std::time::{Duration, Instant};
- use tile_source::TileSource;
-
-
- #[derive(Copy, Clone, Debug, PartialEq, Eq)]
- enum Action {
- Nothing,
- Redraw,
- Resize(u32, u32),
- Close,
- }
-
- impl Action {
- fn combine_with(&mut self, newer_action: Self) {
- *self = match (*self, newer_action) {
- (Action::Close, _) | (_, Action::Close) => Action::Close,
- (Action::Resize(..), Action::Resize(w, h)) => Action::Resize(w, h),
- (Action::Resize(w, h), _) | (_, Action::Resize(w, h)) => Action::Resize(w, h),
- (Action::Redraw, _) | (_, Action::Redraw) => Action::Redraw,
- (Action::Nothing, Action::Nothing) => Action::Nothing,
- };
- }
- }
-
- #[derive(Copy, Clone, Debug, PartialEq)]
- struct InputState {
- mouse_position: (f64, f64),
- mouse_pressed: bool,
- }
-
- fn handle_event(
- event: &Event,
- map: &mut MapViewGl,
- input_state: &mut InputState,
- sources: &mut TileSources,
- marker_rx: &mpsc::Receiver<Vec<LatLonDeg>>,
- ) -> Action {
- match *event {
- Event::Awakened => {
- for pos in marker_rx.try_iter().flat_map(|c| c.into_iter()) {
- map.add_marker(pos.into());
- }
- Action::Redraw
- },
- Event::WindowEvent{ref event, ..} => match *event {
- WindowEvent::CloseRequested => Action::Close,
- WindowEvent::MouseInput { state: ElementState::Pressed, button: MouseButton::Left, .. } => {
- input_state.mouse_pressed = true;
- Action::Nothing
- },
- WindowEvent::MouseInput { state: ElementState::Released, button: MouseButton::Left, .. } => {
- input_state.mouse_pressed = false;
- Action::Nothing
- },
- WindowEvent::CursorMoved { position: (x, y), .. } => {
- if input_state.mouse_pressed {
- map.move_pixel(
- input_state.mouse_position.0 - x,
- input_state.mouse_position.1 - y,
- );
- input_state.mouse_position = (x, y);
- Action::Redraw
- } else {
- input_state.mouse_position = (x, y);
- Action::Nothing
- }
- },
- WindowEvent::MouseWheel { delta, modifiers, .. } => {
- let (dx, dy) = match delta {
- MouseScrollDelta::LineDelta(dx, dy) => {
- // filter strange wheel events with huge values.
- // (maybe this is just a personal touchpad driver issue)
- if dx.abs() < 16.0 && dy.abs() < 16.0 {
- //TODO find a sensible line height value (servo (the glutin port) uses 38)
- (dx, dy * 38.0)
- } else {
- (0.0, 0.0)
- }
- },
- MouseScrollDelta::PixelDelta(dx, dy) => (dx, dy),
- };
-
- //TODO add option for default mouse wheel behavior (scroll or zoom?)
- //TODO add option to reverse scroll/zoom direction
-
- if modifiers.ctrl {
- map.move_pixel(f64::from(-dx), f64::from(-dy));
- } else {
- map.zoom_at(
- ScreenCoord::new(
- input_state.mouse_position.0,
- input_state.mouse_position.1,
- ),
- f64::from(dy) * (1.0 / 320.0),
- );
- }
- Action::Redraw
- },
- WindowEvent::KeyboardInput {
- input: glutin::KeyboardInput {
- state: glutin::ElementState::Pressed,
- virtual_keycode: Some(keycode),
- modifiers,
- .. },
- .. } => {
- match keycode {
- VirtualKeyCode::Escape => {
- Action::Close
- },
- VirtualKeyCode::PageUp => {
- sources.switch_to_prev();
- Action::Redraw
- },
- VirtualKeyCode::PageDown => {
- sources.switch_to_next();
- Action::Redraw
- },
- VirtualKeyCode::Left => {
- map.move_pixel(-50.0, 0.0);
- Action::Redraw
- },
- VirtualKeyCode::Right => {
- map.move_pixel(50.0, 0.0);
- Action::Redraw
- },
- VirtualKeyCode::Up => {
- map.move_pixel(0.0, -50.0);
- Action::Redraw
- },
- VirtualKeyCode::Down => {
- map.move_pixel(0.0, 50.0);
- Action::Redraw
- },
- VirtualKeyCode::Add => {
- if modifiers.ctrl {
- map.change_tile_zoom_offset(1.0);
- } else {
- map.step_zoom(1, 1.0);
- }
- Action::Redraw
- },
- VirtualKeyCode::Subtract => {
- if modifiers.ctrl {
- map.change_tile_zoom_offset(-1.0);
- } else {
- map.step_zoom(-1, 1.0);
- }
- Action::Redraw
- },
- VirtualKeyCode::G => {
- if modifiers.ctrl {
- map.toggle_projection();
- Action::Redraw
- } else {
- Action::Nothing
- }
- },
- _ => Action::Nothing,
- }
- },
- WindowEvent::Refresh => {
- Action::Redraw
- },
- WindowEvent::Resized(w, h) => {
- Action::Resize(w, h)
- },
- _ => Action::Nothing,
- },
- _ => Action::Nothing,
- }
- }
-
- fn dur_to_sec(dur: Duration) -> f64 {
- dur.as_secs() as f64 + f64::from(dur.subsec_nanos()) * 1e-9
- }
-
- fn run() -> Result<(), Box<Error>> {
- let config = {
- let arg_matches = args::parse();
- let config = config::Config::from_arg_matches(&arg_matches)?;
- if arg_matches.is_present("list-paths") {
- config.list_paths();
- return Ok(());
- }
- config
- };
-
- let mut sources = TileSources::new(config.tile_sources())
- .ok_or_else(|| "no tile sources provided.")?;
-
- let last_session = if config.open_last_session() {
- config::read_last_session().ok()
- } else {
- None
- };
-
- if let Some(tile_source) = last_session.as_ref().and_then(|s| s.tile_source.as_ref()) {
- sources.switch_to_name(tile_source);
- }
-
- let mut events_loop = glutin::EventsLoop::new();
- let builder = glutin::WindowBuilder::new()
- .with_title(format!("DeltaMap - {}", sources.current_name()));
-
- let gl_context = glutin::ContextBuilder::new();
- let gl_window = glutin::GlWindow::new(builder, gl_context, &events_loop)?;
- let window = gl_window.window();
-
- let _ = unsafe { gl_window.make_current() };
- let mut cx = context::Context::from_gl_window(&gl_window);
-
- let mut map = {
- let proxy = events_loop.create_proxy();
-
- map_view_gl::MapViewGl::new(
- &mut cx,
- window.get_inner_size().unwrap(),
- move || { proxy.wakeup().unwrap(); },
- config.use_network(),
- config.async(),
- )
- };
-
- if let Some(ref session) = last_session {
- map.restore_session(session);
- }
-
- let mut input_state = InputState {
- mouse_position: (0.0, 0.0),
- mouse_pressed: false,
- };
-
- let (marker_tx, marker_rx) = mpsc::channel();
- if let (Some(path), Some(pattern)) = (config.pbf_path(), config.search_pattern()) {
- let proxy = events_loop.create_proxy();
-
- search::par_search(
- path,
- pattern,
- move |coords| {
- if coords.is_empty() {
- search::ControlFlow::Continue
- } else {
- if marker_tx.send(coords).is_err() {
- return search::ControlFlow::Break;
- }
- proxy.wakeup().into()
- }
- },
- move |result| {
- if let Err(err) = result {
- println!("search error: {}", err);
- } else {
- info!("finished searching");
- }
- },
- )?;
- }
-
- let duration_per_frame = Duration::from_millis((1000.0 / config.fps() - 0.5).max(0.0).floor() as u64);
- info!("milliseconds per frame: {}", dur_to_sec(duration_per_frame) * 1000.0);
-
- // estimated draw duration
- let mut est_draw_dur = duration_per_frame;
- let mut last_draw = Instant::now();
- let mut increase_atlas_size_possible = true;
-
- loop {
- let start_source_id = sources.current().id();
- let mut action = Action::Nothing;
-
- events_loop.run_forever(|event| {
- let a = handle_event(&event, &mut map, &mut input_state, &mut sources, &marker_rx);
- action.combine_with(a);
- ControlFlow::Break
- });
-
- if action == Action::Close {
- break;
- }
-
- events_loop.poll_events(|event| {
- let a = handle_event(&event, &mut map, &mut input_state, &mut sources, &marker_rx);
- action.combine_with(a);
- if action == Action::Close {
- return;
- }
- });
-
- if action == Action::Close {
- break;
- }
-
- {
- let diff = last_draw.elapsed();
- if diff + est_draw_dur * 2 < duration_per_frame {
- if let Some(dur) = duration_per_frame.checked_sub(est_draw_dur * 2) {
- std::thread::sleep(dur);
-
- events_loop.poll_events(|event| {
- let a = handle_event(&event, &mut map, &mut input_state, &mut sources, &marker_rx);
- action.combine_with(a);
- if action == Action::Close {
- return;
- }
- });
-
- if action == Action::Close {
- break;
- }
- }
- }
- }
-
- if let Action::Resize(w, h) = action {
- gl_window.resize(w, h);
- map.set_viewport_size(&mut cx, w, h);
- }
-
- let redraw = match action {
- Action::Redraw => true,
- Action::Resize(..) => true,
- _ => false,
- };
-
- if redraw {
- let draw_start = Instant::now();
-
- if !map.map_covers_viewport() {
- cx.clear_color((0.2, 0.2, 0.2, 1.0));
- }
- let draw_result = map.draw(&mut cx, sources.current());
-
- let draw_dur = draw_start.elapsed();
-
-
- let _ = gl_window.swap_buffers();
-
- last_draw = Instant::now();
-
- //TODO increase atlas size earlier to avoid excessive copying to the GPU
- //TODO increase max tile cache size?
- if increase_atlas_size_possible {
- let draws = match draw_result {
- Ok(x) => x,
- Err(x) => x,
- };
- if draws > 1 {
- increase_atlas_size_possible = map.increase_atlas_size(&mut cx).is_ok();
- }
- }
-
- debug!("draw: {} sec (est {} sec)", dur_to_sec(draw_dur), dur_to_sec(est_draw_dur));
-
- est_draw_dur = if draw_dur > est_draw_dur {
- draw_dur
- } else {
- (draw_dur / 4) + ((est_draw_dur / 4) * 3)
- };
- }
-
- // set window title
- if sources.current().id() != start_source_id {
- window.set_title(&format!("DeltaMap - {}", sources.current_name()));
- }
- }
-
- if config.open_last_session() {
- let mut session = map.to_session();
- session.tile_source = Some(sources.current_name().to_string());
- config::save_session(&session)?;
- }
-
- Ok(())
- }
-
- fn main() {
- env_logger::init();
-
- if let Err(err) = run() {
- println!("{}", err);
- std::process::exit(1);
- }
- }
-
- struct TileSources<'a> {
- current_index: usize,
- sources: &'a [(String, TileSource)],
- }
-
- impl<'a> TileSources<'a> {
- pub fn new(sources: &'a [(String, TileSource)]) -> Option<TileSources> {
- if sources.is_empty() {
- None
- } else {
- Some(TileSources {
- current_index: 0,
- sources,
- })
- }
- }
-
- pub fn current(&self) -> &TileSource {
- &self.sources[self.current_index].1
- }
-
- pub fn current_name(&self) -> &str {
- &self.sources[self.current_index].0
- }
-
- pub fn switch_to_next(&mut self) {
- self.current_index = (self.current_index + 1) % self.sources.len();
- }
-
- pub fn switch_to_prev(&mut self) {
- self.current_index = (self.current_index + self.sources.len().saturating_sub(1)) % self.sources.len();
- }
-
- pub fn switch_to_name(&mut self, name: &str) {
- for (index, &(ref n, _)) in self.sources.iter().enumerate() {
- if n == name {
- self.current_index = index;
- break;
- }
- }
- }
- }
|