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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use crate::{prelude::*, renderer::*};
use bitflags::bitflags;
use num_traits::AsPrimitive;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DrawMode {
Corner,
Center,
}
pub type RectMode = DrawMode;
pub type EllipseMode = DrawMode;
pub type ImageMode = DrawMode;
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ArcMode {
Default,
Pie,
}
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BlendMode {
None,
Blend,
Add,
Mod,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum AngleMode {
Radians,
Degrees,
}
bitflags! {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct FontStyle: i32 {
const NORMAL = 0x00;
const BOLD = 0x01;
const ITALIC = 0x02;
const UNDERLINE = 0x03;
const STRIKETHROUGH = 0x04;
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub(crate) struct Settings {
pub(crate) background: Color,
pub(crate) fill: Option<Color>,
pub(crate) stroke: Option<Color>,
pub(crate) running: bool,
pub(crate) run_count: usize,
pub(crate) show_frame_rate: bool,
pub(crate) rect_mode: RectMode,
pub(crate) ellipse_mode: EllipseMode,
pub(crate) image_mode: ImageMode,
pub(crate) image_tint: Option<Color>,
pub(crate) arc_mode: ArcMode,
pub(crate) angle_mode: AngleMode,
pub(crate) blend_mode: BlendMode,
pub(crate) cursor: Option<Cursor>,
}
impl Default for Settings {
fn default() -> Self {
Self {
background: Color::default(),
fill: Some(WHITE),
stroke: Some(BLACK),
running: true,
run_count: 0,
show_frame_rate: false,
rect_mode: RectMode::Corner,
ellipse_mode: EllipseMode::Corner,
image_mode: ImageMode::Corner,
image_tint: None,
arc_mode: ArcMode::Default,
angle_mode: AngleMode::Radians,
blend_mode: BlendMode::None,
cursor: Some(Cursor::default()),
}
}
}
impl PixState {
pub fn background<C>(&mut self, color: C) -> PixResult<()>
where
C: Into<Color>,
{
self.settings.background = color.into();
self.clear()
}
pub fn fill<C>(&mut self, color: C)
where
C: Into<Color>,
{
self.settings.fill = Some(color.into());
}
pub fn no_fill(&mut self) {
self.settings.fill = None;
}
pub fn stroke<C>(&mut self, color: C)
where
C: Into<Color>,
{
self.settings.stroke = Some(color.into());
}
pub fn no_stroke(&mut self) {
self.settings.stroke = None;
}
pub fn clip<R>(&mut self, rect: R) -> PixResult<()>
where
R: Into<Rect<i32>>,
{
Ok(self.renderer.clip(Some(rect.into()))?)
}
pub fn no_clip(&mut self) -> PixResult<()> {
Ok(self.renderer.clip(None)?)
}
pub fn fullscreen(&mut self) -> PixResult<bool> {
Ok(self.renderer.fullscreen()?)
}
pub fn set_fullscreen(&mut self, val: bool) -> PixResult<()> {
Ok(self.renderer.set_fullscreen(val)?)
}
pub fn vsync(&mut self) -> bool {
self.renderer.vsync()
}
pub fn set_vsync(&mut self, val: bool) -> PixResult<()> {
Ok(self.renderer.set_vsync(val)?)
}
pub fn cursor(&mut self, cursor: &Cursor) -> PixResult<()> {
self.settings.cursor = Some(cursor.clone());
Ok(self.renderer.cursor(Some(cursor))?)
}
pub fn no_cursor(&mut self) {
self.settings.cursor = None;
self.renderer.cursor(None).expect("hiding cursor");
}
pub fn running(&mut self) -> bool {
self.settings.running
}
pub fn run(&mut self) {
self.settings.running = true;
}
pub fn no_run(&mut self) {
self.settings.running = false;
}
pub fn redraw(&mut self) {
self.settings.run_count = 1;
}
pub fn run_times(&mut self, n: usize) {
self.settings.run_count = n;
}
pub fn show_frame_rate(&mut self, show: bool) {
self.settings.show_frame_rate = show;
}
pub fn scale<T: AsPrimitive<f32>>(&mut self, x: T, y: T) -> PixResult<()> {
Ok(self.renderer.scale(x.as_(), y.as_())?)
}
pub fn font_size<S: AsPrimitive<u32>>(&mut self, size: S) -> PixResult<()> {
Ok(self.renderer.font_size(size.as_())?)
}
pub fn size_of<S: AsRef<str>>(&self, text: S) -> PixResult<(u32, u32)> {
Ok(self.renderer.size_of(text.as_ref())?)
}
pub fn font_style(&mut self, style: FontStyle) {
self.renderer.font_style(style);
}
pub fn font_family<S: AsRef<str>>(&mut self, family: S) -> PixResult<()> {
Ok(self.renderer.font_family(family.as_ref())?)
}
pub fn rect_mode(&mut self, mode: RectMode) {
self.settings.rect_mode = mode;
}
pub fn ellipse_mode(&mut self, mode: EllipseMode) {
self.settings.ellipse_mode = mode;
}
pub fn image_mode(&mut self, mode: ImageMode) {
self.settings.image_mode = mode;
}
pub fn image_tint<C>(&mut self, tint: C)
where
C: Into<Option<Color>>,
{
let tint = tint.into();
self.settings.image_tint = tint;
}
pub fn arc_mode(&mut self, mode: ArcMode) {
self.settings.arc_mode = mode;
}
pub fn angle_mode(&mut self, mode: AngleMode) {
self.settings.angle_mode = mode;
}
pub fn blend_mode(&mut self, mode: BlendMode) {
self.settings.blend_mode = mode;
self.renderer.blend_mode(mode);
}
pub fn push(&mut self) {
self.setting_stack.push(self.settings.clone());
}
pub fn pop(&mut self) {
if let Some(settings) = self.setting_stack.pop() {
self.settings = settings;
}
}
}
impl PixState {
pub(crate) fn frame_cursor(&mut self, cursor: &Cursor) -> PixResult<()> {
Ok(self.renderer.cursor(Some(cursor))?)
}
}