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
#![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.
    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,
}

/// An extension for [`Renderer`] to paste from another texture.
pub trait PasteExt {
    /// Pastes the texture into `target_area`, or whole if `None`.
    fn paste(&self, texture: &Texture, target_area: Option<Rect>);
    /// Pastes the texture with options [`PasteExOption`].
    fn paste_ex(&self, texture: &Texture, options: PasteExOption);
}

impl PasteExt for Renderer<'_> {
    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.as_ptr(), texture.as_ptr(), as_raw(&src), as_raw(&dst))
        };
        if ret != 0 {
            Sdl::error_then_panic("Pasting texture to renderer");
        }
    }

    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.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");
        }
    }
}