2019-12-19 21:59:39 +03:00
|
|
|
use crate::{Color, ScreenDims, ScreenPt, ScreenRectangle, Text, UserInput};
|
2019-10-27 02:16:32 +03:00
|
|
|
use abstutil::Timer;
|
2019-12-20 23:42:49 +03:00
|
|
|
use geom::{Bounds, Polygon, Pt2D};
|
2019-11-24 18:21:30 +03:00
|
|
|
use glium::texture::Texture2dArray;
|
2019-10-27 02:16:32 +03:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2018-12-17 01:56:28 +03:00
|
|
|
use std::cell::RefCell;
|
2019-04-23 05:01:55 +03:00
|
|
|
use std::collections::HashMap;
|
2018-03-13 18:04:21 +03:00
|
|
|
|
|
|
|
pub struct Canvas {
|
2018-09-19 00:41:30 +03:00
|
|
|
// All of these f64's are in screen-space, so do NOT use Pt2D.
|
|
|
|
// Public for saving/loading... should probably do better
|
2018-03-13 18:04:21 +03:00
|
|
|
pub cam_x: f64,
|
|
|
|
pub cam_y: f64,
|
|
|
|
pub cam_zoom: f64,
|
|
|
|
|
2018-12-23 03:58:36 +03:00
|
|
|
// TODO We probably shouldn't even track screen-space cursor when we don't have the cursor.
|
2019-02-01 10:32:38 +03:00
|
|
|
pub(crate) cursor_x: f64,
|
|
|
|
pub(crate) cursor_y: f64,
|
2019-10-12 20:24:07 +03:00
|
|
|
pub(crate) window_has_cursor: bool,
|
2018-03-13 18:04:21 +03:00
|
|
|
|
2019-12-12 00:52:01 +03:00
|
|
|
// Only for drags starting on the map. Only used to pan the map.
|
|
|
|
pub(crate) drag_canvas_from: Option<ScreenPt>,
|
|
|
|
pub(crate) actually_dragging: bool,
|
|
|
|
pub(crate) drag_just_ended: bool,
|
2018-07-06 20:02:01 +03:00
|
|
|
|
2018-12-21 20:49:46 +03:00
|
|
|
pub window_width: f64,
|
|
|
|
pub window_height: f64,
|
2019-12-12 03:59:13 +03:00
|
|
|
pub(crate) hidpi_factor: f64,
|
2020-01-14 01:39:02 +03:00
|
|
|
pub map_dims: (f64, f64),
|
2018-12-17 01:56:28 +03:00
|
|
|
|
2018-12-23 05:02:03 +03:00
|
|
|
// TODO Bit weird and hacky to mutate inside of draw() calls.
|
2019-02-01 10:32:38 +03:00
|
|
|
pub(crate) covered_areas: RefCell<Vec<ScreenRectangle>>,
|
2019-05-04 01:32:14 +03:00
|
|
|
|
|
|
|
// Kind of just ezgui state awkwardly stuck here...
|
2019-05-29 23:28:51 +03:00
|
|
|
pub(crate) lctrl_held: bool,
|
2019-11-01 21:45:02 +03:00
|
|
|
// This should be mutually exclusive among all buttons and things in map-space.
|
|
|
|
pub(crate) button_tooltip: Option<Text>,
|
2019-09-11 01:21:58 +03:00
|
|
|
|
|
|
|
// TODO Definitely a weird place to stash this!
|
2019-11-24 18:21:30 +03:00
|
|
|
pub(crate) texture_arrays: Vec<Texture2dArray>,
|
2019-09-11 03:04:33 +03:00
|
|
|
pub(crate) texture_lookups: HashMap<String, Color>,
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Canvas {
|
2019-12-12 03:59:13 +03:00
|
|
|
pub(crate) fn new(initial_width: f64, initial_height: f64, hidpi_factor: f64) -> Canvas {
|
2019-11-29 09:41:08 +03:00
|
|
|
Canvas {
|
2018-03-13 18:04:21 +03:00
|
|
|
cam_x: 0.0,
|
|
|
|
cam_y: 0.0,
|
|
|
|
cam_zoom: 1.0,
|
|
|
|
|
|
|
|
cursor_x: 0.0,
|
|
|
|
cursor_y: 0.0,
|
2018-12-23 03:58:36 +03:00
|
|
|
window_has_cursor: true,
|
2018-03-13 18:04:21 +03:00
|
|
|
|
2019-12-12 00:52:01 +03:00
|
|
|
drag_canvas_from: None,
|
|
|
|
actually_dragging: false,
|
|
|
|
drag_just_ended: false,
|
|
|
|
|
2019-01-25 20:48:33 +03:00
|
|
|
window_width: initial_width,
|
|
|
|
window_height: initial_height,
|
2019-12-12 03:59:13 +03:00
|
|
|
hidpi_factor,
|
2020-01-14 01:39:02 +03:00
|
|
|
map_dims: (0.0, 0.0),
|
2018-12-17 01:56:28 +03:00
|
|
|
|
2018-12-23 05:02:03 +03:00
|
|
|
covered_areas: RefCell::new(Vec::new()),
|
2019-05-04 01:32:14 +03:00
|
|
|
|
2019-05-29 23:28:51 +03:00
|
|
|
lctrl_held: false,
|
2019-11-01 21:45:02 +03:00
|
|
|
button_tooltip: None,
|
2019-09-11 01:21:58 +03:00
|
|
|
|
2019-11-24 18:21:30 +03:00
|
|
|
texture_arrays: Vec::new(),
|
2019-09-11 03:04:33 +03:00
|
|
|
texture_lookups: HashMap::new(),
|
2019-11-29 09:41:08 +03:00
|
|
|
}
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2020-01-14 01:39:02 +03:00
|
|
|
pub fn min_zoom(&self) -> f64 {
|
|
|
|
let percent_window = 0.8;
|
|
|
|
(percent_window * self.window_width / self.map_dims.0)
|
|
|
|
.min(percent_window * self.window_height / self.map_dims.1)
|
|
|
|
}
|
|
|
|
|
2020-01-07 20:43:34 +03:00
|
|
|
pub(crate) fn handle_event(&mut self, input: &mut UserInput) {
|
2019-08-06 22:05:09 +03:00
|
|
|
// Can't start dragging or zooming on top of covered area
|
2019-12-12 00:52:01 +03:00
|
|
|
if self.get_cursor_in_map_space().is_some() {
|
|
|
|
if input.left_mouse_button_pressed() {
|
2020-01-18 01:11:07 +03:00
|
|
|
self.drag_canvas_from = Some(self.get_cursor());
|
2019-12-12 00:52:01 +03:00
|
|
|
}
|
|
|
|
|
2020-01-15 02:47:35 +03:00
|
|
|
if let Some((_, scroll)) = input.get_mouse_scroll() {
|
2019-10-29 21:50:29 +03:00
|
|
|
let old_zoom = self.cam_zoom;
|
2020-01-10 00:57:34 +03:00
|
|
|
// By popular request, some limits ;)
|
|
|
|
self.cam_zoom = 1.1_f64
|
|
|
|
.powf(old_zoom.log(1.1) + scroll)
|
2020-01-14 01:39:02 +03:00
|
|
|
.max(self.min_zoom())
|
2020-01-10 00:57:34 +03:00
|
|
|
.min(150.0);
|
2019-10-29 21:50:29 +03:00
|
|
|
|
|
|
|
// Make screen_to_map of cursor_{x,y} still point to the same thing after zooming.
|
|
|
|
self.cam_x =
|
|
|
|
((self.cam_zoom / old_zoom) * (self.cursor_x + self.cam_x)) - self.cursor_x;
|
|
|
|
self.cam_y =
|
|
|
|
((self.cam_zoom / old_zoom) * (self.cursor_y + self.cam_y)) - self.cursor_y;
|
2019-08-06 22:05:09 +03:00
|
|
|
}
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
2019-12-12 00:52:01 +03:00
|
|
|
|
|
|
|
// If we start the drag on the map and move the mouse off the map, keep dragging.
|
|
|
|
if let Some(click) = self.drag_canvas_from {
|
2020-01-18 01:11:07 +03:00
|
|
|
let pt = self.get_cursor();
|
2019-12-12 00:52:01 +03:00
|
|
|
self.cam_x += click.x - pt.x;
|
|
|
|
self.cam_y += click.y - pt.y;
|
|
|
|
self.drag_canvas_from = Some(pt);
|
|
|
|
if !self.actually_dragging && click != pt {
|
|
|
|
self.actually_dragging = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.left_mouse_button_released() {
|
|
|
|
self.drag_canvas_from = None;
|
|
|
|
if self.actually_dragging {
|
|
|
|
self.drag_just_ended = true;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
self.actually_dragging = false;
|
|
|
|
}
|
|
|
|
} else if self.drag_just_ended {
|
|
|
|
self.drag_just_ended = false;
|
|
|
|
}
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 03:13:18 +03:00
|
|
|
pub(crate) fn start_drawing(&self) {
|
2018-12-23 05:02:03 +03:00
|
|
|
self.covered_areas.borrow_mut().clear();
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:58:33 +03:00
|
|
|
pub(crate) fn mark_covered_area(&self, rect: ScreenRectangle) {
|
2018-12-23 05:02:03 +03:00
|
|
|
self.covered_areas.borrow_mut().push(rect);
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2020-01-18 01:11:07 +03:00
|
|
|
// Might be hovering anywhere.
|
|
|
|
pub fn get_cursor(&self) -> ScreenPt {
|
2018-12-18 01:29:59 +03:00
|
|
|
ScreenPt::new(self.cursor_x, self.cursor_y)
|
|
|
|
}
|
|
|
|
|
2020-01-18 01:11:07 +03:00
|
|
|
pub fn get_cursor_in_screen_space(&self) -> Option<ScreenPt> {
|
|
|
|
if self.window_has_cursor && self.get_cursor_in_map_space().is_none() {
|
|
|
|
Some(self.get_cursor())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 03:37:18 +03:00
|
|
|
pub fn get_cursor_in_map_space(&self) -> Option<Pt2D> {
|
2018-12-23 03:58:36 +03:00
|
|
|
if self.window_has_cursor {
|
2020-01-18 01:11:07 +03:00
|
|
|
let pt = self.get_cursor();
|
2018-12-23 05:02:03 +03:00
|
|
|
|
|
|
|
for rect in self.covered_areas.borrow().iter() {
|
|
|
|
if rect.contains(pt) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(self.screen_to_map(pt))
|
2018-12-23 03:58:36 +03:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2018-12-18 01:12:53 +03:00
|
|
|
pub fn screen_to_map(&self, pt: ScreenPt) -> Pt2D {
|
2018-09-19 00:41:30 +03:00
|
|
|
Pt2D::new(
|
2018-12-18 01:12:53 +03:00
|
|
|
(pt.x + self.cam_x) / self.cam_zoom,
|
|
|
|
(pt.y + self.cam_y) / self.cam_zoom,
|
2018-09-19 00:41:30 +03:00
|
|
|
)
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2018-12-18 01:29:59 +03:00
|
|
|
pub fn center_to_screen_pt(&self) -> ScreenPt {
|
2018-12-21 20:49:46 +03:00
|
|
|
ScreenPt::new(self.window_width / 2.0, self.window_height / 2.0)
|
2018-12-18 01:29:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn center_to_map_pt(&self) -> Pt2D {
|
|
|
|
self.screen_to_map(self.center_to_screen_pt())
|
2018-11-12 21:44:24 +03:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:41:30 +03:00
|
|
|
pub fn center_on_map_pt(&mut self, pt: Pt2D) {
|
2018-12-21 20:49:46 +03:00
|
|
|
self.cam_x = (pt.x() * self.cam_zoom) - (self.window_width / 2.0);
|
|
|
|
self.cam_y = (pt.y() * self.cam_zoom) - (self.window_height / 2.0);
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2020-01-23 02:36:17 +03:00
|
|
|
pub fn map_to_screen(&self, pt: Pt2D) -> ScreenPt {
|
2018-12-18 01:12:53 +03:00
|
|
|
ScreenPt::new(
|
2018-09-19 00:41:30 +03:00
|
|
|
(pt.x() * self.cam_zoom) - self.cam_x,
|
|
|
|
(pt.y() * self.cam_zoom) - self.cam_y,
|
|
|
|
)
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2018-11-02 01:29:35 +03:00
|
|
|
pub fn get_screen_bounds(&self) -> Bounds {
|
|
|
|
let mut b = Bounds::new();
|
2018-12-18 01:12:53 +03:00
|
|
|
b.update(self.screen_to_map(ScreenPt::new(0.0, 0.0)));
|
2018-12-21 20:49:46 +03:00
|
|
|
b.update(self.screen_to_map(ScreenPt::new(self.window_width, self.window_height)));
|
2018-11-02 01:29:35 +03:00
|
|
|
b
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
2019-01-24 00:46:05 +03:00
|
|
|
|
2019-10-24 01:46:00 +03:00
|
|
|
pub fn texture(&self, filename: &str) -> Color {
|
|
|
|
if let Some(c) = self.texture_lookups.get(filename) {
|
|
|
|
return *c;
|
|
|
|
}
|
|
|
|
panic!("Don't know texture {}", filename);
|
|
|
|
}
|
2019-10-27 02:16:32 +03:00
|
|
|
|
2019-11-28 20:55:56 +03:00
|
|
|
pub fn texture_rect(&self, filename: &str) -> (Color, Polygon) {
|
|
|
|
let color = self.texture(filename);
|
|
|
|
let dims = color.texture_dims();
|
2019-12-20 23:42:49 +03:00
|
|
|
(color, Polygon::rectangle(dims.width, dims.height))
|
2019-11-28 20:55:56 +03:00
|
|
|
}
|
|
|
|
|
2019-10-27 02:16:32 +03:00
|
|
|
pub fn save_camera_state(&self, map_name: &str) {
|
|
|
|
let state = CameraState {
|
|
|
|
cam_x: self.cam_x,
|
|
|
|
cam_y: self.cam_y,
|
|
|
|
cam_zoom: self.cam_zoom,
|
|
|
|
};
|
2019-12-03 20:59:43 +03:00
|
|
|
abstutil::write_json(abstutil::path_camera_state(map_name), &state);
|
2019-10-27 02:16:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// True if this succeeds
|
|
|
|
pub fn load_camera_state(&mut self, map_name: &str) -> bool {
|
2019-12-03 20:59:43 +03:00
|
|
|
match abstutil::maybe_read_json::<CameraState>(
|
|
|
|
abstutil::path_camera_state(map_name),
|
|
|
|
&mut Timer::throwaway(),
|
|
|
|
) {
|
2019-10-27 02:16:32 +03:00
|
|
|
Ok(ref loaded) => {
|
|
|
|
self.cam_x = loaded.cam_x;
|
|
|
|
self.cam_y = loaded.cam_y;
|
|
|
|
self.cam_zoom = loaded.cam_zoom;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 21:59:39 +03:00
|
|
|
|
|
|
|
pub fn align_window(
|
|
|
|
&self,
|
|
|
|
dims: ScreenDims,
|
|
|
|
horiz: HorizontalAlignment,
|
|
|
|
vert: VerticalAlignment,
|
|
|
|
) -> ScreenPt {
|
|
|
|
let x1 = match horiz {
|
|
|
|
HorizontalAlignment::Left => 0.0,
|
|
|
|
HorizontalAlignment::Center => (self.window_width - dims.width) / 2.0,
|
|
|
|
HorizontalAlignment::Right => self.window_width - dims.width,
|
|
|
|
HorizontalAlignment::FillScreen => 0.0,
|
2020-01-14 05:52:44 +03:00
|
|
|
HorizontalAlignment::Percent(pct) => pct * self.window_width,
|
2019-12-19 21:59:39 +03:00
|
|
|
};
|
|
|
|
let y1 = match vert {
|
|
|
|
VerticalAlignment::Top => 0.0,
|
|
|
|
VerticalAlignment::Center => (self.window_height - dims.height) / 2.0,
|
|
|
|
VerticalAlignment::Bottom => self.window_height - dims.height,
|
|
|
|
// TODO Hack
|
|
|
|
VerticalAlignment::BottomAboveOSD => self.window_height - dims.height - 40.0,
|
2020-01-14 05:52:44 +03:00
|
|
|
VerticalAlignment::Percent(pct) => pct * self.window_height,
|
2020-01-31 01:38:26 +03:00
|
|
|
VerticalAlignment::TopAt(y) => y - dims.height,
|
2019-12-19 21:59:39 +03:00
|
|
|
};
|
|
|
|
ScreenPt::new(x1, y1)
|
|
|
|
}
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
2018-09-28 17:52:36 +03:00
|
|
|
|
2019-12-19 21:59:39 +03:00
|
|
|
#[derive(Clone, Copy)]
|
2018-09-28 17:52:36 +03:00
|
|
|
pub enum HorizontalAlignment {
|
|
|
|
Left,
|
|
|
|
Center,
|
|
|
|
Right,
|
2019-05-02 20:21:11 +03:00
|
|
|
FillScreen,
|
2020-01-14 05:52:44 +03:00
|
|
|
Percent(f64),
|
2018-09-28 17:52:36 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 21:59:39 +03:00
|
|
|
#[derive(Clone, Copy)]
|
2018-09-28 17:52:36 +03:00
|
|
|
pub enum VerticalAlignment {
|
|
|
|
Top,
|
|
|
|
Center,
|
|
|
|
Bottom,
|
2019-12-19 21:59:39 +03:00
|
|
|
BottomAboveOSD,
|
2020-01-14 05:52:44 +03:00
|
|
|
Percent(f64),
|
2020-01-31 01:38:26 +03:00
|
|
|
TopAt(f64),
|
2018-09-28 17:52:36 +03:00
|
|
|
}
|
2019-10-27 02:16:32 +03:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct CameraState {
|
|
|
|
cam_x: f64,
|
|
|
|
cam_y: f64,
|
|
|
|
cam_zoom: f64,
|
|
|
|
}
|