forked from gurkenlabs/litiengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from DD2480-Group-11/issue/12
Implemented import functionality for Aseprite animations
- Loading branch information
Showing
6 changed files
with
352 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 64 additions & 4 deletions
68
tests/de/gurkenlabs/litiengine/graphics/animation/AsepriteHandlerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,92 @@ | ||
package de.gurkenlabs.litiengine.graphics.animation; | ||
|
||
import java.io.IOException; | ||
import java.io.FileNotFoundException; | ||
import java.awt.image.BufferedImage; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import de.gurkenlabs.litiengine.graphics.Spritesheet; | ||
import de.gurkenlabs.litiengine.graphics.animation.Animation; | ||
import de.gurkenlabs.litiengine.graphics.animation.AsepriteHandler.ImportAnimationException; | ||
import de.gurkenlabs.litiengine.resources.ImageFormat; | ||
|
||
public class AsepriteHandlerTests { | ||
|
||
/** | ||
* Tests that Aseprite animation import works as expected when given valid input. | ||
*/ | ||
@Test | ||
public void importAsepriteAnimationTest() { | ||
try { | ||
Animation animation = AsepriteHandler.importAnimation("tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0001.json"); | ||
assertEquals("Sprite-0001-sheet", animation.getName()); | ||
assertEquals(300, animation.getTotalDuration()); | ||
for(int keyFrameDuration : animation.getKeyFrameDurations()) | ||
assertEquals(100, keyFrameDuration); | ||
|
||
Spritesheet spriteSheet = animation.getSpritesheet(); | ||
assertEquals(32, spriteSheet.getSpriteHeight()); | ||
assertEquals(32, spriteSheet.getSpriteWidth()); | ||
assertEquals(3, spriteSheet.getTotalNumberOfSprites()); | ||
assertEquals(1, spriteSheet.getRows()); | ||
assertEquals(3, spriteSheet.getColumns()); | ||
assertEquals(ImageFormat.PNG, spriteSheet.getImageFormat()); | ||
|
||
BufferedImage image = spriteSheet.getImage(); | ||
assertEquals(96, image.getWidth()); | ||
assertEquals(32, image.getHeight()); | ||
} | ||
catch(FileNotFoundException e) { | ||
fail(e.getMessage()); | ||
} | ||
catch(IOException e) { | ||
fail(e.getMessage()); | ||
} | ||
catch(AsepriteHandler.ImportAnimationException e) { | ||
fail(e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Test that if AsepriteHandler.ImportAnimationException will be throwed if different frame dimensions are provided. | ||
*/ | ||
@Test | ||
public void ImportAnimationExceptionTest() { | ||
|
||
Throwable exception = assertThrows(ImportAnimationException.class, () -> AsepriteHandler.importAnimation("tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0002.json")); | ||
assertEquals("AsepriteHandler.ImportAnimationException: animation key frames require same dimensions.", exception.getMessage()); | ||
} | ||
|
||
/** | ||
* Tests thrown FileNotFoundException when importing an Aseprite animation. | ||
* | ||
* 1.first, we test if FileNotFoundException would be throwed if .json file cannot be found. | ||
* 2.then we test if FileNotFoundException would be throwed if spritesheet file cannot be found. | ||
*/ | ||
@Test | ||
public void FileNotFoundExceptionTest(){ | ||
Throwable exception_withoutJsonFile = assertThrows(FileNotFoundException.class, () -> AsepriteHandler.importAnimation("tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0003.json")); | ||
assertEquals("FileNotFoundException: Could not find .json file tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0003.json", exception_withoutJsonFile.getMessage()); | ||
Throwable exception_withoutSpriteSheet = assertThrows(FileNotFoundException.class, () -> AsepriteHandler.importAnimation("tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0004.json")); | ||
assertEquals("FileNotFoundException: Could not find sprite sheet file. Expected location is 'image' in .json metadata, which evaluates to: tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0002-sheet.png", exception_withoutSpriteSheet.getMessage()); | ||
} | ||
|
||
/** | ||
* Test that just create a json and prints in to standard output. | ||
*/ | ||
@Test | ||
public void exportAnimationTest() { | ||
String spritesheetPath = "C:/Users/Nikla/Documents/Programmering/SoftwareFundamentals/Assignment-3-EC/litiengine/tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animation/Sprite-0001-sheet.png"; | ||
String spritesheetPath = "tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0001-sheet.png"; | ||
BufferedImage image = new BufferedImage(96, 32, BufferedImage.TYPE_4BYTE_ABGR); | ||
Spritesheet spritesheet = new Spritesheet(image, spritesheetPath, 32, 32); | ||
Animation animation = new Animation(spritesheet, false, false, 2,2,2); | ||
|
||
AsepriteHandler aseprite = new AsepriteHandler(); | ||
String result = aseprite.exportAnimation(animation); | ||
System.out.println(result); | ||
|
||
} | ||
} | ||
} |
File renamed without changes
40 changes: 40 additions & 0 deletions
40
tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0001.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ "frames": { | ||
"Sprite-0001 0.png": { | ||
"frame": { "x": 0, "y": 0, "w": 32, "h": 32 }, | ||
"rotated": false, | ||
"trimmed": false, | ||
"spriteSourceSize": { "x": 0, "y": 0, "w": 32, "h": 32 }, | ||
"sourceSize": { "w": 32, "h": 32 }, | ||
"duration": 100 | ||
}, | ||
"Sprite-0001 1.png": { | ||
"frame": { "x": 32, "y": 0, "w": 32, "h": 32 }, | ||
"rotated": false, | ||
"trimmed": false, | ||
"spriteSourceSize": { "x": 0, "y": 0, "w": 32, "h": 32 }, | ||
"sourceSize": { "w": 32, "h": 32 }, | ||
"duration": 100 | ||
}, | ||
"Sprite-0001 2.png": { | ||
"frame": { "x": 64, "y": 0, "w": 32, "h": 32 }, | ||
"rotated": false, | ||
"trimmed": false, | ||
"spriteSourceSize": { "x": 0, "y": 0, "w": 32, "h": 32 }, | ||
"sourceSize": { "w": 32, "h": 32 }, | ||
"duration": 100 | ||
} | ||
}, | ||
"meta": { | ||
"app": "http://www.aseprite.org/", | ||
"version": "1.1.9-dev", | ||
"image": "tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0001-sheet.png", | ||
"format": "RGBA8888", | ||
"size": { "w": 96, "h": 32 }, | ||
"scale": "1", | ||
"frameTags": [ | ||
], | ||
"layers": [ | ||
{ "name": "Layer 1", "opacity": 255, "blendMode": "normal" } | ||
] | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0002.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ "frames": { | ||
"Sprite-0002 0.png": { | ||
"frame": { "x": 0, "y": 0, "w": 32, "h": 32 }, | ||
"rotated": false, | ||
"trimmed": false, | ||
"spriteSourceSize": { "x": 0, "y": 0, "w": 32, "h": 32 }, | ||
"sourceSize": { "w": 32, "h": 32 }, | ||
"duration": 100 | ||
}, | ||
"Sprite-0002 1.png": { | ||
"frame": { "x": 32, "y": 0, "w": 32, "h": 32 }, | ||
"rotated": false, | ||
"trimmed": false, | ||
"spriteSourceSize": { "x": 0, "y": 0, "w": 32, "h": 32 }, | ||
"sourceSize": { "w": 64, "h": 64 }, | ||
"duration": 100 | ||
}, | ||
"Sprite-0002 2.png": { | ||
"frame": { "x": 64, "y": 0, "w": 32, "h": 32 }, | ||
"rotated": false, | ||
"trimmed": false, | ||
"spriteSourceSize": { "x": 0, "y": 0, "w": 32, "h": 32 }, | ||
"sourceSize": { "w": 32, "h": 32 }, | ||
"duration": 100 | ||
} | ||
}, | ||
"meta": { | ||
"app": "http://www.aseprite.org/", | ||
"version": "1.1.9-dev", | ||
"image": "tests/de/gurkenlabs/litiengine/graphics/animation/aseprite_test_animations/Sprite-0001-sheet.png", | ||
"format": "RGBA8888", | ||
"size": { "w": 96, "h": 32 }, | ||
"scale": "1", | ||
"frameTags": [ | ||
], | ||
"layers": [ | ||
{ "name": "Layer 1", "opacity": 255, "blendMode": "normal" } | ||
] | ||
} | ||
} | ||
|
Oops, something went wrong.