Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Add autosave (closes #64)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislo27 committed Feb 17, 2017
1 parent 002fbef commit 70ff6b9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 35 deletions.
1 change: 1 addition & 0 deletions core/assets/localization/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Click a pattern to split it into its separate parts (you cannot undo this action
editor.stretchStatus1=Current length:
editor.stretchStatus2=Original length:
editor.inspectionStatus={0} warning(s) found\nTook {1} ms
editor.autosaved=Autosaved.

editor.button.paletteSwap=Swap Palette
editor.button.music=Music Select
Expand Down
5 changes: 5 additions & 0 deletions core/src/chrislo27/rhre/SaveLoadScreens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class SaveScreen(m: Main) : Updateable<Main>(m) {
val json: String = gsonBuilder.create().toJson(obj)

handle.writeString(json, false, "UTF-8")

es.editor.file = handle
}
}

Expand Down Expand Up @@ -238,6 +240,7 @@ class LoadScreen(m: Main) : Updateable<Main>(m) {
if (remixObj != null) {
val es = ScreenRegistry.get("editor", EditorScreen::class.java)
es.editor.remix = Remix.readFromObject(remixObj!!)
es.editor.file = remixObj?.fileHandle

main.screen = ScreenRegistry.get("editor")
}
Expand Down Expand Up @@ -267,6 +270,7 @@ class LoadScreen(m: Main) : Updateable<Main>(m) {

val obj: RemixObject = gson.fromJson(handle.readString("UTF-8"), RemixObject::class.java)

obj.fileHandle = handle
remixObj = obj

missingContent = obj.entities.filter { entity ->
Expand Down Expand Up @@ -360,6 +364,7 @@ class NewScreen(m: Main) : Updateable<Main>(m) {
val es = ScreenRegistry.get("editor", EditorScreen::class.java)

es.editor.remix = Remix()
es.editor.file = null

main.screen = ScreenRegistry.get("editor")
}
Expand Down
101 changes: 66 additions & 35 deletions core/src/chrislo27/rhre/editor/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Cursor;
import com.badlogic.gdx.graphics.GL20;
Expand All @@ -31,6 +32,7 @@
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Disposable;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import ionium.registry.AssetRegistry;
import ionium.util.DebugSetting;
Expand All @@ -39,6 +41,7 @@
import ionium.util.i18n.Localization;
import ionium.util.render.StencilMaskUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -61,6 +64,7 @@ public class Editor extends InputAdapter implements Disposable {
private static final int PATTERNS_ABOVE_BELOW = 2;
private static final float STRETCHABLE_AREA = 16f / Entity.PX_WIDTH;
private static final int MAX_SEMITONE = Semitones.SEMITONES_IN_OCTAVE * 2;
private static final float AUTOSAVE_PERIOD = 60f;

private final Main main;
private final OrthographicCamera camera = new OrthographicCamera();
Expand All @@ -72,6 +76,7 @@ public class Editor extends InputAdapter implements Disposable {
public String status;
public Tool currentTool = Tool.NORMAL;
public Remix remix;
public FileHandle file = null;
float snappingInterval;
private Map<Series, Scroll> scrolls = new HashMap<>();
private Series currentSeries = Series.TENGOKU;
Expand All @@ -87,6 +92,8 @@ public class Editor extends InputAdapter implements Disposable {
private boolean isCursorStretching = false;
private int isStretching = 0;
private TempoChange selectedTempoChange;
private float timeUntilAutosave = AUTOSAVE_PERIOD;
private float autosaveMessageShow = 0f;

public Editor(Main m) {
this.main = m;
Expand Down Expand Up @@ -203,18 +210,18 @@ public void render(SpriteBatch batch) {
batch.setColor(1, 1, 1, 1);

main.getFontBordered().setColor(main.getPalette().getMusicStartTracker());
main.getFontBordered().draw(batch,
Localization.get("editor.musicStartTracker", String.format("%.3f", musicToBeats)),
musicToBeats * Entity.PX_WIDTH + 4, Entity.PX_HEIGHT * (TRACK_COUNT + 3));
main.getFontBordered()
.draw(batch, Localization.get("editor.musicStartTracker", String.format("%.3f", musicToBeats)),
musicToBeats * Entity.PX_WIDTH + 4, Entity.PX_HEIGHT * (TRACK_COUNT + 3));
main.getFontBordered().getData().setScale(0.5f);
main.getFontBordered().draw(batch, Localization.get("editor.beatTrackerSec",
String.format("%1$02d:%2$02.3f", (int) (Math.abs(remix.getMusicStartTime()) / 60),
Math.abs(remix.getMusicStartTime()) % 60)), musicToBeats * Entity.PX_WIDTH + 4,
Entity.PX_HEIGHT * (TRACK_COUNT + 3) + main.getFontBordered().getLineHeight());
main.getFontBordered().draw(batch, Localization.get("editor.musicTrackerHint"),
musicToBeats * Entity.PX_WIDTH - 4,
Entity.PX_HEIGHT * (TRACK_COUNT + 3) - main.getFontBordered().getCapHeight(),
0, Align.right, false);
main.getFontBordered()
.draw(batch, Localization.get("editor.musicTrackerHint"), musicToBeats * Entity.PX_WIDTH - 4,
Entity.PX_HEIGHT * (TRACK_COUNT + 3) - main.getFontBordered().getCapHeight(), 0,
Align.right, false);
main.getFontBordered().getData().setScale(1);
main.getFontBordered().setColor(1, 1, 1, 1);
}
Expand All @@ -234,8 +241,8 @@ public void render(SpriteBatch batch) {
main.getFontBordered().getData().setScale(0.5f);
main.getFontBordered().draw(batch, Localization.get("editor.playbackTrackerHint"),
remix.getPlaybackStart() * Entity.PX_WIDTH - 4,
Entity.PX_HEIGHT * (TRACK_COUNT + 2) - main.getFontBordered().getLineHeight() * 1.25f,
0, Align.right, false);
Entity.PX_HEIGHT * (TRACK_COUNT + 2) - main.getFontBordered().getLineHeight() * 1.25f, 0,
Align.right, false);
main.getFontBordered().getData().setScale(1);
main.getFontBordered().setColor(1, 1, 1, 1);
}
Expand Down Expand Up @@ -265,11 +272,9 @@ public void render(SpriteBatch batch) {
if (isSelected) {
main.getFontBordered().setColor(main.getPalette().getBpmTracker());
main.getFontBordered().getData().setScale(0.5f);
main.getFontBordered()
.draw(batch, Localization.get("editor.bpmTrackerHint"),
tc.getBeat() * Entity.PX_WIDTH - 4,
-Entity.PX_HEIGHT + main.getFontBordered().getLineHeight() * 2,
0, Align.right, false);
main.getFontBordered().draw(batch, Localization.get("editor.bpmTrackerHint"),
tc.getBeat() * Entity.PX_WIDTH - 4,
-Entity.PX_HEIGHT + main.getFontBordered().getLineHeight() * 2, 0, Align.right, false);
main.getFontBordered().getData().setScale(1);
}
}
Expand Down Expand Up @@ -475,6 +480,17 @@ public void render(SpriteBatch batch) {
main.getFont().getData().setScale(0.5f);
}

if (remix.getPlayingState() == PlayingState.STOPPED) {
if (autosaveMessageShow > 0) {
Color c = Main.getRainbow(1.0f);
main.getFontBordered().setColor(c.r, c.g, c.b, Math.min(autosaveMessageShow, 1f));
main.getFontBordered().draw(batch, Localization.get("editor.autosaved"), Gdx.graphics.getWidth() *
0.5f,
Gdx.graphics.getHeight() * 0.5f - main.getFontBordered().getCapHeight() * 0.5f, 0, Align.center, false);
main.getFontBordered().setColor(1, 1, 1, 1);
}
}

// tool icons
{
batch.setColor(0, 0, 0, 0.5f);
Expand Down Expand Up @@ -667,33 +683,47 @@ public void render(SpriteBatch batch) {
}
}

// // FIXME renderer
// if (DebugSetting.debug && remix.getCurrentGame() != null) {
// rendererCamera.update();
// batch.setProjectionMatrix(rendererCamera.combined);
//
// Renderer renderer = VisualRegistry.INSTANCE.getMap().get(remix.getCurrentGame().getId());
// Gdx.gl.glClearColor(0, 0, 0, 1);
// Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// if (renderer != null) {
// renderer.render(batch, remix);
// }
//
// batch.setProjectionMatrix(main.camera.combined);
// }

batch.end();
}

public void renderUpdate() {
remix.update(Gdx.graphics.getDeltaTime());

if (autosaveMessageShow > 0)
autosaveMessageShow = Math.max(0f, autosaveMessageShow - Gdx.graphics.getDeltaTime());

if (remix.getPlayingState() == PlayingState.PLAYING) {
if (camera.position.x + camera.viewportWidth * 0.5f < remix.getBeat() * Entity.PX_WIDTH) {
camera.position.x = remix.getBeat() * Entity.PX_WIDTH + camera.viewportWidth * 0.5f;
} else if (remix.getBeat() * Entity.PX_WIDTH < camera.position.x - camera.viewportWidth * 0.5f) {
camera.position.x = remix.getBeat() * Entity.PX_WIDTH + camera.viewportWidth * 0.25f;
}
} else if (remix.getPlayingState() == PlayingState.STOPPED && file != null) {
timeUntilAutosave -= Gdx.graphics.getDeltaTime();

if (timeUntilAutosave <= 0) {
timeUntilAutosave = AUTOSAVE_PERIOD;
autosave();
}
}
}

private void autosave() {
if (file != null) {
FileHandle sibling = file.sibling(file.nameWithoutExtension() + ".autosave." + file.extension());

try {
sibling.file().createNewFile();

sibling.writeString(new Gson().toJson(Remix.Companion.writeToObject(remix)), false, "UTF-8");

autosaveMessageShow = 3f;
} catch (IOException e) {
e.printStackTrace();
} finally {
System.gc();
}

}
}

Expand Down Expand Up @@ -799,18 +829,19 @@ public void inputUpdate() {
Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT) || Gdx.input.isKeyPressed(Input.Keys
.SHIFT_RIGHT);

if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)
&& !(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT) ||
Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT))) {
if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT) &&
!(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT) ||
Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT))) {
remix.setPlaybackStart(vec3Tmp2.x);
if (!shift) {
remix.setPlaybackStart(MathHelper.snapToNearest(remix.getPlaybackStart(), snappingInterval));
}
}

if (Gdx.input.isButtonPressed(Input.Buttons.MIDDLE) || Gdx.input.isButtonPressed(Input.Buttons.RIGHT)
&& (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT) ||
Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT))) {
if (Gdx.input.isButtonPressed(Input.Buttons.MIDDLE) || Gdx.input.isButtonPressed(Input.Buttons
.RIGHT) &&
(Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT) ||
Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT))) {
remix.setMusicStartTime(remix.getTempoChanges().beatsToSeconds(vec3Tmp2.x));
if (!shift) {
remix.setMusicStartTime(remix.getTempoChanges()
Expand Down
4 changes: 4 additions & 0 deletions core/src/chrislo27/rhre/json/persistent/RemixObject.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package chrislo27.rhre.json.persistent;

import com.badlogic.gdx.files.FileHandle;

import java.util.List;

public class RemixObject {

public transient FileHandle fileHandle;

public String version;
public List<EntityObject> entities;

Expand Down

0 comments on commit 70ff6b9

Please sign in to comment.