forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.rs
More file actions
329 lines (297 loc) · 14.2 KB
/
support.rs
File metadata and controls
329 lines (297 loc) · 14.2 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use imgui::{FontConfig, Context, MouseCursor};
use imgui_gfx_renderer::{Renderer, Shaders};
use std::time::Instant;
use crate::{ColorFormat, DepthFormat, EditorScene};
#[derive(Copy, Clone, PartialEq, Debug, Default)]
struct MouseState {
pos: (i32, i32),
pressed: [bool; 5],
wheel: f32,
}
pub fn run(title: String, clear_color: [f32; 4]) -> EditorScene {
use gfx::Device;
let mut events_loop = glutin::EventsLoop::new();
let context = glutin::ContextBuilder::new().with_vsync(true);
let window = glutin::WindowBuilder::new()
.with_title(title)
.with_window_icon(glutin::Icon::from_rgba(include_bytes!("res/gasmask.raw").to_vec(), 16, 16).ok())
.with_min_dimensions(glutin::dpi::LogicalSize::new(640.0, 480.0))
.with_dimensions(glutin::dpi::LogicalSize::new(1300.0, 730.0));
let (window, mut device, mut factory, mut main_color, mut main_depth) =
gfx_window_glutin::init::<ColorFormat, DepthFormat>(window, context, &events_loop)
.expect("failed to initialize glutin window");
// gfx's gl backend sets FRAMEBUFFER_SRGB permanently by default - actually
// we want it off permanently. I've been told this is most sanely set on a
// per-render-pass basis, but neither the world nor imgui blend colors
// accurately if it is set.
unsafe {
device.with_gl(|gl| {
gl.Disable(::gl::FRAMEBUFFER_SRGB);
});
}
let (ww, wh): (f64, f64) = window.window().get_outer_size().unwrap().into();
let (dw, dh): (f64, f64) = window.window().get_primary_monitor().get_dimensions().into();
window.window().set_position(((dw - ww) / 2.0, (dh - wh) / 2.0).into());
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
let shaders = {
let version = device.get_info().shading_language;
if version.is_embedded {
if version.major >= 3 {
Shaders::GlSlEs300
} else {
Shaders::GlSlEs100
}
} else if version.major >= 4 {
Shaders::GlSl400
} else if version.major >= 3 {
Shaders::GlSl130
} else {
Shaders::GlSl110
}
};
let mut imgui = imgui::Context::create();
imgui.style_mut().use_dark_colors();
imgui.set_ini_filename(None);
// In the examples we only use integer DPI factors, because the UI can get very blurry
// otherwise. This might or might not be what you want in a real application.
let mut window_hidpi_factor = window.window().get_hidpi_factor();
let mut hidpi_factor = window_hidpi_factor.round();
let mut logical_size = window.window()
.get_inner_size()
.unwrap()
.to_physical(window_hidpi_factor)
.to_logical(hidpi_factor);
let font_size = (13.0 * hidpi_factor) as f32;
imgui.fonts().add_font(&[
imgui::FontSource::DefaultFontData {
config: Some(FontConfig {
oversample_h: 1,
pixel_snap_h: true,
size_pixels: font_size,
.. Default::default()
})
}
]);
let mut renderer = Renderer::init(&mut imgui, &mut factory, shaders)
.expect("Failed to initialize renderer");
configure_keys(&mut imgui);
let mut scene = EditorScene::new(&mut factory, &main_color, &main_depth);
let mut last_frame = Instant::now();
let mut mouse_state = MouseState::default();
let mut quit = false;
let mut mouse_captured = false;
let mut kbd_captured = false;
loop {
events_loop.poll_events(|event| {
use glutin::ElementState::Pressed;
use glutin::WindowEvent::*;
use glutin::{Event, MouseButton, MouseScrollDelta, TouchPhase};
if let Event::WindowEvent { event, .. } = event {
match event {
CloseRequested => quit = true,
Resized(new_logical_size) => {
gfx_window_glutin::update_views(&window, &mut main_color, &mut main_depth);
window.resize(new_logical_size.to_physical(hidpi_factor));
scene.update_render_target(&main_color, &main_depth);
logical_size = new_logical_size
.to_physical(window_hidpi_factor)
.to_logical(hidpi_factor);
},
HiDpiFactorChanged(new_factor) => {
window_hidpi_factor = new_factor;
hidpi_factor = window_hidpi_factor.round();
logical_size = window.window()
.get_inner_size()
.unwrap()
.to_physical(window_hidpi_factor)
.to_logical(hidpi_factor);
},
Focused(false) => {
// If the window is unfocused, unset modifiers, or
// Alt-Tab will set it permanently & cause trouble. No,
// I don't know why this doesn't just work.
imgui.io_mut().key_ctrl = false;
imgui.io_mut().key_alt = false;
imgui.io_mut().key_shift = false;
imgui.io_mut().key_super = false;
},
KeyboardInput { input, .. } => {
use glutin::VirtualKeyCode as Key;
let pressed = input.state == Pressed;
match input.virtual_keycode {
Some(Key::Tab) => imgui.io_mut().keys_down[0] = pressed,
Some(Key::Left) => imgui.io_mut().keys_down[1] = pressed,
Some(Key::Right) => imgui.io_mut().keys_down[2] = pressed,
Some(Key::Up) => imgui.io_mut().keys_down[3] = pressed,
Some(Key::Down) => imgui.io_mut().keys_down[4] = pressed,
Some(Key::PageUp) => imgui.io_mut().keys_down[5] = pressed,
Some(Key::PageDown) => imgui.io_mut().keys_down[6] = pressed,
Some(Key::Home) => imgui.io_mut().keys_down[7] = pressed,
Some(Key::End) => imgui.io_mut().keys_down[8] = pressed,
Some(Key::Delete) => imgui.io_mut().keys_down[9] = pressed,
Some(Key::Back) => imgui.io_mut().keys_down[10] = pressed,
Some(Key::Return) => imgui.io_mut().keys_down[11] = pressed,
Some(Key::Escape) => imgui.io_mut().keys_down[12] = pressed,
Some(Key::A) => imgui.io_mut().keys_down[13] = pressed,
Some(Key::C) => imgui.io_mut().keys_down[14] = pressed,
Some(Key::V) => imgui.io_mut().keys_down[15] = pressed,
Some(Key::X) => imgui.io_mut().keys_down[16] = pressed,
Some(Key::Y) => imgui.io_mut().keys_down[17] = pressed,
Some(Key::Z) => imgui.io_mut().keys_down[18] = pressed,
Some(Key::LControl) | Some(Key::RControl) => imgui.io_mut().key_ctrl = pressed,
Some(Key::LShift) | Some(Key::RShift) => imgui.io_mut().key_shift = pressed,
Some(Key::LAlt) | Some(Key::RAlt) => imgui.io_mut().key_alt = pressed,
Some(Key::LWin) | Some(Key::RWin) => imgui.io_mut().key_super = pressed,
_ => {}
}
if pressed && !kbd_captured {
if let Some(key) = input.virtual_keycode {
scene.chord(ctrl(&imgui), imgui.io().key_shift, imgui.io().key_alt, key);
}
}
},
CursorMoved { position, .. } => {
// Rescale position from glutin logical coordinates to our logical
// coordinates
let pos = position
.to_physical(window_hidpi_factor)
.to_logical(hidpi_factor)
.into();
mouse_state.pos = pos;
scene.mouse_moved(pos);
},
MouseInput { state, button, .. } => match button {
MouseButton::Left => mouse_state.pressed[0] = state == Pressed,
MouseButton::Right => mouse_state.pressed[1] = state == Pressed,
MouseButton::Middle => mouse_state.pressed[2] = state == Pressed,
MouseButton::Other(i) => if let Some(b) = mouse_state.pressed.get_mut(2 + i as usize) {
*b = state == Pressed;
},
},
MouseWheel {
delta: MouseScrollDelta::LineDelta(x, y),
phase: TouchPhase::Moved,
..
} => {
mouse_state.wheel = y;
if !mouse_captured {
scene.mouse_wheel(ctrl(&imgui), imgui.io().key_shift, imgui.io().key_alt, 4.0 * 32.0 * x, 4.0 * 32.0 * y);
}
},
MouseWheel {
delta: MouseScrollDelta::PixelDelta(pos),
phase: TouchPhase::Moved,
..
} => {
// Rescale pixel delta from glutin logical coordinates to our logical
// coordinates
let diff = pos
.to_physical(window_hidpi_factor)
.to_logical(hidpi_factor);
mouse_state.wheel = diff.y as f32;
if !mouse_captured {
#[cfg(not(target_os = "macos"))]
let diff_x = diff.x as f32;
#[cfg(target_os = "macos")]
let diff_x = -diff.x as f32;
scene.mouse_wheel(
ctrl(&imgui),
imgui.io().key_shift,
imgui.io().key_alt,
diff_x,
diff.y as f32,
);
}
},
ReceivedCharacter(c) => imgui.io_mut().add_input_character(c),
_ => (),
}
}
});
if quit {
break;
}
let now = Instant::now();
let delta = now - last_frame;
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0;
last_frame = now;
update_mouse(&mut imgui, &mut mouse_state);
// Workaround: imgui-gfx-renderer will not call ui.render() under this
// condition, which occurs when minimized, and imgui will assert
// because of missing either a Render() or EndFrame() call.
if logical_size.width > 0.0 && logical_size.height > 0.0 {
imgui.io_mut().display_size = [logical_size.width as f32, logical_size.height as f32];
imgui.io_mut().display_framebuffer_scale = [hidpi_factor as f32, hidpi_factor as f32];
imgui.io_mut().font_global_scale = 1.0 / hidpi_factor as f32;
imgui.io_mut().delta_time = delta_s;
let ui = imgui.frame();
if !scene.run_ui(&ui, &mut renderer) {
break;
}
mouse_captured = ui.io().want_capture_mouse;
kbd_captured = ui.io().want_capture_keyboard;
if let Some(mouse_cursor) = ui.mouse_cursor() {
// Set OS cursor
window.window().hide_cursor(false);
window.window().set_cursor(match mouse_cursor {
MouseCursor::Arrow => glutin::MouseCursor::Arrow,
MouseCursor::TextInput => glutin::MouseCursor::Text,
MouseCursor::ResizeAll => glutin::MouseCursor::Move,
MouseCursor::ResizeNS => glutin::MouseCursor::NsResize,
MouseCursor::ResizeEW => glutin::MouseCursor::EwResize,
MouseCursor::ResizeNESW => glutin::MouseCursor::NeswResize,
MouseCursor::ResizeNWSE => glutin::MouseCursor::NwseResize,
MouseCursor::Hand => glutin::MouseCursor::Hand,
});
} else {
// Hide OS cursor
window.window().hide_cursor(true);
}
encoder.clear(&main_color, clear_color);
scene.render(&mut encoder);
renderer
.render(&mut factory, &mut encoder, &mut main_color, ui.render())
.expect("Rendering failed");
encoder.flush(&mut device);
window.swap_buffers().unwrap();
device.cleanup();
}
}
scene
}
fn configure_keys(imgui: &mut Context) {
use imgui::Key::*;
imgui.io_mut()[Tab] = 0;
imgui.io_mut()[LeftArrow] = 1;
imgui.io_mut()[RightArrow] = 2;
imgui.io_mut()[UpArrow] = 3;
imgui.io_mut()[DownArrow] = 4;
imgui.io_mut()[PageUp] = 5;
imgui.io_mut()[PageDown] = 6;
imgui.io_mut()[Home] = 7;
imgui.io_mut()[End] = 8;
imgui.io_mut()[Delete] = 9;
imgui.io_mut()[Backspace] = 10;
imgui.io_mut()[Enter] = 11;
imgui.io_mut()[Escape] = 12;
imgui.io_mut()[A] = 13;
imgui.io_mut()[C] = 14;
imgui.io_mut()[V] = 15;
imgui.io_mut()[X] = 16;
imgui.io_mut()[Y] = 17;
imgui.io_mut()[Z] = 18;
}
fn update_mouse(imgui: &mut Context, mouse_state: &mut MouseState) {
imgui.io_mut().mouse_pos = [mouse_state.pos.0 as f32, mouse_state.pos.1 as f32];
imgui.io_mut().mouse_down = mouse_state.pressed;
imgui.io_mut().mouse_wheel = mouse_state.wheel;
mouse_state.wheel = 0.0;
}
#[cfg(not(target_os = "macos"))]
fn ctrl(imgui: &Context) -> bool {
imgui.io().key_ctrl
}
#[cfg(target_os = "macos")]
fn ctrl(imgui: &Context) -> bool {
imgui.io().key_super
}