OLD | NEW |
1 <!-- | 1 <!-- |
2 -- Copyright 2013 The Chromium Authors. All rights reserved. | 2 -- Copyright 2013 The Chromium Authors. All rights reserved. |
3 -- Use of this source code is governed by a BSD-style license that can be | 3 -- Use of this source code is governed by a BSD-style license that can be |
4 -- found in the LICENSE file. | 4 -- found in the LICENSE file. |
5 --> | 5 --> |
6 | 6 |
7 <polymer-element name="kb-keyboard" on-key-over="{{keyOver}}" | 7 <polymer-element name="kb-keyboard" on-key-over="{{keyOver}}" |
8 on-key-up="{{keyUp}}" on-key-down="{{keyDown}}" | 8 on-key-up="{{keyUp}}" on-key-down="{{keyDown}}" |
9 on-key-longpress="{{keyLongpress}}" on-pointerup="{{up}}" | 9 on-key-longpress="{{keyLongpress}}" on-pointerup="{{up}}" |
10 on-pointerdown="{{down}}" on-pointerout="{{out}}" | 10 on-pointerdown="{{down}}" on-pointerout="{{out}}" |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 | 327 |
328 /** | 328 /** |
329 * Caches the specified sound on the keyboard. | 329 * Caches the specified sound on the keyboard. |
330 * @param {string} soundId The name of the .wav file in the "sounds" | 330 * @param {string} soundId The name of the .wav file in the "sounds" |
331 directory. | 331 directory. |
332 */ | 332 */ |
333 addSound: function(soundId) { | 333 addSound: function(soundId) { |
334 // Check if already loaded. | 334 // Check if already loaded. |
335 if (soundId == Sound.NONE || this.sounds[soundId]) | 335 if (soundId == Sound.NONE || this.sounds[soundId]) |
336 return; | 336 return; |
337 var audio = document.createElement('audio'); | 337 var pool = []; |
338 audio.preload = "auto"; | 338 for (var i = 0; i < SOUND_POOL_SIZE; i++) { |
339 audio.id = soundId; | 339 var audio = document.createElement('audio'); |
340 audio.src = "../sounds/" + soundId + ".wav"; | 340 audio.preload = "auto"; |
341 audio.volume = this.volume; | 341 audio.id = soundId; |
342 this.sounds[soundId] = audio; | 342 audio.src = "../sounds/" + soundId + ".wav"; |
| 343 audio.volume = this.volume; |
| 344 pool.push(audio); |
| 345 } |
| 346 this.sounds[soundId] = pool; |
343 }, | 347 }, |
344 | 348 |
345 /** | 349 /** |
346 * Changes the current keyset. | 350 * Changes the current keyset. |
347 * @param {Object} detail The detail of the event that called this | 351 * @param {Object} detail The detail of the event that called this |
348 * function. | 352 * function. |
349 */ | 353 */ |
350 changeKeyset: function(detail) { | 354 changeKeyset: function(detail) { |
351 if (detail.relegateToShift && this.shift) { | 355 if (detail.relegateToShift && this.shift) { |
352 this.keyset = this.shift.textKeyset; | 356 this.keyset = this.shift.textKeyset; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 if(this.changeKeyset(detail)) | 538 if(this.changeKeyset(detail)) |
535 return; | 539 return; |
536 if (detail.repeat) { | 540 if (detail.repeat) { |
537 this.keyTyped(detail); | 541 this.keyTyped(detail); |
538 this.onNonControlKeyTyped(); | 542 this.onNonControlKeyTyped(); |
539 repeatKey.key = this.lastPressedKey; | 543 repeatKey.key = this.lastPressedKey; |
540 var self = this; | 544 var self = this; |
541 repeatKey.timer = setTimeout(function() { | 545 repeatKey.timer = setTimeout(function() { |
542 repeatKey.timer = undefined; | 546 repeatKey.timer = undefined; |
543 repeatKey.interval = setInterval(function() { | 547 repeatKey.interval = setInterval(function() { |
| 548 self.playSound(detail.sound); |
544 self.keyTyped(detail); | 549 self.keyTyped(detail); |
545 }, REPEAT_INTERVAL_MSEC); | 550 }, REPEAT_INTERVAL_MSEC); |
546 }, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC)); | 551 }, Math.max(0, REPEAT_DELAY_MSEC - REPEAT_INTERVAL_MSEC)); |
547 } | 552 } |
548 }, | 553 }, |
549 | 554 |
550 /** | 555 /** |
551 * Handles key-out event that is sent by kb-shift-key. | 556 * Handles key-out event that is sent by kb-shift-key. |
552 * @param {CustomEvent} event The key-out event dispatched by | 557 * @param {CustomEvent} event The key-out event dispatched by |
553 * kb-shift-key. | 558 * kb-shift-key. |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 } | 796 } |
792 }, | 797 }, |
793 | 798 |
794 /** | 799 /** |
795 * Plays the specified sound. | 800 * Plays the specified sound. |
796 * @param {Sound} sound The id of the audio tag. | 801 * @param {Sound} sound The id of the audio tag. |
797 */ | 802 */ |
798 playSound: function(sound) { | 803 playSound: function(sound) { |
799 if (!sound || sound == Sound.NONE) | 804 if (!sound || sound == Sound.NONE) |
800 return; | 805 return; |
801 var soundElement = this.sounds[sound]; | 806 var pool = this.sounds[sound]; |
802 if (!soundElement) | 807 if (!pool) { |
803 console.error("Cannot find audio tag: " + sound); | 808 console.error("Cannot find audio tag: " + sound); |
804 else | 809 return; |
805 soundElement.play(); | 810 } |
| 811 // Search the sound pool for a free resource. |
| 812 for (var i = 0; i < pool.length; i++) { |
| 813 if (pool[i].paused) { |
| 814 pool[i].play(); |
| 815 return; |
| 816 } |
| 817 } |
806 }, | 818 }, |
807 | 819 |
808 /** | 820 /** |
809 * Whether we should transit to upper case when seeing a space after | 821 * Whether we should transit to upper case when seeing a space after |
810 * punctuation. | 822 * punctuation. |
811 * @return {boolean} | 823 * @return {boolean} |
812 */ | 824 */ |
813 shouldUpperOnSpace: function() { | 825 shouldUpperOnSpace: function() { |
814 // TODO(rsadam): Add other input types in which we should not | 826 // TODO(rsadam): Add other input types in which we should not |
815 // transition to upper after a space. | 827 // transition to upper after a space. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 if (this.alt) | 916 if (this.alt) |
905 this.alt.onNonControlKeyTyped(); | 917 this.alt.onNonControlKeyTyped(); |
906 this.classList.remove('ctrl-active'); | 918 this.classList.remove('ctrl-active'); |
907 this.classList.remove('alt-active'); | 919 this.classList.remove('alt-active'); |
908 }, | 920 }, |
909 | 921 |
910 /** | 922 /** |
911 * Callback function for when volume is changed. | 923 * Callback function for when volume is changed. |
912 */ | 924 */ |
913 volumeChanged: function() { | 925 volumeChanged: function() { |
914 var toChange = keys(this.sounds); | 926 var toChange = Object.keys(this.sounds); |
915 for (var i = 0; i < toChange.length; i++) { | 927 for (var i = 0; i < toChange.length; i++) { |
916 this.sounds[toChange[i]].volume = this.volume; | 928 var pool = this.sounds[toChange[i]]; |
| 929 for (var j = 0; j < pool.length; j++) { |
| 930 pool[j].volume = this.volume; |
| 931 } |
917 } | 932 } |
918 }, | 933 }, |
919 | 934 |
920 /** | 935 /** |
921 * Id for the active keyset. | 936 * Id for the active keyset. |
922 * @type {string} | 937 * @type {string} |
923 */ | 938 */ |
924 get activeKeysetId() { | 939 get activeKeysetId() { |
925 return this.layout + '-' + this.keyset; | 940 return this.layout + '-' + this.keyset; |
926 }, | 941 }, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1037 } | 1052 } |
1038 } | 1053 } |
1039 } | 1054 } |
1040 if (keysetsLoaded) | 1055 if (keysetsLoaded) |
1041 console.error('No default keyset found for ' + this.layout); | 1056 console.error('No default keyset found for ' + this.layout); |
1042 return false; | 1057 return false; |
1043 } | 1058 } |
1044 }); | 1059 }); |
1045 </script> | 1060 </script> |
1046 </polymer-element> | 1061 </polymer-element> |
OLD | NEW |