-
Notifications
You must be signed in to change notification settings - Fork 108
/
lib.rs
342 lines (305 loc) · 10.6 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
#![deny(clippy::all)]
#![allow(
clippy::collapsible_if,
clippy::many_single_char_names,
clippy::expect_fun_call,
clippy::useless_format,
clippy::new_without_default,
clippy::cognitive_complexity,
clippy::comparison_chain,
clippy::type_complexity,
clippy::or_fun_call,
clippy::nonminimal_bool,
clippy::single_match,
clippy::large_enum_variant
)]
pub mod data;
pub mod execution;
pub mod gfx;
pub mod logger;
pub mod session;
mod alloc;
mod autocomplete;
mod brush;
mod cmd;
mod color;
mod draw;
mod event;
mod flood;
mod font;
mod gl;
mod history;
mod image;
mod io;
mod palette;
mod parser;
mod pixels;
mod platform;
mod renderer;
mod sprite;
mod timer;
mod view;
#[macro_use]
pub mod util;
use cmd::Value;
use event::Event;
use execution::{DigestMode, Execution, ExecutionMode};
use platform::{WindowEvent, WindowHint};
use renderer::Renderer;
use session::*;
use timer::FrameTimer;
use view::FileStatus;
#[macro_use]
extern crate log;
use directories as dirs;
use std::alloc::System;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
/// Program version.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
#[global_allocator]
pub static ALLOCATOR: alloc::Allocator = alloc::Allocator::new(System);
#[derive(Debug)]
pub struct Options<'a> {
pub width: u32,
pub height: u32,
pub resizable: bool,
pub headless: bool,
pub source: Option<PathBuf>,
pub exec: ExecutionMode,
pub glyphs: &'a [u8],
pub debug: bool,
}
impl<'a> Default for Options<'a> {
fn default() -> Self {
Self {
width: 1280,
height: 720,
headless: false,
resizable: true,
source: None,
exec: ExecutionMode::Normal,
glyphs: data::GLYPHS,
debug: false,
}
}
}
pub fn init<P: AsRef<Path>>(paths: &[P], options: Options<'_>) -> std::io::Result<()> {
use std::io;
debug!("options: {:?}", options);
let hints = &[
WindowHint::Resizable(options.resizable),
WindowHint::Visible(!options.headless),
];
let (mut win, mut events) = platform::init(
"rx",
options.width,
options.height,
hints,
platform::GraphicsContext::Gl,
)?;
let scale_factor = win.scale_factor();
let win_size = win.size();
let (win_w, win_h) = (win_size.width as u32, win_size.height as u32);
info!("framebuffer size: {}x{}", win_size.width, win_size.height);
info!("scale factor: {}", scale_factor);
let assets = data::Assets::new(options.glyphs);
let proj_dirs = dirs::ProjectDirs::from("io", "cloudhead", "rx")
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "config directory not found"))?;
let base_dirs = dirs::BaseDirs::new()
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "home directory not found"))?;
let cwd = std::env::current_dir()?;
let mut session = Session::new(win_w, win_h, cwd, proj_dirs, base_dirs)
.with_blank(
FileStatus::NoFile,
Session::DEFAULT_VIEW_W,
Session::DEFAULT_VIEW_H,
)
.init(options.source.clone())?;
if options.debug {
session
.settings
.set("debug", Value::Bool(true))
.expect("'debug' is a bool'");
}
let mut execution = match options.exec {
ExecutionMode::Normal => Execution::normal(),
ExecutionMode::Replay(path, digest) => Execution::replaying(path, digest),
ExecutionMode::Record(path, digest, gif) => {
Execution::recording(path, digest, win_w as u16, win_h as u16, gif)
}
}?;
// When working with digests, certain settings need to be overwritten
// to ensure things work correctly.
match &execution {
Execution::Replaying { digest, .. } | Execution::Recording { digest, .. }
if digest.mode != DigestMode::Ignore =>
{
session
.settings
.set("animation", Value::Bool(false))
.expect("'animation' is a bool");
}
_ => {}
}
let wait_events = execution.is_normal() || execution.is_recording();
let mut renderer: gl::Renderer = Renderer::new(&mut win, win_size, scale_factor, assets)?;
if let Err(e) = session.edit(paths) {
session.message(format!("Error loading path(s): {}", e), MessageType::Error);
}
// Make sure our session ticks once before anything is rendered.
let effects = session.update(
&mut vec![],
&mut execution,
Duration::default(),
Duration::default(),
);
renderer.init(effects, &session);
let mut render_timer = FrameTimer::new();
let mut update_timer = FrameTimer::new();
let mut session_events = Vec::with_capacity(16);
let mut last = Instant::now();
let mut resized = false;
let mut hovering = false;
let mut delta;
while !win.is_closing() {
match session.animation_delay() {
Some(delay) if session.is_running() => {
// How much time is left until the next animation frame?
let remaining = delay - session.accumulator;
// If more than 1ms remains, let's wait.
if remaining.as_millis() > 1 {
events.wait_timeout(remaining);
} else {
events.poll();
}
}
_ if wait_events => events.wait(),
_ => events.poll(),
}
for event in events.flush() {
if event.is_input() {
debug!("event: {:?}", event);
}
match event {
WindowEvent::Resized(size) => {
if size.is_zero() {
// On certain operating systems, the window size will be set to
// zero when the window is minimized. Since a zero-sized framebuffer
// is not valid, we pause the session until the window is restored.
session.transition(State::Paused);
} else {
resized = true;
session.transition(State::Running);
}
}
WindowEvent::CursorEntered { .. } => {
if win.is_focused() {
win.set_cursor_visible(false);
}
hovering = true;
}
WindowEvent::CursorLeft { .. } => {
win.set_cursor_visible(true);
hovering = false;
}
WindowEvent::Minimized => {
session.transition(State::Paused);
}
WindowEvent::Restored => {
if win.is_focused() {
session.transition(State::Running);
}
}
WindowEvent::Focused(true) => {
session.transition(State::Running);
if hovering {
win.set_cursor_visible(false);
}
}
WindowEvent::Focused(false) => {
win.set_cursor_visible(true);
session.transition(State::Paused);
}
WindowEvent::RedrawRequested => {
render_timer.run(|avg| {
renderer
.frame(&mut session, &mut execution, vec![], &avg)
.unwrap_or_else(|err| {
log::error!("{}", err);
});
});
win.present();
}
WindowEvent::ScaleFactorChanged(factor) => {
renderer.handle_scale_factor_changed(factor);
}
WindowEvent::CloseRequested => {
session.quit(ExitReason::Normal);
}
WindowEvent::CursorMoved { position } => {
session_events.push(Event::CursorMoved(position));
}
WindowEvent::MouseInput { state, button, .. } => {
session_events.push(Event::MouseInput(button, state));
}
WindowEvent::MouseWheel { delta, .. } => {
session_events.push(Event::MouseWheel(delta));
}
WindowEvent::KeyboardInput(input) => match input {
// Intercept `<insert>` key for pasting.
//
// Reading from the clipboard causes the loop to wake up for some strange
// reason I cannot comprehend. So we only read from clipboard when we
// need to paste.
platform::KeyboardInput {
key: Some(platform::Key::Insert),
state: platform::InputState::Pressed,
modifiers: platform::ModifiersState { shift: true, .. },
} => {
session_events.push(Event::Paste(win.clipboard()));
}
_ => session_events.push(Event::KeyboardInput(input)),
},
WindowEvent::ReceivedCharacter(c, mods) => {
session_events.push(Event::ReceivedCharacter(c, mods));
}
_ => {}
};
}
if resized {
// Instead of responded to each resize event by creating a new framebuffer,
// we respond to the event *once*, here.
resized = false;
session.handle_resized(win.size());
}
delta = last.elapsed();
last += delta;
// If we're paused, we want to keep the timer running to not get a
// "jump" when we unpause, but skip session updates and rendering.
if session.state == State::Paused {
continue;
}
let effects =
update_timer.run(|avg| session.update(&mut session_events, &mut execution, delta, avg));
render_timer.run(|avg| {
renderer
.frame(&mut session, &mut execution, effects, &avg)
.unwrap_or_else(|err| {
log::error!("{}", err);
});
});
session.cleanup();
win.present();
match session.state {
State::Closing(ExitReason::Normal) => {
return Ok(());
}
State::Closing(ExitReason::Error(e)) => {
return Err(io::Error::new(io::ErrorKind::Other, e));
}
_ => {}
}
}
Ok(())
}