radiant_rs/core/
display.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
use prelude::*;
use core::*;
use core::builder::*;
use backends::backend;

/// A target to render to, e.g. a window or full screen.
#[derive(Clone)]
pub struct Display {
    pub(crate) handle: backend::Display,
    pub(crate) frame: Rc<RefCell<Option<backend::Frame>>>,
    pub(crate) input_data: Arc<RwLock<InputData>>,
}

impl Debug for Display {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Display")
    }
}

impl Display {

    /// Returns a [display builder](support/struct.DisplayBuilder.html) for display construction.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use radiant_rs::*;
    /// let display = Display::builder().dimensions((640, 480)).vsync().title("Window!").build().unwrap();
    /// ```
    pub fn builder() -> DisplayBuilder {
        DisplayBuilder::new()
    }

    /// Sets the window title.
    pub fn set_title(self: &Self, title: &str) {
        self.handle.set_title(title);
    }

    /// Makes the previously hidden window visible.
    pub fn show(self: &Self) {
        self.handle.show();
    }

    /// Hides the window.
    pub fn hide(self: &Self) {
        self.handle.hide();
    }

    /// Switches to fullscreen mode on the primary monitor.
    pub fn set_fullscreen(self: &Self) -> Result<()> {
        if let Some(backend_monitor) = backend::MonitorIterator::new(self).next() {
            let monitor = Monitor::new(backend_monitor);
            self.set_fullscreen_on(monitor)
        } else {
            Err(Error::FullscreenError("Failed select monitor device.".to_string()))
        }
    }

    /// Switches to fullscreen mode on the given monitor.
    pub fn set_fullscreen_on(self: &Self, monitor: Monitor) -> Result<()> {
        if !self.handle.set_fullscreen(Some(monitor)) {
            self.handle.set_fullscreen(None);
            Err(Error::FullscreenError("Failed to switch to fullscreen.".to_string()))
        } else {
            Ok(())
        }
    }

    /// Switches to windowed mode.
    pub fn set_windowed(self: &Self) {
        self.handle.set_fullscreen(None);
    }

    /// Prepares a frame for rendering.
    pub fn prepare_frame(self: &Self) {
        if self.frame.borrow().is_some() {
            panic!("Current frame needs to be swapped before a new frame can be prepared.");
        }
        *self.frame.borrow_mut() = Some(self.handle.draw());
    }

    /// Prepares a frame for rendering and clears it.
    pub fn clear_frame(self: &Self, color: Color) {
        self.prepare_frame();
        if let Some(ref mut frame) = self.frame.borrow_mut().as_mut() {
            frame.clear(color);
        } else {
            panic!("Failed to prepare a frame for clear.");
        }
    }

    /// Swaps current drawing frame with visible frame.
    pub fn swap_frame(self: &Self) {
        let frame = mem::replace(&mut *self.frame.borrow_mut(), None);
        if let Some(frame) = frame {
            frame.finish();
        } else {
            panic!("No frame currently prepared, nothing to swap.");
        }
    }

    /// Enables cursor grab mode. While in this mode, the mouse cursor will be hidden and
    /// constrained to the window.
    ///
    /// Grab mode will be temporarily released when the window loses focus and automatically
    /// restored once it regains focus.
    pub fn grab_cursor(self: &Self) {
        let mut input_data = self.input_data.write().unwrap();
        if input_data.has_focus {
            self.handle.set_cursor_state(CursorState::Grab);
        }
        input_data.cursor_grabbed = true;
    }

    /// Hides the mouse cursor while it is inside the window.
    pub fn hide_cursor(self: &Self) {
        self.handle.set_cursor_state(CursorState::Hide);
        self.input_data.write().unwrap().cursor_grabbed = false;
    }

    /// Releases a previously grabbed or hidden cursor and makes it visible again.
    pub fn free_cursor(self: &Self) {
        self.handle.set_cursor_state(CursorState::Normal);
        self.input_data.write().unwrap().cursor_grabbed = false;
    }

    /// Sets the mouse cursor position.
    pub fn set_cursor_position(self: &Self, position: Point2<i32>) {
        self.handle.set_cursor_position(position);
    }

    /// Returns the window dimensions.
    pub fn dimensions(self: &Self) -> Point2<u32> {
        self.handle.framebuffer_dimensions()
    }

    /// Returns a vector of available monitors.
    pub fn monitors(self: &Self) -> Vec<Monitor> {
        let iter = backend::MonitorIterator::new(self);
        let mut result = Vec::<Monitor>::new();
        for monitor in iter {
            result.push(Monitor::new(monitor));
        }
        result
    }

    /// Polls for events like keyboard or mouse input and changes to the window. See
    /// [`Input`](struct.Input.html) for basic keyboard and mouse support.
    pub fn poll_events(self: &Self) -> &Self {
        let mut input_data = self.input_data.write().unwrap();

        // todo: poll_id, check if released/pressed(poll_id) == poll_id
        for key_id in 0..NUM_KEYS {
            match input_data.key[key_id] {
                InputState::Pressed | InputState::Repeat => {
                    input_data.key[key_id] = InputState::Down;
                }
                InputState::Released => {
                    input_data.key[key_id] = InputState::Up;
                }
                _ => { }
            }
        }

        for button_id in 0..NUM_BUTTONS {
            match input_data.button[button_id] {
                InputState::Pressed => {
                    input_data.button[button_id] = InputState::Down;
                }
                InputState::Released => {
                    input_data.button[button_id] = InputState::Up;
                }
                _ => { }
            }
        }

        input_data.mouse_delta = (0, 0);

        self.handle.poll_events(|event| {
            match event {
                Event::KeyboardInput(key_id, down) => {
                    let currently_down = match input_data.key[key_id] {
                        InputState::Down | InputState::Pressed | InputState::Repeat => true,
                        _ => false
                    };
                    if !currently_down && down {
                        input_data.key[key_id] = InputState::Pressed;
                    } else if currently_down && !down {
                        input_data.key[key_id] = InputState::Released;
                    } else if currently_down && down {
                        input_data.key[key_id] = InputState::Repeat;
                    }
                },
                Event::MouseDelta(x, y) => {
                    input_data.mouse_delta = (x, y);
                },
                Event::MousePosition(x, y) => {
                    input_data.mouse = (x, y);
                },
                Event::MouseInput(button_id, down) => {
                    let currently_down = match input_data.button[button_id] {
                        InputState::Down | InputState::Pressed => true,
                        _ => false
                    };
                    if !currently_down && down {
                        input_data.button[button_id] = InputState::Pressed
                    } else if currently_down && !down {
                        input_data.button[button_id] = InputState::Released
                    }
                },
                Event::Focus => {
                    input_data.has_focus = true;
                    // restore grab after focus loss
                    if input_data.cursor_grabbed {
                        self.handle.set_cursor_state(CursorState::Grab);
                    }
                }
                Event::Blur => {
                    input_data.has_focus = false;
                    self.handle.set_cursor_state(CursorState::Normal);
                }
                Event::Close => {
                    input_data.should_close = true;
                }
            }
        });

        input_data.dimensions = self.handle.window_dimensions().into();

        self
    }

    /// Returns true once after the attached window was closed
    pub fn was_closed(self: &Self) -> bool {
        let mut input_data = self.input_data.write().unwrap();
        let result = input_data.should_close;
        input_data.should_close = false;
        result
    }

    /// Creates a new instance from given [`DisplayInfo`](support/struct.DisplayInfo.html).
    pub(crate) fn new(descriptor: DisplayInfo) -> Result<Display> {
        Ok(Display {
            handle: backend::Display::new(descriptor)?,
            frame: Rc::new(RefCell::new(None)),
            input_data: Arc::new(RwLock::new(InputData::new())),
        })
    }

    /// Provides a mutable reference to the backend frame to the given function.
    pub(crate) fn frame<T>(self: &Self, func: T) where T: FnOnce(&mut backend::Frame) {
        let mut frame = self.frame.borrow_mut();
        func(frame.as_mut().expect(NO_FRAME_PREPARED));
    }
}

impl AsRenderTarget for Display {
    fn as_render_target(self: &Self) -> RenderTarget {
        RenderTarget::frame(&self.frame)
    }
}

/// The current state of the mouse cursor.
#[derive(Debug)]
pub enum CursorState {
    Normal,
    Hide,
    Grab,
}

// An input event.
#[derive(Debug, PartialEq)]
pub enum Event {
    KeyboardInput(usize, bool),
    MouseInput(usize, bool),
    MouseDelta(i32, i32),
    MousePosition(i32, i32),
    Focus,
    Blur,
    Close,
}

/// A struct describing a [`Display`](struct.Display.html) to be created.
#[derive(Clone, Debug)]
pub struct DisplayInfo {
    pub width       : u32,
    pub height      : u32,
    pub title       : String,
    pub transparent : bool,
    pub decorations : bool,
    pub monitor     : Option<Monitor>,
    pub vsync       : bool,
    pub visible     : bool,
}

impl Default for DisplayInfo {
    fn default() -> DisplayInfo {
        DisplayInfo {
            width       : 640,
            height      : 480,
            title       : "".to_string(),
            transparent : false,
            decorations : true,
            monitor     : None,
            vsync       : false,
            visible     : true,
        }
   }
}