Add History object to handle history

This commit is contained in:
David Rowe 2017-09-12 20:53:15 +12:00
parent afb47dcfb2
commit 68c4a2f7f6
3 changed files with 123 additions and 0 deletions

View file

@ -23,6 +23,8 @@ Feedback = (function () {
CREATE_SOUND = SoundCache.getSound(Script.resolvePath("../assets/audio/create.wav")),
EQUIP_SOUND = SoundCache.getSound(Script.resolvePath("../assets/audio/equip.wav")),
ERROR_SOUND = SoundCache.getSound(Script.resolvePath("../assets/audio/error.wav")),
UNDO_SOUND = DROP_SOUND, // TODO
REDO_SOUND = DROP_SOUND, // TODO
FEEDBACK_PARAMETERS = {
DROP_TOOL: { sound: DROP_SOUND, volume: 0.3, haptic: 0.75 },
@ -35,6 +37,8 @@ Feedback = (function () {
EQUIP_TOOL: { sound: EQUIP_SOUND, volume: 0.3, haptic: 0.6 },
APPLY_PROPERTY: { sound: null, volume: 0, haptic: 0.3 },
APPLY_ERROR: { sound: ERROR_SOUND, volume: 0.2, haptic: 0.7 },
UNDO_ACTION: { sound: UNDO_SOUND, volume: 0.2, haptic: 0.2 }, // TODO
REDO_ACTION: { sound: REDO_SOUND, volume: 0.2, haptic: 0.2 }, // TODO
GENERAL_ERROR: { sound: ERROR_SOUND, volume: 0.2, haptic: 0.7 }
},
@ -68,6 +72,8 @@ Feedback = (function () {
EQUIP_TOOL: "EQUIP_TOOL",
APPLY_PROPERTY: "APPLY_PROPERTY",
APPLY_ERROR: "APPLY_ERROR",
UNDO_ACTION: "UNDO_ACTION",
REDO_ACTION: "REDO_ACTION",
GENERAL_ERROR: "GENERAL_ERROR",
play: play
};

View file

@ -0,0 +1,99 @@
//
// history.js
//
// Created by David Rowe on 12 Sep 2017.
// Copyright 2017 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
/* global History */
History = (function () {
// Provides undo facility.
// Global object.
"use strict";
var history = [
/*
{
undoData: {
setProperties: [
{
entityID: <entity ID>,
properties: { <name>: <value>, <name>: <value>, ... }
}
],
createEntities: [
{
entityID: <entity ID>,
properties: { <name>: <value>, <name>: <value>, ... }
}
],
deleteEntities: [
{
entityID: <entity ID>,
properties: { <name>: <value>, <name>: <value>, ... }
}
]
},
redoData: {
""
}
}
*/
],
MAX_HISTORY_ITEMS = 100,
undoPosition = -1; // The next history item to undo; the next history item to redo = undoIndex + 1.
function push(undoData, redoData) {
// Wipe any redo history after current undo position.
if (undoPosition < history.length - 1) {
history.splice(undoPosition + 1, history.length - undoPosition - 1);
}
// Limit the number of history items.
if (history.length >= MAX_HISTORY_ITEMS) {
history.splice(0, history.length - MAX_HISTORY_ITEMS + 1);
}
history.push({ undoData: undoData, redoData: redoData });
undoPosition += 1;
}
function hasUndo() {
return undoPosition > -1;
}
function hasRedo() {
return undoPosition < history.length - 1;
}
function undo() {
if (undoPosition > -1) {
// TODO
undoPosition -= 1;
}
}
function redo() {
if (undoPosition < history.length - 1) {
// TODO
undoPosition += 1;
}
}
return {
push: push,
hasUndo: hasUndo,
hasRedo: hasRedo,
undo: undo,
redo: redo
};
}());

View file

@ -83,6 +83,7 @@
Script.include("./modules/hand.js");
Script.include("./modules/handles.js");
Script.include("./modules/highlights.js");
Script.include("./modules/history.js");
Script.include("./modules/laser.js");
Script.include("./modules/selection.js");
Script.include("./modules/toolIcon.js");
@ -1535,6 +1536,23 @@
}
break;
case "undoAction":
if (History.hasUndo()) {
Feedback.play(dominantHand, Feedback.UNDO_ACTION)
History.undo();
} else {
Feedback.play(dominantHand, Feedback.GENERAL_ERROR);
}
break;
case "redoAction":
if (History.hasRedo()) {
Feedback.play(dominantHand, Feedback.REDO_ACTION)
History.redo();
} else {
Feedback.play(dominantHand, Feedback.GENERAL_ERROR);
}
break;
default:
log("ERROR: Unexpected command in onUICommand(): " + command + ", " + parameter);
}