diff --git a/scripts/vr-edit/modules/feedback.js b/scripts/vr-edit/modules/feedback.js index 23a82e66aa..e87a0867fa 100644 --- a/scripts/vr-edit/modules/feedback.js +++ b/scripts/vr-edit/modules/feedback.js @@ -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 }; diff --git a/scripts/vr-edit/modules/history.js b/scripts/vr-edit/modules/history.js new file mode 100644 index 0000000000..d1c03c218a --- /dev/null +++ b/scripts/vr-edit/modules/history.js @@ -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: , + properties: { : , : , ... } + } + ], + createEntities: [ + { + entityID: , + properties: { : , : , ... } + } + ], + deleteEntities: [ + { + entityID: , + properties: { : , : , ... } + } + ] + }, + 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 + }; +}()); diff --git a/scripts/vr-edit/vr-edit.js b/scripts/vr-edit/vr-edit.js index 38f72950f8..293e85a775 100644 --- a/scripts/vr-edit/vr-edit.js +++ b/scripts/vr-edit/vr-edit.js @@ -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); }