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
|
use crate::*;
use buffer::{Dimensions, HasDimensions};
use event_queue::EventQueue;
use winit::dpi::PhysicalSize;
use winit::event_loop::ActiveEventLoop;
use winit::window::{CursorIcon, Fullscreen, Window};
use std::rc::Rc;
use std::cmp::{min, max};
fn to_physical_size(dimensions: Dimensions, scale: u32) -> PhysicalSize<u32> {
PhysicalSize {
width: max(1, dimensions.width * scale),
height: max(1, dimensions.height * scale),
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum PointerState {
In,
Out,
PendingOut,
}
pub(crate) struct PhosphorWindow {
pub winit: Rc<Window>,
pub program: Box<dyn WindowProgram>,
pub requests: EventQueue<Request>,
pub pointer: Option<Position>,
pub pointer_state: PointerState,
pub mouse_buttons: [bool; 5],
pub marked_for_destruction: bool,
pub size_bounds: SizeBounds, // logical dimensions
scale: u32,
buffer: buffer::Buffer,
surface: softbuffer::Surface<Rc<Window>, Rc<Window>>,
surface_dimensions: Dimensions,
redraw_full: bool,
}
impl PhosphorWindow {
pub fn from_builder(builder: WindowBuilder, event_loop: &ActiveEventLoop) -> Self {
let mut attr = Window::default_attributes();
let scale = builder.scale;
let size_bounds = builder.size_bounds.unwrap_or(SizeBounds::default());
if let Some(size_bounds) = builder.size_bounds {
let (min_size, max_size) = size_bounds.as_min_max_size(scale);
attr = attr.with_min_inner_size(min_size);
attr = attr.with_max_inner_size(max_size);
}
if let Some(dimensions) = builder.dimensions {
attr = attr.with_inner_size(to_physical_size(dimensions, scale));
}
match builder.fullscreen {
true => attr = attr.with_fullscreen(Some(Fullscreen::Borderless(None))),
false => attr = attr.with_fullscreen(None),
};
let increment = PhysicalSize { width: scale, height: scale };
attr = attr.with_resize_increments(increment);
attr = attr.with_title(match builder.title {
Some(title) => title,
None => String::from("Untitled"),
});
let window = Rc::new(event_loop.create_window(attr).unwrap());
let context = softbuffer::Context::new(window.clone()).unwrap();
let surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
if let Some(cursor) = builder.cursor {
window.set_cursor(cursor);
window.set_cursor_visible(true);
} else {
window.set_cursor_visible(false);
}
window.set_window_icon(builder.icon);
Self {
winit: window,
program: builder.program,
requests: EventQueue::new(),
pointer: None,
pointer_state: PointerState::Out,
mouse_buttons: [false; 5],
marked_for_destruction: false,
size_bounds,
scale,
buffer: buffer::Buffer::new(Dimensions::ZERO),
surface,
surface_dimensions: Dimensions::ZERO,
redraw_full: true,
}
}
pub fn handle_requests(&mut self, builders: &mut Vec<WindowBuilder>) {
for request in self.requests.drain() {
match request {
Request::CreateWindow(builder) => builders.push(builder),
Request::SetTitle(title) => self.winit.set_title(&title),
Request::SetSize(dimensions) => self.set_size(dimensions),
Request::SetSizeBounds(bounds) => self.set_size_bounds(bounds),
Request::SetResizable(resizable) => self.winit.set_resizable(resizable),
Request::SetFullscreen(fullscreen) => self.set_fullscreen(fullscreen),
Request::SetVisible(visible) => self.winit.set_visible(visible),
Request::SetPixelScale(scale) => self.set_scale(scale),
Request::SetCursor(cursor) => self.set_cursor(cursor),
Request::SetIcon(icon) => self.winit.set_window_icon(icon),
Request::Redraw => self.winit.request_redraw(),
Request::CloseWindow => {
self.marked_for_destruction = true;
self.program.handle_event(Event::Close, &mut self.requests.as_writer());
},
}
}
}
pub fn set_size(&mut self, dimensions: Dimensions) {
let size = to_physical_size(dimensions, self.scale);
let _ = self.winit.request_inner_size(size);
}
pub fn set_size_bounds(&mut self, bounds: SizeBounds) {
if self.size_bounds != bounds {
self.size_bounds = bounds;
self.update_size_bounds();
}
}
pub fn update_size_bounds(&mut self) {
// Set the minimum and maximum dimensions of the window.
let (min_size, max_size) = self.size_bounds.as_min_max_size(self.scale);
self.winit.set_min_inner_size(Some(min_size));
self.winit.set_max_inner_size(Some(max_size));
// Resize the window to fit the new size bounds.
let current_size = self.winit.inner_size();
let width = min(max(current_size.width, min_size.width), max_size.width);
let height = min(max(current_size.height, min_size.height), max_size.height);
let _ = self.winit.request_inner_size(PhysicalSize { width, height });
self.update_buffer_size(Dimensions { width, height });
}
pub fn scale(&self) -> u32 {
self.scale
}
pub fn set_scale(&mut self, scale: u32) {
if self.scale != scale {
self.scale = max(1, scale);
let increment = PhysicalSize { width: scale, height: scale };
self.winit.set_resize_increments(Some(increment));
self.update_size_bounds();
self.requests.write(Request::Redraw);
}
}
pub fn set_fullscreen(&mut self, fullscreen: bool) {
self.winit.set_fullscreen(
match fullscreen {
true => Some(Fullscreen::Borderless(None)),
false => None,
}
);
}
pub fn set_cursor(&mut self, cursor: Option<CursorIcon>) {
if let Some(cursor) = cursor {
self.winit.set_cursor(cursor);
self.winit.set_cursor_visible(true);
} else {
self.winit.set_cursor_visible(false);
}
}
/// Called with new physical dimensions when window is resized.
pub fn update_buffer_size(&mut self, dimensions: Dimensions) {
if dimensions.is_zero() {
return;
}
// Set surface to physical dimensions.
unsafe {
self.surface.resize(
std::num::NonZeroU32::new_unchecked(dimensions.width),
std::num::NonZeroU32::new_unchecked(dimensions.height),
).unwrap();
}
self.surface_dimensions = dimensions;
// Set buffer to logical dimensions.
let logical = dimensions / self.scale;
if logical != self.buffer.dimensions() {
self.buffer.resize(logical);
self.program.handle_event(Event::Resize(logical), &mut self.requests.as_writer());
self.redraw_full = true;
}
}
pub fn redraw(&mut self) {
// The surface dimensions will be zero until the first resize event
// arrives, which would cause a panic when calling `surface.present()`.
if self.surface_dimensions.is_zero() {
return;
}
// Render the program to the buffer.
self.program.render(&mut self.buffer, self.redraw_full);
self.redraw_full = false;
// Render the scaled buffer to the surface.
let buffer = self.buffer.as_u32_slice();
let mut surface = self.surface.buffer_mut().unwrap();
if self.scale == 1 && self.buffer.dimensions() == self.surface_dimensions {
let length = min(buffer.len(), surface.len());
surface[..length].copy_from_slice(&buffer[..length]);
} else {
let scale = self.scale as usize;
let surface_width = self.surface_dimensions.width as usize;
let surface_height = self.surface_dimensions.height as usize;
let buffer_width = self.buffer.dimensions().width as usize;
let buffer_height = self.buffer.dimensions().height as usize;
let blank_width = surface_width.saturating_sub(buffer_width * scale);
let min_width = min(buffer_width, surface_width / scale);
let min_height = min(buffer_height, surface_height / scale);
let mut bi: usize = 0;
let mut si: usize = 0;
for _ in 0..min_height {
// Horizontally scale a buffer row.
let row_start = si;
for _ in 0..min_width {
surface[si..si+scale].fill(buffer[bi]);
si += scale;
bi += 1;
}
// Fill fractional right edge with black.
surface[si..si+blank_width].fill(0);
si += blank_width;
let row_end = si;
// Vertically scale the buffer row.
for _ in 0..scale.saturating_sub(1) {
surface.copy_within(row_start..row_end, si);
si += surface_width;
}
}
// Fill fractional bottom edge with black.
surface[si..].fill(0);
}
surface.present().unwrap();
}
}
|