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
use std::marker::PhantomData;
use crate::asset_id::{AppAssetId, IdU16};
use crate::core::CoreAudio;
pub struct AppContext<A: AppAssetId> {
pub audio: Audio<A>,
dims: (f64, f64),
cursor: (f64, f64),
close_requested: bool,
native_px: f64,
is_fullscreen: bool,
desires_fullscreen: bool,
cookie: Vec<u8>,
cookie_updated: bool,
}
impl<A: AppAssetId> AppContext<A> {
pub(crate) fn new(audio: CoreAudio, dims: (f64, f64), native_px: f64) -> AppContext<A> {
AppContext {
audio: Audio { core: audio, phantom: PhantomData },
dims,
cursor: (0., 0.),
close_requested: false,
native_px,
is_fullscreen: false,
desires_fullscreen: false,
cookie: Vec::new(),
cookie_updated: false,
}
}
pub(crate) fn set_cursor(&mut self, cursor: (f64, f64)) {
self.cursor = cursor;
self.bound_cursor();
}
pub(crate) fn set_dims(&mut self, dims: (f64, f64), native_px: f64) {
self.dims = dims;
self.native_px = native_px;
self.bound_cursor();
}
fn bound_cursor(&mut self) {
self.cursor = (
self.cursor.0.max(0.).min(self.dims.0),
self.cursor.1.max(0.).min(self.dims.1),
);
}
pub fn dims(&self) -> (f64, f64) { self.dims }
pub fn cursor(&self) -> (f64, f64) { self.cursor }
pub fn native_px(&self) -> f64 { self.native_px }
pub fn native_px_align(&self, x: f64, y: f64) -> (f64, f64) {
(
(x / self.native_px).round() * self.native_px,
(y / self.native_px).round() * self.native_px,
)
}
pub fn request_fullscreen(&mut self) { self.desires_fullscreen = true; }
pub fn cancel_fullscreen(&mut self) { self.desires_fullscreen = false; }
pub fn is_fullscreen(&self) -> bool { self.is_fullscreen }
pub(crate) fn desires_fullscreen(&self) -> bool { self.desires_fullscreen }
pub(crate) fn set_is_fullscreen(&mut self, is_fullscreen: bool) {
self.is_fullscreen = is_fullscreen;
self.desires_fullscreen = is_fullscreen;
}
pub fn close(&mut self) { self.close_requested = true; }
pub(crate) fn take_close_request(&mut self) -> bool {
let result = self.close_requested;
self.close_requested = false;
result
}
pub fn cookie(&self) -> &[u8] {
&self.cookie
}
pub fn set_cookie(&mut self, cookie: Vec<u8>) {
assert!(cookie.len() < 700);
if cookie != self.cookie {
self.cookie_updated = true;
self.cookie = cookie;
}
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn take_cookie_updated_flag(&mut self) -> bool {
let was_updated = self.cookie_updated;
self.cookie_updated = false;
was_updated
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn cookie_buffer(&mut self) -> &mut Vec<u8> {
&mut self.cookie
}
}
pub struct Audio<A: AppAssetId> { core: CoreAudio, phantom: PhantomData<A> }
impl<A: AppAssetId> Audio<A> {
pub fn play_sound(&mut self, sound: A::Sound) { self.core.play_sound(sound.id_u16()); }
pub fn play_music(&mut self, music: A::Music) { self.core.play_music(music.id_u16(), false); }
pub fn loop_music(&mut self, music: A::Music) { self.core.play_music(music.id_u16(), true); }
pub fn stop_music(&mut self) { self.core.stop_music(); }
}