API Documentation
Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_extra_glge_input.js.html, line: 125 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29
Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_extra_glge_input.js.html, line: 125 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29
Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_extra_glge_input.js.html, line: 125 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29
Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: no name in jsdoc/symbols/src/src_extra_glge_input.js.html, line: 125 in /homepages/22/d163487924/htdocs/glge/wp-content/themes/glge/manual.php on line 29
1 /* 2 GLGE WebGL Graphics Engine 3 Copyright (c) 2010, Paul Brunt 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions are met: 8 * Redistributions of source code must retain the above copyright 9 notice, this list of conditions and the following disclaimer. 10 * Redistributions in binary form must reproduce the above copyright 11 notice, this list of conditions and the following disclaimer in the 12 documentation and/or other materials provided with the distribution. 13 * Neither the name of GLGE nor the 14 names of its contributors may be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 DISCLAIMED. IN NO EVENT SHALL PAUL BRUNT BE LIABLE FOR ANY 21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @fileOverview 31 * @name glge_input.js 32 * @author me@paulbrunt.co.uk 33 */ 34 35 36 if(!GLGE){ 37 /** 38 * @namespace Holds the functionality of the library 39 */ 40 var GLGE={}; 41 } 42 43 (function(GLGE){ 44 /** 45 * @class Creates a heightmap for a region of the world based on an image. Originally created as a quick and easy collision detection. At least until we have a better physics implementation. 46 * @deprecated not intended as a permanent addition 47 * @param {string} imageURL The url of the image to generate the hightmap for 48 * @param {number} imageWidth The width of the image 49 * @param {number} imageHeight The height of the image 50 * @param {number} x1 The lower X bound of the height map in world coords 51 * @param {number} x2 The upper X bound of the height map in world coords 52 * @param {number} y1 The lower Y bound of the height map in world coords 53 * @param {number} y2 The upper Y bound of the height map in world coords 54 * @param {number} z1 The lower Z bound of the height map in world coords 55 * @param {number} z2 The upper Z bound of the height map in world coords 56 */ 57 GLGE.HeightMap = function(imageURL, imageWidth, imageHeight, x1, x2, y1, y2, z1, z2){ 58 this.canvas = document.createElement("canvas"); 59 this.context = this.canvas.getContext('2d'); 60 this.canvas.width = imageWidth; 61 this.canvas.height = imageHeight; 62 this.minX = x1; 63 this.maxX = x2; 64 this.minY = y1; 65 this.maxY = y2; 66 this.minZ = z1; 67 this.maxZ = z2; 68 69 var image = new Image(); 70 image.heightmap = this; 71 image.onload = function(e){ 72 this.heightmap.context.drawImage(this, 0, 0); 73 this.heightmap.data = this.heightmap.context.getImageData(0, 0, this.heightmap.canvas.width, this.heightmap.canvas.height).data; 74 this.heightmap.minImgValue = this.heightmap.data[0]; 75 this.heightmap.maxImgValue = this.heightmap.data[0]; 76 for (i = 0; i < this.heightmap.data.length; i += 4) { 77 if (this.heightmap.data[i] < this.heightmap.minImgValue) { 78 this.heightmap.minImgValue = this.heightmap.data[i]; 79 } 80 if (this.heightmap.data[i] > this.heightmap.maxImgValue) { 81 this.heightmap.maxImgValue = this.heightmap.data[i]; 82 } 83 } 84 }; 85 image.src = imageURL; 86 } 87 GLGE.HeightMap.prototype.canvas = null; 88 GLGE.HeightMap.prototype.context = null; 89 GLGE.HeightMap.prototype.minZ = null; 90 GLGE.HeightMap.prototype.maxZ = null; 91 GLGE.HeightMap.prototype.minY = null; 92 GLGE.HeightMap.prototype.maxY = null; 93 GLGE.HeightMap.prototype.minX = null; 94 GLGE.HeightMap.prototype.maxX = null; 95 GLGE.HeightMap.prototype.data = null; 96 /** 97 * Gets the pixel height at the specified image coords 98 * @param {number} x the x image coord 99 * @param {number} y the y image coord 100 * @private 101 */ 102 GLGE.HeightMap.prototype.getPixelAt = function(x, y){ 103 if (this.data) { 104 return (((this.data[(this.canvas.width * y + x) * 4]) - this.minImgValue) / (this.maxImgValue - this.minImgValue)) * (this.maxZ - this.minZ) + this.minZ; 105 } 106 else { 107 return 0; 108 } 109 } 110 /** 111 * Function to get he height as specified x, y world coords 112 * @param {number} x the x world coord 113 * @param {number} y the y world coord 114 * @returns {number} the height of the level in world units 115 */ 116 GLGE.HeightMap.prototype.getHeightAt = function(x, y){ 117 var retValue; 118 if (this.lastx != undefined && x == this.lastx && y == this.lasty) { 119 retValue = this.lastValue; 120 } 121 else { 122 var imgX = Math.round((x - this.minX) / (this.maxX - this.minX) * this.canvas.width); 123 var imgY = Math.round((y - this.minY) / (this.maxY - this.minY) * this.canvas.height); 124 retValue = this.getPixelAt(imgX, imgY); 125 this.lastValue = retValue; 126 } 127 this.lastx = x; 128 this.lasty = y; 129 return retValue; 130 } 131 /** 132 * @class Monitors keyboard input for use in render loops 133 */ 134 GLGE.KeyInput=function(){ 135 if(!document.keyStates) document.keyStates=[]; 136 document.addEventListener("keydown",this.onKeyDown,false); 137 document.addEventListener("keyup",this.onKeyUp,false); 138 } 139 /** 140 * Tests if a key is pressed 141 * @param {number} the keycode to check 142 * @returns {boolean} key returns true if the key is being pressed 143 */ 144 GLGE.KeyInput.prototype.isKeyPressed=function(key){ 145 if(document.keyStates[key]) return true; 146 else return false; 147 }; 148 var skiptimmer=null; 149 /** 150 * document keydown event used to monitor the key states 151 * @param {event} e the event being fired 152 * @private 153 */ 154 GLGE.KeyInput.prototype.onKeyDown=function(e){ 155 document.keyStates[e.keyCode]=true; 156 }; 157 /** 158 * Document keyup event used to monitor the key states 159 * @param {event} e the event being fired 160 * @private 161 */ 162 GLGE.KeyInput.prototype.onKeyUp=function(e){ 163 document.keyStates[e.keyCode]=false; 164 }; 165 /** 166 * @class Monitors mouse input for use in render loops 167 */ 168 GLGE.MouseInput=function(element){ 169 this.element=element; 170 this.element.mouseX=0; 171 this.element.mouseY=0; 172 if(!this.element.buttonState) this.element.buttonState=[]; 173 element.addEventListener("mousemove",this.onMouseMove,false); 174 element.addEventListener("mousedown",this.onMouseDown,false); 175 element.addEventListener("mouseup",this.onMouseUp,false); 176 } 177 GLGE.MouseInput.prototype.element=null; 178 /** 179 * Elements mousemove event used to monitor the mouse states 180 * @param {event} e the event being fired 181 * @private 182 */ 183 GLGE.MouseInput.prototype.onMouseMove=function(e){ 184 this.mouseX=e.clientX; 185 this.mouseY=e.clientY; 186 } 187 /** 188 * Elements mousedown event used to monitor the mouse states 189 * @param {event} e the event being fired 190 * @private 191 */ 192 GLGE.MouseInput.prototype.onMouseDown=function(e){ 193 this.buttonState[e.button]=true; 194 } 195 /** 196 * Elements mouseup event used to monitor the mouse states 197 * @param {event} e the event being fired 198 * @private 199 */ 200 GLGE.MouseInput.prototype.onMouseUp=function(e){ 201 this.buttonState[e.button]=false; 202 } 203 /** 204 * Tests if a mouse button is pressed 205 * @param {number} button the button to check 206 * @returns {boolean} returns true if the button is being pressed 207 */ 208 GLGE.MouseInput.prototype.isButtonDown=function(button){ 209 if(this.element.buttonState[button]) return true; 210 else return false; 211 } 212 /** 213 * Gets the mouse coords 214 * @returns {object} the current mouse coors 215 */ 216 GLGE.MouseInput.prototype.getMousePosition=function(){ 217 return {x:this.element.mouseX,y:this.element.mouseY} 218 } 219 220 /** 221 * @constant 222 * @description Enumeration for the left mouse button 223 */ 224 GLGE.MI_LEFT=0; 225 /** 226 * @constant 227 * @description Enumeration for the middle mouse button 228 */ 229 GLGE.MI_MIDDLE=1; 230 /** 231 * @constant 232 * @description Enumeration for the right mouse button 233 */ 234 GLGE.MI_RIGHT=2; 235 236 /** 237 * @constant 238 * @description Enumeration for the backspace key 239 */ 240 GLGE.KI_BACKSPACE=8; 241 /** 242 * @constant 243 * @description Enumeration for the tab key 244 */ 245 GLGE.KI_TAB=9; 246 /** 247 * @constant 248 * @description Enumeration for the enter key 249 */ 250 GLGE.KI_ENTER=13; 251 /** 252 * @constant 253 * @description Enumeration for the shift key 254 */ 255 GLGE.KI_SHIFT=16; 256 /** 257 * @constant 258 * @description Enumeration for the ctrl key 259 */ 260 GLGE.KI_CTRL=17; 261 /** 262 * @constant 263 * @description Enumeration for the alt key 264 */ 265 GLGE.KI_ALT=18; 266 /** 267 * @constant 268 * @description Enumeration for the pause/break key 269 */ 270 GLGE.KI_PAUSE_BREAK=19; 271 /** 272 * @constant 273 * @description Enumeration for the caps lock key 274 */ 275 GLGE.KI_CAPS_LOCK=20; 276 /** 277 * @constant 278 * @description Enumeration for the escape key 279 */ 280 GLGE.KI_ESCAPE=27; 281 /** 282 * @constant 283 * @description Enumeration for the page up key 284 */ 285 GLGE.KI_PAGE_UP=33; 286 /** 287 * @constant 288 * @description Enumeration for the page down key 289 */ 290 GLGE.KI_PAGE_DOWN=34; 291 /** 292 * @constant 293 * @description Enumeration for the end key 294 */ 295 GLGE.KI_END=35; 296 /** 297 * @constant 298 * @description Enumeration for the home key 299 */ 300 GLGE.KI_HOME=36; 301 /** 302 * @constant 303 * @description Enumeration for the left arrow key 304 */ 305 GLGE.KI_LEFT_ARROW=37; 306 /** 307 * @constant 308 * @description Enumeration for the up arrow key 309 */ 310 GLGE.KI_UP_ARROW=38; 311 /** 312 * @constant 313 * @description Enumeration for the right arrow key 314 */ 315 GLGE.KI_RIGHT_ARROW=39; 316 /** 317 * @constant 318 * @description Enumeration for the down arrow key 319 */ 320 GLGE.KI_DOWN_ARROW=40; 321 /** 322 * @constant 323 * @description Enumeration for the insert key 324 */ 325 GLGE.KI_INSERT=45; 326 /** 327 * @constant 328 * @description Enumeration for the delete key 329 */ 330 GLGE.KI_DELETE=46; 331 332 /** 333 * @constant 334 * @description Enumeration for the 0 key 335 */ 336 GLGE.KI_0=48; 337 /** 338 * @constant 339 * @description Enumeration for the 1 key 340 */ 341 GLGE.KI_1=49; 342 /** 343 * @constant 344 * @description Enumeration for the 2 key 345 */ 346 GLGE.KI_2=50; 347 /** 348 * @constant 349 * @description Enumeration for the 3 key 350 */ 351 GLGE.KI_3=51; 352 /** 353 * @constant 354 * @description Enumeration for the 4 key 355 */ 356 GLGE.KI_4=52; 357 /** 358 * @constant 359 * @description Enumeration for the 5 key 360 */ 361 GLGE.KI_5=53; 362 /** 363 * @constant 364 * @description Enumeration for the 6 key 365 */ 366 GLGE.KI_6=54; 367 /** 368 * @constant 369 * @description Enumeration for the 7 key 370 */ 371 GLGE.KI_7=55; 372 /** 373 * @constant 374 * @description Enumeration for the 8 key 375 */ 376 GLGE.KI_8=56; 377 /** 378 * @constant 379 * @description Enumeration for the 9 key 380 */ 381 GLGE.KI_9=57; 382 /** 383 * @constant 384 * @description Enumeration for the a key 385 */ 386 GLGE.KI_A=65; 387 /** 388 * @constant 389 * @description Enumeration for the b key 390 */ 391 GLGE.KI_B=66; 392 /** 393 * @constant 394 * @description Enumeration for the c key 395 */ 396 GLGE.KI_C=67; 397 /** 398 * @constant 399 * @description Enumeration for the d key 400 */ 401 GLGE.KI_D=68; 402 /** 403 * @constant 404 * @description Enumeration for the e key 405 */ 406 GLGE.KI_E=69; 407 /** 408 * @constant 409 * @description Enumeration for the f key 410 */ 411 GLGE.KI_F=70; 412 /** 413 * @constant 414 * @description Enumeration for the g key 415 */ 416 GLGE.KI_G=71; 417 /** 418 * @constant 419 * @description Enumeration for the h key 420 */ 421 GLGE.KI_H=72; 422 /** 423 * @constant 424 * @description Enumeration for the i key 425 */ 426 GLGE.KI_I=73; 427 /** 428 * @constant 429 * @description Enumeration for the j key 430 */ 431 GLGE.KI_J=74; 432 /** 433 * @constant 434 * @description Enumeration for the k key 435 */ 436 GLGE.KI_K=75; 437 /** 438 * @constant 439 * @description Enumeration for the l key 440 */ 441 GLGE.KI_L=76; 442 /** 443 * @constant 444 * @description Enumeration for the m key 445 */ 446 GLGE.KI_M=77; 447 /** 448 * @constant 449 * @description Enumeration for the n key 450 */ 451 GLGE.KI_N=78; 452 /** 453 * @constant 454 * @description Enumeration for the o key 455 */ 456 GLGE.KI_O=79; 457 /** 458 * @constant 459 * @description Enumeration for the p key 460 */ 461 GLGE.KI_P=80; 462 /** 463 * @constant 464 * @description Enumeration for the q key 465 */ 466 GLGE.KI_Q=81; 467 /** 468 * @constant 469 * @description Enumeration for the r key 470 */ 471 GLGE.KI_R=82; 472 /** 473 * @constant 474 * @description Enumeration for the s key 475 */ 476 GLGE.KI_S=83; 477 /** 478 * @constant 479 * @description Enumeration for the t key 480 */ 481 GLGE.KI_T=84; 482 /** 483 * @constant 484 * @description Enumeration for the u key 485 */ 486 GLGE.KI_U=85; 487 /** 488 * @constant 489 * @description Enumeration for the v key 490 */ 491 GLGE.KI_V=86; 492 /** 493 * @constant 494 * @description Enumeration for the w key 495 */ 496 GLGE.KI_W=87; 497 /** 498 * @constant 499 * @description Enumeration for the x key 500 */ 501 GLGE.KI_X=88; 502 /** 503 * @constant 504 * @description Enumeration for the y key 505 */ 506 GLGE.KI_Y=89; 507 /** 508 * @constant 509 * @description Enumeration for the z key 510 */ 511 GLGE.KI_Z=90; 512 /** 513 * @constant 514 * @description Enumeration for the left window key key 515 */ 516 GLGE.KI_LEFT_WINDOW_KEY=91; 517 /** 518 * @constant 519 * @description Enumeration for the right window key key 520 */ 521 GLGE.KI_RIGHT_WINDOW_KEY=92; 522 /** 523 * @constant 524 * @description Enumeration for the select key key 525 */ 526 GLGE.KI_SELECT_KEY=93; 527 /** 528 * @constant 529 * @description Enumeration for the numpad 0 key 530 */ 531 GLGE.KI_NUMPAD_0=96; 532 /** 533 * @constant 534 * @description Enumeration for the numpad 1 key 535 */ 536 537 GLGE.KI_NUMPAD_1=97; 538 /** 539 * @constant 540 * @description Enumeration for the numpad 2 key 541 */ 542 GLGE.KI_NUMPAD_2=98; 543 /** 544 * @constant 545 * @description Enumeration for the numpad 3 key 546 */ 547 GLGE.KI_NUMPAD_3=99; 548 /** 549 * @constant 550 * @description Enumeration for the numpad 4 key 551 */ 552 GLGE.KI_NUMPAD_4=100; 553 /** 554 * @constant 555 * @description Enumeration for the numpad 5 key 556 */ 557 GLGE.KI_NUMPAD_5=101; 558 /** 559 * @constant 560 * @description Enumeration for the numpad 6 key 561 */ 562 GLGE.KI_NUMPAD_6=102; 563 /** 564 * @constant 565 * @description Enumeration for the numpad 7 key 566 */ 567 GLGE.KI_NUMPAD_7=103; 568 /** 569 * @constant 570 * @description Enumeration for the numpad 8 key 571 */ 572 GLGE.KI_NUMPAD_8=104; 573 /** 574 * @constant 575 * @description Enumeration for the numpad 9 key 576 */ 577 GLGE.KI_NUMPAD_9=105; 578 /** 579 * @constant 580 * @description Enumeration for the multiply key 581 */ 582 GLGE.KI_MULTIPLY=106; 583 /** 584 * @constant 585 * @description Enumeration for the add key 586 */ 587 GLGE.KI_ADD=107; 588 /** 589 * @constant 590 * @description Enumeration for the subtract key 591 */ 592 GLGE.KI_SUBTRACT=109; 593 /** 594 * @constant 595 * @description Enumeration for the decimal point key 596 */ 597 GLGE.KI_DECIMAL_POINT=110; 598 /** 599 * @constant 600 * @description Enumeration for the divide key 601 */ 602 GLGE.KI_DIVIDE=111; 603 /** 604 * @constant 605 * @description Enumeration for the f1 key 606 */ 607 GLGE.KI_F1=112; 608 /** 609 * @constant 610 * @description Enumeration for the f2 key 611 */ 612 GLGE.KI_F2=113; 613 /** 614 * @constant 615 * @description Enumeration for the f3 key 616 */ 617 GLGE.KI_F3=114; 618 /** 619 * @constant 620 * @description Enumeration for the f4 key 621 */ 622 GLGE.KI_F4=115; 623 /** 624 * @constant 625 * @description Enumeration for the f5 key 626 */ 627 GLGE.KI_F5=116; 628 /** 629 * @constant 630 * @description Enumeration for the f6 key 631 */ 632 GLGE.KI_F6=117; 633 /** 634 * @constant 635 * @description Enumeration for the f7 key 636 */ 637 GLGE.KI_F7=118; 638 /** 639 * @constant 640 * @description Enumeration for the f8 key 641 */ 642 GLGE.KI_F8=119; 643 /** 644 * @constant 645 * @description Enumeration for the f9 key 646 */ 647 GLGE.KI_F9=120; 648 /** 649 * @constant 650 * @description Enumeration for the f10 key 651 */ 652 GLGE.KI_F10=121; 653 /** 654 * @constant 655 * @description Enumeration for the f11 key 656 */ 657 GLGE.KI_F11=122; 658 /** 659 * @constant 660 * @description Enumeration for the f12 key 661 */ 662 GLGE.KI_F12=123; 663 /** 664 * @constant 665 * @description Enumeration for the num lock key 666 */ 667 GLGE.KI_NUM_LOCK=144; 668 /** 669 * @constant 670 * @description Enumeration for the scroll lock key 671 */ 672 GLGE.KI_SCROLL_LOCK=145; 673 /** 674 * @constant 675 * @description Enumeration for the semi-colon key 676 */ 677 GLGE.KI_SEMI_COLON=186; 678 /** 679 * @constant 680 * @description Enumeration for the equal sign key 681 */ 682 GLGE.KI_EQUAL_SIGN=187; 683 /** 684 * @constant 685 * @description Enumeration for the comma key 686 */ 687 GLGE.KI_COMMA=188; 688 /** 689 * @constant 690 * @description Enumeration for the dash key 691 */ 692 GLGE.KI_DASH=189; 693 /** 694 * @constant 695 * @description Enumeration for the period key 696 */ 697 GLGE.KI_PERIOD=190; 698 /** 699 * @constant 700 * @description Enumeration for the forward slash key 701 */ 702 GLGE.KI_FORWARD_SLASH=191; 703 /** 704 * @constant 705 * @description Enumeration for the grave accent key 706 */ 707 GLGE.KI_GRAVE_ACCENT=192; 708 /** 709 * @constant 710 * @description Enumeration for the open bracket key 711 */ 712 GLGE.KI_OPEN_BRACKET=219; 713 /** 714 * @constant 715 * @description Enumeration for the back slash key 716 */ 717 GLGE.KI_BACK_SLASH=220; 718 /** 719 * @constant 720 * @description Enumeration for the close braket key 721 */ 722 GLGE.KI_CLOSE_BRAKET=221; 723 /** 724 * @constant 725 * @description Enumeration for the single quote key 726 */ 727 GLGE.KI_SINGLE_QUOTE=222; 728 /** 729 * @constant 730 * @description Enumeration for the space key 731 */ 732 GLGE.KI_SPACE=32; 733 734 735 //code by @paul_irish 736 if ( !window.requestAnimationFrame ) { 737 738 window.requestAnimationFrame = ( function() { 739 740 return window.webkitRequestAnimationFrame || 741 window.mozRequestAnimationFrame || 742 window.oRequestAnimationFrame || 743 window.msRequestAnimationFrame || 744 function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) { 745 746 window.setTimeout( callback, 1000 / 60 ); 747 748 }; 749 750 } )(); 751 752 } 753 754 })(GLGE); 755 756 757 758