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
//! Settings for the current [PixEngine] [PixState].
//!
//! [PixEngine]: crate::prelude::PixEngine

use crate::{prelude::*, renderer::*};
use bitflags::bitflags;
use num_traits::AsPrimitive;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Drawing mode which changes how `(x, y)` coordinates are interpreted.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DrawMode {
    /// Use `(x, y)` as the top-left corner. Default.
    Corner,
    /// Use `(x, y)` as the center.
    Center,
}

/// Drawing mode which changes how `(x, y)` coordinates are interpreted when drawing [Rect]s.
pub type RectMode = DrawMode;

/// Drawing mode which changes how `(x, y)` coordinates are interpreted when drawing [Ellipse]s.
pub type EllipseMode = DrawMode;

/// Drawing mode which changes how `(x, y)` coordinates are interpreted when drawing [Image]s.
pub type ImageMode = DrawMode;

/// Drawing mode which changes how arcs are drawn.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ArcMode {
    /// Draws arc with fill as an open pie segment.
    Default,
    /// Draws arc with fill as an closed pie segment.
    Pie,
}

/// Drawing mode which changes how textures are blended together.
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BlendMode {
    /// Disable blending.
    None,
    /// Alpha blending.
    Blend,
    /// Additive blending.
    Add,
    /// Color modulate.
    Mod,
}

/// Angle mode which changes how math functions interpreted.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum AngleMode {
    /// Radians.
    Radians,
    /// Degrees.
    Degrees,
}

bitflags! {
    /// Font style for drawing text.
    #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
    #[cfg_attr(feature = "serde", serde(transparent))]
    pub struct FontStyle: i32 {
        /// Normal.
        const NORMAL = 0x00;
        /// Bold.
        const BOLD = 0x01;
        /// Italic.
        const ITALIC = 0x02;
        /// Underline
        const UNDERLINE = 0x03;
        /// Strike-through
        const STRIKETHROUGH = 0x04;
    }
}

/// Several settings used to change various functionality of the engine.
#[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 {
    /// Sets the [Color] value used to clear the canvas.
    pub fn background<C>(&mut self, color: C) -> PixResult<()>
    where
        C: Into<Color>,
    {
        self.settings.background = color.into();
        self.clear()
    }

    /// Sets the [Color] value used to fill shapes drawn on the canvas.
    pub fn fill<C>(&mut self, color: C)
    where
        C: Into<Color>,
    {
        self.settings.fill = Some(color.into());
    }

    /// Disables filling shapes drawn on the canvas.
    pub fn no_fill(&mut self) {
        self.settings.fill = None;
    }

    /// Sets the [Color] value used to outline shapes drawn on the canvas.
    pub fn stroke<C>(&mut self, color: C)
    where
        C: Into<Color>,
    {
        self.settings.stroke = Some(color.into());
    }

    /// Disables outlining shapes drawn on the canvas.
    pub fn no_stroke(&mut self) {
        self.settings.stroke = None;
    }

    /// Sets the clip [Rect] used by the renderer to draw to the current canvas.
    pub fn clip<R>(&mut self, rect: R) -> PixResult<()>
    where
        R: Into<Rect<i32>>,
    {
        Ok(self.renderer.clip(Some(rect.into()))?)
    }

    /// Clears the clip [Rect] used by the renderer to draw to the current canvas.
    pub fn no_clip(&mut self) -> PixResult<()> {
        Ok(self.renderer.clip(None)?)
    }

    /// Returns whether the application is fullscreen or not.
    pub fn fullscreen(&mut self) -> PixResult<bool> {
        Ok(self.renderer.fullscreen()?)
    }

    /// Set the application to fullscreen or not.
    pub fn set_fullscreen(&mut self, val: bool) -> PixResult<()> {
        Ok(self.renderer.set_fullscreen(val)?)
    }

    /// Returns whether the window synchronizes frame rate to the screens refresh rate.
    pub fn vsync(&mut self) -> bool {
        self.renderer.vsync()
    }

    /// Set the window to synchronize frame rate to the screens refresh rate.
    pub fn set_vsync(&mut self, val: bool) -> PixResult<()> {
        Ok(self.renderer.set_vsync(val)?)
    }

    /// Set the mouse cursor to a predefined symbol or image.
    pub fn cursor(&mut self, cursor: &Cursor) -> PixResult<()> {
        self.settings.cursor = Some(cursor.clone());
        Ok(self.renderer.cursor(Some(cursor))?)
    }

    /// Hide the mouse cursor.
    pub fn no_cursor(&mut self) {
        self.settings.cursor = None;
        // SAFETY: Setting to NONE to hide cursor can't error.
        self.renderer.cursor(None).expect("hiding cursor");
    }

    /// Whether the render loop is running or not.
    pub fn running(&mut self) -> bool {
        self.settings.running
    }

    /// Unpause the render loop.
    pub fn run(&mut self) {
        self.settings.running = true;
    }

    /// Pause the render loop by no longer calling [AppState::on_update] every frame.
    ///
    /// [AppState::on_update]: crate::prelude::AppState::on_update
    pub fn no_run(&mut self) {
        self.settings.running = false;
    }

    /// Run the render loop 1 time by calling [AppState::on_update].
    ///
    /// This can be used to only redraw in response to user actions such as
    /// [AppState::on_mouse_pressed] or [AppState::on_key_pressed].
    ///
    /// [AppState::on_update]: crate::prelude::AppState::on_update
    /// [AppState::on_mouse_pressed]: crate::prelude::AppState::on_mouse_pressed
    /// [AppState::on_key_pressed]: crate::prelude::AppState::on_key_pressed
    pub fn redraw(&mut self) {
        self.settings.run_count = 1;
    }

    /// Run the render loop N times by calling [AppState::on_update].
    ///
    /// This can be used to only redraw in response to user actions such as
    /// [AppState::on_mouse_pressed] or [AppState::on_key_pressed].
    ///
    /// [AppState::on_update]: crate::prelude::AppState::on_update
    /// [AppState::on_mouse_pressed]: crate::prelude::AppState::on_mouse_pressed
    /// [AppState::on_key_pressed]: crate::prelude::AppState::on_key_pressed
    pub fn run_times(&mut self, n: usize) {
        self.settings.run_count = n;
    }

    /// Set whether to show the current frame rate per second in the title or not.
    pub fn show_frame_rate(&mut self, show: bool) {
        self.settings.show_frame_rate = show;
    }

    /// Set the rendering scale of the current canvas.
    pub fn scale<T: AsPrimitive<f32>>(&mut self, x: T, y: T) -> PixResult<()> {
        Ok(self.renderer.scale(x.as_(), y.as_())?)
    }

    /// Set the font size for drawing to the current canvas.
    pub fn font_size<S: AsPrimitive<u32>>(&mut self, size: S) -> PixResult<()> {
        Ok(self.renderer.font_size(size.as_())?)
    }

    /// Return the dimensions of given text for drawing to the current canvas.
    pub fn size_of<S: AsRef<str>>(&self, text: S) -> PixResult<(u32, u32)> {
        Ok(self.renderer.size_of(text.as_ref())?)
    }

    /// Set the font style for drawing to the current canvas.
    pub fn font_style(&mut self, style: FontStyle) {
        self.renderer.font_style(style);
    }

    /// Set the font family for drawing to the current canvas.
    pub fn font_family<S: AsRef<str>>(&mut self, family: S) -> PixResult<()> {
        Ok(self.renderer.font_family(family.as_ref())?)
    }

    /// Change the way parameters are interpreted for drawing [Square](Rect)s and
    /// [Rectangle](Rect)s.
    pub fn rect_mode(&mut self, mode: RectMode) {
        self.settings.rect_mode = mode;
    }

    /// Change the way parameters are interpreted for drawing [Ellipse]s.
    ///
    /// [Ellipse]: crate::prelude::Ellipse
    pub fn ellipse_mode(&mut self, mode: EllipseMode) {
        self.settings.ellipse_mode = mode;
    }

    /// Change the way parameters are interpreted for drawing [Image]s.
    ///
    /// [Image]: crate::prelude::Image
    pub fn image_mode(&mut self, mode: ImageMode) {
        self.settings.image_mode = mode;
    }

    /// Add a color tint to [Image]s when drawing.
    ///
    /// [Image]: crate::prelude::Image
    pub fn image_tint<C>(&mut self, tint: C)
    where
        C: Into<Option<Color>>,
    {
        let tint = tint.into();
        self.settings.image_tint = tint;
    }

    /// Change the way arcs are drawn.
    pub fn arc_mode(&mut self, mode: ArcMode) {
        self.settings.arc_mode = mode;
    }

    /// Change the way angles are interprted for matrix transformations.
    pub fn angle_mode(&mut self, mode: AngleMode) {
        self.settings.angle_mode = mode;
    }

    /// Change the way textures are blended together.
    pub fn blend_mode(&mut self, mode: BlendMode) {
        self.settings.blend_mode = mode;
        self.renderer.blend_mode(mode);
    }

    /// Saves the current draw settings and transforms.
    pub fn push(&mut self) {
        self.setting_stack.push(self.settings.clone());
    }

    /// Restores the previous draw settings and transforms, if present. If the settings stack is
    /// empty, the settings will remain unchanged.
    pub fn pop(&mut self) {
        if let Some(settings) = self.setting_stack.pop() {
            self.settings = settings;
        }
    }
}

impl PixState {
    /// Set the mouse cursor to a predefined symbol or image for a single frame.
    ///
    /// Cursor will get reset to the current setting next frame.
    pub(crate) fn frame_cursor(&mut self, cursor: &Cursor) -> PixResult<()> {
        Ok(self.renderer.cursor(Some(cursor))?)
    }
}