rx-editor 0.3.0

a modern, extensible pixel editor
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#![allow(dead_code)]
use std::io;

use std::fmt;

#[cfg(not(any(feature = "winit", feature = "glfw")))]
#[path = "dummy.rs"]
mod backend;

#[cfg(feature = "winit")]
#[path = "winit.rs"]
mod backend;

#[cfg(all(feature = "glfw", not(feature = "winit")))]
#[path = "glfw.rs"]
mod backend;

/// Initialize the platform.
pub fn init<T>(
    title: &str,
    w: u32,
    h: u32,
    hints: &[WindowHint],
) -> io::Result<(backend::Window<T>, backend::Events)> {
    backend::init(title, w, h, hints)
}

/// Run the main event loop.
pub fn run<F, T>(win: backend::Window<T>, events: backend::Events, callback: F) -> T
where
    F: 'static + FnMut(&mut backend::Window<T>, WindowEvent) -> ControlFlow<T>,
    T: Default,
{
    backend::run(win, events, callback)
}

#[derive(Debug, PartialEq, Eq)]
pub enum ControlFlow<T> {
    Continue,
    Wait,
    Exit(T),
}

#[derive(Debug, Copy, Clone)]
pub enum WindowHint {
    Resizable(bool),
    Visible(bool),
}

/// Describes an event from a `Window`.
#[derive(Clone, Debug, PartialEq)]
pub enum WindowEvent {
    /// The size of the window has changed. Contains the client area's new dimensions.
    Resized(LogicalSize),

    /// The position of the window has changed. Contains the window's new position.
    Moved(LogicalPosition),

    /// The window was minimized.
    Minimized,

    /// The window was restored after having been minimized.
    Restored,

    /// The window has been requested to close.
    CloseRequested,

    /// The window has been destroyed.
    Destroyed,

    /// The window received a unicode character.
    ReceivedCharacter(char),

    /// The window gained or lost focus.
    Focused(bool),

    /// An event from the keyboard has been received.
    KeyboardInput(KeyboardInput),

    /// The cursor has moved on the window.
    CursorMoved {
        /// Coords in pixels relative to the top-left corner of the window.
        position: LogicalPosition,
    },

    /// The cursor has entered the window.
    CursorEntered,

    /// The cursor has left the window.
    CursorLeft,

    /// A mouse button press has been received.
    MouseInput {
        state: InputState,
        button: MouseButton,
        modifiers: ModifiersState,
    },

    /// The mouse wheel has been used.
    MouseWheel { delta: LogicalDelta },

    /// The OS or application has requested that the window be redrawn.
    RedrawRequested,

    /// There are no more inputs to process, the application can do work.
    Ready,

    /// The DPI factor of the window has changed.
    HiDpiFactorChanged(f64),

    /// No-op event, for events we don't handle.
    Noop,
}

impl WindowEvent {
    /// Events that are triggered by user input.
    pub fn is_input(&self) -> bool {
        match self {
            Self::Resized(_)
            | Self::Moved(_)
            | Self::Minimized
            | Self::Restored
            | Self::CloseRequested
            | Self::Destroyed
            | Self::ReceivedCharacter(_)
            | Self::Focused(_)
            | Self::KeyboardInput(_)
            | Self::CursorMoved { .. }
            | Self::CursorEntered
            | Self::CursorLeft
            | Self::MouseInput { .. }
            | Self::HiDpiFactorChanged(_) => true,
            _ => false,
        }
    }
}

/// Describes a keyboard input event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyboardInput {
    pub state: InputState,
    pub key: Option<Key>,
    pub modifiers: ModifiersState,
}

/// Describes the input state of a key.
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum InputState {
    Pressed,
    Released,
    Repeated,
}

/// Describes a mouse button.
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum MouseButton {
    Left,
    Right,
    Middle,
    Other(u8),
}

/// Symbolic name for a keyboard key.
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
#[repr(u32)]
#[rustfmt::skip]
pub enum Key {
    // Number keys.
    Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, Num0,

    // Alpha keys.
    A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,

    // Arrow keys.
    Left, Up, Right, Down,

    // Control characters.
    Backspace, Return, Space, Tab,
    Escape, Insert, Home, Delete, End, PageDown, PageUp,

    // Punctuation.
    Apostrophe, Grave, Caret, Comma, Period, Colon, Semicolon,
    LBracket, RBracket,
    Slash, Backslash,

    // Modifiers.
    Alt, Control, Shift,

    // Math keys.
    Equal, Minus,

    // Key is unknown/unsupported.
    Unknown,
}

impl From<char> for Key {
    #[rustfmt::skip]
    fn from(c: char) -> Self {
        match c {
            '0' => Key::Num0, '1' => Key::Num1, '2' => Key::Num2,
            '3' => Key::Num3, '4' => Key::Num4, '5' => Key::Num5,
            '6' => Key::Num6, '7' => Key::Num7, '8' => Key::Num8,
            '9' => Key::Num9,

            'a' => Key::A, 'b' => Key::B, 'c' => Key::C, 'd' => Key::D,
            'e' => Key::E, 'f' => Key::F, 'g' => Key::G, 'h' => Key::H,
            'i' => Key::I, 'j' => Key::J, 'k' => Key::K, 'l' => Key::L,
            'm' => Key::M, 'n' => Key::N, 'o' => Key::O, 'p' => Key::P,
            'q' => Key::Q, 'r' => Key::R, 's' => Key::S, 't' => Key::T,
            'u' => Key::U, 'v' => Key::V, 'w' => Key::W, 'x' => Key::X,
            'y' => Key::Y, 'z' => Key::Z,

            '/' => Key::Slash, '[' => Key::LBracket, ']' => Key::RBracket,
            '`' => Key::Grave, ',' => Key::Comma, '.' => Key::Period,
            '=' => Key::Equal, '-' => Key::Minus, '\'' => Key::Apostrophe,
            ';' => Key::Semicolon, ':' => Key::Colon, ' ' => Key::Space,
            '\\' => Key::Backslash,
            _ => Key::Unknown,
        }
    }
}

impl fmt::Display for Key {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Key::A => "a".fmt(f),
            Key::B => "b".fmt(f),
            Key::C => "c".fmt(f),
            Key::D => "d".fmt(f),
            Key::E => "e".fmt(f),
            Key::F => "f".fmt(f),
            Key::G => "g".fmt(f),
            Key::H => "h".fmt(f),
            Key::I => "i".fmt(f),
            Key::J => "j".fmt(f),
            Key::K => "k".fmt(f),
            Key::L => "l".fmt(f),
            Key::M => "m".fmt(f),
            Key::N => "n".fmt(f),
            Key::O => "o".fmt(f),
            Key::P => "p".fmt(f),
            Key::Q => "q".fmt(f),
            Key::R => "r".fmt(f),
            Key::S => "s".fmt(f),
            Key::T => "t".fmt(f),
            Key::U => "u".fmt(f),
            Key::V => "v".fmt(f),
            Key::W => "w".fmt(f),
            Key::X => "x".fmt(f),
            Key::Y => "y".fmt(f),
            Key::Z => "z".fmt(f),
            Key::Num0 => "0".fmt(f),
            Key::Num1 => "1".fmt(f),
            Key::Num2 => "2".fmt(f),
            Key::Num3 => "3".fmt(f),
            Key::Num4 => "4".fmt(f),
            Key::Num5 => "5".fmt(f),
            Key::Num6 => "6".fmt(f),
            Key::Num7 => "7".fmt(f),
            Key::Num8 => "8".fmt(f),
            Key::Num9 => "9".fmt(f),
            Key::LBracket => "[".fmt(f),
            Key::RBracket => "]".fmt(f),
            Key::Comma => ",".fmt(f),
            Key::Period => ".".fmt(f),
            Key::Slash => "/".fmt(f),
            Key::Backslash => "\\".fmt(f),
            Key::Apostrophe => "'".fmt(f),
            Key::Control => "<ctrl>".fmt(f),
            Key::Shift => "<shift>".fmt(f),
            Key::Alt => "<alt>".fmt(f),
            Key::Up => "<up>".fmt(f),
            Key::Down => "<down>".fmt(f),
            Key::Left => "<left>".fmt(f),
            Key::Right => "<right>".fmt(f),
            Key::Return => "<return>".fmt(f),
            Key::Backspace => "<backspace>".fmt(f),
            Key::Space => "<space>".fmt(f),
            Key::Tab => "<tab>".fmt(f),
            Key::Escape => "<esc>".fmt(f),
            Key::Insert => "<insert>".fmt(f),
            Key::Delete => "<delete>".fmt(f),
            Key::Home => "<home>".fmt(f),
            Key::PageUp => "<pgup>".fmt(f),
            Key::PageDown => "<pgdown>".fmt(f),
            Key::Grave => "`".fmt(f),
            Key::Caret => "^".fmt(f),
            Key::End => "<end>".fmt(f),
            Key::Colon => ":".fmt(f),
            Key::Semicolon => ";".fmt(f),
            Key::Equal => "=".fmt(f),
            Key::Minus => "-".fmt(f),
            _ => "???".fmt(f),
        }
    }
}

/// Represents the current state of the keyboard modifiers
#[derive(Default, Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub struct ModifiersState {
    /// The "shift" key
    pub shift: bool,
    /// The "control" key
    pub ctrl: bool,
    /// The "alt" key
    pub alt: bool,
    /// The "meta" key. This is the "windows" key on PC and "command" key on Mac.
    pub meta: bool,
}

impl fmt::Display for ModifiersState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut s = String::new();
        if self.ctrl {
            s.push_str("<ctrl>");
        }
        if self.alt {
            s.push_str("<alt>");
        }
        if self.meta {
            s.push_str("<meta>");
        }
        if self.shift {
            s.push_str("<shift>");
        }
        s.fmt(f)
    }
}

/// A delta represented in logical pixels.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct LogicalDelta {
    pub x: f64,
    pub y: f64,
}

/// A position represented in logical pixels.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct LogicalPosition {
    pub x: f64,
    pub y: f64,
}

impl LogicalPosition {
    pub fn new(x: f64, y: f64) -> Self {
        LogicalPosition { x, y }
    }

    pub fn from_physical<T: Into<PhysicalPosition>>(physical: T, dpi_factor: f64) -> Self {
        physical.into().to_logical(dpi_factor)
    }

    pub fn to_physical(&self, dpi_factor: f64) -> PhysicalPosition {
        let x = self.x * dpi_factor;
        let y = self.y * dpi_factor;
        PhysicalPosition::new(x, y)
    }
}

/// A position represented in physical pixels.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct PhysicalPosition {
    pub x: f64,
    pub y: f64,
}

impl PhysicalPosition {
    pub fn new(x: f64, y: f64) -> Self {
        PhysicalPosition { x, y }
    }

    pub fn from_logical<T: Into<LogicalPosition>>(logical: T, dpi_factor: f64) -> Self {
        logical.into().to_physical(dpi_factor)
    }

    pub fn to_logical(&self, dpi_factor: f64) -> LogicalPosition {
        let x = self.x / dpi_factor;
        let y = self.y / dpi_factor;
        LogicalPosition::new(x, y)
    }
}

/// A size represented in logical pixels.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct LogicalSize {
    pub width: f64,
    pub height: f64,
}

impl LogicalSize {
    pub const fn new(width: f64, height: f64) -> Self {
        LogicalSize { width, height }
    }

    pub fn from_physical<T: Into<PhysicalSize>>(physical: T, dpi_factor: f64) -> Self {
        physical.into().to_logical(dpi_factor)
    }

    pub fn to_physical(&self, dpi_factor: f64) -> PhysicalSize {
        let width = self.width * dpi_factor;
        let height = self.height * dpi_factor;
        PhysicalSize::new(width, height)
    }

    pub fn is_zero(&self) -> bool {
        self.width < 1. || self.height < 1.
    }
}

impl From<(u32, u32)> for LogicalSize {
    fn from((width, height): (u32, u32)) -> Self {
        Self::new(width as f64, height as f64)
    }
}

impl Into<(u32, u32)> for LogicalSize {
    /// Note that this rounds instead of truncating.
    fn into(self) -> (u32, u32) {
        (self.width.round() as _, self.height.round() as _)
    }
}

/// A size represented in physical pixels.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct PhysicalSize {
    pub width: f64,
    pub height: f64,
}

impl PhysicalSize {
    pub fn new(width: f64, height: f64) -> Self {
        PhysicalSize { width, height }
    }

    pub fn from_logical<T: Into<LogicalSize>>(logical: T, dpi_factor: f64) -> Self {
        logical.into().to_physical(dpi_factor)
    }

    pub fn to_logical(&self, dpi_factor: f64) -> LogicalSize {
        let width = self.width / dpi_factor;
        let height = self.height / dpi_factor;
        LogicalSize::new(width, height)
    }
}

impl From<(u32, u32)> for PhysicalSize {
    fn from((width, height): (u32, u32)) -> Self {
        Self::new(width as f64, height as f64)
    }
}

impl Into<(u32, u32)> for PhysicalSize {
    /// Note that this rounds instead of truncating.
    fn into(self) -> (u32, u32) {
        (self.width.round() as _, self.height.round() as _)
    }
}