[libgdx] SkeletonViewer, reload if the atlas file changes.

This commit is contained in:
NathanSweet 2019-05-13 18:53:02 +02:00
parent 07cf3f1f2c
commit d5f5ff004f

View File

@ -111,7 +111,7 @@ public class SkeletonViewer extends ApplicationAdapter {
Skeleton skeleton; Skeleton skeleton;
AnimationState state; AnimationState state;
FileHandle skeletonFile; FileHandle skeletonFile;
long lastModified; long skeletonModified, atlasModified;
float lastModifiedCheck, reloadTimer; float lastModifiedCheck, reloadTimer;
StringBuilder status = new StringBuilder(); StringBuilder status = new StringBuilder();
Preferences prefs; Preferences prefs;
@ -144,17 +144,7 @@ public class SkeletonViewer extends ApplicationAdapter {
ui.prefsLoaded = true; ui.prefsLoaded = true;
} }
void loadSkeleton (final FileHandle skeletonFile) { FileHandle atlasFile (FileHandle skeletonFile) {
if (skeletonFile == null) return;
try {
// Setup a texture atlas that uses a white image for images not found in the atlas.
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(new Color(1, 1, 1, 0.33f));
pixmap.fill();
final AtlasRegion fake = new AtlasRegion(new Texture(pixmap), 0, 0, 32, 32);
pixmap.dispose();
String atlasFileName = skeletonFile.nameWithoutExtension(); String atlasFileName = skeletonFile.nameWithoutExtension();
if (atlasFileName.endsWith(".json") || atlasFileName.endsWith(".skel")) if (atlasFileName.endsWith(".json") || atlasFileName.endsWith(".skel"))
atlasFileName = atlasFileName.substring(0, atlasFileName.length() - 5); atlasFileName = atlasFileName.substring(0, atlasFileName.length() - 5);
@ -167,12 +157,27 @@ public class SkeletonViewer extends ApplicationAdapter {
if (atlasFile.exists()) break; if (atlasFile.exists()) break;
} }
} }
TextureAtlasData data = !atlasFile.exists() ? null : new TextureAtlasData(atlasFile, atlasFile.parent(), false); return atlasFile;
}
if (data != null) { void loadSkeleton (final FileHandle skeletonFile) {
if (skeletonFile == null) return;
FileHandle atlasFile = atlasFile(skeletonFile);
try {
// Setup a texture atlas that uses a white image for images not found in the atlas.
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(new Color(1, 1, 1, 0.33f));
pixmap.fill();
final AtlasRegion fake = new AtlasRegion(new Texture(pixmap), 0, 0, 32, 32);
pixmap.dispose();
TextureAtlasData atlasData = null;
if (atlasFile.exists()) {
atlasData = new TextureAtlasData(atlasFile, atlasFile.parent(), false);
boolean linear = true; boolean linear = true;
for (int i = 0, n = data.getPages().size; i < n; i++) { for (int i = 0, n = atlasData.getPages().size; i < n; i++) {
Page page = data.getPages().get(i); Page page = atlasData.getPages().get(i);
if (page.minFilter != TextureFilter.Linear || page.magFilter != TextureFilter.Linear) { if (page.minFilter != TextureFilter.Linear || page.magFilter != TextureFilter.Linear) {
linear = false; linear = false;
break; break;
@ -181,7 +186,7 @@ public class SkeletonViewer extends ApplicationAdapter {
ui.linearCheckbox.setChecked(linear); ui.linearCheckbox.setChecked(linear);
} }
atlas = new TextureAtlas(data) { atlas = new TextureAtlas(atlasData) {
public AtlasRegion findRegion (String name) { public AtlasRegion findRegion (String name) {
AtlasRegion region = super.findRegion(name); AtlasRegion region = super.findRegion(name);
if (region == null) { if (region == null) {
@ -232,10 +237,11 @@ public class SkeletonViewer extends ApplicationAdapter {
}); });
this.skeletonFile = skeletonFile; this.skeletonFile = skeletonFile;
skeletonModified = skeletonFile.lastModified();
atlasModified = atlasFile.lastModified();
lastModifiedCheck = checkModifiedInterval;
prefs.putString("lastFile", skeletonFile.path()); prefs.putString("lastFile", skeletonFile.path());
prefs.flush(); prefs.flush();
lastModified = skeletonFile.lastModified();
lastModifiedCheck = checkModifiedInterval;
// Populate UI. // Populate UI.
@ -306,7 +312,9 @@ public class SkeletonViewer extends ApplicationAdapter {
if (lastModifiedCheck < 0) { if (lastModifiedCheck < 0) {
lastModifiedCheck = checkModifiedInterval; lastModifiedCheck = checkModifiedInterval;
long time = skeletonFile.lastModified(); long time = skeletonFile.lastModified();
if (time != 0 && lastModified != time) reloadTimer = reloadDelay; if (time != 0 && skeletonModified != time) reloadTimer = reloadDelay;
time = atlasFile(skeletonFile).lastModified();
if (time != 0 && atlasModified != time) reloadTimer = reloadDelay;
} }
} else { } else {
reloadTimer -= delta; reloadTimer -= delta;