-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
main.rs
204 lines (172 loc) · 7.01 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
mod controls;
mod scene;
use controls::Controls;
use scene::Scene;
use iced_wgpu::{
wgpu, window::SwapChain, Primitive, Renderer, Settings, Target,
};
use iced_winit::{winit, Cache, Clipboard, MouseCursor, Size, UserInterface};
use winit::{
event::{Event, ModifiersState, WindowEvent},
event_loop::{ControlFlow, EventLoop},
};
pub fn main() {
env_logger::init();
// Initialize winit
let event_loop = EventLoop::new();
let window = winit::window::Window::new(&event_loop).unwrap();
let mut logical_size =
window.inner_size().to_logical(window.scale_factor());
let mut modifiers = ModifiersState::default();
// Initialize WGPU
let adapter = wgpu::Adapter::request(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default,
backends: wgpu::BackendBit::PRIMARY,
})
.expect("Request adapter");
let (mut device, mut queue) =
adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
});
let surface = wgpu::Surface::create(&window);
let format = wgpu::TextureFormat::Bgra8UnormSrgb;
let mut swap_chain = {
let size = window.inner_size();
SwapChain::new(&device, &surface, format, size.width, size.height)
};
let mut resized = false;
// Initialize iced
let mut events = Vec::new();
let mut cache = Some(Cache::default());
let mut renderer = Renderer::new(&mut device, Settings::default());
let mut output = (Primitive::None, MouseCursor::OutOfBounds);
let clipboard = Clipboard::new(&window);
// Initialize scene and GUI controls
let mut scene = Scene::new(&device);
let mut controls = Controls::new();
// Run event loop
event_loop.run(move |event, _, control_flow| {
// You should change this if you want to render continuosly
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { event, .. } => {
match event {
WindowEvent::ModifiersChanged(new_modifiers) => {
modifiers = new_modifiers;
}
WindowEvent::Resized(new_size) => {
logical_size =
new_size.to_logical(window.scale_factor());
resized = true;
}
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}
// Map window event to iced event
if let Some(event) = iced_winit::conversion::window_event(
event,
window.scale_factor(),
modifiers,
) {
events.push(event);
}
}
Event::MainEventsCleared => {
// If no relevant events happened, we can simply skip this
if events.is_empty() {
return;
}
// We need to:
// 1. Process events of our user interface.
// 2. Update state as a result of any interaction.
// 3. Generate a new output for our renderer.
// First, we build our user interface.
let mut user_interface = UserInterface::build(
controls.view(&scene),
Size::new(logical_size.width, logical_size.height),
cache.take().unwrap(),
&mut renderer,
);
// Then, we process the events, obtaining messages in return.
let messages = user_interface.update(
events.drain(..),
clipboard.as_ref().map(|c| c as _),
&renderer,
);
let user_interface = if messages.is_empty() {
// If there are no messages, no interactions we care about have
// happened. We can simply leave our user interface as it is.
user_interface
} else {
// If there are messages, we need to update our state
// accordingly and rebuild our user interface.
// We can only do this if we drop our user interface first
// by turning it into its cache.
cache = Some(user_interface.into_cache());
// In this example, `Controls` is the only part that cares
// about messages, so updating our state is pretty
// straightforward.
for message in messages {
controls.update(message, &mut scene);
}
// Once the state has been changed, we rebuild our updated
// user interface.
UserInterface::build(
controls.view(&scene),
Size::new(logical_size.width, logical_size.height),
cache.take().unwrap(),
&mut renderer,
)
};
// Finally, we just need to draw a new output for our renderer,
output = user_interface.draw(&mut renderer);
// update our cache,
cache = Some(user_interface.into_cache());
// and request a redraw
window.request_redraw();
}
Event::RedrawRequested(_) => {
if resized {
let size = window.inner_size();
swap_chain = SwapChain::new(
&device,
&surface,
format,
size.width,
size.height,
);
}
let (frame, viewport) = swap_chain.next_frame();
let mut encoder = device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { todo: 0 },
);
// We draw the scene first
scene.draw(&mut encoder, &frame.view);
// And then iced on top
let mouse_cursor = renderer.draw(
&mut device,
&mut encoder,
Target {
texture: &frame.view,
viewport,
},
&output,
window.scale_factor(),
&["Some debug information!"],
);
// Then we submit the work
queue.submit(&[encoder.finish()]);
// And update the mouse cursor
window.set_cursor_icon(iced_winit::conversion::mouse_cursor(
mouse_cursor,
));
}
_ => {}
}
})
}