use bevy_asset::Handle;
use bevy_image::{Image, TextureAtlas};
use bevy_math::URect;
use bevy_reflect::prelude::*;
use bevy_window::{CustomCursor, CustomCursorImage};
use crate::{ani::asset::AnimatedCursor, cur::asset::StaticCursor};
#[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 {
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()
}
}
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()
}
}
pub fn handle(mut self, handle: Handle<Image>) -> Self {
self.handle = handle;
self
}
pub fn texture_atlas(mut self, texture_atlas: Option<TextureAtlas>) -> Self {
self.texture_atlas = texture_atlas;
self
}
pub fn flip_x(mut self, flip_x: bool) -> Self {
self.flip_x = flip_x;
self
}
pub fn flip_y(mut self, flip_y: bool) -> Self {
self.flip_y = flip_y;
self
}
pub fn rect(mut self, rect: URect) -> Self {
self.rect = Some(rect);
self
}
pub fn hotspot(mut self, hotspot: (u16, u16)) -> Self {
self.hotspot = hotspot;
self
}
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,
})
}
}