Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer lock #1

Merged
merged 1 commit into from
Apr 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions lib/camera.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,19 +12,38 @@ class Controls
@lookSpeed = 0.20 @lookSpeed = 0.20
@mouseX = 0 @mouseX = 0
@mouseY = 0 @mouseY = 0
@deltaX = 0
@deltaY = 0
@lat = 0 @lat = 0
@lon = 0 @lon = 0
@mouseDragOn = false @mouseDragOn = false
@anchorx = null @anchorx = null
@anchory = null @anchory = null
@mouseLocked = false
@defineBindings() @defineBindings()
@enablePointerLock()


defineBindings: -> defineBindings: ->
$(@domElement).mousemove (e) => @onMouseMove e $(@domElement).mousemove (e) => @onMouseMove e
$(@domElement).mousedown (e) => @onMouseDown e $(@domElement).mousedown (e) => @onMouseDown e
$(@domElement).mouseup (e) => @onMouseUp e $(@domElement).mouseup (e) => @onMouseUp e
$(@domElement).mouseenter (e) => @onMouserEnter e $(@domElement).mouseenter (e) => @onMouserEnter e


enablePointerLock: ->
@domElement.requestPointerLock ?=
@domElement.webkitRequestPointerLock or
@domElement.mozRequestPointerLock
$(document).bind 'pointerlockchange mozpointerlockchange webkitpointerlockchange', =>
d = document
@mouseLocked = (d.pointerLockElement or d.mozPointerLockElement or d.webkitPointerLockElement)?
if @mouseLocked then @showCrosshair() else @hideCrosshair()

lockPointer: ->
@domElement.requestPointerLock?()

showCrosshair: -> document.getElementById('cursor').style.display = 'block'
hideCrosshair: -> document.getElementById('cursor').style.display = 'none'

onMouserEnter: (event) -> onMouserEnter: (event) ->
@onMouseUp(event) unless MouseEvent.isLeftButtonDown event @onMouseUp(event) unless MouseEvent.isLeftButtonDown event


Expand All @@ -33,21 +52,31 @@ class Controls
@domElement.focus() if @domElement isnt document @domElement.focus() if @domElement isnt document
@anchorx = event.pageX @anchorx = event.pageX
@anchory = event.pageY @anchory = event.pageY
@setMouse event @setMouse @anchorx, @anchory
@mouseDragOn = true @mouseDragOn = true
return false return false


onMouseUp: (event) -> onMouseUp: (event) ->
@mouseDragOn = false @mouseDragOn = false
return false return false


setMouse: (event) -> setMouse: (x, y) ->
@mouseX = event.pageX @mouseX = x
@mouseY = event.pageY @mouseY = y
@setDelta x - @anchorx, y - @anchory

setDelta: (x, y) ->
@deltaX = x
@deltaY = y


onMouseMove: (event) -> onMouseMove: (event) ->
return unless @mouseDragOn if @mouseDragOn
@setMouse event @setMouse event.pageX, event.pageY
else if @mouseLocked
e = event.originalEvent
x = e.movementX or e.mozMovementX or e.webkitMovementX
y = e.movementY or e.mozMovementY or e.webkitMovementY
@setDelta x, y
return return


halfCircle: Math.PI / 180 halfCircle: Math.PI / 180
Expand All @@ -71,13 +100,22 @@ class Controls
return return


update: -> update: ->
return unless @mouseDragOn return unless @mouseDragOn or @mouseLocked
return if @mouseX is @anchorx and @mouseY is @anchory return if @mouseDragOn and @mouseX is @anchorx and @mouseY is @anchory
{max, min} = Math {max, min} = Math
@lon += (@mouseX - @anchorx) * @lookSpeed if @mouseLocked
@lat -= (@mouseY - @anchory) * @lookSpeed return if @deltaX is @previousDeltaX and @deltaY is @previousDeltaY
@anchorx = @mouseX @previousDeltaX = @deltaX
@anchory = @mouseY @previousDeltaY = @deltaY
@anchorx = window.innerWidth/2
@anchory = window.innerHeight/2
else if @mouseDragOn
return if @mouseX is @anchorx and @mouseY is @anchory
@anchorx = @mouseX
@anchory = @mouseY

@lon += @deltaX * @lookSpeed
@lat -= @deltaY * @lookSpeed
@lat = max(-85, min(85, @lat)) @lat = max(-85, min(85, @lat))
@updateLook() @updateLook()
return return
Expand Down
6 changes: 3 additions & 3 deletions lib/minecraft.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -617,16 +617,16 @@ class Instructions
img: (name) -> "<img src='./instructions/#{name}.png'/>" img: (name) -> "<img src='./instructions/#{name}.png'/>"






window.init_web_app = -> window.init_web_app = ->
game = null
$("#blocks").hide() $("#blocks").hide()
$('#instructions').hide() $('#instructions').hide()
$(document).bind "contextmenu", -> false $(document).bind "contextmenu", -> false
$(document).bind "keydown", 'c', -> game?.controls.lockPointer()
return Detector.addGetWebGLMessage() unless Detector.webgl return Detector.addGetWebGLMessage() unless Detector.webgl
startGame = -> startGame = ->
game = new Game() game = new Game()
new BlockSelection(game).insert() new BlockSelection(game).insert()
game.start() game.start()
return
new Instructions(startGame).insert() new Instructions(startGame).insert()

98 changes: 73 additions & 25 deletions public/camera.js
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,3 @@
// Generated by CoffeeScript 1.3.3
(function() { (function() {
var Controls, MouseEvent; var Controls, MouseEvent;


Expand All @@ -23,12 +22,16 @@
this.lookSpeed = 0.20; this.lookSpeed = 0.20;
this.mouseX = 0; this.mouseX = 0;
this.mouseY = 0; this.mouseY = 0;
this.deltaX = 0;
this.deltaY = 0;
this.lat = 0; this.lat = 0;
this.lon = 0; this.lon = 0;
this.mouseDragOn = false; this.mouseDragOn = false;
this.anchorx = null; this.anchorx = null;
this.anchory = null; this.anchory = null;
this.mouseLocked = false;
this.defineBindings(); this.defineBindings();
this.enablePointerLock();
} }


Controls.prototype.defineBindings = function() { Controls.prototype.defineBindings = function() {
Expand All @@ -47,22 +50,47 @@
}); });
}; };


Controls.prototype.onMouserEnter = function(event) { Controls.prototype.enablePointerLock = function() {
if (!MouseEvent.isLeftButtonDown(event)) { var _base,
return this.onMouseUp(event); _this = this;
if ((_base = this.domElement).requestPointerLock == null) {
_base.requestPointerLock = this.domElement.webkitRequestPointerLock || this.domElement.mozRequestPointerLock;
} }
return $(document).bind('pointerlockchange mozpointerlockchange webkitpointerlockchange', function() {
var d;
d = document;
_this.mouseLocked = (d.pointerLockElement || d.mozPointerLockElement || d.webkitPointerLockElement) != null;
if (_this.mouseLocked) {
return _this.showCrosshair();
} else {
return _this.hideCrosshair();
}
});
};

Controls.prototype.lockPointer = function() {
var _base;
return typeof (_base = this.domElement).requestPointerLock === "function" ? _base.requestPointerLock() : void 0;
};

Controls.prototype.showCrosshair = function() {
return document.getElementById('cursor').style.display = 'block';
};

Controls.prototype.hideCrosshair = function() {
return document.getElementById('cursor').style.display = 'none';
};

Controls.prototype.onMouserEnter = function(event) {
if (!MouseEvent.isLeftButtonDown(event)) return this.onMouseUp(event);
}; };


Controls.prototype.onMouseDown = function(event) { Controls.prototype.onMouseDown = function(event) {
if (!MouseEvent.isLeftButton(event)) { if (!MouseEvent.isLeftButton(event)) return;
return; if (this.domElement !== document) this.domElement.focus();
}
if (this.domElement !== document) {
this.domElement.focus();
}
this.anchorx = event.pageX; this.anchorx = event.pageX;
this.anchory = event.pageY; this.anchory = event.pageY;
this.setMouse(event); this.setMouse(this.anchorx, this.anchory);
this.mouseDragOn = true; this.mouseDragOn = true;
return false; return false;
}; };
Expand All @@ -72,16 +100,27 @@
return false; return false;
}; };


Controls.prototype.setMouse = function(event) { Controls.prototype.setMouse = function(x, y) {
this.mouseX = event.pageX; this.mouseX = x;
return this.mouseY = event.pageY; this.mouseY = y;
return this.setDelta(x - this.anchorx, y - this.anchory);
};

Controls.prototype.setDelta = function(x, y) {
this.deltaX = x;
return this.deltaY = y;
}; };


Controls.prototype.onMouseMove = function(event) { Controls.prototype.onMouseMove = function(event) {
if (!this.mouseDragOn) { var e, x, y;
return; if (this.mouseDragOn) {
this.setMouse(event.pageX, event.pageY);
} else if (this.mouseLocked) {
e = event.originalEvent;
x = e.movementX || e.mozMovementX || e.webkitMovementX;
y = e.movementY || e.mozMovementY || e.webkitMovementY;
this.setDelta(x, y);
} }
this.setMouse(event);
}; };


Controls.prototype.halfCircle = Math.PI / 180; Controls.prototype.halfCircle = Math.PI / 180;
Expand Down Expand Up @@ -111,17 +150,26 @@


Controls.prototype.update = function() { Controls.prototype.update = function() {
var max, min; var max, min;
if (!this.mouseDragOn) { if (!(this.mouseDragOn || this.mouseLocked)) return;
return; if (this.mouseDragOn && this.mouseX === this.anchorx && this.mouseY === this.anchory) {
}
if (this.mouseX === this.anchorx && this.mouseY === this.anchory) {
return; return;
} }
max = Math.max, min = Math.min; max = Math.max, min = Math.min;
this.lon += (this.mouseX - this.anchorx) * this.lookSpeed; if (this.mouseLocked) {
this.lat -= (this.mouseY - this.anchory) * this.lookSpeed; if (this.deltaX === this.previousDeltaX && this.deltaY === this.previousDeltaY) {
this.anchorx = this.mouseX; return;
this.anchory = this.mouseY; }
this.previousDeltaX = this.deltaX;
this.previousDeltaY = this.deltaY;
this.anchorx = window.innerWidth / 2;
this.anchory = window.innerHeight / 2;
} else if (this.mouseDragOn) {
if (this.mouseX === this.anchorx && this.mouseY === this.anchory) return;
this.anchorx = this.mouseX;
this.anchory = this.mouseY;
}
this.lon += this.deltaX * this.lookSpeed;
this.lat -= this.deltaY * this.lookSpeed;
this.lat = max(-85, min(85, this.lat)); this.lat = max(-85, min(85, this.lat));
this.updateLook(); this.updateLook();
}; };
Expand Down
17 changes: 17 additions & 0 deletions public/index.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@
text-align: center; text-align: center;
} }


#cursor {
display:none;
position:absolute;
top:50%;
left:50%;
width:20px;
margin-left:-10px;
font-size:18px;
text-align:center;
color:#fff;
z-index:2;
}
#cursor::before {
content: '+'
}

#oldie a { color:#fff } #oldie a { color:#fff }


body { body {
Expand All @@ -94,6 +110,7 @@
<body> <body>
<div id="instructions"></div> <div id="instructions"></div>
<div id="container"></div> <div id="container"></div>
<div id="cursor"></div>
<div id="blocks"> <div id="blocks">
</div> </div>
</body> </body>
Expand Down
Loading