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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
use std::collections::{hash_map, HashMap};
use std::fmt::{Debug, Display};
use std::hash::Hash;
use std::iter::IntoIterator;
use std::ops::{Deref, Div};
use std::sync::Arc;
use std::time::Duration;
use figures::units::UPx;
use figures::{Point, Rect, Size};
use intentional::{Assert, Cast};
use justjson::Value;
use crate::pipeline::Vertex;
use crate::sealed::{self, TextureSource as _};
use crate::{
CanRenderTo, CollectedTexture, Graphics, Kludgine, PreparedGraphic, ShareableTexture,
SharedTexture, TextureRegion, TextureSource,
};
/// Includes an [Aseprite](https://www.aseprite.org/) sprite sheet and Json
/// export. For more information, see [`Sprite::load_aseprite_json`]. This macro
/// will append ".png" and ".json" to the path provided and include both files
/// in your binary.
#[macro_export]
macro_rules! include_aseprite_sprite {
($path:expr) => {{
$crate::include_texture!(concat!($path, ".png"))
.map_err($crate::sprite::SpriteParseError::from)
.and_then(|texture| {
$crate::sprite::Sprite::load_aseprite_json(
include_str!(concat!($path, ".json")),
texture,
)
})
}};
}
/// The animation mode of the sprite.
#[derive(Debug, Clone)]
pub enum AnimationMode {
/// Iterate frames in order. When at the end, reset to the start.
Forward,
/// Iterate frames in reverse order. When at the start, reset to the end.
Reverse,
/// Iterate frames starting at the beginning and continuously iterating
/// forwards and backwards across the frames, changing direction whenever
/// the start or end are reached.
PingPong,
}
impl AnimationMode {
const fn default_direction(&self) -> AnimationDirection {
match self {
AnimationMode::Forward | AnimationMode::PingPong => AnimationDirection::Forward,
AnimationMode::Reverse => AnimationDirection::Reverse,
}
}
}
#[derive(Debug, Clone)]
enum AnimationDirection {
Forward,
Reverse,
}
/// An error occurred parsing a [`Sprite`].
#[derive(Debug)]
pub enum SpriteParseError {
/// The `meta` field is missing or invalid.
Meta,
/// The size information is missing.
SizeMissing,
/// The size does not match the provided texture.
SizeMismatch,
/// An error parsing a frame tag (animation).
FrameTag {
/// The name of the frame tag.
name: String,
/// The error that occurred.
error: FrameTagError,
},
/// An error occurred parsing a frame.
Frame {
/// The object key for the frame.
key: String,
/// The error that occurred.
error: FrameParseError,
},
/// Invalid JSON.
Json(justjson::Error),
/// An image parsing error.
#[cfg(feature = "image")]
Image(image::ImageError),
}
impl SpriteParseError {
fn frame(key: &impl Display, error: FrameParseError) -> Self {
Self::Frame {
key: key.to_string(),
error,
}
}
fn frame_tag(name: &impl Display, error: FrameTagError) -> Self {
Self::FrameTag {
name: name.to_string(),
error,
}
}
}
#[cfg(feature = "image")]
impl From<image::ImageError> for SpriteParseError {
fn from(value: image::ImageError) -> Self {
Self::Image(value)
}
}
/// An error parsing a single frame in a sprite animation.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum FrameParseError {
/// The frame number was not able to be parsed as a number.
NotNumeric,
/// The duration is invalid or missing.
Duration,
/// The data is missing the `frame` field, which contains the region in the
/// texture to use for this frame.
MissingRegion,
/// The `frame.x` value is missing or invalid.
X,
/// The `frame.y` value is missing or invalid.
Y,
/// The `frame.w` value is missing or invalid.
Width,
/// The `frame.h` value is missing or invalid.
Height,
}
/// An error parsing a `frameTags` entry.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum FrameTagError {
/// The direction field is missing.
DirectionMissing,
/// The direction is not a recognized value.
DirectionUnknown,
/// The from field is missing or invalid.
From,
/// The to field is missing or invalid.
To,
/// The frame could not be found.
InvalidFrame,
}
impl From<justjson::Error> for SpriteParseError {
fn from(error: justjson::Error) -> Self {
Self::Json(error)
}
}
/// A [`Sprite`]'s tag did not correspond to an animation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct InvalidSpriteTag;
/// A sprite is a renderable graphic with optional animations.
///
/// Cloning a sprite is cheap. When cloning, the animations will be shared
/// between all clones of the sprite, but each sprite will track its current
/// frame/tag independently.
#[derive(Debug, Clone)]
pub struct Sprite {
/// The animations that form this sprite.
pub animations: SpriteAnimations,
elapsed_since_frame_change: Duration,
current_tag: Option<String>,
current_frame: usize,
current_animation_direction: AnimationDirection,
}
impl From<SpriteAnimations> for Sprite {
fn from(animations: SpriteAnimations) -> Self {
Self::new(animations)
}
}
impl Sprite {
/// Returns a new sprite with `animations`.
#[must_use]
pub const fn new(animations: SpriteAnimations) -> Self {
Self {
animations,
current_frame: 0,
current_tag: None,
elapsed_since_frame_change: Duration::from_millis(0),
current_animation_direction: AnimationDirection::Forward,
}
}
/// For merging multiple Sprites that have no tags within them
#[must_use]
pub fn merged<S: Into<String>, I: IntoIterator<Item = (S, Self)>>(source: I) -> Self {
let mut combined = HashMap::new();
for (name, sprite) in source {
let Some(animation) = sprite.animations.animation_for(&Option::<&str>::None) else {
continue;
};
combined.insert(Some(name.into()), animation.clone());
}
Self::new(SpriteAnimations::new(combined))
}
/// Creates an instance from a texture. This creates a `SpriteAnimation`
/// with no tag and a single frame.
#[must_use]
pub fn single_frame(texture: SharedTexture) -> Self {
let mut frames = HashMap::new();
frames.insert(
None,
SpriteAnimation::new(vec![SpriteFrame::new(TextureRegion::from(texture))])
.with_mode(AnimationMode::Forward),
);
let frames = SpriteAnimations::new(frames);
Self::new(frames)
}
/// Loads [Aseprite](https://www.aseprite.org/) JSON export format, when
/// using the correct settings.
///
/// For the JSON data, use the Hash export option (default), and use either
/// spaces or underscores (_) inbetween the fields in the name. Ensure
/// `{frame}` is the last field in the name before the extension. E.g.,
/// `{tag}_{frame}.{extension}`
///
/// # Errors
///
/// Returns an error when `raw_json` does not match the expected format.
#[allow(clippy::too_many_lines)]
// TODO refactor. Now that I know more about serde, this probably can be parsed
// with a complex serde type.
pub fn load_aseprite_json(
raw_json: &str,
texture: impl Into<ShareableTexture>,
) -> Result<Self, SpriteParseError> {
let texture = texture.into();
let json = justjson::Value::from_json(raw_json)?;
let Some(Value::Object(meta)) = json.get("meta") else {
return Err(SpriteParseError::Meta);
};
let texture_size = texture.default_rect().size;
let Some(size) = meta.get("size") else {
return Err(SpriteParseError::SizeMissing);
};
if size["w"].as_u32() != Some(texture_size.width.get())
|| size["h"].as_u32() != Some(texture_size.height.get())
{
return Err(SpriteParseError::SizeMismatch);
}
let mut frames = HashMap::new();
for frame in json["frames"]
.as_object()
.map(|frame| frame.iter())
.into_iter()
.flatten()
{
// Remove the extension, if present
let key = frame.key.decode_if_needed();
let name = key.split('.').next().assert_expected();
// Split by _ or ' 'as per the documentation of this method.
let name_parts = name.split(|c| c == '_' || c == ' ').collect::<Vec<_>>();
let frame_number = name_parts[name_parts.len() - 1]
.parse::<usize>()
.or_else(|_| {
if json["frames"]
.as_array()
.map_or(false, |frames| frames.len() == 1)
{
Ok(0)
} else {
Err(SpriteParseError::frame(
&frame.key,
FrameParseError::NotNumeric,
))
}
})?;
let duration = match frame.value.get("duration").and_then(Value::as_u64) {
Some(millis) => Duration::from_millis(millis),
None => {
return Err(SpriteParseError::frame(
&frame.key,
FrameParseError::Duration,
))
}
};
let Some(rect) = frame.value.get("frame") else {
return Err(SpriteParseError::frame(
&frame.key,
FrameParseError::MissingRegion,
));
};
let region = Rect::new(
Point::new(
rect["x"]
.as_u32()
.ok_or_else(|| SpriteParseError::frame(&frame.key, FrameParseError::X))?,
rect["y"]
.as_u32()
.ok_or_else(|| SpriteParseError::frame(&frame.key, FrameParseError::Y))?,
),
Size::new(
rect["w"].as_u32().ok_or_else(|| {
SpriteParseError::frame(&frame.key, FrameParseError::Width)
})?,
rect["h"].as_u32().ok_or_else(|| {
SpriteParseError::frame(&frame.key, FrameParseError::Height)
})?,
),
)
.cast();
let source = SpriteSource::Region(TextureRegion {
region,
texture: texture.clone(),
});
frames.insert(
frame_number,
SpriteFrame {
duration: Some(duration),
source,
},
);
}
let mut animations = HashMap::new();
for tag in meta
.get("frameTags")
.and_then(|tags| tags.as_array())
.into_iter()
.flatten()
{
let Some(name) = tag["name"].as_string() else {
continue;
};
let Some(direction) = tag["direction"].as_string() else {
return Err(SpriteParseError::frame_tag(
name,
FrameTagError::DirectionMissing,
));
};
let direction = if direction == "forward" {
AnimationMode::Forward
} else if direction == "reverse" {
AnimationMode::Reverse
} else if direction == "pingpong" {
AnimationMode::PingPong
} else {
return Err(SpriteParseError::frame_tag(
name,
FrameTagError::DirectionUnknown,
));
};
let start_frame = tag["from"]
.as_usize()
.ok_or_else(|| SpriteParseError::frame_tag(name, FrameTagError::From))?;
let end_frame = tag["to"]
.as_usize()
.ok_or_else(|| SpriteParseError::frame_tag(name, FrameTagError::To))?;
let mut animation_frames = Vec::new();
for i in start_frame..=end_frame {
let frame = frames.get(&i).ok_or_else(|| {
SpriteParseError::frame_tag(name, FrameTagError::InvalidFrame)
})?;
animation_frames.push(frame.clone());
}
animations.insert(
Some(name.to_string()),
SpriteAnimation::new(animation_frames).with_mode(direction),
);
}
let mut frames: Vec<_> = frames.into_iter().collect();
frames.sort_by(|a, b| a.0.cmp(&b.0));
animations.insert(
None,
SpriteAnimation::new(frames.iter().map(|(_, f)| f.clone()).collect())
.with_mode(AnimationMode::Forward),
);
Ok(Self::new(SpriteAnimations::new(animations)))
}
/// Sets the current tag for the animation. If the tag currently matches,
/// nothing will happen. If it is a new tag, the current frame and animation
/// direction will be switched to the values from the new tag.
///
/// # Errors
///
/// Returns an error if `tag` is not a valid animation tag.
pub fn set_current_tag<S: Into<String>>(
&mut self,
tag: Option<S>,
) -> Result<(), InvalidSpriteTag> {
let new_tag = tag.map(Into::into);
if self.current_tag != new_tag {
self.current_animation_direction = {
let animation = self
.animations
.animations
.get(&new_tag)
.ok_or(InvalidSpriteTag)?;
animation.mode.default_direction()
};
self.current_frame = 0;
self.current_tag = new_tag;
}
Ok(())
}
/// Returns the current tag.
#[must_use]
pub fn current_tag(&self) -> Option<&'_ str> {
self.current_tag.as_deref()
}
/// Gets the current frame after advancing the animation for `elapsed`
/// duration. If you need to invoke this multiple times in a single frame,
/// pass `None` on subsequent calls. In general, you should clone sprites
/// rather than reuse them. Kludgine ensures that your texture and animation
/// data will be shared and not cloned.
///
/// # Errors
///
/// Returns an error the current animation tag does not match any defined
/// animation.
pub fn get_frame(
&mut self,
elapsed: Option<Duration>,
) -> Result<SpriteSource, InvalidSpriteTag> {
if let Some(elapsed) = elapsed {
self.elapsed_since_frame_change += elapsed;
let current_frame_duration = self.with_current_frame(|frame| frame.duration)?;
if let Some(frame_duration) = current_frame_duration {
if self.elapsed_since_frame_change > frame_duration {
self.elapsed_since_frame_change = Duration::from_nanos(
(self.elapsed_since_frame_change.as_nanos() % frame_duration.as_nanos())
.cast(),
);
self.advance_frame()?;
}
}
}
self.current_frame()
}
/// Retrieve the current animation frame, if set and valid.
///
/// # Errors
///
/// Returns an error the current animation tag does not match any defined
/// animation.
#[inline]
pub fn current_frame(&self) -> Result<SpriteSource, InvalidSpriteTag> {
self.with_current_frame(|frame| frame.source.clone())
}
/// Returns the amount of time remaining until the next frame is due to be
/// shown for this sprite. Can be used to calculate redraws more efficiently
/// if you're not rendering at a constant framerate.
///
/// # Errors
///
/// Returns an error the current animation tag does not match any defined
/// animation.
pub fn remaining_frame_duration(&self) -> Result<Option<Duration>, InvalidSpriteTag> {
let duration = self
.with_current_frame(|frame| frame.duration)?
.map(|frame_duration| {
frame_duration
.checked_sub(self.elapsed_since_frame_change)
.unwrap_or_default()
});
Ok(duration)
}
fn advance_frame(&mut self) -> Result<(), InvalidSpriteTag> {
self.current_frame = self.next_frame()?;
Ok(())
}
#[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
fn next_frame(&mut self) -> Result<usize, InvalidSpriteTag> {
let starting_frame = self.current_frame.cast::<i32>();
let animation = self
.animations
.animations
.get(&self.current_tag)
.ok_or(InvalidSpriteTag)?;
let next_frame = match self.current_animation_direction {
AnimationDirection::Forward => starting_frame + 1,
AnimationDirection::Reverse => starting_frame - 1,
};
Ok(if next_frame < 0 {
match animation.mode {
AnimationMode::Forward => unreachable!(),
AnimationMode::Reverse => {
// Cycle back to the last frame
animation.frames.len() - 1
}
AnimationMode::PingPong => {
self.current_animation_direction = AnimationDirection::Forward;
1
}
}
} else if next_frame as usize >= animation.frames.len() {
match animation.mode {
AnimationMode::Reverse => unreachable!(),
AnimationMode::Forward => 0,
AnimationMode::PingPong => {
self.current_animation_direction = AnimationDirection::Reverse;
(animation.frames.len() - 2).max(0)
}
}
} else {
next_frame as usize
})
}
/// If tag is valid, invoke `f` with the current animation frame.
fn with_current_frame<F, R>(&self, f: F) -> Result<R, InvalidSpriteTag>
where
F: Fn(&SpriteFrame) -> R,
{
let animation = self
.animations
.animations
.get(&self.current_tag)
.ok_or(InvalidSpriteTag)?;
Ok(f(&animation.frames[self.current_frame]))
}
}
/// A collection of [`SpriteAnimation`]s. This is an immutable object that
/// shares data when cloned to minimize data copies.
#[derive(Debug, Clone)]
pub struct SpriteAnimations {
animations: Arc<HashMap<Option<String>, SpriteAnimation>>,
}
impl SpriteAnimations {
/// Creates a new collection from `animations`.
#[must_use]
pub fn new(animations: HashMap<Option<String>, SpriteAnimation>) -> Self {
Self {
animations: Arc::new(animations),
}
}
/// Returns the animation for `tag`.
#[must_use]
pub fn animation_for(&self, tag: &Option<impl ToString>) -> Option<&'_ SpriteAnimation> {
self.animations.get(&tag.as_ref().map(ToString::to_string))
}
}
/// An animation of one or more [`SpriteFrame`]s.
#[derive(Debug, Clone)]
pub struct SpriteAnimation {
/// The frames of the animation.
pub frames: Arc<Vec<SpriteFrame>>,
/// The mode of the animation.
pub mode: AnimationMode,
}
impl SpriteAnimation {
/// Creates a new animation with `frames` and [`AnimationMode::Forward`].
#[must_use]
pub fn new(frames: Vec<SpriteFrame>) -> Self {
Self {
frames: Arc::new(frames),
mode: AnimationMode::Forward,
}
}
/// Builder-style function. Sets `mode` and returns self.
#[must_use]
pub const fn with_mode(mut self, mode: AnimationMode) -> Self {
self.mode = mode;
self
}
}
/// A single frame for a [`SpriteAnimation`].
#[derive(Debug, Clone)]
pub struct SpriteFrame {
/// The source to render.
pub source: SpriteSource,
/// The length the frame should be displayed. `None` will act as an infinite
/// duration.
pub duration: Option<Duration>,
}
impl SpriteFrame {
/// Creates a new frame with `source` and no duration.
#[must_use]
pub fn new(source: impl Into<SpriteSource>) -> Self {
Self {
source: source.into(),
duration: None,
}
}
/// Builder-style function. Sets `duration` and returns self.
#[must_use]
pub const fn with_duration(mut self, duration: Duration) -> Self {
self.duration = Some(duration);
self
}
}
/// A collection of sprites from a single [`ShareableTexture`].
#[derive(Debug, Clone)]
pub struct SpriteSheet<T>
where
T: Debug,
{
/// The source texture.
pub texture: ShareableTexture,
data: Arc<SpriteSheetData<T>>,
}
#[derive(Debug)]
struct SpriteSheetData<T>
where
T: Debug,
{
tile_size: Size<UPx>,
sprites: HashMap<T, Rect<UPx>>,
}
impl<T> SpriteSheet<T>
where
T: Debug + Eq + Hash,
{
/// Creates a new sprite sheet, diving `texture` into a grid of `tile_size`
/// with `gutter_size` spacing between each row and column. The order of
/// `tiles` will be read left-to-right, top-to-bottom.
#[must_use]
pub fn new(
texture: impl Into<ShareableTexture>,
tile_size: Size<UPx>,
gutter_size: Size<UPx>,
tiles: Vec<T>,
) -> Self {
let texture = texture.into();
let dimensions = texture.size() / tile_size;
Self {
texture,
data: Arc::new(SpriteSheetData::from_tiles(
tiles,
tile_size,
gutter_size,
dimensions,
)),
}
}
/// Returns the size of the tiles within this sheet.
#[must_use]
pub fn tile_size(&self) -> Size<UPx> {
self.data.tile_size
}
/// Returns the sprites identified by each element in `iterator`.
///
/// # Panics
///
/// Panics if a tile isn't found.
#[must_use]
pub fn sprites<I: IntoIterator<Item = T>>(&self, iterator: I) -> Vec<SpriteSource> {
iterator
.into_iter()
.map(|tile| {
let location = self.data.sprites.get(&tile).unwrap();
SpriteSource::Region(TextureRegion {
region: *location,
texture: self.texture.clone(),
})
})
.collect()
}
/// Returns the sprites identified by each element in `iterator` into a
/// [`SpriteMap`].
///
/// # Panics
///
/// This function panics if any `T` cannot be found in `self`.
#[must_use]
pub fn sprite_map<I: IntoIterator<Item = T>>(&self, iterator: I) -> SpriteMap<T> {
let map = iterator
.into_iter()
.map(|tile| {
let location = self.data.sprites.get(&tile).expect("missing sprite");
(
tile,
SpriteSource::Region(TextureRegion {
region: *location,
texture: self.texture.clone(),
}),
)
})
.collect::<HashMap<_, _>>();
SpriteMap::new(map)
}
}
impl<T: Debug + Eq + Hash> SpriteSheetData<T> {
fn from_tiles(
tiles: Vec<T>,
tile_size: Size<UPx>,
gutters: Size<UPx>,
dimensions: Size<UPx>,
) -> Self {
let mut sprites = HashMap::new();
let full_size = tile_size + gutters;
for (index, tile) in tiles.into_iter().enumerate() {
let index = UPx::new(index.cast::<u32>());
let y = index / dimensions.width;
let x = index - y * dimensions.width;
sprites.insert(
tile,
Rect::new(
Point::new(x * full_size.width, y * full_size.height),
tile_size,
),
);
}
Self { tile_size, sprites }
}
}
impl<T> SpriteSheet<T>
where
T: Clone + Debug + Eq + Hash,
{
/// Returns a collection of all tiles in the sheet as
#[must_use]
pub fn to_sprite_map(&self) -> SpriteMap<T> {
SpriteMap::new(
self.data
.sprites
.clone()
.iter()
.map(|(tile, location)| {
(
tile.clone(),
SpriteSource::Region(TextureRegion {
region: *location,
texture: self.texture.clone(),
}),
)
})
.collect(),
)
}
}
impl<T> SpriteCollection<T> for SpriteSheet<T>
where
T: Debug + Send + Sync + Eq + Hash,
{
fn sprite(&self, tile: &T) -> Option<SpriteSource> {
let location = self.data.sprites.get(tile);
location.map(|location| {
SpriteSource::Region(TextureRegion {
region: *location,
texture: self.texture.clone(),
})
})
}
}
/// A collection of [`SpriteSource`]s.
#[derive(Debug, Clone)]
pub struct SpriteMap<T> {
sprites: HashMap<T, SpriteSource>,
}
impl<T> Default for SpriteMap<T> {
fn default() -> Self {
Self {
sprites: HashMap::default(),
}
}
}
impl<T> SpriteMap<T>
where
T: Debug + Eq + Hash,
{
/// Creates a new collection with `sprites`.
#[must_use]
pub fn new(sprites: HashMap<T, SpriteSource>) -> Self {
Self { sprites }
}
/// Creates a collection from `sheet` using `converter` to convert from `O`
/// to `T`.
#[must_use]
pub fn from_foreign_sheet<O: Clone + Debug + Eq + Hash, F: Fn(O) -> T>(
sheet: &SpriteSheet<O>,
converter: F,
) -> Self {
let mut map = Self::default();
map.add_foreign_sheet(sheet, converter);
map
}
/// Adds a collection from `sheet` using `converter` to convert from `O` to
/// `T`.
pub fn add_foreign_sheet<O: Clone + Debug + Eq + Hash, F: Fn(O) -> T>(
&mut self,
sheet: &SpriteSheet<O>,
converter: F,
) {
for (tile, sprite) in sheet.to_sprite_map() {
self.sprites.insert(converter(tile), sprite);
}
}
}
impl<T> SpriteMap<T>
where
T: Clone + Debug + Eq + Hash,
{
/// Adds all sprites from `sheet`.
pub fn add_sheet(&mut self, sheet: &SpriteSheet<T>) {
self.add_foreign_sheet(sheet, |a| a);
}
}
impl<T> Deref for SpriteMap<T> {
type Target = HashMap<T, SpriteSource>;
fn deref(&self) -> &HashMap<T, SpriteSource> {
&self.sprites
}
}
impl<T> IntoIterator for SpriteMap<T> {
type IntoIter = hash_map::IntoIter<T, SpriteSource>;
type Item = (T, SpriteSource);
fn into_iter(self) -> Self::IntoIter {
self.sprites.into_iter()
}
}
/// A region of a texture that is used as frame in a sprite animation.
#[derive(Debug, Clone)]
pub enum SpriteSource {
/// The sprite's source is a [`TextureRegion`].
Region(TextureRegion),
/// The sprite's source is a [`CollectedTexture`].
Collected(CollectedTexture),
}
impl SpriteSource {
/// Returns a [`PreparedGraphic`] that renders this texture at `dest`.
pub fn prepare<Unit>(&self, dest: Rect<Unit>, graphics: &Graphics<'_>) -> PreparedGraphic<Unit>
where
Unit: figures::Unit + Div<i32, Output = Unit>,
Vertex<Unit>: bytemuck::Pod,
{
match self {
SpriteSource::Region(texture) => texture.prepare(dest, graphics),
SpriteSource::Collected(texture) => texture.prepare(dest, graphics),
}
}
}
impl CanRenderTo for SpriteSource {
fn can_render_to(&self, kludgine: &Kludgine) -> bool {
match self {
SpriteSource::Region(texture) => texture.can_render_to(kludgine),
SpriteSource::Collected(texture) => texture.can_render_to(kludgine),
}
}
}
impl TextureSource for SpriteSource {}
impl sealed::TextureSource for SpriteSource {
fn id(&self) -> crate::sealed::TextureId {
match self {
SpriteSource::Region(texture) => texture.id(),
SpriteSource::Collected(texture) => texture.id(),
}
}
fn is_mask(&self) -> bool {
match self {
SpriteSource::Region(texture) => texture.is_mask(),
SpriteSource::Collected(texture) => texture.is_mask(),
}
}
fn bind_group(&self, graphics: &impl crate::sealed::KludgineGraphics) -> Arc<wgpu::BindGroup> {
match self {
SpriteSource::Region(texture) => texture.bind_group(graphics),
SpriteSource::Collected(texture) => texture.bind_group(graphics),
}
}
fn default_rect(&self) -> Rect<UPx> {
match self {
SpriteSource::Region(texture) => texture.default_rect(),
SpriteSource::Collected(texture) => texture.default_rect(),
}
}
}
impl From<TextureRegion> for SpriteSource {
fn from(texture: TextureRegion) -> Self {
Self::Region(texture)
}
}
impl From<CollectedTexture> for SpriteSource {
fn from(texture: CollectedTexture) -> Self {
Self::Collected(texture)
}
}
/// A collection of sprites.
pub trait SpriteCollection<T>
where
T: Send + Sync,
{
/// Returns the sprite referred to by `tile`.
#[must_use]
fn sprite(&self, tile: &T) -> Option<SpriteSource>;
/// Returns all of the requested `tiles`.
///
/// # Panics
///
/// Panics if a tile is not found.
#[must_use]
fn sprites(&self, tiles: &[T]) -> Vec<SpriteSource> {
tiles
.iter()
.map(|t| self.sprite(t).unwrap())
.collect::<Vec<_>>()
}
}
impl<T> SpriteCollection<T> for SpriteMap<T>
where
T: Send + Sync + Eq + Hash,
{
fn sprite(&self, tile: &T) -> Option<SpriteSource> {
self.sprites.get(tile).cloned()
}
}