From 3cab7de58b435345a2cf1a1fe83a226c486037a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noah=20H=C3=BCsser?= Date: Sun, 15 Sep 2019 00:02:17 +0200 Subject: [PATCH] Text works; fixed intersection testing with new system --- Cargo.lock | 71 +++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/app_state.rs | 2 +- src/bin/drawing/painter.rs | 27 +++++++++++ src/lib/interaction/collider.rs | 22 +++++---- src/lib/vector_tile/tile.rs | 12 +++++ src/lib/vector_tile/visible_tile.rs | 36 +++++++++++++++ 7 files changed, 162 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 224a766..f433ed2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -749,6 +749,28 @@ dependencies = [ "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "glyph_brush" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glyph_brush_layout 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "hashbrown 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rusttype 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", + "twox-hash 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "glyph_brush_layout" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rusttype 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)", + "xi-unicode 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "h2" version = "0.1.26" @@ -766,6 +788,11 @@ dependencies = [ "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hashbrown" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "heapsize" version = "0.4.2" @@ -2021,6 +2048,14 @@ name = "rustc-demangle" version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rustc-hash" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rustc-hex" version = "2.0.1" @@ -2041,7 +2076,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "approx 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", + "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "stb_truetype 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2081,6 +2121,7 @@ dependencies = [ "size_format 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "varint 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "wgpu 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "wgpu_glyph 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "winit 0.20.0-alpha3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2536,6 +2577,14 @@ dependencies = [ "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "twox-hash" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "typenum" version = "1.11.2" @@ -2809,6 +2858,16 @@ dependencies = [ "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "wgpu_glyph" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glyph_brush 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "wgpu 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi" version = "0.2.8" @@ -2924,6 +2983,11 @@ name = "xdg" version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "xi-unicode" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "xml-rs" version = "0.8.0" @@ -3048,7 +3112,10 @@ dependencies = [ "checksum gfx-backend-vulkan 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5facf0b6e4306a6a93682077f85602c89b49aca8ecceb73b9558e7baa37f8c54" "checksum gfx-hal 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00a079b66250a6437c3822c8bfc25e5bed5ba0186d312e7df87bc503325ab33c" "checksum glsl-to-spirv 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "28caebc98746d507603a2d3df66dcbe04e41d4febad0320f3eec1ef72b6bbef1" +"checksum glyph_brush 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9cb83c7711eb335fed6a825b8b50a2ad3418bbfcb2b3bd4394c6e68346fb660d" +"checksum glyph_brush_layout 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f417496b6038edc9d5e068531d74ef56769f9ae592840d819c99cafc1aa25f72" "checksum h2 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b34c246847f938a410a03c5458c7fee2274436675e76d8b903c08efc29c462" +"checksum hashbrown 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e1de41fb8dba9714efd92241565cdff73f78508c95697dd56787d3cba27e2353" "checksum heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1679e6ea370dee694f91f1dc469bf94cf8f52051d147aec3e1f9497c6fc22461" "checksum hibitset 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "47e7292fd9f7fe89fa35c98048f2d0a69b79ed243604234d18f6f8a1aa6f408d" "checksum http 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "372bcb56f939e449117fb0869c2e8fd8753a8223d92a172c6e808cf123a5b6e4" @@ -3183,6 +3250,7 @@ dependencies = [ "checksum rlp 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fa2f7f9c612d133da9101ef7bcd3e603ca7098901eca852e71f87a83dd3e6b59" "checksum rust-ini 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e52c148ef37f8c375d49d5a73aa70713125b7f19095948a923f80afdeb22ec2" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" +"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8" "checksum rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "403bb3a286107a04825a5f82e1270acc1e14028d3d554d7a1e08914549575ab8" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum rusttype 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)" = "654103d61a05074b268a107cf6581ce120f0fc0115f2610ed9dfea363bb81139" @@ -3237,6 +3305,7 @@ dependencies = [ "checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" "checksum try_from 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "283d3b89e1368717881a9d51dad843cc435380d8109c9e47d38780a324698d8b" +"checksum twox-hash 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3bfd5b7557925ce778ff9b9ef90e3ade34c524b5ff10e239c69a42d546d2af56" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" "checksum uint 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8f0f47ed099f0db671ce82c66548c5de012e3c0cba3963514d1db15c7588701" "checksum unicase 2.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2e2e6bd1e59e56598518beb94fd6db628ded570326f0a98c679a304bd9f00150" @@ -3268,6 +3337,7 @@ dependencies = [ "checksum wayland-sys 0.23.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b7676aeac62796e1624d1f615c9073ddf8cdd951cbcb7b0bb555e4cdc1818114" "checksum wgpu 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "811952b5c4404d0344b5b5af74ed61ba11d4a2bef4ce5dae31fc25def21c7a25" "checksum wgpu-native 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2bdb3ad9009d231cfa5a68ca58f28c5f12c4b86570ef8777e2ebac1a4adc8373" +"checksum wgpu_glyph 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b88adea3f37bef995c5290fc255e43108996a868e73ec751b55e9e51d7475ba0" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" @@ -3281,6 +3351,7 @@ dependencies = [ "checksum x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39697e3123f715483d311b5826e254b6f3cfebdd83cf7ef3358f579c3d68e235" "checksum x11-dl 2.18.4 (registry+https://github.com/rust-lang/crates.io-index)" = "be65e1342a3baae65439cd03306778831a3d133b0d20243a7fb83fd5cf403c58" "checksum xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" +"checksum xi-unicode 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7395cdb9d0a6219fa0ea77d08c946adf9c1984c72fcd443ace30365f3daadef7" "checksum xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "541b12c998c5b56aa2b4e6f18f03664eef9a4fd0a246a55594efae6cc2d964b5" "checksum yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d" "checksum zerocopy 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "992b9b31f80fd4a167f903f879b8ca43d6716cc368ea01df90538baa2dd34056" diff --git a/Cargo.toml b/Cargo.toml index c5edbc4..6f33de1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,7 @@ parity-util-mem = "0.2.0" malloc_size_of_derive = "0.1.0" size_format = "1.0.2" derivative = "1.0.3" +wgpu_glyph = "0.4.0" [dependencies.imgui-winit-support] version = "0.2.0" diff --git a/src/bin/app_state.rs b/src/bin/app_state.rs index 37cc34a..38ed982 100644 --- a/src/bin/app_state.rs +++ b/src/bin/app_state.rs @@ -129,7 +129,7 @@ impl AppState { } pub fn update_hovered_objects(&mut self, point: (f32, f32)) { - self.hovered_objects = Collider::get_hovered_objects(&self.tile_cache, &self.screen, self.zoom ,point); + self.hovered_objects = Collider::get_hovered_objects(&self.visible_tiles, &self.screen, self.zoom ,point); } pub fn update_selected_hover_objects(&mut self) { diff --git a/src/bin/drawing/painter.rs b/src/bin/drawing/painter.rs index a617372..975066d 100644 --- a/src/bin/drawing/painter.rs +++ b/src/bin/drawing/painter.rs @@ -45,6 +45,11 @@ use wgpu::{ RenderPassDepthStencilAttachmentDescriptor, DepthStencilStateDescriptor, }; +use wgpu_glyph::{ + Section, + GlyphBrushBuilder, + GlyphBrush, +}; use osm::*; use crate::drawing::helpers::{ @@ -72,6 +77,7 @@ pub struct Painter { bind_group: BindGroup, rx: crossbeam_channel::Receiver>, _watcher: RecommendedWatcher, + glyph_brush: GlyphBrush<'static, ()>, temperature: crate::drawing::weather::Temperature, } @@ -222,6 +228,10 @@ impl Painter { &tile_transform_buffer ); + let font: &[u8] = include_bytes!("../../../config/Ruda-Bold.ttf"); + let mut glyph_brush = GlyphBrushBuilder::using_font_bytes(font) + .build(&mut device, wgpu::TextureFormat::Bgra8Unorm); + let mut temperature = crate::drawing::weather::Temperature::init(&mut device); let init_command_buf = init_encoder.finish(); @@ -249,6 +259,7 @@ impl Painter { bind_group, _watcher: watcher, rx, + glyph_brush, temperature, } } @@ -687,6 +698,22 @@ impl Painter { } } + for (i, vt) in app_state.visible_tiles().values().enumerate() { + vt.queue_text( + &mut self.glyph_brush, + &app_state.screen, + app_state.zoom + ); + } + + let _ = self.glyph_brush.draw_queued( + &mut self.device, + &mut encoder, + &frame.view, + app_state.screen.width, + app_state.screen.height, + ); + // self.temperature.paint(&mut encoder, &frame.view); hud.paint( diff --git a/src/lib/interaction/collider.rs b/src/lib/interaction/collider.rs index 9633457..d0f15b4 100644 --- a/src/lib/interaction/collider.rs +++ b/src/lib/interaction/collider.rs @@ -1,3 +1,4 @@ +use std::collections::BTreeMap; use ncollide2d::math::Point; use nalgebra::base::Vector4; @@ -7,13 +8,18 @@ pub struct Collider { } impl Collider { - pub fn get_hovered_objects<'a>(cache: &'a TileCache, screen: &Screen, zoom: f32, point: (f32, f32)) -> Vec { + pub fn get_hovered_objects<'a>( + visible_tiles: &'a BTreeMap, + screen: &Screen, + zoom: f32, + point: (f32, f32) + ) -> Vec { let mut return_objects = vec![]; let tile_field = screen.get_tile_boundaries_for_zoom_level(zoom, 1); for tile_id in tile_field.iter() { - if let Some(tile) = cache.try_get_tile(&tile_id) { - let read_tile = tile.read().unwrap(); + if let Some(visible_tile) = visible_tiles.get(&tile_id) { + let extent = visible_tile.extent() as f32; let matrix = screen.tile_to_global_space( zoom, &tile_id @@ -24,12 +30,12 @@ impl Collider { point.1 / (screen.height / 2) as f32 - 1.0 ); let global_point = matrix * Vector4::new(screen_point.x, screen_point.y, 0.0, 1.0); - let tile_point = Point::new(global_point.x, global_point.y) * read_tile.extent() as f32; + let tile_point = Point::new(global_point.x, global_point.y) * extent; - if tile_point.x >= 0.0 && tile_point.x <= read_tile.extent() as f32 - && tile_point.y >= 0.0 && tile_point.y <= read_tile.extent() as f32 { - if let Ok(collider) = read_tile.collider().try_read() { - if let Ok(objects) = read_tile.objects().try_read() { + if tile_point.x >= 0.0 && tile_point.x <= extent + && tile_point.y >= 0.0 && tile_point.y <= extent { + if let Ok(collider) = visible_tile.collider().try_read() { + if let Ok(objects) = visible_tile.objects().try_read() { let object_ids = collider.get_hovered_objects(&tile_point); for object_id in object_ids { return_objects.push(objects[object_id].clone()) diff --git a/src/lib/vector_tile/tile.rs b/src/lib/vector_tile/tile.rs index 5afb3ce..e725d2d 100644 --- a/src/lib/vector_tile/tile.rs +++ b/src/lib/vector_tile/tile.rs @@ -79,6 +79,7 @@ pub struct Tile { objects: Arc>>, features: Vec<(u32, Range)>, collider: Arc>, + text: Vec<((f32, f32), String)>, stats: TileStats, } @@ -124,6 +125,7 @@ impl Tile { let mut builder = MeshBuilder::new(&mut mesh, LayerVertexCtor::new(tile_id, 1.0)); let extent = tile.layers[0].extent as u16; let mut features = vec![]; + let mut text = vec![]; // Add a background feature to the tile data. let ( @@ -151,6 +153,11 @@ impl Tile { &feature.geometry ); + if let Some(tag) = tags.get("name:en") { + let point = paths[0].points()[0]; + text.push(((point.x / extent as f32, point.y / extent as f32), tag.clone())); + } + // If we have a valid object at hand, insert it into the object list let object_type = match feature.type_pb { @@ -254,6 +261,7 @@ impl Tile { objects, features, collider, + text, stats, } } @@ -278,6 +286,10 @@ impl Tile { &self.features } + pub fn text(&self) -> &Vec<((f32, f32), String)> { + &self.text + } + pub fn stats(&self) -> &TileStats { &self.stats } diff --git a/src/lib/vector_tile/visible_tile.rs b/src/lib/vector_tile/visible_tile.rs index 9bde252..df4f89c 100644 --- a/src/lib/vector_tile/visible_tile.rs +++ b/src/lib/vector_tile/visible_tile.rs @@ -4,6 +4,10 @@ use std::sync::{ }; use wgpu::*; +use wgpu_glyph::{ + Section, + GlyphBrush, +}; use crate::*; @@ -29,6 +33,10 @@ impl VisibleTile { pub fn extent(&self) -> u16 { self.tile.read().unwrap().extent() } + + pub fn objects(&self) -> Arc>> { + self.tile.read().unwrap().objects() + } pub fn load_to_gpu(&self, device: &Device) { let read_tile = self.tile.read().unwrap(); @@ -49,6 +57,10 @@ impl VisibleTile { self.tile_collider.load(self.tile.clone()); } + pub fn collider(&self) -> Arc> { + self.tile_collider.clone() + } + pub fn paint( &self, render_pass: &mut RenderPass, @@ -89,4 +101,28 @@ impl VisibleTile { } } } + + pub fn queue_text( + &self, + glyph_brush: &mut GlyphBrush<'static, ()>, + screen: &Screen, + z: f32 + ) { + let read_tile = self.tile.read().unwrap(); + let matrix = screen.tile_to_global_space(z, &read_tile.tile_id()); + for text in read_tile.text() { + let position = matrix * glm::vec4((text.0).0, (text.0).1, 0.0, 1.0); + // dbg!(&position); + let section = Section { + text: &text.1, + screen_position: ( + (position.x + 1.0) * screen.width as f32 / 2.0, + (position.y + 1.0) * screen.height as f32 / 2.0 + ), + ..Section::default() // color, position, etc + }; + + glyph_brush.queue(section); + } + } } \ No newline at end of file