Skip to content

Commit

Permalink
Text works; fixed intersection testing with new system
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatekii committed Sep 14, 2019
1 parent bff85fa commit 3cab7de
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 9 deletions.
71 changes: 71 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions src/bin/drawing/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ use wgpu::{
RenderPassDepthStencilAttachmentDescriptor,
DepthStencilStateDescriptor,
};
use wgpu_glyph::{
Section,
GlyphBrushBuilder,
GlyphBrush,
};
use osm::*;

use crate::drawing::helpers::{
Expand Down Expand Up @@ -72,6 +77,7 @@ pub struct Painter {
bind_group: BindGroup,
rx: crossbeam_channel::Receiver<std::result::Result<notify::event::Event, notify::Error>>,
_watcher: RecommendedWatcher,
glyph_brush: GlyphBrush<'static, ()>,
temperature: crate::drawing::weather::Temperature,
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -249,6 +259,7 @@ impl Painter {
bind_group,
_watcher: watcher,
rx,
glyph_brush,
temperature,
}
}
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 14 additions & 8 deletions src/lib/interaction/collider.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use ncollide2d::math::Point;
use nalgebra::base::Vector4;

Expand All @@ -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<Object> {
pub fn get_hovered_objects<'a>(
visible_tiles: &'a BTreeMap<TileId, VisibleTile>,
screen: &Screen,
zoom: f32,
point: (f32, f32)
) -> Vec<Object> {
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
Expand All @@ -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())
Expand Down
12 changes: 12 additions & 0 deletions src/lib/vector_tile/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub struct Tile {
objects: Arc<RwLock<Vec<Object>>>,
features: Vec<(u32, Range<u32>)>,
collider: Arc<RwLock<TileCollider>>,
text: Vec<((f32, f32), String)>,
stats: TileStats,
}

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -254,6 +261,7 @@ impl Tile {
objects,
features,
collider,
text,
stats,
}
}
Expand All @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions src/lib/vector_tile/visible_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use std::sync::{
};

use wgpu::*;
use wgpu_glyph::{
Section,
GlyphBrush,
};

use crate::*;

Expand All @@ -29,6 +33,10 @@ impl VisibleTile {
pub fn extent(&self) -> u16 {
self.tile.read().unwrap().extent()
}

pub fn objects(&self) -> Arc<RwLock<Vec<Object>>> {
self.tile.read().unwrap().objects()
}

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

pub fn collider(&self) -> Arc<RwLock<TileCollider>> {
self.tile_collider.clone()
}

pub fn paint(
&self,
render_pass: &mut RenderPass,
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 3cab7de

Please sign in to comment.