2019-10-05 23:56:43 +03:00
|
|
|
use crate::{Color, ScreenPt, ScreenRectangle, Text, UserInput};
|
2018-11-02 01:29:35 +03:00
|
|
|
use geom::{Bounds, Pt2D};
|
2019-09-11 01:21:58 +03:00
|
|
|
use glium::texture::Texture2d;
|
2019-04-23 05:01:55 +03:00
|
|
|
use glium_glyph::glyph_brush::rusttype::Scale;
|
2019-07-26 17:31:56 +03:00
|
|
|
use glium_glyph::glyph_brush::GlyphCruncher;
|
2019-01-23 21:30:06 +03:00
|
|
|
use glium_glyph::GlyphBrush;
|
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
|
|
|
|
2018-08-06 01:22:55 +03:00
|
|
|
const ZOOM_SPEED: f64 = 0.1;
|
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,
|
2018-12-23 03:58:36 +03:00
|
|
|
window_has_cursor: bool,
|
2018-03-13 18:04:21 +03:00
|
|
|
|
2018-12-18 01:12:53 +03:00
|
|
|
left_mouse_drag_from: Option<ScreenPt>,
|
2018-07-06 20:02:01 +03:00
|
|
|
|
2018-12-21 20:49:46 +03:00
|
|
|
pub window_width: f64,
|
|
|
|
pub window_height: f64,
|
2018-12-17 01:56:28 +03:00
|
|
|
|
2019-08-08 22:56:29 +03:00
|
|
|
pub(crate) screenspace_glyphs: RefCell<GlyphBrush<'static, 'static>>,
|
|
|
|
pub(crate) mapspace_glyphs: RefCell<GlyphBrush<'static, 'static>>,
|
2019-04-23 05:01:55 +03:00
|
|
|
line_height_per_font_size: RefCell<HashMap<usize, f64>>,
|
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...
|
|
|
|
pub(crate) hide_modal_menus: bool,
|
2019-05-29 23:28:51 +03:00
|
|
|
pub(crate) lctrl_held: bool,
|
2019-09-11 01:21:58 +03:00
|
|
|
|
|
|
|
// TODO Definitely a weird place to stash this!
|
2019-09-11 01:44:07 +03:00
|
|
|
pub(crate) textures: Vec<(String, Texture2d)>,
|
2019-09-11 03:04:33 +03:00
|
|
|
pub(crate) texture_lookups: HashMap<String, Color>,
|
2019-10-07 20:39:09 +03:00
|
|
|
// Of the default font size
|
|
|
|
pub line_height: f64,
|
|
|
|
pub(crate) font_size: usize,
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Canvas {
|
2019-01-25 20:48:33 +03:00
|
|
|
pub(crate) fn new(
|
|
|
|
initial_width: f64,
|
|
|
|
initial_height: f64,
|
2019-08-08 22:56:29 +03:00
|
|
|
screenspace_glyphs: GlyphBrush<'static, 'static>,
|
|
|
|
mapspace_glyphs: GlyphBrush<'static, 'static>,
|
2019-10-07 20:39:09 +03:00
|
|
|
font_size: usize,
|
2019-01-25 20:48:33 +03:00
|
|
|
) -> Canvas {
|
2019-10-07 20:39:09 +03:00
|
|
|
let mut c = 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
|
|
|
|
|
|
|
left_mouse_drag_from: None,
|
2019-01-25 20:48:33 +03:00
|
|
|
window_width: initial_width,
|
|
|
|
window_height: initial_height,
|
2018-12-17 01:56:28 +03:00
|
|
|
|
2019-08-08 22:56:29 +03:00
|
|
|
screenspace_glyphs: RefCell::new(screenspace_glyphs),
|
|
|
|
mapspace_glyphs: RefCell::new(mapspace_glyphs),
|
2019-04-23 05:01:55 +03:00
|
|
|
line_height_per_font_size: RefCell::new(HashMap::new()),
|
2018-12-23 05:02:03 +03:00
|
|
|
covered_areas: RefCell::new(Vec::new()),
|
2019-05-04 01:32:14 +03:00
|
|
|
|
|
|
|
hide_modal_menus: false,
|
2019-05-29 23:28:51 +03:00
|
|
|
lctrl_held: false,
|
2019-09-11 01:21:58 +03:00
|
|
|
|
2019-09-11 01:44:07 +03:00
|
|
|
textures: Vec::new(),
|
2019-09-11 03:04:33 +03:00
|
|
|
texture_lookups: HashMap::new(),
|
2019-10-07 20:39:09 +03:00
|
|
|
line_height: 0.0,
|
|
|
|
font_size,
|
|
|
|
};
|
|
|
|
c.line_height = c.line_height(c.font_size);
|
|
|
|
c
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2019-06-06 22:12:03 +03:00
|
|
|
pub(crate) fn is_dragging(&self) -> bool {
|
2018-03-13 18:04:21 +03:00
|
|
|
self.left_mouse_drag_from.is_some()
|
|
|
|
}
|
|
|
|
|
2018-10-06 01:44:12 +03:00
|
|
|
pub fn handle_event(&mut self, input: &mut UserInput) {
|
2018-12-18 01:12:53 +03:00
|
|
|
if let Some(pt) = input.get_moved_mouse() {
|
|
|
|
self.cursor_x = pt.x;
|
|
|
|
self.cursor_y = pt.y;
|
|
|
|
|
|
|
|
if let Some(click) = self.left_mouse_drag_from {
|
|
|
|
self.cam_x += click.x - pt.x;
|
|
|
|
self.cam_y += click.y - pt.y;
|
|
|
|
self.left_mouse_drag_from = Some(pt);
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-06 22:05:09 +03:00
|
|
|
// Can't start dragging or zooming on top of covered area
|
|
|
|
let mouse_on_map = self.get_cursor_in_map_space().is_some();
|
|
|
|
if input.left_mouse_button_pressed() && mouse_on_map {
|
2018-12-18 01:29:59 +03:00
|
|
|
self.left_mouse_drag_from = Some(self.get_cursor_in_screen_space());
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
2018-12-16 05:27:43 +03:00
|
|
|
if input.left_mouse_button_released() {
|
2018-03-13 18:04:21 +03:00
|
|
|
self.left_mouse_drag_from = None;
|
|
|
|
}
|
2019-08-06 22:05:09 +03:00
|
|
|
if mouse_on_map {
|
|
|
|
if let Some(scroll) = input.get_mouse_scroll() {
|
|
|
|
// Zoom slower at low zooms, faster at high.
|
|
|
|
let delta = scroll * ZOOM_SPEED * self.cam_zoom;
|
|
|
|
self.zoom_towards_mouse(delta);
|
|
|
|
}
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
2018-12-23 03:58:36 +03:00
|
|
|
if input.window_gained_cursor() {
|
|
|
|
self.window_has_cursor = true;
|
|
|
|
}
|
|
|
|
if input.window_lost_cursor() {
|
|
|
|
self.window_has_cursor = 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();
|
|
|
|
}
|
|
|
|
|
2019-07-28 13:16:41 +03:00
|
|
|
pub 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
|
|
|
}
|
|
|
|
|
|
|
|
fn zoom_towards_mouse(&mut self, delta_zoom: f64) {
|
|
|
|
let old_zoom = self.cam_zoom;
|
|
|
|
self.cam_zoom += delta_zoom;
|
|
|
|
if self.cam_zoom <= ZOOM_SPEED {
|
|
|
|
self.cam_zoom = ZOOM_SPEED;
|
|
|
|
}
|
|
|
|
|
2018-09-19 00:41:30 +03:00
|
|
|
// Make screen_to_map of cursor_{x,y} still point to the same thing after zooming.
|
2018-03-13 18:04:21 +03:00
|
|
|
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-07-28 10:59:25 +03:00
|
|
|
pub fn get_cursor_in_screen_space(&self) -> ScreenPt {
|
2018-12-18 01:29:59 +03:00
|
|
|
ScreenPt::new(self.cursor_x, self.cursor_y)
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-12-23 05:02:03 +03:00
|
|
|
let pt = self.get_cursor_in_screen_space();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-01 10:32:38 +03:00
|
|
|
pub(crate) 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-02-01 10:32:38 +03:00
|
|
|
pub fn text_dims(&self, txt: &Text) -> (f64, f64) {
|
|
|
|
txt.dims(self)
|
|
|
|
}
|
2019-04-23 05:01:55 +03:00
|
|
|
|
2019-08-08 22:56:29 +03:00
|
|
|
// Don't call this while screenspace_glyphs is mutably borrowed.
|
2019-04-23 05:01:55 +03:00
|
|
|
pub(crate) fn line_height(&self, font_size: usize) -> f64 {
|
|
|
|
let mut hash = self.line_height_per_font_size.borrow_mut();
|
|
|
|
if hash.contains_key(&font_size) {
|
|
|
|
return hash[&font_size];
|
|
|
|
}
|
2019-08-08 22:56:29 +03:00
|
|
|
let vmetrics =
|
|
|
|
self.screenspace_glyphs.borrow().fonts()[0].v_metrics(Scale::uniform(font_size as f32));
|
2019-04-23 05:01:55 +03:00
|
|
|
// TODO This works for this font, but could be more paranoid with abs()
|
|
|
|
let line_height = f64::from(vmetrics.ascent - vmetrics.descent + vmetrics.line_gap);
|
|
|
|
hash.insert(font_size, line_height);
|
|
|
|
line_height
|
|
|
|
}
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
2018-09-28 17:52:36 +03:00
|
|
|
|
|
|
|
pub enum HorizontalAlignment {
|
|
|
|
Left,
|
|
|
|
Center,
|
|
|
|
Right,
|
2019-05-02 20:21:11 +03:00
|
|
|
FillScreen,
|
2018-09-28 17:52:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum VerticalAlignment {
|
|
|
|
Top,
|
|
|
|
Center,
|
|
|
|
Bottom,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const BOTTOM_LEFT: (HorizontalAlignment, VerticalAlignment) =
|
|
|
|
(HorizontalAlignment::Left, VerticalAlignment::Bottom);
|
|
|
|
pub const CENTERED: (HorizontalAlignment, VerticalAlignment) =
|
|
|
|
(HorizontalAlignment::Center, VerticalAlignment::Center);
|