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
extern crate glutin;
use self::glutin::{EventsLoop, Event, WindowEvent, ElementState};
use self::glutin::{GlWindow, GlContext, GlRequest, Api};
use self::glutin::{WindowBuilder, ContextBuilder, dpi::LogicalSize};
use std::error::Error;
use std::fmt::{Display, Formatter, Error as FmtError};
use std::collections::HashSet;
use std::rc::Rc;
mod monitor;
pub use self::monitor::*;
mod config;
pub use self::config::*;
use ::{Point, Size, input::Input};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum WindowError {
UnknownMonitor,
InternalError(String)
}
impl Display for WindowError {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
match self {
&WindowError::UnknownMonitor => write!(f, "Unknown monitor"),
&WindowError::InternalError(ref error) => write!(f, "{}", error)
}
}
}
impl Error for WindowError {}
pub struct Window {
events: EventsLoop,
window: Rc<GlWindow>,
input: Input
}
impl Window {
pub fn new(config: Config) -> Result<Window, WindowError> {
let events = EventsLoop::new();
let mut window = WindowBuilder::new()
.with_title(config.title)
.with_maximized(config.maximized)
.with_resizable(config.resizable)
.with_dimensions(LogicalSize {
width: config.size.width,
height: config.size.height
});
if let Some(size) = config.min_size {
window = window.with_min_dimensions(LogicalSize {
width: size.width,
height: size.height
});
}
if !config.resizable {
window = window.with_max_dimensions(LogicalSize {
width: config.size.width,
height: config.size.height
})
} else if let Some(size) = config.max_size {
window = window.with_max_dimensions(LogicalSize {
width: size.width,
height: size.height
});
}
window = window.with_fullscreen(match config.fullscreen {
Fullscreen::Disabled => None,
Fullscreen::Primary => Some(events.get_primary_monitor()),
Fullscreen::Monitor(name) => {
let mut result = Err(WindowError::UnknownMonitor);
for monitor in events.get_available_monitors() {
if let Some(n) = monitor.get_name() {
if name == n {
result = Ok(monitor);
}
}
}
Some(result?)
}
});
let context = ContextBuilder::new()
.with_gl(GlRequest::Specific(Api::OpenGl, (3, 2)))
.with_vsync(config.vsync)
.with_multisampling(config.msaa);
let window = GlWindow::new(window, context, &events)
.map_err(|error| WindowError::InternalError(ToString::to_string(&error)))?;
unsafe {
window.make_current()
.map_err(|error| WindowError::InternalError(ToString::to_string(&error)))?;
}
let rc = Rc::new(window);
Ok(Window {
events,
window: Rc::clone(&rc),
input: Input {
window: Rc::clone(&rc),
keys: HashSet::new(),
buttons: HashSet::new(),
cursor: Point::default()
}
})
}
pub fn update(&mut self) -> Result<bool, String> {
let input = &mut self.input;
let mut result = true;
self.events.poll_events(|event| {
if let Event::WindowEvent {event, ..} = event {
match event {
WindowEvent::CloseRequested => result = false,
WindowEvent::KeyboardInput {input: event, ..} => {
if let Some(key) = event.virtual_keycode {
match event.state {
ElementState::Pressed => input.keys.insert(key),
ElementState::Released => input.keys.remove(&key)
};
}
},
WindowEvent::MouseInput {button, state, ..} => {
match state {
ElementState::Pressed => input.buttons.insert(button),
ElementState::Released => input.buttons.remove(&button)
};
},
WindowEvent::CursorMoved {position, ..} => {
input.cursor = Point {
x: position.x,
y: position.y
}
}
_ => ()
}
}
});
Ok(result)
}
pub fn get_primary_monitor(&self) -> Monitor {
Monitor {
monitor: self.events.get_primary_monitor()
}
}
pub fn get_all_monitors(&self) -> MonitorIter {
MonitorIter {
iter: self.events.get_available_monitors()
}
}
pub fn get_size(&self) -> Size {
self.window.get_inner_size()
.map_or(Size {
width: 1.0,
height: 1.0
}, |size| Size {
width: size.width,
height: size.height
})
}
pub fn input(&mut self) -> &mut Input {
&mut self.input
}
}