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
use std::sync::mpsc::Sender;
use crate::event::{Action, Key, MouseButton, WindowEvent};
#[cfg(not(target_arch = "wasm32"))]
use crate::window::GLCanvas as CanvasImpl;
#[cfg(target_arch = "wasm32")]
use crate::window::WebGLCanvas as CanvasImpl;
use image::{GenericImage, Pixel};
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum NumSamples {
Zero = 0,
One = 1,
Two = 2,
Four = 4,
Eight = 8,
Sixteen = 16,
}
impl NumSamples {
pub fn from_u32(i: u32) -> Option<NumSamples> {
match i {
0 => Some(NumSamples::Zero),
1 => Some(NumSamples::One),
2 => Some(NumSamples::Two),
4 => Some(NumSamples::Four),
8 => Some(NumSamples::Eight),
16 => Some(NumSamples::Sixteen),
_ => None,
}
}
}
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CanvasSetup {
pub vsync: bool,
pub samples: NumSamples,
}
pub struct Canvas {
canvas: CanvasImpl,
}
impl Canvas {
pub fn open(
title: &str,
hide: bool,
width: u32,
height: u32,
canvas_setup: Option<CanvasSetup>,
out_events: Sender<WindowEvent>,
) -> Self {
Canvas {
canvas: CanvasImpl::open(title, hide, width, height, canvas_setup, out_events),
}
}
pub fn render_loop(data: impl RenderLoopClosure) {
CanvasImpl::render_loop(data)
}
pub fn poll_events(&mut self) {
self.canvas.poll_events()
}
pub fn swap_buffers(&mut self) {
self.canvas.swap_buffers()
}
pub fn size(&self) -> (u32, u32) {
self.canvas.size()
}
pub fn cursor_pos(&self) -> Option<(f64, f64)> {
self.canvas.cursor_pos()
}
pub fn scale_factor(&self) -> f64 {
self.canvas.scale_factor()
}
pub fn set_title(&mut self, title: &str) {
self.canvas.set_title(title)
}
pub fn set_icon(&mut self, icon: impl GenericImage<Pixel = impl Pixel<Subpixel = u8>>) {
self.canvas.set_icon(icon)
}
pub fn set_cursor_grab(&self, grab: bool) {
self.canvas.set_cursor_grab(grab);
}
pub fn set_cursor_position(&self, x: f64, y: f64) {
self.canvas.set_cursor_position(x, y);
}
pub fn hide_cursor(&self, hide: bool) {
self.canvas.hide_cursor(hide);
}
pub fn hide(&mut self) {
self.canvas.hide()
}
pub fn show(&mut self) {
self.canvas.show()
}
pub fn get_mouse_button(&self, button: MouseButton) -> Action {
self.canvas.get_mouse_button(button)
}
pub fn get_key(&self, key: Key) -> Action {
self.canvas.get_key(key)
}
}
pub trait RenderLoopClosure: 'static {
fn call(&mut self, x: f64) -> bool;
}
pub(crate) trait AbstractCanvas {
fn open(
title: &str,
hide: bool,
width: u32,
height: u32,
window_setup: Option<CanvasSetup>,
out_events: Sender<WindowEvent>,
) -> Self;
fn render_loop(data: impl RenderLoopClosure);
fn poll_events(&mut self);
fn swap_buffers(&mut self);
fn size(&self) -> (u32, u32);
fn cursor_pos(&self) -> Option<(f64, f64)>;
fn scale_factor(&self) -> f64;
fn set_title(&mut self, title: &str);
fn set_icon(&mut self, icon: impl GenericImage<Pixel = impl Pixel<Subpixel = u8>>);
fn set_cursor_grab(&self, grab: bool);
fn set_cursor_position(&self, x: f64, y: f64);
fn hide_cursor(&self, hide: bool);
fn hide(&mut self);
fn show(&mut self);
fn get_mouse_button(&self, button: MouseButton) -> Action;
fn get_key(&self, key: Key) -> Action;
}