2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /*jslint devel: true*/
18 /*global $, Audio, window, tizen, app: true */
34 Piano.prototype.bindEvents = function bindEvents() {
37 $(".button").bind('touchstart touchmove', function (e) { self.onPianoKeyTouchDown(e); });
38 $(".button").bind('touchend', function (e) { self.onPianoKeyTouchUp(e); });
39 $('#back').bind('touchstart', function (event) {
40 event.preventDefault();
41 event.stopPropagation();
42 $(this).addClass('active');
43 tizen.application.getCurrentApplication().exit();
46 $(window).on('tizenhwkey', function (e) {
47 if (e.originalEvent.keyName === "back") {
48 tizen.application.getCurrentApplication().exit();
52 $('#back').bind('touchend', function (event) {
53 $(this).removeClass('active');
56 document.addEventListener('webkitvisibilitychange', function () {
57 if (document.webkitVisibilityState === 'visible') {
63 Piano.prototype.onPianoKeyTouchDown = function onPianoKeyTouchDown(data) {
64 data.originalEvent.preventDefault();
65 var element, touch, i, len;
66 for (i = 0, len = data.originalEvent.changedTouches.length; i < len; i += 1) {
67 touch = data.originalEvent.changedTouches[i];
68 element = document.elementFromPoint(touch.clientX, touch.clientY);
70 if (this.touchPianoKey[touch.identifier] !== element.id) {
71 $('#' + this.touchPianoKey[touch.identifier]).removeClass('pressed');
72 this.playAudioByElement(element, touch.identifier, function () {
73 this.lockIds[element.id] = true;
80 Piano.prototype.onPianoKeyTouchUp = function onPianoKeyTouchUp(data) {
81 var i, touchId, len, buttonId;
82 for (i = 0, len = data.originalEvent.changedTouches.length; i < len; i += 1) {
83 touchId = data.originalEvent.changedTouches[i].identifier;
84 buttonId = this.touchPianoKey[touchId];
85 setTimeout(function () {
87 $('#' + id).removeClass('pressed');
90 this.touchPianoKey[touchId] = undefined;
94 Piano.prototype.audioInit = function audioInit() {
96 for (i = 0; i <= 13; i += 1) {
97 this.audio[i] = document.createElement('audio');
98 this.audio[i].name = i;
99 this.audio[i].addEventListener("playing", function(){
100 self.lockIds["play" + this.name] = false;
106 * Plays sound for specified element
107 * @param {Object} element
108 * @param {Number} touchId
110 Piano.prototype.playAudioByElement = function playAudioByElement(element, touchId, callback) {
111 var nrAudio = parseInt(element.id.substr(4), 10);
112 $(element).addClass('pressed');
113 this.touchPianoKey[touchId] = element.id;
114 if (this.audio[nrAudio]) {
115 if (this.audio[nrAudio].src) {
117 this.audio[nrAudio].pause();
118 this.audio[nrAudio].currentTime = 0;
123 this.audio[nrAudio].src = this.audio[nrAudio].name + ".wav";
125 this.audio[nrAudio].play();
126 if (callback instanceof Function) {
132 Piano.prototype.cacheImages = function cacheImages() {
133 this.whiteCache = new Image();
134 this.whiteCache.src = "white_pressed.png";
135 this.blackCache = new Image();
136 this.blackCache.src = "black_pressed.png";
139 Piano.prototype.init = function init() {
147 $(document).ready(function () {