Skip to content

Commit 3cab7de

Browse files
committed
Text works; fixed intersection testing with new system
1 parent bff85fa commit 3cab7de

File tree

7 files changed

+162
-9
lines changed

7 files changed

+162
-9
lines changed

Cargo.lock

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ parity-util-mem = "0.2.0"
5454
malloc_size_of_derive = "0.1.0"
5555
size_format = "1.0.2"
5656
derivative = "1.0.3"
57+
wgpu_glyph = "0.4.0"
5758

5859
[dependencies.imgui-winit-support]
5960
version = "0.2.0"

src/bin/app_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl AppState {
129129
}
130130

131131
pub fn update_hovered_objects(&mut self, point: (f32, f32)) {
132-
self.hovered_objects = Collider::get_hovered_objects(&self.tile_cache, &self.screen, self.zoom ,point);
132+
self.hovered_objects = Collider::get_hovered_objects(&self.visible_tiles, &self.screen, self.zoom ,point);
133133
}
134134

135135
pub fn update_selected_hover_objects(&mut self) {

src/bin/drawing/painter.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ use wgpu::{
4545
RenderPassDepthStencilAttachmentDescriptor,
4646
DepthStencilStateDescriptor,
4747
};
48+
use wgpu_glyph::{
49+
Section,
50+
GlyphBrushBuilder,
51+
GlyphBrush,
52+
};
4853
use osm::*;
4954

5055
use crate::drawing::helpers::{
@@ -72,6 +77,7 @@ pub struct Painter {
7277
bind_group: BindGroup,
7378
rx: crossbeam_channel::Receiver<std::result::Result<notify::event::Event, notify::Error>>,
7479
_watcher: RecommendedWatcher,
80+
glyph_brush: GlyphBrush<'static, ()>,
7581
temperature: crate::drawing::weather::Temperature,
7682
}
7783

@@ -222,6 +228,10 @@ impl Painter {
222228
&tile_transform_buffer
223229
);
224230

231+
let font: &[u8] = include_bytes!("../../../config/Ruda-Bold.ttf");
232+
let mut glyph_brush = GlyphBrushBuilder::using_font_bytes(font)
233+
.build(&mut device, wgpu::TextureFormat::Bgra8Unorm);
234+
225235
let mut temperature = crate::drawing::weather::Temperature::init(&mut device);
226236

227237
let init_command_buf = init_encoder.finish();
@@ -249,6 +259,7 @@ impl Painter {
249259
bind_group,
250260
_watcher: watcher,
251261
rx,
262+
glyph_brush,
252263
temperature,
253264
}
254265
}
@@ -687,6 +698,22 @@ impl Painter {
687698
}
688699
}
689700

701+
for (i, vt) in app_state.visible_tiles().values().enumerate() {
702+
vt.queue_text(
703+
&mut self.glyph_brush,
704+
&app_state.screen,
705+
app_state.zoom
706+
);
707+
}
708+
709+
let _ = self.glyph_brush.draw_queued(
710+
&mut self.device,
711+
&mut encoder,
712+
&frame.view,
713+
app_state.screen.width,
714+
app_state.screen.height,
715+
);
716+
690717
// self.temperature.paint(&mut encoder, &frame.view);
691718

692719
hud.paint(

src/lib/interaction/collider.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::BTreeMap;
12
use ncollide2d::math::Point;
23
use nalgebra::base::Vector4;
34

@@ -7,13 +8,18 @@ pub struct Collider {
78
}
89

910
impl Collider {
10-
pub fn get_hovered_objects<'a>(cache: &'a TileCache, screen: &Screen, zoom: f32, point: (f32, f32)) -> Vec<Object> {
11+
pub fn get_hovered_objects<'a>(
12+
visible_tiles: &'a BTreeMap<TileId, VisibleTile>,
13+
screen: &Screen,
14+
zoom: f32,
15+
point: (f32, f32)
16+
) -> Vec<Object> {
1117
let mut return_objects = vec![];
1218
let tile_field = screen.get_tile_boundaries_for_zoom_level(zoom, 1);
1319

1420
for tile_id in tile_field.iter() {
15-
if let Some(tile) = cache.try_get_tile(&tile_id) {
16-
let read_tile = tile.read().unwrap();
21+
if let Some(visible_tile) = visible_tiles.get(&tile_id) {
22+
let extent = visible_tile.extent() as f32;
1723
let matrix = screen.tile_to_global_space(
1824
zoom,
1925
&tile_id
@@ -24,12 +30,12 @@ impl Collider {
2430
point.1 / (screen.height / 2) as f32 - 1.0
2531
);
2632
let global_point = matrix * Vector4::new(screen_point.x, screen_point.y, 0.0, 1.0);
27-
let tile_point = Point::new(global_point.x, global_point.y) * read_tile.extent() as f32;
33+
let tile_point = Point::new(global_point.x, global_point.y) * extent;
2834

29-
if tile_point.x >= 0.0 && tile_point.x <= read_tile.extent() as f32
30-
&& tile_point.y >= 0.0 && tile_point.y <= read_tile.extent() as f32 {
31-
if let Ok(collider) = read_tile.collider().try_read() {
32-
if let Ok(objects) = read_tile.objects().try_read() {
35+
if tile_point.x >= 0.0 && tile_point.x <= extent
36+
&& tile_point.y >= 0.0 && tile_point.y <= extent {
37+
if let Ok(collider) = visible_tile.collider().try_read() {
38+
if let Ok(objects) = visible_tile.objects().try_read() {
3339
let object_ids = collider.get_hovered_objects(&tile_point);
3440
for object_id in object_ids {
3541
return_objects.push(objects[object_id].clone())

src/lib/vector_tile/tile.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub struct Tile {
7979
objects: Arc<RwLock<Vec<Object>>>,
8080
features: Vec<(u32, Range<u32>)>,
8181
collider: Arc<RwLock<TileCollider>>,
82+
text: Vec<((f32, f32), String)>,
8283
stats: TileStats,
8384
}
8485

@@ -124,6 +125,7 @@ impl Tile {
124125
let mut builder = MeshBuilder::new(&mut mesh, LayerVertexCtor::new(tile_id, 1.0));
125126
let extent = tile.layers[0].extent as u16;
126127
let mut features = vec![];
128+
let mut text = vec![];
127129

128130
// Add a background feature to the tile data.
129131
let (
@@ -151,6 +153,11 @@ impl Tile {
151153
&feature.geometry
152154
);
153155

156+
if let Some(tag) = tags.get("name:en") {
157+
let point = paths[0].points()[0];
158+
text.push(((point.x / extent as f32, point.y / extent as f32), tag.clone()));
159+
}
160+
154161
// If we have a valid object at hand, insert it into the object list
155162

156163
let object_type = match feature.type_pb {
@@ -254,6 +261,7 @@ impl Tile {
254261
objects,
255262
features,
256263
collider,
264+
text,
257265
stats,
258266
}
259267
}
@@ -278,6 +286,10 @@ impl Tile {
278286
&self.features
279287
}
280288

289+
pub fn text(&self) -> &Vec<((f32, f32), String)> {
290+
&self.text
291+
}
292+
281293
pub fn stats(&self) -> &TileStats {
282294
&self.stats
283295
}

src/lib/vector_tile/visible_tile.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use std::sync::{
44
};
55

66
use wgpu::*;
7+
use wgpu_glyph::{
8+
Section,
9+
GlyphBrush,
10+
};
711

812
use crate::*;
913

@@ -29,6 +33,10 @@ impl VisibleTile {
2933
pub fn extent(&self) -> u16 {
3034
self.tile.read().unwrap().extent()
3135
}
36+
37+
pub fn objects(&self) -> Arc<RwLock<Vec<Object>>> {
38+
self.tile.read().unwrap().objects()
39+
}
3240

3341
pub fn load_to_gpu(&self, device: &Device) {
3442
let read_tile = self.tile.read().unwrap();
@@ -49,6 +57,10 @@ impl VisibleTile {
4957
self.tile_collider.load(self.tile.clone());
5058
}
5159

60+
pub fn collider(&self) -> Arc<RwLock<TileCollider>> {
61+
self.tile_collider.clone()
62+
}
63+
5264
pub fn paint(
5365
&self,
5466
render_pass: &mut RenderPass,
@@ -89,4 +101,28 @@ impl VisibleTile {
89101
}
90102
}
91103
}
104+
105+
pub fn queue_text(
106+
&self,
107+
glyph_brush: &mut GlyphBrush<'static, ()>,
108+
screen: &Screen,
109+
z: f32
110+
) {
111+
let read_tile = self.tile.read().unwrap();
112+
let matrix = screen.tile_to_global_space(z, &read_tile.tile_id());
113+
for text in read_tile.text() {
114+
let position = matrix * glm::vec4((text.0).0, (text.0).1, 0.0, 1.0);
115+
// dbg!(&position);
116+
let section = Section {
117+
text: &text.1,
118+
screen_position: (
119+
(position.x + 1.0) * screen.width as f32 / 2.0,
120+
(position.y + 1.0) * screen.height as f32 / 2.0
121+
),
122+
..Section::default() // color, position, etc
123+
};
124+
125+
glyph_brush.queue(section);
126+
}
127+
}
92128
}

0 commit comments

Comments
 (0)