2015-04-02 12:38:06 +02:00
|
|
|
// class camera extends THREE.PerspectiveCamera
|
|
|
|
var PointerCamera = function() {
|
|
|
|
THREE.PerspectiveCamera.apply(this, arguments);
|
|
|
|
|
2015-04-11 17:18:07 +02:00
|
|
|
if (arguments[4] === undefined)
|
|
|
|
listenerTarget = document;
|
|
|
|
else
|
|
|
|
listenerTarget = arguments[4];
|
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
// Set Position
|
|
|
|
this.theta = Math.PI;
|
|
|
|
this.phi = Math.PI;
|
|
|
|
|
2015-04-07 11:33:33 +02:00
|
|
|
// this.keyboard = undefined;
|
2015-04-08 11:36:15 +02:00
|
|
|
this.moving = false;
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-04-07 11:13:19 +02:00
|
|
|
this.dragging = false;
|
|
|
|
this.mouse = {x: 0, y: 0};
|
2015-04-08 11:36:15 +02:00
|
|
|
this.mouseMove = {x: 0, y: 0};
|
2015-04-07 11:13:19 +02:00
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
|
|
|
|
// Stuff for rendering
|
|
|
|
this.position = new THREE.Vector3();
|
|
|
|
this.forward = new THREE.Vector3();
|
|
|
|
this.left = new THREE.Vector3();
|
|
|
|
this.target = new THREE.Vector3(0,1,0);
|
|
|
|
|
|
|
|
// Stuff for events
|
2015-05-19 14:25:35 +02:00
|
|
|
this.motion = {};
|
2015-04-02 12:38:06 +02:00
|
|
|
|
|
|
|
this.sensitivity = 0.05;
|
|
|
|
this.speed = 1;
|
|
|
|
|
2015-04-22 10:17:54 +02:00
|
|
|
// Raycaster for collisions
|
|
|
|
this.raycaster = new THREE.Raycaster();
|
|
|
|
|
2015-04-30 14:53:55 +02:00
|
|
|
// Create history object
|
|
|
|
this.history = new History();
|
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
// Set events from the document
|
|
|
|
var self = this;
|
|
|
|
var onKeyDown = function(event) {self.onKeyDown(event);};
|
|
|
|
var onKeyUp = function(event) {self.onKeyUp(event);};
|
2015-04-07 11:13:19 +02:00
|
|
|
var onMouseDown = function(event) {self.onMouseDown(event); };
|
|
|
|
var onMouseMove = function(event) {self.onMouseMove(event); };
|
|
|
|
var onMouseUp = function(event) {self.onMouseUp(event); };
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-04-28 09:43:24 +02:00
|
|
|
document.addEventListener('keydown', onKeyDown, false);
|
|
|
|
document.addEventListener('keyup', onKeyUp, false);
|
2015-04-23 10:40:02 +02:00
|
|
|
listenerTarget.addEventListener('mousedown', function(event) { if (event.which == 1) onMouseDown(event);}, false);
|
|
|
|
listenerTarget.addEventListener('mousemove', function(event) { if (event.which == 1) onMouseMove(event);}, false);
|
2015-04-11 17:18:07 +02:00
|
|
|
listenerTarget.addEventListener('mouseup', onMouseUp, false);
|
2015-04-24 17:15:00 +02:00
|
|
|
// listenerTarget.addEventListener('mouseup', function() { console.log("mouseup");}, false);
|
|
|
|
// listenerTarget.addEventListener('mouseout', onMouseUp, false);
|
2015-04-22 11:02:54 +02:00
|
|
|
|
|
|
|
this.collisions = true;
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
|
|
|
PointerCamera.prototype = Object.create(THREE.PerspectiveCamera.prototype);
|
|
|
|
PointerCamera.prototype.constructor = PointerCamera;
|
|
|
|
|
|
|
|
// Update function
|
2015-05-13 09:11:54 +02:00
|
|
|
PointerCamera.prototype.update = function(time) {
|
2015-04-08 11:36:15 +02:00
|
|
|
if (this.moving) {
|
2015-05-13 09:29:37 +02:00
|
|
|
this.linearMotion(time);
|
2015-05-11 10:20:19 +02:00
|
|
|
} else if (this.movingHermite) {
|
2015-05-13 09:29:37 +02:00
|
|
|
this.hermiteMotion(time);
|
2015-05-11 10:20:19 +02:00
|
|
|
} else {
|
2015-05-13 09:11:54 +02:00
|
|
|
this.normalMotion(time);
|
2015-05-11 10:20:19 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-13 09:29:37 +02:00
|
|
|
PointerCamera.prototype.linearMotion = function(time) {
|
2015-05-11 10:20:19 +02:00
|
|
|
var position_direction = Tools.diff(this.new_position, this.position);
|
|
|
|
var target_direction = Tools.diff(this.new_target, this.target);
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-13 09:29:37 +02:00
|
|
|
this.position.add(Tools.mul(position_direction, 0.05 * time / 20));
|
|
|
|
this.target.add(Tools.mul(target_direction, 0.05 * time / 20));
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
if (Tools.norm2(Tools.diff(this.position, this.new_position)) < 0.01 &&
|
|
|
|
Tools.norm2(Tools.diff(this.target, this.new_target)) < 0.01) {
|
|
|
|
this.moving = false;
|
|
|
|
this.anglesFromVectors();
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 16:52:39 +02:00
|
|
|
|
2015-05-13 09:29:37 +02:00
|
|
|
PointerCamera.prototype.hermiteMotion = function(time) {
|
2015-05-11 10:20:19 +02:00
|
|
|
var eval = this.hermitePosition.eval(this.t);
|
|
|
|
this.position.x = eval.x;
|
|
|
|
this.position.y = eval.y;
|
|
|
|
this.position.z = eval.z;
|
2015-05-05 16:52:39 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
this.target = Tools.sum(this.position, this.hermiteAngles.eval(this.t));
|
2015-05-05 16:52:39 +02:00
|
|
|
|
2015-05-13 09:29:37 +02:00
|
|
|
this.t += 0.01 * time / 20;
|
2015-05-22 10:03:34 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
if (this.t > 1) {
|
|
|
|
this.movingHermite = false;
|
|
|
|
this.anglesFromVectors();
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-13 09:11:54 +02:00
|
|
|
PointerCamera.prototype.normalMotion = function(time) {
|
2015-05-11 10:20:19 +02:00
|
|
|
// Update angles
|
2015-05-19 14:25:35 +02:00
|
|
|
if (this.motion.increasePhi) {this.phi += this.sensitivity; this.changed = true; }
|
|
|
|
if (this.motion.decreasePhi) {this.phi -= this.sensitivity; this.changed = true; }
|
|
|
|
if (this.motion.increaseTheta) {this.theta += this.sensitivity; this.changed = true; }
|
|
|
|
if (this.motion.decreaseTheta) {this.theta -= this.sensitivity; this.changed = true; }
|
2015-04-30 14:53:55 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
if (this.dragging) {
|
|
|
|
this.theta += this.mouseMove.x;
|
|
|
|
this.phi -= this.mouseMove.y;
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
this.mouseMove.x = 0;
|
|
|
|
this.mouseMove.y = 0;
|
|
|
|
|
|
|
|
this.changed = true;
|
|
|
|
}
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
// Clamp phi and theta
|
|
|
|
this.phi = Math.min(Math.max(-(Math.PI/2-0.1),this.phi), Math.PI/2-0.1);
|
|
|
|
this.theta = ((this.theta - Math.PI) % (2*Math.PI)) + Math.PI;
|
|
|
|
|
|
|
|
// Compute vectors (position and target)
|
|
|
|
this.vectorsFromAngles();
|
|
|
|
|
|
|
|
// Update with events
|
|
|
|
var delta = 0.1;
|
|
|
|
var forward = this.forward.clone();
|
|
|
|
forward.multiplyScalar(400.0 * delta);
|
|
|
|
var left = this.up.clone();
|
|
|
|
left.cross(forward);
|
|
|
|
left.normalize();
|
|
|
|
left.multiplyScalar(400.0 * delta);
|
|
|
|
|
|
|
|
// Move only if no collisions
|
2015-05-13 09:29:37 +02:00
|
|
|
var speed = this.speed * time / 20;
|
2015-05-11 10:20:19 +02:00
|
|
|
var direction = new THREE.Vector3();
|
|
|
|
|
2015-05-19 14:25:35 +02:00
|
|
|
if (this.motion.boost) speed *= 10;
|
|
|
|
if (this.motion.moveForward) {direction.add(Tools.mul(forward, speed)); this.changed = true;}
|
|
|
|
if (this.motion.moveBackward) {direction.sub(Tools.mul(forward, speed)); this.changed = true;}
|
|
|
|
if (this.motion.moveLeft) {direction.add(Tools.mul(left, speed)); this.changed = true;}
|
|
|
|
if (this.motion.moveRight) {direction.sub(Tools.mul(left, speed)); this.changed = true;}
|
2015-05-11 10:20:19 +02:00
|
|
|
|
|
|
|
if (!this.collisions || !this.isColliding(direction)) {
|
|
|
|
this.position.add(direction);
|
2015-04-07 11:13:19 +02:00
|
|
|
}
|
2015-05-11 10:20:19 +02:00
|
|
|
|
|
|
|
// Update angle
|
|
|
|
this.target = this.position.clone();
|
|
|
|
this.target.add(forward);
|
2015-04-17 10:33:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.reset = function() {
|
2015-05-11 12:04:37 +02:00
|
|
|
this.resetBobomb();
|
2015-05-12 16:02:42 +02:00
|
|
|
this.moving = false;
|
|
|
|
this.movingHermite = false;
|
2015-05-20 15:20:59 +02:00
|
|
|
(new BD.Event.ResetClicked()).send();
|
2015-05-11 12:04:37 +02:00
|
|
|
// this.position.copy(new THREE.Vector3(-8.849933489419644, 9.050627639459208, 0.6192960680432451));
|
|
|
|
// this.target.copy(new THREE.Vector3(17.945323228767702, -15.156828589982375, -16.585740412769756));
|
|
|
|
// this.anglesFromVectors();
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.resetBobomb = function() {
|
|
|
|
this.position.copy(new THREE.Vector3(34.51854618261728,10.038879540840306,-21.772598201888613));
|
|
|
|
this.target.copy(new THREE.Vector3(-2.593404107644737,8.039712770013185,-6.983870133675925));
|
2015-04-17 10:33:41 +02:00
|
|
|
this.anglesFromVectors();
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.vectorsFromAngles = function() {
|
|
|
|
// Update direction
|
|
|
|
this.forward.y = Math.sin(this.phi);
|
|
|
|
|
|
|
|
var cos = Math.cos(this.phi);
|
|
|
|
this.forward.z = cos * Math.cos(this.theta);
|
|
|
|
this.forward.x = cos * Math.sin(this.theta);
|
|
|
|
this.forward.normalize();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.anglesFromVectors = function() {
|
|
|
|
var forward = Tools.diff(this.target, this.position);
|
|
|
|
forward.normalize();
|
2015-04-07 11:13:19 +02:00
|
|
|
|
2015-04-17 10:33:41 +02:00
|
|
|
this.phi = Math.asin(forward.y);
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-04-17 10:33:41 +02:00
|
|
|
// Don't know why this line works... But thanks Thierry-san and
|
|
|
|
// Bastien because it seems to work...
|
|
|
|
this.theta = Math.atan2(forward.x, forward.z);
|
2015-04-08 11:36:15 +02:00
|
|
|
}
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-04-30 14:53:55 +02:00
|
|
|
PointerCamera.prototype.move = function(otherCamera, toSave) {
|
|
|
|
if (toSave === undefined)
|
|
|
|
toSave = true;
|
|
|
|
|
2015-04-08 11:36:15 +02:00
|
|
|
this.moving = true;
|
|
|
|
this.new_target = otherCamera.target.clone();
|
|
|
|
this.new_position = otherCamera.position.clone();
|
2015-04-16 12:23:47 +02:00
|
|
|
var t = [0,1];
|
|
|
|
var f = [this.position.clone(), this.new_position];
|
|
|
|
var fp = [Tools.diff(this.target, this.position), Tools.diff(this.new_target, this.new_position)];
|
|
|
|
this.hermite = new Hermite.Polynom(t,f,fp);
|
|
|
|
this.t = 0;
|
2015-04-30 14:53:55 +02:00
|
|
|
|
|
|
|
if (toSave) {
|
|
|
|
if (this.changed) {
|
|
|
|
this.save();
|
|
|
|
this.changed = false;
|
|
|
|
}
|
|
|
|
this.history.addState({position: otherCamera.position.clone(), target: otherCamera.target.clone()});
|
|
|
|
}
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
|
|
|
|
2015-05-05 16:52:39 +02:00
|
|
|
PointerCamera.prototype.moveHermite = function(otherCamera, toSave) {
|
|
|
|
if (toSave === undefined)
|
|
|
|
toSave = true;
|
|
|
|
|
|
|
|
this.movingHermite = true;
|
|
|
|
this.t = 0;
|
|
|
|
|
|
|
|
this.hermitePosition = new Hermite.special.Polynom(
|
|
|
|
this.position.clone(),
|
|
|
|
otherCamera.position.clone(),
|
2015-05-11 10:20:19 +02:00
|
|
|
Tools.mul(Tools.diff(otherCamera.target, otherCamera.position).normalize(),4)
|
2015-05-05 16:52:39 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
this.hermiteAngles = new Hermite.special.Polynom(
|
|
|
|
Tools.diff(this.target, this.position),
|
|
|
|
Tools.diff(otherCamera.target, otherCamera.position),
|
|
|
|
new THREE.Vector3()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (toSave) {
|
|
|
|
if (this.changed) {
|
|
|
|
this.save();
|
|
|
|
this.changed = false;
|
|
|
|
}
|
|
|
|
this.history.addState({position: otherCamera.position.clone(), target: otherCamera.target.clone()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 10:17:54 +02:00
|
|
|
PointerCamera.prototype.isColliding = function(direction) {
|
|
|
|
this.raycaster.set(this.position, direction.clone().normalize());
|
|
|
|
var intersects = this.raycaster.intersectObjects(this.collidableObjects, true);
|
|
|
|
|
|
|
|
for (var i in intersects) {
|
2015-05-11 12:04:37 +02:00
|
|
|
if (intersects[i].distance < 100*this.speed) {
|
2015-04-22 10:17:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
// Look function
|
|
|
|
PointerCamera.prototype.look = function() {
|
|
|
|
this.lookAt(this.target);
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.addToScene = function(scene) {
|
|
|
|
scene.add(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.onKeyEvent = function(event, toSet) {
|
2015-05-19 14:25:35 +02:00
|
|
|
// Create copy of state
|
|
|
|
var motionJsonCopy = JSON.stringify(this.motion);
|
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
switch ( event.keyCode ) {
|
|
|
|
// Azerty keyboards
|
2015-05-19 14:25:35 +02:00
|
|
|
case 38: case 90: this.motion.moveForward = toSet; break; // up / z
|
|
|
|
case 37: case 81: this.motion.moveLeft = toSet; break; // left / q
|
|
|
|
case 40: case 83: this.motion.moveBackward = toSet; break; // down / s
|
|
|
|
case 39: case 68: this.motion.moveRight = toSet; break; // right / d
|
|
|
|
case 32: this.motion.boost = toSet; break;
|
2015-04-02 12:38:06 +02:00
|
|
|
|
|
|
|
// Qwerty keyboards
|
2015-05-19 14:25:35 +02:00
|
|
|
case 38: case 87: this.motion.moveForward = toSet; break; // up / w
|
|
|
|
case 37: case 65: this.motion.moveLeft = toSet; break; // left / a
|
|
|
|
case 40: case 83: this.motion.moveBackward = toSet; break; // down / s
|
|
|
|
case 39: case 68: this.motion.moveRight = toSet; break; // right / d
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-05-19 14:25:35 +02:00
|
|
|
case 73: case 104: this.motion.increasePhi = toSet; break; // 8 Up for angle
|
|
|
|
case 75: case 98: this.motion.decreasePhi = toSet; break; // 2 Down for angle
|
|
|
|
case 74: case 100: this.motion.increaseTheta = toSet; break; // 4 Left for angle
|
|
|
|
case 76: case 102: this.motion.decreaseTheta = toSet; break; // 6 Right for angle
|
2015-04-16 12:23:47 +02:00
|
|
|
|
|
|
|
case 13: if (toSet) this.log(); break;
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
2015-05-19 14:25:35 +02:00
|
|
|
if (motionJsonCopy != JSON.stringify(this.motion)) {
|
|
|
|
// Log any change
|
|
|
|
var event = new BD.Event.KeyboardEvent();
|
|
|
|
event.camera = this;
|
|
|
|
event.send();
|
|
|
|
}
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.onKeyDown = function(event) {
|
|
|
|
this.onKeyEvent(event, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.onKeyUp = function(event) {
|
|
|
|
this.onKeyEvent(event, false);
|
|
|
|
}
|
|
|
|
|
2015-04-07 11:13:19 +02:00
|
|
|
PointerCamera.prototype.onMouseDown = function(event) {
|
|
|
|
this.mouse.x = ( ( event.clientX - renderer.domElement.offsetLeft ) / renderer.domElement.width ) * 2 - 1;
|
|
|
|
this.mouse.y = - ( ( event.clientY - renderer.domElement.offsetTop ) / renderer.domElement.height ) * 2 + 1;
|
|
|
|
|
|
|
|
this.dragging = true;
|
2015-05-20 15:20:59 +02:00
|
|
|
this.mouseMoved = false;
|
2015-04-07 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.onMouseMove = function(event) {
|
|
|
|
if (this.dragging) {
|
|
|
|
var mouse = {x: this.mouse.x, y: this.mouse.y};
|
|
|
|
this.mouse.x = ( ( event.clientX - renderer.domElement.offsetLeft ) / renderer.domElement.width ) * 2 - 1;
|
|
|
|
this.mouse.y = - ( ( event.clientY - renderer.domElement.offsetTop ) / renderer.domElement.height ) * 2 + 1;
|
|
|
|
|
2015-04-08 11:36:15 +02:00
|
|
|
this.mouseMove.x = this.mouse.x - mouse.x;
|
|
|
|
this.mouseMove.y = this.mouse.y - mouse.y;
|
2015-05-20 15:20:59 +02:00
|
|
|
this.mouseMoved = true;
|
2015-04-07 11:13:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.onMouseUp = function(event) {
|
|
|
|
this.onMouseMove(event);
|
2015-05-20 15:20:59 +02:00
|
|
|
|
|
|
|
// Send log to DB
|
|
|
|
if (this.dragging && this.mouseMoved && !this.moving && !this.movingHermite) {
|
|
|
|
var event = new BD.Event.KeyboardEvent();
|
|
|
|
event.camera = this;
|
|
|
|
event.send();
|
|
|
|
}
|
|
|
|
|
2015-04-07 11:13:19 +02:00
|
|
|
this.dragging = false;
|
|
|
|
}
|
|
|
|
|
2015-04-16 12:23:47 +02:00
|
|
|
PointerCamera.prototype.log = function() {
|
2015-05-11 16:47:45 +02:00
|
|
|
console.log("createCamera(\nnew THREE.Vector3(" + this.position.x + "," + this.position.y + ',' + this.position.z + '),\n'
|
|
|
|
+ "new THREE.Vector3(" + this.target.x + "," + this.target.y + ',' + this.target.z + ')\n)');
|
2015-04-16 12:23:47 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 15:51:25 +02:00
|
|
|
PointerCamera.prototype.save = function() {
|
2015-04-30 14:53:55 +02:00
|
|
|
var backup = {};
|
|
|
|
backup.position = this.position.clone();
|
|
|
|
backup.target = this.target.clone();
|
|
|
|
this.history.addState(backup);
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.undo = function() {
|
|
|
|
var move = this.history.undo();
|
2015-05-20 16:28:53 +02:00
|
|
|
if (move !== undefined) {
|
|
|
|
var event = new BD.Event.PreviousNextClicked();
|
|
|
|
event.previous = true;
|
|
|
|
event.camera = move;
|
|
|
|
event.send();
|
|
|
|
|
2015-04-30 14:53:55 +02:00
|
|
|
this.move(move, false);
|
2015-05-20 16:28:53 +02:00
|
|
|
}
|
2015-04-30 14:53:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.redo = function() {
|
|
|
|
var move = this.history.redo();
|
2015-05-20 16:28:53 +02:00
|
|
|
if (move !== undefined) {
|
|
|
|
var event = new BD.Event.PreviousNextClicked();
|
|
|
|
event.previous = false;
|
|
|
|
event.camera = move;
|
|
|
|
event.send();
|
|
|
|
|
2015-04-30 14:53:55 +02:00
|
|
|
this.move(move, false);
|
2015-05-20 16:28:53 +02:00
|
|
|
}
|
2015-04-29 15:51:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 14:53:55 +02:00
|
|
|
PointerCamera.prototype.undoable = function() {
|
|
|
|
return this.history.undoable();
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerCamera.prototype.redoable = function() {
|
|
|
|
return this.history.redoable();
|
2015-04-29 15:51:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-30 14:53:55 +02:00
|
|
|
var History = function() {
|
|
|
|
this.states = new Array();
|
|
|
|
this.index = -1;
|
|
|
|
this.size = 0;
|
|
|
|
console.log('New state ' + this.index + ' / ' + this.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
History.prototype.addState = function(state) {
|
|
|
|
++this.index;
|
|
|
|
this.size = this.index + 1;
|
|
|
|
this.states[this.size-1] = state;
|
|
|
|
console.log('New state ' + this.index + ' / ' + this.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
History.prototype.undo = function() {
|
|
|
|
if (this.undoable()) {
|
|
|
|
this.index--;
|
|
|
|
console.log('New state ' + this.index + ' / ' + this.size);
|
|
|
|
return this.currentState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
History.prototype.redo = function() {
|
|
|
|
if (this.redoable()) {
|
|
|
|
this.index++;
|
|
|
|
console.log('New state ' + this.index + ' / ' + this.size);
|
|
|
|
return this.currentState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
History.prototype.undoable = function() {
|
|
|
|
return this.index > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
History.prototype.redoable = function() {
|
|
|
|
return this.index < this.size - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
History.prototype.currentState = function() {
|
|
|
|
return this.states[this.index];
|
|
|
|
}
|