-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.rs
101 lines (90 loc) · 2.89 KB
/
builder.rs
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
use bevy_asset::Handle;
use bevy_image::{Image, TextureAtlas};
use bevy_math::URect;
use bevy_reflect::prelude::*;
use bevy_winit::cursor::{CustomCursor, CustomCursorImage};
use crate::{ani::asset::AnimatedCursor, cur::asset::StaticCursor};
/// A builder for a [`CustomCursor::Image`].
#[derive(Debug, Default, Reflect)]
#[reflect(Debug, Default)]
pub struct CustomCursorImageBuilder {
handle: Handle<Image>,
texture_atlas: Option<TextureAtlas>,
flip_x: bool,
flip_y: bool,
rect: Option<URect>,
hotspot: (u16, u16),
}
impl CustomCursorImageBuilder {
/// Create a builder from a [`StaticCursor`].
///
/// The `index` parameter is used to select the texture atlas index to use.
/// If `None`, index 0 is used.
pub fn from_static_cursor(c: &StaticCursor, index: Option<usize>) -> Self {
Self {
handle: c.image.clone(),
texture_atlas: Some(TextureAtlas {
layout: c.texture_atlas_layout.clone(),
index: index.unwrap_or(0),
}),
hotspot: c.hotspot_or_default(index.unwrap_or(0)),
..Default::default()
}
}
/// Create a builder from an [`AnimatedCursor`].
///
/// The `index` parameter is used to select the texture atlas index to use.
/// If `None`, index 0 is used.
pub fn from_animated_cursor(c: &AnimatedCursor, index: Option<usize>) -> Self {
Self {
handle: c.image.clone(),
texture_atlas: Some(TextureAtlas {
layout: c.texture_atlas_layout.clone(),
index: index.unwrap_or(0),
}),
hotspot: c.hotspot_or_default(index.unwrap_or(0)),
..Default::default()
}
}
/// Set the handle.
pub fn handle(mut self, handle: Handle<Image>) -> Self {
self.handle = handle;
self
}
/// Set the texture atlas.
pub fn texture_atlas(mut self, texture_atlas: Option<TextureAtlas>) -> Self {
self.texture_atlas = texture_atlas;
self
}
/// Set whether to flip horizontally.
pub fn flip_x(mut self, flip_x: bool) -> Self {
self.flip_x = flip_x;
self
}
/// Set whether to flip vertically.
pub fn flip_y(mut self, flip_y: bool) -> Self {
self.flip_y = flip_y;
self
}
/// Set the rectangle.
pub fn rect(mut self, rect: URect) -> Self {
self.rect = Some(rect);
self
}
/// Set the hotspot.
pub fn hotspot(mut self, hotspot: (u16, u16)) -> Self {
self.hotspot = hotspot;
self
}
/// Build the [`CustomCursor`].
pub fn build(self) -> CustomCursor {
CustomCursor::Image(CustomCursorImage {
handle: self.handle,
texture_atlas: self.texture_atlas,
flip_x: self.flip_x,
flip_y: self.flip_y,
rect: self.rect,
hotspot: self.hotspot,
})
}
}