-
Notifications
You must be signed in to change notification settings - Fork 22
/
lib.rs
176 lines (153 loc) · 5.4 KB
/
lib.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
use winit::event_loop::{EventLoop, EventLoopBuilder, EventLoopWindowTarget};
#[cfg(target_os = "android")]
use winit::platform::android::activity::AndroidApp;
use winit::event_loop::ControlFlow;
use egui_wgpu::winit::Painter;
use egui_winit::State;
//use egui_winit_platform::{Platform, PlatformDescriptor};
use winit::event::Event::*;
const INITIAL_WIDTH: u32 = 1920;
const INITIAL_HEIGHT: u32 = 1080;
/// A custom event type for the winit app.
enum Event {
RequestRedraw,
}
/// Enable egui to request redraws via a custom Winit event...
#[derive(Clone)]
struct RepaintSignal(std::sync::Arc<std::sync::Mutex<winit::event_loop::EventLoopProxy<Event>>>);
fn create_window<T>(
event_loop: &EventLoopWindowTarget<T>,
state: &mut State,
painter: &mut Painter,
) -> winit::window::Window {
let window = winit::window::WindowBuilder::new()
.with_decorations(true)
.with_resizable(true)
.with_transparent(false)
.with_title("egui winit + wgpu example")
.with_inner_size(winit::dpi::PhysicalSize {
width: INITIAL_WIDTH,
height: INITIAL_HEIGHT,
})
.build(event_loop)
.unwrap();
unsafe { painter.set_window(Some(&window)) };
// NB: calling set_window will lazily initialize render state which
// means we will be able to query the maximum supported texture
// dimensions
if let Some(max_size) = painter.max_texture_side() {
state.set_max_texture_side(max_size);
}
let pixels_per_point = window.scale_factor() as f32;
state.set_pixels_per_point(pixels_per_point);
window.request_redraw();
window
}
fn _main(event_loop: EventLoop<Event>) {
let ctx = egui::Context::default();
let repaint_signal = RepaintSignal(std::sync::Arc::new(std::sync::Mutex::new(
event_loop.create_proxy(),
)));
ctx.set_request_repaint_callback(move || {
repaint_signal
.0
.lock()
.unwrap()
.send_event(Event::RequestRedraw)
.ok();
});
let mut state = State::new(&event_loop);
let mut painter = Painter::new(
egui_wgpu::WgpuConfiguration {
backends: wgpu::Backends::all(),
power_preference: wgpu::PowerPreference::LowPower,
device_descriptor: wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::default(),
limits: wgpu::Limits::default(),
},
present_mode: wgpu::PresentMode::Fifo,
..Default::default()
},
1, // msaa samples
8, // depth bits
);
let mut window: Option<winit::window::Window> = None;
let mut egui_demo_windows = egui_demo_lib::DemoWindows::default();
event_loop.run(move |event, event_loop, control_flow| match event {
Resumed => match window {
None => {
window = Some(create_window(event_loop, &mut state, &mut painter));
}
Some(ref window) => {
unsafe { painter.set_window(Some(window)) };
window.request_redraw();
}
},
Suspended => {
window = None;
}
RedrawRequested(..) => {
if let Some(window) = window.as_ref() {
let raw_input = state.take_egui_input(window);
let full_output = ctx.run(raw_input, |ctx| {
egui_demo_windows.ui(ctx);
});
state.handle_platform_output(window, &ctx, full_output.platform_output);
painter.paint_and_update_textures(
state.pixels_per_point(),
egui::Rgba::default(),
&ctx.tessellate(full_output.shapes),
&full_output.textures_delta,
);
if full_output.repaint_after.is_zero() {
window.request_redraw();
}
}
}
MainEventsCleared | UserEvent(Event::RequestRedraw) => {
if let Some(window) = window.as_ref() {
window.request_redraw();
}
}
WindowEvent { event, .. } => {
match event {
winit::event::WindowEvent::Resized(size) => {
painter.on_window_resized(size.width, size.height);
}
winit::event::WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}
let response = state.on_event(&ctx, &event);
if response.repaint {
if let Some(window) = window.as_ref() {
window.request_redraw();
}
}
}
_ => (),
});
}
#[allow(dead_code)]
#[cfg(target_os = "android")]
#[no_mangle]
fn android_main(app: AndroidApp) {
use winit::platform::android::EventLoopBuilderExtAndroid;
android_logger::init_once(android_logger::Config::default().with_min_level(log::Level::Trace));
let event_loop = EventLoopBuilder::with_user_event()
.with_android_app(app)
.build();
_main(event_loop);
}
#[allow(dead_code)]
#[cfg(not(target_os = "android"))]
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Warn) // Default Log Level
.parse_default_env()
.init();
let event_loop = EventLoopBuilder::with_user_event().build();
_main(event_loop);
}