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
#![allow(clippy::unnecessary_cast)]

use bitflags::bitflags;

use crate::texture::Texture;
use crate::{
    as_raw,
    geo::{Point, Rect},
};
use crate::{bind, EnumInt, Sdl};

use super::Renderer;

bitflags! {
    /// Flip mode on pasting from another texture.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct PasteExFlip: u32 {
        /// Flips horizontal.
        const HORIZONTAL = bind::SDL_FLIP_HORIZONTAL as u32;
        /// Flips vertical.
        const VERTICAL = bind::SDL_FLIP_VERTICAL as u32;
        /// Flips both horizontal and vertical.
        const BOTH = Self::HORIZONTAL.bits() | Self::VERTICAL.bits();
    }
}

impl Default for PasteExFlip {
    fn default() -> Self {
        Self::empty()
    }
}

/// An option for [`PasteExt::paste_ex`].
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct PasteExOption {
    /// The target area of pasting, or whole if `None`.
    pub target_area: Option<Rect>,
    /// The degrees of rotating another texture.
    pub rotation_degrees: f64,
    /// The center point of pasting.
    pub center: Option<Point>,
    /// The flip mode of pasting.
    pub flip: PasteExFlip,
}

/// A paster controls pasting from a texture.
///
/// This will render when be dropped. So you should re-create on every render.
#[derive(Debug)]
pub struct Paster<'renderer> {
    renderer: &'renderer Renderer<'renderer>,
}

impl<'renderer> Drop for Paster<'renderer> {
    fn drop(&mut self) {
        unsafe { bind::SDL_RenderPresent(self.renderer.as_ptr()) }
    }
}

impl<'renderer> Paster<'renderer> {
    /// Constructs a pen from the renderer [`Renderer`].
    #[must_use]
    pub fn new(renderer: &'renderer Renderer) -> Self {
        Self { renderer }
    }

    /// Returns the renderer that the pen is drawing.
    #[must_use]
    pub fn renderer(&self) -> &Renderer {
        self.renderer
    }
}

impl Paster<'_> {
    /// Pastes the texture into `target_area`, or whole if `None`.
    pub fn paste(&self, texture: &Texture, target_area: Option<Rect>) {
        let src = texture.clip().map(Into::into);
        let dst = target_area.map(Into::into);
        let ret = unsafe {
            bind::SDL_RenderCopy(
                self.renderer.as_ptr(),
                texture.as_ptr(),
                as_raw(&src),
                as_raw(&dst),
            )
        };
        if ret != 0 {
            Sdl::error_then_panic("Pasting texture to renderer");
        }
    }

    /// Pastes the texture with options [`PasteExOption`].
    pub fn paste_ex(
        &self,
        texture: &Texture,
        PasteExOption {
            target_area,
            rotation_degrees,
            center,
            flip,
        }: PasteExOption,
    ) {
        let src = texture.clip().map(Into::into);
        let dst = target_area.map(Into::into);
        let center = center.map(Into::into);
        let ret = unsafe {
            bind::SDL_RenderCopyEx(
                self.renderer.as_ptr(),
                texture.as_ptr(),
                as_raw(&src),
                as_raw(&dst),
                rotation_degrees,
                as_raw(&center),
                flip.bits() as EnumInt,
            )
        };
        if ret != 0 {
            Sdl::error_then_panic("Pasting texture to renderer ex");
        }
    }
}