Move Target to its own module
This commit is contained in:
parent
58e04af824
commit
ef056d8489
13 changed files with 96 additions and 78 deletions
|
|
@ -4,26 +4,19 @@ use iced_native::{
|
||||||
MouseCursor, Point, Rectangle, Widget,
|
MouseCursor, Point, Rectangle, Widget,
|
||||||
};
|
};
|
||||||
|
|
||||||
use raw_window_handle::HasRawWindowHandle;
|
|
||||||
use wgpu::{
|
use wgpu::{
|
||||||
Adapter, BackendBit, CommandEncoderDescriptor, Device, DeviceDescriptor,
|
Adapter, BackendBit, CommandEncoderDescriptor, Device, DeviceDescriptor,
|
||||||
Extensions, Limits, PowerPreference, Queue, RequestAdapterOptions, Surface,
|
Extensions, Limits, PowerPreference, Queue, RequestAdapterOptions,
|
||||||
SwapChain, SwapChainDescriptor, TextureFormat, TextureUsage,
|
TextureFormat,
|
||||||
};
|
};
|
||||||
use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, Section};
|
use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, Section};
|
||||||
|
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
mod button;
|
mod target;
|
||||||
mod checkbox;
|
mod widget;
|
||||||
mod column;
|
|
||||||
mod image;
|
pub use target::Target;
|
||||||
mod radio;
|
|
||||||
mod row;
|
|
||||||
mod scrollable;
|
|
||||||
mod slider;
|
|
||||||
mod text;
|
|
||||||
mod text_input;
|
|
||||||
|
|
||||||
pub struct Renderer {
|
pub struct Renderer {
|
||||||
device: Device,
|
device: Device,
|
||||||
|
|
@ -34,62 +27,6 @@ pub struct Renderer {
|
||||||
glyph_brush: Rc<RefCell<GlyphBrush<'static, ()>>>,
|
glyph_brush: Rc<RefCell<GlyphBrush<'static, ()>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Target {
|
|
||||||
surface: Surface,
|
|
||||||
width: u16,
|
|
||||||
height: u16,
|
|
||||||
transformation: Transformation,
|
|
||||||
swap_chain: SwapChain,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl iced_native::renderer::Target for Target {
|
|
||||||
type Renderer = Renderer;
|
|
||||||
|
|
||||||
fn new<W: HasRawWindowHandle>(
|
|
||||||
window: &W,
|
|
||||||
width: u16,
|
|
||||||
height: u16,
|
|
||||||
renderer: &Renderer,
|
|
||||||
) -> Target {
|
|
||||||
let surface = Surface::create(window);
|
|
||||||
|
|
||||||
let swap_chain = renderer.device.create_swap_chain(
|
|
||||||
&surface,
|
|
||||||
&SwapChainDescriptor {
|
|
||||||
usage: TextureUsage::OUTPUT_ATTACHMENT,
|
|
||||||
format: TextureFormat::Bgra8UnormSrgb,
|
|
||||||
width: u32::from(width),
|
|
||||||
height: u32::from(height),
|
|
||||||
present_mode: wgpu::PresentMode::Vsync,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
Target {
|
|
||||||
surface,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
transformation: Transformation::orthographic(width, height),
|
|
||||||
swap_chain,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn resize(&mut self, width: u16, height: u16, renderer: &Renderer) {
|
|
||||||
self.width = width;
|
|
||||||
self.height = height;
|
|
||||||
self.transformation = Transformation::orthographic(width, height);
|
|
||||||
self.swap_chain = renderer.device.create_swap_chain(
|
|
||||||
&self.surface,
|
|
||||||
&SwapChainDescriptor {
|
|
||||||
usage: TextureUsage::OUTPUT_ATTACHMENT,
|
|
||||||
format: TextureFormat::Bgra8UnormSrgb,
|
|
||||||
width: u32::from(width),
|
|
||||||
height: u32::from(height),
|
|
||||||
present_mode: wgpu::PresentMode::Vsync,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Layer<'a> {
|
pub struct Layer<'a> {
|
||||||
bounds: Rectangle<u32>,
|
bounds: Rectangle<u32>,
|
||||||
y_offset: u32,
|
y_offset: u32,
|
||||||
|
|
@ -153,7 +90,9 @@ impl Renderer {
|
||||||
) -> MouseCursor {
|
) -> MouseCursor {
|
||||||
log::debug!("Drawing");
|
log::debug!("Drawing");
|
||||||
|
|
||||||
let frame = target.swap_chain.get_next_texture();
|
let (width, height) = target.dimensions();
|
||||||
|
let transformation = target.transformation();
|
||||||
|
let frame = target.next_frame();
|
||||||
|
|
||||||
let mut encoder = self
|
let mut encoder = self
|
||||||
.device
|
.device
|
||||||
|
|
@ -181,8 +120,8 @@ impl Renderer {
|
||||||
Rectangle {
|
Rectangle {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
width: u32::from(target.width),
|
width: u32::from(width),
|
||||||
height: u32::from(target.height),
|
height: u32::from(height),
|
||||||
},
|
},
|
||||||
0,
|
0,
|
||||||
));
|
));
|
||||||
|
|
@ -190,12 +129,7 @@ impl Renderer {
|
||||||
self.draw_primitive(primitive, &mut layers);
|
self.draw_primitive(primitive, &mut layers);
|
||||||
|
|
||||||
for layer in layers {
|
for layer in layers {
|
||||||
self.flush(
|
self.flush(transformation, &layer, &mut encoder, &frame.view);
|
||||||
target.transformation,
|
|
||||||
&layer,
|
|
||||||
&mut encoder,
|
|
||||||
&frame.view,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.queue.submit(&[encoder.finish()]);
|
self.queue.submit(&[encoder.finish()]);
|
||||||
|
|
|
||||||
74
wgpu/src/renderer/target.rs
Normal file
74
wgpu/src/renderer/target.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
use crate::{Renderer, Transformation};
|
||||||
|
|
||||||
|
use raw_window_handle::HasRawWindowHandle;
|
||||||
|
|
||||||
|
pub struct Target {
|
||||||
|
surface: wgpu::Surface,
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
transformation: Transformation,
|
||||||
|
swap_chain: wgpu::SwapChain,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Target {
|
||||||
|
pub fn dimensions(&self) -> (u16, u16) {
|
||||||
|
(self.width, self.height)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn transformation(&self) -> Transformation {
|
||||||
|
self.transformation
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next_frame(&mut self) -> wgpu::SwapChainOutput {
|
||||||
|
self.swap_chain.get_next_texture()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl iced_native::renderer::Target for Target {
|
||||||
|
type Renderer = Renderer;
|
||||||
|
|
||||||
|
fn new<W: HasRawWindowHandle>(
|
||||||
|
window: &W,
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
renderer: &Renderer,
|
||||||
|
) -> Target {
|
||||||
|
let surface = wgpu::Surface::create(window);
|
||||||
|
let swap_chain =
|
||||||
|
new_swap_chain(&surface, width, height, &renderer.device);
|
||||||
|
|
||||||
|
Target {
|
||||||
|
surface,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
transformation: Transformation::orthographic(width, height),
|
||||||
|
swap_chain,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resize(&mut self, width: u16, height: u16, renderer: &Renderer) {
|
||||||
|
self.width = width;
|
||||||
|
self.height = height;
|
||||||
|
self.transformation = Transformation::orthographic(width, height);
|
||||||
|
self.swap_chain =
|
||||||
|
new_swap_chain(&self.surface, width, height, &renderer.device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_swap_chain(
|
||||||
|
surface: &wgpu::Surface,
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
device: &wgpu::Device,
|
||||||
|
) -> wgpu::SwapChain {
|
||||||
|
device.create_swap_chain(
|
||||||
|
&surface,
|
||||||
|
&wgpu::SwapChainDescriptor {
|
||||||
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
||||||
|
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||||
|
width: u32::from(width),
|
||||||
|
height: u32::from(height),
|
||||||
|
present_mode: wgpu::PresentMode::Vsync,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
10
wgpu/src/renderer/widget.rs
Normal file
10
wgpu/src/renderer/widget.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
mod button;
|
||||||
|
mod checkbox;
|
||||||
|
mod column;
|
||||||
|
mod image;
|
||||||
|
mod radio;
|
||||||
|
mod row;
|
||||||
|
mod scrollable;
|
||||||
|
mod slider;
|
||||||
|
mod text;
|
||||||
|
mod text_input;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue