2018-06-04 03:47:46 +03:00
|
|
|
// Copyright 2018 Google LLC, licensed under http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-13 18:04:21 +03:00
|
|
|
|
2018-12-18 01:12:53 +03:00
|
|
|
use crate::{text, GfxCtx, ScreenPt, Text, UserInput};
|
2018-11-02 01:29:35 +03:00
|
|
|
use geom::{Bounds, Pt2D};
|
2018-07-06 20:33:03 +03:00
|
|
|
use graphics::Transformed;
|
2018-12-17 01:56:28 +03:00
|
|
|
use opengl_graphics::{Filter, GlyphCache, TextureSettings};
|
|
|
|
use std::cell::RefCell;
|
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.
|
2018-03-13 18:04:21 +03:00
|
|
|
cursor_x: f64,
|
|
|
|
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
|
|
|
|
|
|
|
glyphs: RefCell<GlyphCache<'static>>,
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Canvas {
|
2018-12-19 02:30:06 +03:00
|
|
|
pub fn new(initial_width: u32, initial_height: u32) -> Canvas {
|
2018-12-17 01:56:28 +03:00
|
|
|
let texture_settings = TextureSettings::new().filter(Filter::Nearest);
|
|
|
|
// TODO We could also preload everything and not need the RefCell.
|
|
|
|
let glyphs = RefCell::new(
|
|
|
|
GlyphCache::new(
|
|
|
|
// TODO don't assume this exists!
|
|
|
|
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
|
|
|
|
(),
|
|
|
|
texture_settings,
|
|
|
|
)
|
|
|
|
.expect("Could not load font"),
|
|
|
|
);
|
|
|
|
|
2018-03-13 18:04:21 +03:00
|
|
|
Canvas {
|
|
|
|
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,
|
2018-12-21 20:49:46 +03:00
|
|
|
window_width: f64::from(initial_width),
|
|
|
|
window_height: f64::from(initial_height),
|
2018-12-17 01:56:28 +03:00
|
|
|
|
|
|
|
glyphs,
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_dragging(&self) -> bool {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-12-16 05:27:43 +03:00
|
|
|
if input.left_mouse_button_pressed() {
|
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;
|
|
|
|
}
|
2018-12-16 05:27:43 +03:00
|
|
|
if let Some(scroll) = input.get_mouse_scroll() {
|
2018-08-06 01:22:55 +03:00
|
|
|
// Zoom slower at low zooms, faster at high.
|
2018-10-06 01:44:12 +03:00
|
|
|
let delta = scroll * ZOOM_SPEED * self.cam_zoom;
|
2018-08-06 01:22:55 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-19 21:51:35 +03:00
|
|
|
pub(crate) fn start_drawing(&self, g: &mut GfxCtx) {
|
2018-09-16 02:54:00 +03:00
|
|
|
g.ctx = g
|
|
|
|
.orig_ctx
|
2018-07-06 20:33:03 +03:00
|
|
|
.trans(-self.cam_x, -self.cam_y)
|
|
|
|
.zoom(self.cam_zoom)
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2018-09-28 18:01:15 +03:00
|
|
|
pub fn draw_mouse_tooltip(&self, g: &mut GfxCtx, txt: Text) {
|
2018-12-17 01:56:28 +03:00
|
|
|
let glyphs = &mut self.glyphs.borrow_mut();
|
|
|
|
let (width, height) = txt.dims(glyphs);
|
2018-03-13 18:04:21 +03:00
|
|
|
let x1 = self.cursor_x - (width / 2.0);
|
|
|
|
let y1 = self.cursor_y - (height / 2.0);
|
2018-12-18 01:12:53 +03:00
|
|
|
text::draw_text_bubble(g, glyphs, ScreenPt::new(x1, y1), txt);
|
2018-03-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
|
2018-12-18 01:12:53 +03:00
|
|
|
pub fn draw_text_at(&self, g: &mut GfxCtx, txt: Text, map_pt: Pt2D) {
|
2018-12-17 01:56:28 +03:00
|
|
|
let glyphs = &mut self.glyphs.borrow_mut();
|
|
|
|
let (width, height) = txt.dims(glyphs);
|
2018-12-18 01:12:53 +03:00
|
|
|
let pt = self.map_to_screen(map_pt);
|
|
|
|
text::draw_text_bubble(
|
|
|
|
g,
|
|
|
|
glyphs,
|
|
|
|
ScreenPt::new(pt.x - (width / 2.0), pt.y - (height / 2.0)),
|
|
|
|
txt,
|
|
|
|
);
|
2018-09-28 17:28:30 +03:00
|
|
|
}
|
|
|
|
|
2018-12-17 07:01:47 +03:00
|
|
|
pub fn draw_text_at_topleft(&self, g: &mut GfxCtx, txt: Text, pt: Pt2D) {
|
2018-12-18 01:12:53 +03:00
|
|
|
text::draw_text_bubble(
|
|
|
|
g,
|
|
|
|
&mut self.glyphs.borrow_mut(),
|
|
|
|
self.map_to_screen(pt),
|
|
|
|
txt,
|
|
|
|
);
|
2018-12-17 07:01:47 +03:00
|
|
|
}
|
|
|
|
|
2018-12-18 01:12:53 +03:00
|
|
|
pub fn draw_text_at_screenspace_topleft(&self, g: &mut GfxCtx, txt: Text, pt: ScreenPt) {
|
|
|
|
text::draw_text_bubble(g, &mut self.glyphs.borrow_mut(), pt, txt);
|
2018-12-04 22:18:52 +03:00
|
|
|
}
|
|
|
|
|
2018-09-28 17:52:36 +03:00
|
|
|
pub fn draw_text(
|
|
|
|
&self,
|
|
|
|
g: &mut GfxCtx,
|
2018-09-28 18:01:15 +03:00
|
|
|
txt: Text,
|
2018-09-28 17:52:36 +03:00
|
|
|
(horiz, vert): (HorizontalAlignment, VerticalAlignment),
|
|
|
|
) {
|
2018-09-28 18:01:15 +03:00
|
|
|
if txt.is_empty() {
|
2018-09-20 02:26:58 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-12-17 01:56:28 +03:00
|
|
|
let glyphs = &mut self.glyphs.borrow_mut();
|
|
|
|
let (width, height) = txt.dims(glyphs);
|
2018-09-28 17:52:36 +03:00
|
|
|
let x1 = match horiz {
|
|
|
|
HorizontalAlignment::Left => 0.0,
|
2018-12-21 20:49:46 +03:00
|
|
|
HorizontalAlignment::Center => (self.window_width - width) / 2.0,
|
|
|
|
HorizontalAlignment::Right => self.window_width - width,
|
2018-09-28 17:52:36 +03:00
|
|
|
};
|
|
|
|
let y1 = match vert {
|
|
|
|
VerticalAlignment::Top => 0.0,
|
2018-12-21 20:49:46 +03:00
|
|
|
VerticalAlignment::Center => (self.window_height - height) / 2.0,
|
|
|
|
VerticalAlignment::Bottom => self.window_height - height,
|
2018-09-28 17:52:36 +03:00
|
|
|
};
|
2018-12-18 01:12:53 +03:00
|
|
|
text::draw_text_bubble(g, glyphs, ScreenPt::new(x1, y1), txt);
|
2018-12-17 01:56:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn text_dims(&self, txt: &Text) -> (f64, f64) {
|
|
|
|
txt.dims(&mut self.glyphs.borrow_mut())
|
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;
|
|
|
|
}
|
|
|
|
|
2018-12-23 03:37:18 +03:00
|
|
|
pub(crate) 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 {
|
|
|
|
Some(self.screen_to_map(self.get_cursor_in_screen_space()))
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2018-12-18 01:12:53 +03:00
|
|
|
fn map_to_screen(&self, pt: Pt2D) -> ScreenPt {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-09-28 17:52:36 +03:00
|
|
|
|
|
|
|
pub enum HorizontalAlignment {
|
|
|
|
Left,
|
|
|
|
Center,
|
|
|
|
Right,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum VerticalAlignment {
|
|
|
|
Top,
|
|
|
|
Center,
|
|
|
|
Bottom,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const BOTTOM_LEFT: (HorizontalAlignment, VerticalAlignment) =
|
|
|
|
(HorizontalAlignment::Left, VerticalAlignment::Bottom);
|
|
|
|
pub const TOP_RIGHT: (HorizontalAlignment, VerticalAlignment) =
|
|
|
|
(HorizontalAlignment::Right, VerticalAlignment::Top);
|
|
|
|
pub const CENTERED: (HorizontalAlignment, VerticalAlignment) =
|
|
|
|
(HorizontalAlignment::Center, VerticalAlignment::Center);
|