From b828bffba9dad31bc380de9bf28c6f034e359ff7 Mon Sep 17 00:00:00 2001 From: Imbris Date: Tue, 14 May 2019 20:04:58 -0400 Subject: [PATCH] Make name labels work Former-commit-id: 633905535d2842b320d347e368a28ac967a8469a --- voxygen/shaders/ui.vert | 3 +-- voxygen/src/hud/mod.rs | 23 ++++++++++++++------ voxygen/src/render/pipelines/ui.rs | 2 +- voxygen/src/ui/mod.rs | 34 +++++++++++++++++------------- voxygen/src/ui/widgets/ingame.rs | 32 +++++++++++++++++++++++----- 5 files changed, 65 insertions(+), 29 deletions(-) diff --git a/voxygen/shaders/ui.vert b/voxygen/shaders/ui.vert index 6ca9a231da..72593c8e8f 100644 --- a/voxygen/shaders/ui.vert +++ b/voxygen/shaders/ui.vert @@ -26,8 +26,7 @@ void main() { // In-game element gl_Position = proj_mat * - view_mat * - (w_pos + vec4(v_pos, 40.0, 1.0)); + (view_mat * w_pos + vec4(v_pos, 0.0, 0.0)); } else { // Interface element gl_Position = vec4(v_pos, 0.0, 1.0); diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 4f06bb4673..a211ad2484 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -38,6 +38,7 @@ use conrod_core::{ }; use specs::Join; use std::collections::VecDeque; +use vek::*; const XP_COLOR: Color = Color::Rgba(0.59, 0.41, 0.67, 1.0); const TEXT_COLOR: Color = Color::Rgba(1.0, 1.0, 1.0, 1.0); @@ -272,7 +273,7 @@ impl Hud { // Nametags let ecs = client.state().ecs(); - /* { + { let actor_read_storage = ecs.read_storage::(); let pos_read_storage = ecs.read_storage::(); let num = (&actor_read_storage, &pos_read_storage).join().count(); @@ -286,16 +287,26 @@ impl Hud { }) .enumerate() { - Ingame::from_primitive(pos, Text::new(&name)) - .set(self.ids.name_tags[i], ui_widgets); + Ingame::from_primitive( + pos + Vec3::new(0.0, 0.0, 3.0), + Text::new(&name) + .font_size(15) + .color(Color::Rgba(1.0, 1.0, 1.0, 1.0)), + ) + .resolution(50.0) + .set(self.ids.name_tags[i], ui_widgets); } - }*/ + } // test Ingame::from_primitive( [0.0, 25.0, 25.0].into(), - Rectangle::fill_with([1.0, 1.0], Color::Rgba(0.2, 0.0, 0.4, 1.0)), + //Rectangle::fill_with([1.0, 1.0], Color::Rgba(0.2, 0.0, 0.4, 1.0)), + Text::new("Squarefection") + .font_size(20) + .color(TEXT_COLOR) + .font_id(self.fonts.opensans), ) - .x_y(0.0, 0.0) + .resolution(40.0) .set(self.ids.temp, ui_widgets); // Display debug window. diff --git a/voxygen/src/render/pipelines/ui.rs b/voxygen/src/render/pipelines/ui.rs index ac2433ffa1..4e781c88dd 100644 --- a/voxygen/src/render/pipelines/ui.rs +++ b/voxygen/src/render/pipelines/ui.rs @@ -33,7 +33,7 @@ gfx_defines! { scissor: gfx::Scissor = (), tgt_color: gfx::BlendTarget = ("tgt_color", gfx::state::ColorMask::all(), gfx::preset::blend::ALPHA), - tgt_depth: gfx::DepthTarget = gfx::preset::depth::LESS_EQUAL_WRITE, + tgt_depth: gfx::DepthTarget = gfx::preset::depth::LESS_EQUAL_TEST, } } diff --git a/voxygen/src/ui/mod.rs b/voxygen/src/ui/mod.rs index 889eae471f..2237ce32c1 100644 --- a/voxygen/src/ui/mod.rs +++ b/voxygen/src/ui/mod.rs @@ -32,7 +32,7 @@ use conrod_core::{ render::{Primitive, PrimitiveKind}, text::{self, font}, widget::{self, id::Generator}, - Ui as CrUi, UiBuilder, UiCell, + Rect, UiBuilder, UiCell, }; use graphic::Id as GraphicId; use scale::Scale; @@ -82,7 +82,7 @@ impl assets::Asset for Font { } pub struct Ui { - ui: CrUi, + ui: conrod_core::Ui, image_map: Map, cache: Cache, // Draw commands for the next render @@ -282,7 +282,7 @@ impl Ui { } match in_world { - Some(0) => { + Some((0, _)) => { in_world = None; p_scale_factor = self.scale.scale_factor_physical(); // Finish current state @@ -294,19 +294,19 @@ impl Ui { // Push new position command self.draw_commands.push(DrawCommand::WorldPos(None)); } - Some(n) => in_world = Some(n - 1), + Some((n, res)) => in_world = Some((n - 1, res)), None => (), } // Functions for converting for conrod scalar coords to GL vertex coords (-1.0 to 1.0). - let (ui_win_w, ui_win_h) = if in_world.is_some() { - (2.0, 2.0) + let (ui_win_w, ui_win_h) = if let Some((_, res)) = in_world { + (res as f64, res as f64) } else { (self.ui.win_w, self.ui.win_h) }; let vx = |x: f64| (x / ui_win_w * 2.0) as f32; let vy = |y: f64| (y / ui_win_h * 2.0) as f32; - let gl_aabr = |rect: conrod_core::Rect| { + let gl_aabr = |rect: Rect| { let (l, r, b, t) = rect.l_r_b_t(); Aabr { min: Vec2::new(vx(l), vy(b)), @@ -434,12 +434,16 @@ impl Ui { }; let rect = Aabr { min: Vec2::new( - vx(screen_rect.min.x as f64 / p_scale_factor) - 1.0, - 1.0 - vy(screen_rect.max.y as f64 / p_scale_factor), + vx(screen_rect.min.x as f64 / p_scale_factor + - self.ui.win_w / 2.0), + vy(self.ui.win_h / 2.0 + - screen_rect.max.y as f64 / p_scale_factor), ), max: Vec2::new( - vx(screen_rect.max.x as f64 / p_scale_factor) - 1.0, - 1.0 - vy(screen_rect.min.y as f64 / p_scale_factor), + vx(screen_rect.max.x as f64 / p_scale_factor + - self.ui.win_w / 2.0), + vy(self.ui.win_h / 2.0 + - screen_rect.min.y as f64 / p_scale_factor), ), }; mesh.push_quad(create_ui_quad(rect, uv, color, UiMode::Text)); @@ -496,11 +500,11 @@ impl Ui { PrimitiveKind::Other(container) => { if container.type_id == std::any::TypeId::of::() { // Retrieve world position - let pos = container + let (pos, res) = container .state_and_style::() .unwrap() .state - .pos; + .pos_res(); // Finish current state self.draw_commands.push(match current_state { State::Plain => DrawCommand::plain(start..mesh.vertices().len()), @@ -512,8 +516,8 @@ impl Ui { renderer.create_consts(&[pos.into()]).unwrap(), ))); - in_world = Some(1); - p_scale_factor = self.scale.dpi_factor(); + in_world = Some((1, res)); + p_scale_factor = 1.0; } } _ => {} // TODO: Add this. diff --git a/voxygen/src/ui/widgets/ingame.rs b/voxygen/src/ui/widgets/ingame.rs index 0bdba32f06..6b295d4a40 100644 --- a/voxygen/src/ui/widgets/ingame.rs +++ b/voxygen/src/ui/widgets/ingame.rs @@ -1,5 +1,5 @@ use conrod_core::{ - image, + builder_methods, image, position::Dimension, widget::{self, button}, widget_ids, Color, Position, Positionable, Rect, Sizeable, Ui, Widget, WidgetCommon, @@ -15,6 +15,10 @@ where common: widget::CommonBuilder, widget: W, pos: Vec3, + // Number of pixels per 1 unit in world coordinates (ie a voxel) + // Used for widgets that are rasterized before being sent to the gpu (text & images) + // Potentially make this autmatic based on distance to camera? + res: f32, } // TODO: add convenience function to this trait @@ -37,7 +41,14 @@ widget_ids! { pub struct State { ids: Ids, - pub pos: Vec3, + pos: Vec3, + res: f32, +} +impl State { + // retrieve the postion and resolution as a tuple + pub fn pos_res(&self) -> (Vec3, f32) { + (self.pos, self.res) + } } pub type Style = (); @@ -48,8 +59,12 @@ impl Ingame { common: widget::CommonBuilder::default(), pos, widget, + res: 1.0, } } + builder_methods! { + pub resolution { res = f32 } + } } impl Widget for Ingame { @@ -61,6 +76,7 @@ impl Widget for Ingame { State { ids: Ids::new(id_gen), pos: Vec3::default(), + res: 1.0, } } @@ -70,15 +86,21 @@ impl Widget for Ingame { fn update(self, args: widget::UpdateArgs) -> Self::Event { let widget::UpdateArgs { id, state, ui, .. } = args; - let Ingame { widget, pos, .. } = self; + let Ingame { + widget, pos, res, .. + } = self; // Update pos if it has changed - if state.pos != pos { - state.update(|s| s.pos = pos); + if state.pos != pos || state.res != res { + state.update(|s| { + s.pos = pos; + s.res = res; + }); } widget .graphics_for(ui.window) + .x_y(0.0, 0.0) .parent(id) // is this needed .set(state.ids.prim, ui); }