PolyScreenSprite.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyScreenShape.h"
  22. #include <vector>
  23. namespace Polycode {
  24. class ScreenSpriteResourceEntry;
  25. class _PolyExport SpriteAnimation {
  26. public:
  27. Number speed;
  28. String name;
  29. String frames;
  30. int numFrames;
  31. int numFramesX;
  32. int numFramesY;
  33. Number spriteUVWidth;
  34. Number spriteUVHeight;
  35. void setOffsetsFromFrameString(const String& frames);
  36. std::vector<Vector2> framesOffsets;
  37. };
  38. /**
  39. * Animated 2D image sprite. This screen entity can load spritesheet images and play back animations.
  40. */
  41. class _PolyExport ScreenSprite : public ScreenShape
  42. {
  43. public:
  44. /**
  45. * Create a sprite from a sprite file format
  46. * @param fileName Sprite file to load
  47. */
  48. ScreenSprite(const String& fileName);
  49. /**
  50. * Create a sprite from a spritesheet image of specified size.
  51. * @param fileName Image file to load spritesheet from.
  52. * @param spriteWidth Pixel width of each sprite cell.
  53. * @param spriteWidth Pixel height of each sprite cell.
  54. */
  55. ScreenSprite(const String& fileName, Number spriteWidth, Number spriteHeight);
  56. /**
  57. * Create a sprite from a spritesheet image of specified size.
  58. * @param fileName Image file to load spritesheet from.
  59. * @param spriteWidth Pixel width of each sprite cell.
  60. * @param spriteWidth Pixel height of each sprite cell.
  61. */
  62. static ScreenSprite* ScreenSpriteFromImageFile(const String& fileName, Number spriteWidth, Number spriteHeight);
  63. virtual ~ScreenSprite();
  64. virtual Entity *Clone(bool deepClone, bool ignoreEditorOnly) const;
  65. virtual void applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const;
  66. /**
  67. * Adds a new animation to the sprite. Animations are added by specifying a list of frame indexes and then can be played back by the specified name.
  68. * @param name Name of the new animation.
  69. * @param frames A comma separated list of frames indexes to include in the animation.
  70. * @speed Speed at which to play back the animation.
  71. * @return Returns newly added animation
  72. */
  73. SpriteAnimation *addAnimation(const String& name, const String& frames, Number speed);
  74. /**
  75. * Shows a specific frame of the current animation.
  76. * @param frameIndex Frame index of the frame to show.
  77. */
  78. void showFrame(unsigned int frameIndex);
  79. /**
  80. * Play back a previously created animation by name.
  81. * @param name Name of the animation to play.
  82. * @param startFrame Starting frame for playback.
  83. * @param once If true, only plays once, otherwise loops.
  84. */
  85. void playAnimation(const String& name, int startFrame, bool once);
  86. void Update();
  87. void setSpriteSize(const Number spriteWidth, const Number spriteHeight);
  88. Vector2 getSpriteSize();
  89. String getFileName() const;
  90. void recalculateSpriteDimensions();
  91. bool loadFromFile(const String& fileName);
  92. void reloadSprite();
  93. /**
  94. * Pauses or unpauses the current sprite animation.
  95. * @param val If true, pauses the current animation, if false, resumes playing it.
  96. */
  97. void Pause(bool val);
  98. unsigned int getNumAnimations();
  99. SpriteAnimation *getAnimationAtIndex(unsigned int index);
  100. SpriteAnimation *getCurrentAnimation();
  101. unsigned int getCurrentAnimationFrame();
  102. bool isCurrentAnimationFinished();
  103. void updateSprite();
  104. ScreenSpriteResourceEntry *getResourceEntry();
  105. protected:
  106. String fileName;
  107. bool paused;
  108. Number spriteWidth;
  109. Number spriteHeight;
  110. bool playingOnce;
  111. Number lastTick;
  112. ScreenSpriteResourceEntry *resourceEntry;
  113. Number spriteUVWidth;
  114. Number spriteUVHeight;
  115. int currentFrame;
  116. SpriteAnimation *currentAnimation;
  117. std::vector<SpriteAnimation*> animations;
  118. };
  119. class ScreenSpriteResourceEntry : public Resource {
  120. public:
  121. ScreenSpriteResourceEntry(ScreenSprite *sprite);
  122. virtual ~ScreenSpriteResourceEntry();
  123. ScreenSprite *getSprite();
  124. void reloadResource();
  125. protected:
  126. ScreenSprite* sprite;
  127. };
  128. }