mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
Better reloading.
This commit is contained in:
parent
1ea3a3d9e2
commit
27ec8d12ac
@ -72,6 +72,9 @@ import java.awt.Frame;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public class SkeletonTest extends ApplicationAdapter {
|
public class SkeletonTest extends ApplicationAdapter {
|
||||||
|
static final float checkModifiedInterval = 0.250f;
|
||||||
|
static final float reloadDelay = 1;
|
||||||
|
|
||||||
UI ui;
|
UI ui;
|
||||||
|
|
||||||
PolygonSpriteBatch batch;
|
PolygonSpriteBatch batch;
|
||||||
@ -83,7 +86,7 @@ public class SkeletonTest extends ApplicationAdapter {
|
|||||||
int skeletonX, skeletonY;
|
int skeletonX, skeletonY;
|
||||||
FileHandle skeletonFile;
|
FileHandle skeletonFile;
|
||||||
long lastModified;
|
long lastModified;
|
||||||
float lastModifiedCheck;
|
float lastModifiedCheck, reloadTimer;
|
||||||
|
|
||||||
public void create () {
|
public void create () {
|
||||||
ui = new UI();
|
ui = new UI();
|
||||||
@ -93,11 +96,11 @@ public class SkeletonTest extends ApplicationAdapter {
|
|||||||
skeletonX = (int)(ui.window.getWidth() + (Gdx.graphics.getWidth() - ui.window.getWidth()) / 2);
|
skeletonX = (int)(ui.window.getWidth() + (Gdx.graphics.getWidth() - ui.window.getWidth()) / 2);
|
||||||
skeletonY = Gdx.graphics.getHeight() / 4;
|
skeletonY = Gdx.graphics.getHeight() / 4;
|
||||||
|
|
||||||
loadSkeleton(Gdx.files.internal(Gdx.app.getPreferences("spine-skeletontest")
|
loadSkeleton(
|
||||||
.getString("lastFile", "spineboy/spineboy.json")));
|
Gdx.files.internal(Gdx.app.getPreferences("spine-skeletontest").getString("lastFile", "spineboy/spineboy.json")), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadSkeleton (FileHandle skeletonFile) {
|
void loadSkeleton (FileHandle skeletonFile, boolean reload) {
|
||||||
if (skeletonFile == null) return;
|
if (skeletonFile == null) return;
|
||||||
|
|
||||||
// A regular texture atlas would normally usually be used. This returns a white image for images not found in the atlas.
|
// A regular texture atlas would normally usually be used. This returns a white image for images not found in the atlas.
|
||||||
@ -132,7 +135,7 @@ public class SkeletonTest extends ApplicationAdapter {
|
|||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
ui.toast("Error loading skeleton: spineboy");
|
ui.toast("Error loading skeleton: " + skeletonFile.name());
|
||||||
lastModifiedCheck = 5;
|
lastModifiedCheck = 5;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -149,7 +152,7 @@ public class SkeletonTest extends ApplicationAdapter {
|
|||||||
prefs.putString("lastFile", skeletonFile.path());
|
prefs.putString("lastFile", skeletonFile.path());
|
||||||
prefs.flush();
|
prefs.flush();
|
||||||
lastModified = skeletonFile.lastModified();
|
lastModified = skeletonFile.lastModified();
|
||||||
lastModifiedCheck = 0.250f;
|
lastModifiedCheck = checkModifiedInterval;
|
||||||
|
|
||||||
// Populate UI.
|
// Populate UI.
|
||||||
|
|
||||||
@ -171,29 +174,45 @@ public class SkeletonTest extends ApplicationAdapter {
|
|||||||
|
|
||||||
skeleton.setSkin(ui.skinList.getSelected());
|
skeleton.setSkin(ui.skinList.getSelected());
|
||||||
state.setAnimation(0, ui.animationList.getSelected(), ui.loopCheckbox.isChecked());
|
state.setAnimation(0, ui.animationList.getSelected(), ui.loopCheckbox.isChecked());
|
||||||
|
|
||||||
|
if (reload) ui.toast("Reloaded.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render () {
|
public void render () {
|
||||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
float delta = Gdx.graphics.getDeltaTime();
|
||||||
|
|
||||||
if (skeleton != null) {
|
if (skeleton != null) {
|
||||||
lastModifiedCheck -= Gdx.graphics.getDeltaTime();
|
if (reloadTimer <= 0) {
|
||||||
if (lastModifiedCheck < 0 && lastModified != skeletonFile.lastModified()) loadSkeleton(skeletonFile);
|
lastModifiedCheck -= delta;
|
||||||
|
if (lastModifiedCheck < 0) {
|
||||||
|
lastModifiedCheck = checkModifiedInterval;
|
||||||
|
if (lastModified != skeletonFile.lastModified()) reloadTimer = reloadDelay;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reloadTimer -= delta;
|
||||||
|
if (reloadTimer <= 0) loadSkeleton(skeletonFile, true);
|
||||||
|
}
|
||||||
|
|
||||||
state.getData().setDefaultMix(ui.mixSlider.getValue());
|
state.getData().setDefaultMix(ui.mixSlider.getValue());
|
||||||
renderer.setPremultipliedAlpha(ui.premultipliedCheckbox.isChecked());
|
renderer.setPremultipliedAlpha(ui.premultipliedCheckbox.isChecked());
|
||||||
|
|
||||||
float delta = Math.min(Gdx.graphics.getDeltaTime(), 0.032f) * ui.speedSlider.getValue();
|
delta = Math.min(delta, 0.032f) * ui.speedSlider.getValue();
|
||||||
skeleton.update(delta);
|
skeleton.update(delta);
|
||||||
if (!ui.pauseButton.isChecked()) {
|
if (!ui.pauseButton.isChecked()) {
|
||||||
state.update(delta);
|
state.update(delta);
|
||||||
state.apply(skeleton);
|
state.apply(skeleton);
|
||||||
}
|
}
|
||||||
skeleton.updateWorldTransform();
|
|
||||||
skeleton.setX(skeletonX);
|
skeleton.setX(skeletonX);
|
||||||
skeleton.setY(skeletonY);
|
skeleton.setY(skeletonY);
|
||||||
|
// skeleton.setX(0);
|
||||||
|
// skeleton.setY(0);
|
||||||
|
// skeleton.getRootBone().setX(skeletonX);
|
||||||
|
// skeleton.getRootBone().setY(skeletonY);
|
||||||
skeleton.setFlipX(ui.flipXCheckbox.isChecked());
|
skeleton.setFlipX(ui.flipXCheckbox.isChecked());
|
||||||
skeleton.setFlipY(ui.flipYCheckbox.isChecked());
|
skeleton.setFlipY(ui.flipYCheckbox.isChecked());
|
||||||
|
skeleton.updateWorldTransform();
|
||||||
|
|
||||||
batch.begin();
|
batch.begin();
|
||||||
renderer.draw(batch, skeleton);
|
renderer.draw(batch, skeleton);
|
||||||
@ -355,7 +374,7 @@ public class SkeletonTest extends ApplicationAdapter {
|
|||||||
String name = fileDialog.getFile();
|
String name = fileDialog.getFile();
|
||||||
String dir = fileDialog.getDirectory();
|
String dir = fileDialog.getDirectory();
|
||||||
if (name == null || dir == null) return;
|
if (name == null || dir == null) return;
|
||||||
loadSkeleton(new FileHandle(new File(dir, name).getAbsolutePath()));
|
loadSkeleton(new FileHandle(new File(dir, name).getAbsolutePath()), false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -397,7 +416,7 @@ public class SkeletonTest extends ApplicationAdapter {
|
|||||||
scaleSlider.addListener(new ChangeListener() {
|
scaleSlider.addListener(new ChangeListener() {
|
||||||
public void changed (ChangeEvent event, Actor actor) {
|
public void changed (ChangeEvent event, Actor actor) {
|
||||||
scaleLabel.setText(Float.toString((int)(scaleSlider.getValue() * 100) / 100f));
|
scaleLabel.setText(Float.toString((int)(scaleSlider.getValue() * 100) / 100f));
|
||||||
if (!scaleSlider.isDragging()) loadSkeleton(skeletonFile);
|
if (!scaleSlider.isDragging()) loadSkeleton(skeletonFile, false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user