Skip to content

Commit

Permalink
Add horizontal offset to Primitive::Clip
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Nov 5, 2019
1 parent ba470a2 commit 470266f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
21 changes: 16 additions & 5 deletions core/src/vector.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
/// A 2D vector.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vector {
pub x: f32,
pub y: f32,
pub struct Vector<T = f32> {
pub x: T,
pub y: T,
}

impl Vector {
impl<T> Vector<T> {
/// Creates a new [`Vector`] with the given components.
///
/// [`Vector`]: struct.Vector.html
pub fn new(x: f32, y: f32) -> Self {
pub fn new(x: T, y: T) -> Self {
Self { x, y }
}
}

impl<T> std::ops::Add for Vector<T>
where
T: std::ops::Add<Output = T>,
{
type Output = Self;

fn add(self, b: Self) -> Self {
Self::new(self.x + b.x, self.y + b.y)
}
}
4 changes: 1 addition & 3 deletions native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,8 @@ mod node;
mod style;
mod user_interface;

pub(crate) use iced_core::Vector;

pub use iced_core::{
Align, Background, Color, Justify, Length, Point, Rectangle,
Align, Background, Color, Justify, Length, Point, Rectangle, Vector,
};

#[doc(no_inline)]
Expand Down
4 changes: 2 additions & 2 deletions wgpu/src/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use iced_native::{text, Background, Color, Rectangle};
use iced_native::{text, Background, Color, Rectangle, Vector};

#[derive(Debug, Clone)]
pub enum Primitive {
Expand All @@ -25,7 +25,7 @@ pub enum Primitive {
},
Clip {
bounds: Rectangle,
offset: u32,
offset: Vector<u32>,
content: Box<Primitive>,
},
}
28 changes: 17 additions & 11 deletions wgpu/src/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{quad, Image, Primitive, Quad, Transformation};
use iced_native::{
renderer::Debugger, renderer::Windowed, Background, Color, Layout,
MouseCursor, Point, Rectangle, Widget,
MouseCursor, Point, Rectangle, Vector, Widget,
};

use raw_window_handle::HasRawWindowHandle;
Expand Down Expand Up @@ -44,17 +44,17 @@ pub struct Target {

pub struct Layer<'a> {
bounds: Rectangle<u32>,
y_offset: u32,
offset: Vector<u32>,
quads: Vec<Quad>,
images: Vec<Image>,
text: Vec<wgpu_glyph::Section<'a>>,
}

impl<'a> Layer<'a> {
pub fn new(bounds: Rectangle<u32>, y_offset: u32) -> Self {
pub fn new(bounds: Rectangle<u32>, offset: Vector<u32>) -> Self {
Self {
bounds,
y_offset,
offset,
quads: Vec::new(),
images: Vec::new(),
text: Vec::new(),
Expand Down Expand Up @@ -157,7 +157,7 @@ impl Renderer {
width: u32::from(target.width),
height: u32::from(target.height),
},
0,
Vector::new(0, 0),
));

self.draw_primitive(primitive, &mut layers);
Expand Down Expand Up @@ -257,7 +257,10 @@ impl Renderer {
border_radius,
} => {
layer.quads.push(Quad {
position: [bounds.x, bounds.y - layer.y_offset as f32],
position: [
bounds.x - layer.offset.x as f32,
bounds.y - layer.offset.y as f32,
],
scale: [bounds.width, bounds.height],
color: match background {
Background::Color(color) => color.into_linear(),
Expand All @@ -279,15 +282,15 @@ impl Renderer {
} => {
let clip_layer = Layer::new(
Rectangle {
x: bounds.x as u32,
y: bounds.y as u32 - layer.y_offset,
x: bounds.x as u32 - layer.offset.x,
y: bounds.y as u32 - layer.offset.y,
width: bounds.width as u32,
height: bounds.height as u32,
},
layer.y_offset + offset,
layer.offset + *offset,
);

let new_layer = Layer::new(layer.bounds, layer.y_offset);
let new_layer = Layer::new(layer.bounds, layer.offset);

layers.push(clip_layer);

Expand All @@ -307,7 +310,10 @@ impl Renderer {
target: &wgpu::TextureView,
) {
let translated = transformation
* Transformation::translate(0.0, -(layer.y_offset as f32));
* Transformation::translate(
-(layer.offset.x as f32),
-(layer.offset.y as f32),
);

if layer.quads.len() > 0 {
self.quad_pipeline.draw(
Expand Down
4 changes: 2 additions & 2 deletions wgpu/src/renderer/scrollable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{Primitive, Renderer};
use iced_native::{
scrollable, Background, Color, Layout, MouseCursor, Point, Rectangle,
Scrollable, Widget,
Scrollable, Vector, Widget,
};

const SCROLLBAR_WIDTH: u16 = 10;
Expand Down Expand Up @@ -58,7 +58,7 @@ impl scrollable::Renderer for Renderer {

let clip = Primitive::Clip {
bounds,
offset,
offset: Vector::new(0, offset),
content: Box::new(content),
};

Expand Down
4 changes: 2 additions & 2 deletions wgpu/src/renderer/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Primitive, Renderer};

use iced_native::{
text::HorizontalAlignment, text::VerticalAlignment, text_input, Background,
Color, MouseCursor, Point, Rectangle, TextInput,
Color, MouseCursor, Point, Rectangle, TextInput, Vector,
};
use std::f32;

Expand Down Expand Up @@ -89,7 +89,7 @@ impl text_input::Renderer for Renderer {

let content = Primitive::Clip {
bounds: text_bounds,
offset: 0,
offset: Vector::new(0, 0),
content: Box::new(if text_input.state.is_focused {
use wgpu_glyph::{GlyphCruncher, Scale, Section};

Expand Down

0 comments on commit 470266f

Please sign in to comment.