2019-12-30 12:14:26 +01:00
|
|
|
use crate::{
|
2020-02-28 14:38:42 +01:00
|
|
|
quad, text, triangle, Defaults, Primitive, Quad, Settings, Target,
|
|
|
|
|
Transformation,
|
2019-12-30 12:14:26 +01:00
|
|
|
};
|
2020-02-28 14:38:42 +01:00
|
|
|
|
|
|
|
|
#[cfg(any(feature = "image", feature = "svg"))]
|
2020-02-28 14:41:07 +01:00
|
|
|
use crate::image::{self, Image};
|
2020-02-28 14:38:42 +01:00
|
|
|
|
2019-10-08 03:13:41 +02:00
|
|
|
use iced_native::{
|
2020-02-09 03:25:13 +01:00
|
|
|
layout, Background, Color, Layout, MouseCursor, Point, Rectangle, Vector,
|
|
|
|
|
Widget,
|
2019-10-08 03:13:41 +02:00
|
|
|
};
|
2019-10-05 19:22:51 +02:00
|
|
|
|
2019-11-02 20:20:35 +01:00
|
|
|
mod widget;
|
|
|
|
|
|
2019-11-22 22:14:24 +01:00
|
|
|
/// A [`wgpu`] renderer.
|
|
|
|
|
///
|
|
|
|
|
/// [`wgpu`]: https://github.com/gfx-rs/wgpu-rs
|
|
|
|
|
#[derive(Debug)]
|
2019-10-05 19:22:51 +02:00
|
|
|
pub struct Renderer {
|
2019-10-07 03:56:16 +02:00
|
|
|
quad_pipeline: quad::Pipeline,
|
2019-11-13 03:54:36 +01:00
|
|
|
text_pipeline: text::Pipeline,
|
2020-02-28 14:38:42 +01:00
|
|
|
triangle_pipeline: triangle::Pipeline,
|
|
|
|
|
|
|
|
|
|
#[cfg(any(feature = "image", feature = "svg"))]
|
|
|
|
|
image_pipeline: image::Pipeline,
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-22 22:14:24 +01:00
|
|
|
struct Layer<'a> {
|
2019-10-27 02:29:23 +01:00
|
|
|
bounds: Rectangle<u32>,
|
2019-10-25 03:47:34 +02:00
|
|
|
quads: Vec<Quad>,
|
2020-03-07 23:45:54 +01:00
|
|
|
meshes: Vec<(Point, &'a triangle::Mesh2D)>,
|
2019-10-27 03:11:54 +01:00
|
|
|
text: Vec<wgpu_glyph::Section<'a>>,
|
2020-02-28 14:38:42 +01:00
|
|
|
|
|
|
|
|
#[cfg(any(feature = "image", feature = "svg"))]
|
|
|
|
|
images: Vec<Image>,
|
2019-10-25 03:47:34 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-27 03:11:54 +01:00
|
|
|
impl<'a> Layer<'a> {
|
2020-03-07 23:45:54 +01:00
|
|
|
pub fn new(bounds: Rectangle<u32>) -> Self {
|
2019-10-25 03:47:34 +02:00
|
|
|
Self {
|
2019-10-27 02:29:23 +01:00
|
|
|
bounds,
|
2019-10-25 03:47:34 +02:00
|
|
|
quads: Vec::new(),
|
2019-10-27 03:11:54 +01:00
|
|
|
text: Vec::new(),
|
2020-01-02 19:25:00 +01:00
|
|
|
meshes: Vec::new(),
|
2020-02-28 14:38:42 +01:00
|
|
|
|
|
|
|
|
#[cfg(any(feature = "image", feature = "svg"))]
|
|
|
|
|
images: Vec::new(),
|
2019-10-25 03:47:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-05 19:22:51 +02:00
|
|
|
impl Renderer {
|
2020-02-09 03:25:13 +01:00
|
|
|
/// Creates a new [`Renderer`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Renderer`]: struct.Renderer.html
|
2020-04-16 09:06:05 +03:00
|
|
|
pub fn new(device: &wgpu::Device, settings: Settings) -> Self {
|
2020-02-24 20:08:40 +01:00
|
|
|
let text_pipeline =
|
|
|
|
|
text::Pipeline::new(device, settings.format, settings.default_font);
|
|
|
|
|
let quad_pipeline = quad::Pipeline::new(device, settings.format);
|
|
|
|
|
let triangle_pipeline = triangle::Pipeline::new(
|
|
|
|
|
device,
|
|
|
|
|
settings.format,
|
|
|
|
|
settings.antialiasing,
|
|
|
|
|
);
|
2019-10-07 03:56:16 +02:00
|
|
|
|
2020-02-28 14:38:42 +01:00
|
|
|
#[cfg(any(feature = "image", feature = "svg"))]
|
|
|
|
|
let image_pipeline = image::Pipeline::new(device, settings.format);
|
|
|
|
|
|
2019-10-05 19:22:51 +02:00
|
|
|
Self {
|
2019-10-07 03:56:16 +02:00
|
|
|
quad_pipeline,
|
2019-11-11 06:07:31 +01:00
|
|
|
text_pipeline,
|
2020-01-02 19:25:00 +01:00
|
|
|
triangle_pipeline,
|
2020-02-28 14:38:42 +01:00
|
|
|
|
|
|
|
|
#[cfg(any(feature = "image", feature = "svg"))]
|
|
|
|
|
image_pipeline,
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-09 03:25:13 +01:00
|
|
|
/// Draws the provided primitives in the given [`Target`].
|
|
|
|
|
///
|
|
|
|
|
/// The text provided as overlay will be renderer on top of the primitives.
|
|
|
|
|
/// This is useful for rendering debug information.
|
|
|
|
|
///
|
|
|
|
|
/// [`Target`]: struct.Target.html
|
|
|
|
|
pub fn draw<T: AsRef<str>>(
|
2019-10-09 05:36:49 +02:00
|
|
|
&mut self,
|
2020-04-16 09:06:05 +03:00
|
|
|
device: &wgpu::Device,
|
2020-02-09 03:25:13 +01:00
|
|
|
encoder: &mut wgpu::CommandEncoder,
|
|
|
|
|
target: Target<'_>,
|
2019-10-11 23:30:53 +02:00
|
|
|
(primitive, mouse_cursor): &(Primitive, MouseCursor),
|
2020-02-09 03:36:59 +01:00
|
|
|
scale_factor: f64,
|
2019-11-03 04:39:11 +01:00
|
|
|
overlay: &[T],
|
2019-10-09 05:36:49 +02:00
|
|
|
) -> MouseCursor {
|
2019-10-07 04:05:40 +02:00
|
|
|
log::debug!("Drawing");
|
|
|
|
|
|
2020-02-09 03:25:13 +01:00
|
|
|
let (width, height) = target.viewport.dimensions();
|
2020-02-09 03:36:59 +01:00
|
|
|
let scale_factor = scale_factor as f32;
|
2020-02-09 03:25:13 +01:00
|
|
|
let transformation = target.viewport.transformation();
|
2019-10-05 19:22:51 +02:00
|
|
|
|
2019-10-29 02:00:17 +01:00
|
|
|
let mut layers = Vec::new();
|
2019-10-30 03:31:07 +01:00
|
|
|
|
2020-03-07 23:45:54 +01:00
|
|
|
layers.push(Layer::new(Rectangle {
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
2020-03-28 15:25:55 -07:00
|
|
|
width,
|
2020-03-28 15:30:51 -07:00
|
|
|
height,
|
2020-03-07 23:45:54 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
self.draw_primitive(Vector::new(0.0, 0.0), primitive, &mut layers);
|
2019-11-03 04:39:11 +01:00
|
|
|
self.draw_overlay(overlay, &mut layers);
|
2019-10-29 02:00:17 +01:00
|
|
|
|
|
|
|
|
for layer in layers {
|
2020-01-09 03:56:13 +01:00
|
|
|
self.flush(
|
2020-02-09 03:25:13 +01:00
|
|
|
device,
|
2020-01-09 03:56:13 +01:00
|
|
|
scale_factor,
|
|
|
|
|
transformation,
|
|
|
|
|
&layer,
|
2020-02-09 03:25:13 +01:00
|
|
|
encoder,
|
|
|
|
|
target.texture,
|
2020-02-15 10:08:27 +01:00
|
|
|
width,
|
|
|
|
|
height,
|
2020-01-09 03:56:13 +01:00
|
|
|
);
|
2019-10-29 02:00:17 +01:00
|
|
|
}
|
2019-10-23 01:21:23 +02:00
|
|
|
|
2020-02-28 14:38:42 +01:00
|
|
|
#[cfg(any(feature = "image", feature = "svg"))]
|
2019-11-30 02:55:14 +01:00
|
|
|
self.image_pipeline.trim_cache();
|
2019-10-05 19:22:51 +02:00
|
|
|
|
2019-10-11 23:30:53 +02:00
|
|
|
*mouse_cursor
|
2019-10-09 05:36:49 +02:00
|
|
|
}
|
2019-10-11 22:15:39 +02:00
|
|
|
|
2019-10-27 03:11:54 +01:00
|
|
|
fn draw_primitive<'a>(
|
|
|
|
|
&mut self,
|
2020-03-07 23:45:54 +01:00
|
|
|
translation: Vector,
|
2019-10-27 03:11:54 +01:00
|
|
|
primitive: &'a Primitive,
|
2019-10-29 02:00:17 +01:00
|
|
|
layers: &mut Vec<Layer<'a>>,
|
2019-10-27 03:11:54 +01:00
|
|
|
) {
|
2019-10-05 19:22:51 +02:00
|
|
|
match primitive {
|
|
|
|
|
Primitive::None => {}
|
|
|
|
|
Primitive::Group { primitives } => {
|
|
|
|
|
// TODO: Inspect a bit and regroup (?)
|
|
|
|
|
for primitive in primitives {
|
2020-03-07 23:45:54 +01:00
|
|
|
self.draw_primitive(translation, primitive, layers)
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Primitive::Text {
|
|
|
|
|
content,
|
|
|
|
|
bounds,
|
|
|
|
|
size,
|
2019-10-08 03:13:41 +02:00
|
|
|
color,
|
2019-11-13 07:22:21 +01:00
|
|
|
font,
|
2019-10-08 03:13:41 +02:00
|
|
|
horizontal_alignment,
|
|
|
|
|
vertical_alignment,
|
|
|
|
|
} => {
|
2020-03-07 23:45:54 +01:00
|
|
|
let layer = layers.last_mut().unwrap();
|
|
|
|
|
|
2019-11-10 06:05:20 +01:00
|
|
|
layer.text.push(wgpu_glyph::Section {
|
2019-10-08 03:13:41 +02:00
|
|
|
text: &content,
|
2020-04-10 01:34:22 +02:00
|
|
|
screen_position: (
|
|
|
|
|
bounds.x + translation.x,
|
|
|
|
|
bounds.y + translation.y,
|
|
|
|
|
),
|
2019-10-08 03:13:41 +02:00
|
|
|
bounds: (bounds.width, bounds.height),
|
|
|
|
|
scale: wgpu_glyph::Scale { x: *size, y: *size },
|
|
|
|
|
color: color.into_linear(),
|
2019-11-13 07:22:21 +01:00
|
|
|
font_id: self.text_pipeline.find_font(*font),
|
2019-10-08 03:13:41 +02:00
|
|
|
layout: wgpu_glyph::Layout::default()
|
|
|
|
|
.h_align(match horizontal_alignment {
|
2019-11-21 13:47:20 +01:00
|
|
|
iced_native::HorizontalAlignment::Left => {
|
2019-10-08 03:13:41 +02:00
|
|
|
wgpu_glyph::HorizontalAlign::Left
|
|
|
|
|
}
|
2019-11-21 13:47:20 +01:00
|
|
|
iced_native::HorizontalAlignment::Center => {
|
2019-10-08 03:13:41 +02:00
|
|
|
wgpu_glyph::HorizontalAlign::Center
|
|
|
|
|
}
|
2019-11-21 13:47:20 +01:00
|
|
|
iced_native::HorizontalAlignment::Right => {
|
2019-10-08 03:13:41 +02:00
|
|
|
wgpu_glyph::HorizontalAlign::Right
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.v_align(match vertical_alignment {
|
2019-11-21 13:47:20 +01:00
|
|
|
iced_native::VerticalAlignment::Top => {
|
2019-10-08 03:13:41 +02:00
|
|
|
wgpu_glyph::VerticalAlign::Top
|
|
|
|
|
}
|
2019-11-21 13:47:20 +01:00
|
|
|
iced_native::VerticalAlignment::Center => {
|
2019-10-08 03:13:41 +02:00
|
|
|
wgpu_glyph::VerticalAlign::Center
|
|
|
|
|
}
|
2019-11-21 13:47:20 +01:00
|
|
|
iced_native::VerticalAlignment::Bottom => {
|
2019-10-08 03:13:41 +02:00
|
|
|
wgpu_glyph::VerticalAlign::Bottom
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
..Default::default()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
Primitive::Quad {
|
|
|
|
|
bounds,
|
|
|
|
|
background,
|
|
|
|
|
border_radius,
|
2019-12-31 21:35:42 +01:00
|
|
|
border_width,
|
|
|
|
|
border_color,
|
2019-10-08 03:13:41 +02:00
|
|
|
} => {
|
2020-03-07 23:45:54 +01:00
|
|
|
let layer = layers.last_mut().unwrap();
|
|
|
|
|
|
|
|
|
|
// TODO: Move some of these computations to the GPU (?)
|
2019-10-25 03:47:34 +02:00
|
|
|
layer.quads.push(Quad {
|
2019-11-05 03:16:46 +01:00
|
|
|
position: [
|
2020-03-07 23:45:54 +01:00
|
|
|
bounds.x + translation.x,
|
|
|
|
|
bounds.y + translation.y,
|
2019-11-05 03:16:46 +01:00
|
|
|
],
|
2019-10-07 03:56:16 +02:00
|
|
|
scale: [bounds.width, bounds.height],
|
|
|
|
|
color: match background {
|
|
|
|
|
Background::Color(color) => color.into_linear(),
|
|
|
|
|
},
|
2019-11-05 20:40:17 +01:00
|
|
|
border_radius: *border_radius as f32,
|
2019-12-31 21:35:42 +01:00
|
|
|
border_width: *border_width as f32,
|
|
|
|
|
border_color: border_color.into_linear(),
|
2019-10-07 03:56:16 +02:00
|
|
|
});
|
2019-10-06 19:22:25 +02:00
|
|
|
}
|
2020-02-12 08:49:42 +01:00
|
|
|
Primitive::Mesh2D { origin, buffers } => {
|
2020-03-07 23:45:54 +01:00
|
|
|
let layer = layers.last_mut().unwrap();
|
|
|
|
|
|
|
|
|
|
layer.meshes.push((*origin + translation, buffers));
|
2020-01-01 15:44:32 -07:00
|
|
|
}
|
2019-10-29 19:50:34 +01:00
|
|
|
Primitive::Clip {
|
2019-10-25 03:47:34 +02:00
|
|
|
bounds,
|
|
|
|
|
offset,
|
|
|
|
|
content,
|
|
|
|
|
} => {
|
2020-03-07 23:45:54 +01:00
|
|
|
let layer = layers.last_mut().unwrap();
|
|
|
|
|
|
2020-02-22 18:03:49 +01:00
|
|
|
let layer_bounds: Rectangle<f32> = layer.bounds.into();
|
2019-11-06 21:00:24 +01:00
|
|
|
|
2020-02-22 18:03:49 +01:00
|
|
|
let clip = Rectangle {
|
2020-03-07 23:45:54 +01:00
|
|
|
x: bounds.x + translation.x,
|
|
|
|
|
y: bounds.y + translation.y,
|
2020-02-22 18:03:49 +01:00
|
|
|
..*bounds
|
|
|
|
|
};
|
2019-11-06 21:00:24 +01:00
|
|
|
|
2020-02-22 18:03:49 +01:00
|
|
|
// Only draw visible content
|
|
|
|
|
if let Some(clip_bounds) = layer_bounds.intersection(&clip) {
|
2020-03-07 23:45:54 +01:00
|
|
|
let clip_layer = Layer::new(clip_bounds.into());
|
|
|
|
|
let new_layer = Layer::new(layer.bounds);
|
2019-11-06 21:00:24 +01:00
|
|
|
|
|
|
|
|
layers.push(clip_layer);
|
2020-03-07 23:45:54 +01:00
|
|
|
self.draw_primitive(
|
|
|
|
|
translation
|
|
|
|
|
- Vector::new(offset.x as f32, offset.y as f32),
|
|
|
|
|
content,
|
|
|
|
|
layers,
|
|
|
|
|
);
|
2019-11-06 21:00:24 +01:00
|
|
|
layers.push(new_layer);
|
|
|
|
|
}
|
2019-10-25 03:47:34 +02:00
|
|
|
}
|
2020-02-28 14:38:42 +01:00
|
|
|
|
2020-03-07 23:45:54 +01:00
|
|
|
Primitive::Cached { origin, cache } => {
|
|
|
|
|
self.draw_primitive(
|
|
|
|
|
translation + Vector::new(origin.x, origin.y),
|
|
|
|
|
&cache,
|
|
|
|
|
layers,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 14:38:42 +01:00
|
|
|
#[cfg(feature = "image")]
|
|
|
|
|
Primitive::Image { handle, bounds } => {
|
2020-03-07 23:45:54 +01:00
|
|
|
let layer = layers.last_mut().unwrap();
|
|
|
|
|
|
2020-02-28 14:38:42 +01:00
|
|
|
layer.images.push(Image {
|
|
|
|
|
handle: image::Handle::Raster(handle.clone()),
|
2020-03-07 23:45:54 +01:00
|
|
|
position: [
|
|
|
|
|
bounds.x + translation.x,
|
|
|
|
|
bounds.y + translation.y,
|
|
|
|
|
],
|
2020-02-28 14:38:42 +01:00
|
|
|
size: [bounds.width, bounds.height],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
#[cfg(not(feature = "image"))]
|
|
|
|
|
Primitive::Image { .. } => {}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "svg")]
|
|
|
|
|
Primitive::Svg { handle, bounds } => {
|
2020-03-07 23:45:54 +01:00
|
|
|
let layer = layers.last_mut().unwrap();
|
|
|
|
|
|
2020-02-28 14:38:42 +01:00
|
|
|
layer.images.push(Image {
|
|
|
|
|
handle: image::Handle::Vector(handle.clone()),
|
2020-03-07 23:45:54 +01:00
|
|
|
position: [
|
|
|
|
|
bounds.x + translation.x,
|
|
|
|
|
bounds.y + translation.y,
|
|
|
|
|
],
|
2020-02-28 14:38:42 +01:00
|
|
|
size: [bounds.width, bounds.height],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
#[cfg(not(feature = "svg"))]
|
|
|
|
|
Primitive::Svg { .. } => {}
|
2019-10-25 03:47:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 04:39:11 +01:00
|
|
|
fn draw_overlay<'a, T: AsRef<str>>(
|
|
|
|
|
&mut self,
|
|
|
|
|
lines: &'a [T],
|
|
|
|
|
layers: &mut Vec<Layer<'a>>,
|
|
|
|
|
) {
|
|
|
|
|
let first = layers.first().unwrap();
|
2020-03-07 23:45:54 +01:00
|
|
|
let mut overlay = Layer::new(first.bounds);
|
2019-11-03 04:39:11 +01:00
|
|
|
|
2019-11-13 03:54:36 +01:00
|
|
|
let font_id = self.text_pipeline.overlay_font();
|
2019-11-03 04:39:11 +01:00
|
|
|
let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 };
|
|
|
|
|
|
|
|
|
|
for (i, line) in lines.iter().enumerate() {
|
2019-11-10 06:05:20 +01:00
|
|
|
overlay.text.push(wgpu_glyph::Section {
|
2019-11-03 04:39:11 +01:00
|
|
|
text: line.as_ref(),
|
|
|
|
|
screen_position: (11.0, 11.0 + 25.0 * i as f32),
|
|
|
|
|
color: [0.9, 0.9, 0.9, 1.0],
|
|
|
|
|
scale,
|
|
|
|
|
font_id,
|
2019-11-10 06:05:20 +01:00
|
|
|
..wgpu_glyph::Section::default()
|
2019-11-03 04:39:11 +01:00
|
|
|
});
|
|
|
|
|
|
2019-11-10 06:05:20 +01:00
|
|
|
overlay.text.push(wgpu_glyph::Section {
|
2019-11-03 04:39:11 +01:00
|
|
|
text: line.as_ref(),
|
|
|
|
|
screen_position: (10.0, 10.0 + 25.0 * i as f32),
|
|
|
|
|
color: [0.0, 0.0, 0.0, 1.0],
|
|
|
|
|
scale,
|
|
|
|
|
font_id,
|
2019-11-10 06:05:20 +01:00
|
|
|
..wgpu_glyph::Section::default()
|
2019-11-03 04:39:11 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
layers.push(overlay);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 03:47:34 +02:00
|
|
|
fn flush(
|
|
|
|
|
&mut self,
|
2020-04-16 09:06:05 +03:00
|
|
|
device: &wgpu::Device,
|
2020-01-09 03:56:13 +01:00
|
|
|
scale_factor: f32,
|
2019-10-25 03:47:34 +02:00
|
|
|
transformation: Transformation,
|
2019-11-22 22:14:24 +01:00
|
|
|
layer: &Layer<'_>,
|
2019-10-25 03:47:34 +02:00
|
|
|
encoder: &mut wgpu::CommandEncoder,
|
|
|
|
|
target: &wgpu::TextureView,
|
2020-02-15 10:08:27 +01:00
|
|
|
target_width: u32,
|
|
|
|
|
target_height: u32,
|
2019-10-25 03:47:34 +02:00
|
|
|
) {
|
2020-01-09 03:56:13 +01:00
|
|
|
let bounds = layer.bounds * scale_factor;
|
2019-10-25 03:47:34 +02:00
|
|
|
|
2020-03-28 15:25:55 -07:00
|
|
|
if !layer.meshes.is_empty() {
|
2020-03-07 23:45:54 +01:00
|
|
|
let scaled = transformation
|
|
|
|
|
* Transformation::scale(scale_factor, scale_factor);
|
2020-01-02 19:25:00 +01:00
|
|
|
|
|
|
|
|
self.triangle_pipeline.draw(
|
2020-02-09 03:25:13 +01:00
|
|
|
device,
|
2020-01-02 19:25:00 +01:00
|
|
|
encoder,
|
|
|
|
|
target,
|
2020-02-15 10:08:27 +01:00
|
|
|
target_width,
|
|
|
|
|
target_height,
|
2020-03-07 23:45:54 +01:00
|
|
|
scaled,
|
2020-01-02 19:25:00 +01:00
|
|
|
&layer.meshes,
|
|
|
|
|
bounds,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 15:25:55 -07:00
|
|
|
if !layer.quads.is_empty() {
|
2019-10-29 01:21:28 +01:00
|
|
|
self.quad_pipeline.draw(
|
2020-02-09 03:25:13 +01:00
|
|
|
device,
|
2019-10-29 01:21:28 +01:00
|
|
|
encoder,
|
|
|
|
|
&layer.quads,
|
|
|
|
|
transformation,
|
2020-01-09 03:56:13 +01:00
|
|
|
scale_factor,
|
2019-11-05 05:26:20 +01:00
|
|
|
bounds,
|
2019-10-29 01:21:28 +01:00
|
|
|
target,
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-10-25 03:47:34 +02:00
|
|
|
|
2020-02-28 14:38:42 +01:00
|
|
|
#[cfg(any(feature = "image", feature = "svg"))]
|
|
|
|
|
{
|
2020-03-29 15:04:11 +02:00
|
|
|
if !layer.images.is_empty() {
|
2020-03-07 23:45:54 +01:00
|
|
|
let scaled = transformation
|
|
|
|
|
* Transformation::scale(scale_factor, scale_factor);
|
2020-02-28 14:38:42 +01:00
|
|
|
|
|
|
|
|
self.image_pipeline.draw(
|
|
|
|
|
device,
|
|
|
|
|
encoder,
|
|
|
|
|
&layer.images,
|
2020-03-07 23:45:54 +01:00
|
|
|
scaled,
|
2020-02-28 14:38:42 +01:00
|
|
|
bounds,
|
|
|
|
|
target,
|
|
|
|
|
scale_factor,
|
2019-11-05 05:26:20 +01:00
|
|
|
);
|
2020-02-28 14:38:42 +01:00
|
|
|
}
|
2019-12-06 16:47:40 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-28 15:25:55 -07:00
|
|
|
if !layer.text.is_empty() {
|
2019-10-27 03:11:54 +01:00
|
|
|
for text in layer.text.iter() {
|
2019-11-05 05:26:20 +01:00
|
|
|
// Target physical coordinates directly to avoid blurry text
|
2019-11-10 06:05:20 +01:00
|
|
|
let text = wgpu_glyph::Section {
|
2019-11-14 03:00:33 +01:00
|
|
|
// TODO: We `round` here to avoid rerasterizing text when
|
|
|
|
|
// its position changes slightly. This can make text feel a
|
|
|
|
|
// bit "jumpy". We may be able to do better once we improve
|
|
|
|
|
// our text rendering/caching pipeline.
|
2019-11-05 05:26:20 +01:00
|
|
|
screen_position: (
|
2020-01-09 03:56:13 +01:00
|
|
|
(text.screen_position.0 * scale_factor).round(),
|
|
|
|
|
(text.screen_position.1 * scale_factor).round(),
|
2019-11-05 05:26:20 +01:00
|
|
|
),
|
2020-01-09 03:56:13 +01:00
|
|
|
// TODO: Fix precision issues with some scale factors.
|
2019-11-14 03:00:33 +01:00
|
|
|
//
|
|
|
|
|
// The `ceil` here can cause some words to render on the
|
|
|
|
|
// same line when they should not.
|
|
|
|
|
//
|
|
|
|
|
// Ideally, `wgpu_glyph` should be able to compute layout
|
|
|
|
|
// using logical positions, and then apply the proper
|
2020-01-09 03:56:13 +01:00
|
|
|
// scaling when rendering. This would ensure that both
|
|
|
|
|
// measuring and rendering follow the same layout rules.
|
2019-11-14 03:00:33 +01:00
|
|
|
bounds: (
|
2020-01-09 03:56:13 +01:00
|
|
|
(text.bounds.0 * scale_factor).ceil(),
|
|
|
|
|
(text.bounds.1 * scale_factor).ceil(),
|
2019-11-14 03:00:33 +01:00
|
|
|
),
|
2019-11-05 05:26:20 +01:00
|
|
|
scale: wgpu_glyph::Scale {
|
2020-01-09 03:56:13 +01:00
|
|
|
x: text.scale.x * scale_factor,
|
|
|
|
|
y: text.scale.y * scale_factor,
|
2019-11-05 05:26:20 +01:00
|
|
|
},
|
|
|
|
|
..*text
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-11 06:07:31 +01:00
|
|
|
self.text_pipeline.queue(text);
|
2019-10-27 03:11:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-13 03:54:36 +01:00
|
|
|
self.text_pipeline.draw_queued(
|
2020-02-09 03:25:13 +01:00
|
|
|
device,
|
2019-11-13 03:54:36 +01:00
|
|
|
encoder,
|
|
|
|
|
target,
|
|
|
|
|
transformation,
|
|
|
|
|
wgpu_glyph::Region {
|
|
|
|
|
x: bounds.x,
|
|
|
|
|
y: bounds.y,
|
|
|
|
|
width: bounds.width,
|
|
|
|
|
height: bounds.height,
|
|
|
|
|
},
|
|
|
|
|
);
|
2019-10-27 03:11:54 +01:00
|
|
|
}
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl iced_native::Renderer for Renderer {
|
2019-10-11 23:30:53 +02:00
|
|
|
type Output = (Primitive, MouseCursor);
|
2019-12-30 12:14:26 +01:00
|
|
|
type Defaults = Defaults;
|
2019-11-11 06:07:31 +01:00
|
|
|
|
|
|
|
|
fn layout<'a, Message>(
|
|
|
|
|
&mut self,
|
|
|
|
|
element: &iced_native::Element<'a, Message, Self>,
|
2020-01-10 03:10:58 +01:00
|
|
|
limits: &iced_native::layout::Limits,
|
2019-11-11 06:07:31 +01:00
|
|
|
) -> iced_native::layout::Node {
|
2020-01-10 03:10:58 +01:00
|
|
|
let node = element.layout(self, limits);
|
2019-11-11 06:07:31 +01:00
|
|
|
|
2019-11-13 03:54:36 +01:00
|
|
|
self.text_pipeline.clear_measurement_cache();
|
2019-11-11 06:07:31 +01:00
|
|
|
|
|
|
|
|
node
|
|
|
|
|
}
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-10 01:58:55 +01:00
|
|
|
impl layout::Debugger for Renderer {
|
2019-10-05 19:22:51 +02:00
|
|
|
fn explain<Message>(
|
|
|
|
|
&mut self,
|
2019-12-30 12:14:26 +01:00
|
|
|
defaults: &Defaults,
|
2019-10-05 19:22:51 +02:00
|
|
|
widget: &dyn Widget<Message, Self>,
|
2019-11-10 06:05:20 +01:00
|
|
|
layout: Layout<'_>,
|
2019-10-05 19:22:51 +02:00
|
|
|
cursor_position: Point,
|
2019-10-13 18:22:26 +02:00
|
|
|
color: Color,
|
2019-10-11 22:15:39 +02:00
|
|
|
) -> Self::Output {
|
2019-10-13 18:22:26 +02:00
|
|
|
let mut primitives = Vec::new();
|
2019-12-30 12:14:26 +01:00
|
|
|
let (primitive, cursor) =
|
|
|
|
|
widget.draw(self, defaults, layout, cursor_position);
|
2019-10-13 18:22:26 +02:00
|
|
|
|
|
|
|
|
explain_layout(layout, color, &mut primitives);
|
|
|
|
|
primitives.push(primitive);
|
|
|
|
|
|
|
|
|
|
(Primitive::Group { primitives }, cursor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn explain_layout(
|
2019-11-10 06:05:20 +01:00
|
|
|
layout: Layout<'_>,
|
2019-10-13 18:22:26 +02:00
|
|
|
color: Color,
|
|
|
|
|
primitives: &mut Vec<Primitive>,
|
|
|
|
|
) {
|
|
|
|
|
primitives.push(Primitive::Quad {
|
|
|
|
|
bounds: layout.bounds(),
|
2019-12-31 21:35:42 +01:00
|
|
|
background: Background::Color(Color::TRANSPARENT),
|
2019-10-13 18:22:26 +02:00
|
|
|
border_radius: 0,
|
2019-12-31 21:35:42 +01:00
|
|
|
border_width: 1,
|
|
|
|
|
border_color: [0.6, 0.6, 0.6, 0.5].into(),
|
2019-10-13 18:22:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for child in layout.children() {
|
|
|
|
|
explain_layout(child, color, primitives);
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
}
|