-
Notifications
You must be signed in to change notification settings - Fork 1
/
sound.js
305 lines (242 loc) · 7.06 KB
/
sound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* sound.js
Creation: 25/04/13 by Kevin Guillaumond
Purpose: Manages the sound at the bottom left of the screen in 2D view
number of lines :305
*/
#pragma strict
// borders in x-position of sound
private var lBorder : int;
private var rBorder : int;
// border in y-position of sound
private var uBorder : int;
private var dBorder : int;
private var buttonSizeFactor : float = 0.60; // is <= 1. If 1, the button takes all the room available
private var playBtn : Texture;
private var pauseBtn : Texture;
private var currentBtn : Texture;
private var soundNameStr: String;
private var letterStyle : GUIStyle;
/* Sounds to play */
private var nbSounds: int;
private var tabOfSounds: Array;
/* Index of song currently playing, starting 0 */
private var currentIndex: int;
/* width and height of a letter */
private var widthLetter : int = Screen.width / 125;
private var heightLetter : int = Screen.height / 30;
// event and display activate or not
private var eventEnable : boolean ;
private var soundIsHidden : boolean ;
private var chooseNextSoundRandomly: boolean = true;
private var repeatButton : Texture;
private var displaySongName : boolean = false;
private var displayPlayButton : boolean = true;
private var displayChangeButton : boolean = false;
private var mySkin : GUISkin;
function OnGUISound() {
if( !soundIsHidden )
displayMusic();
}
function placeMusic (u: int, d: int, l: int, r: int, tab: Array) { // 4 margins + an array of sound names (with path from folder Resources)
nbSounds = tab.length;
if (nbSounds == 0)
return;
/* Init array of sound names */
tabOfSounds = tab;
currentIndex = 0;
gameObject.AddComponent("AudioSource");
audio.clip = Resources.Load(tab[0]) as AudioClip;
audio.Play();
/* Transparent buttons */
mySkin = new GUISkin ();
mySkin.button.normal.background = null;
enableAll();
if (displayPlayButton) {
playBtn = Resources.Load("Pictures/play");
pauseBtn = Resources.Load("Pictures/pause");
currentBtn = pauseBtn;
}
if (displayChangeButton)
repeatButton = Resources.Load("Pictures/repeat_music");
if (buttonSizeFactor > 1)
buttonSizeFactor = 1;
uBorder = u;
dBorder = d;
lBorder = l;
rBorder = r;
if (displaySongName) {
soundNameStr = fileSystem.getName(tab[0]);
letterStyle = new GUIStyle();
letterStyle.alignment = TextAnchor.MiddleCenter;
letterStyle.normal.textColor = Color.white;
letterStyle.fontStyle = FontStyle.Normal;
letterStyle.fixedHeight = heightLetter;
letterStyle.fixedWidth = widthLetter;
letterStyle.margin = new RectOffset (0,0,0,0);
if (isOnIpad())
letterStyle.fontSize = 20; // Default: 13
}
if (chooseNextSoundRandomly)
changeMusic(""); // Randomise 1st sound
audio.loop = false;
if (!playBtn && displayPlayButton) {
Debug.LogError("No texture for play button");
return;
}
if (!pauseBtn && displayPlayButton) {
Debug.LogError("No texture for pause button");
return;
}
if (!repeatButton && displayChangeButton) {
Debug.LogError("No texture for change music button");
return;
}
}
function placeMusicFactor (u: float, d: float, l: float, r: float, tab: Array) {
placeMusic(u*Screen.height, d*Screen.height, l*Screen.width, r*Screen.width, tab);
}
function displayMusic() {
/* Do not display anything for music if play btn is not loaded or if there is no sound to play */
if( (displayPlayButton && !playBtn) || (nbSounds == 0))
return;
var buttonSize = (Screen.height - uBorder - dBorder ) * buttonSizeFactor; // size of the square
/* The button is centered regarding the y axis */
if (displayPlayButton) {
if (GUI.Button(Rect(lBorder + buttonSize, uBorder + (((buttonSize / buttonSizeFactor) - buttonSize) / 2), buttonSize, buttonSize), currentBtn, mySkin.button) && eventEnable ) { // Rect: left top width height
if (currentBtn == pauseBtn) { // Sound playing
currentBtn = playBtn;
audio.Pause();
}
else {
currentBtn = pauseBtn;
audio.Play();
}
}
}
/* Bouton changer musique */
if (displayChangeButton) {
if (GUI.Button(Rect( Screen.width-rBorder-2*buttonSize, uBorder + (((buttonSize / buttonSizeFactor) - buttonSize) / 2), buttonSize, buttonSize), repeatButton, mySkin.button) && eventEnable)
changeMusic("");
}
/* Change musique si terminée */
if (!audio.isPlaying && (currentBtn == pauseBtn || !displayPlayButton)) {
changeMusic("");
if(displayPlayButton) currentBtn = pauseBtn;
audio.Play();
}
/* Name of the song: Does NOT handle the case when the name is too LONG */
if (displaySongName) {
GUI.Label( Rect(lBorder + buttonSize + 10,
uBorder + buttonSize/(2*buttonSizeFactor) - widthLetter,
Screen.width - lBorder - rBorder,
heightLetter
),
soundNameStr);
}
}
/* If soundName = "", the function chooses the next sound to play (random or not). If not, plays the sound named soundName */
function changeMusic(soundName) {
var audioIsPlaying = audio.isPlaying;
var newSoundPath = ""; // Path of the new sound to play
/*
* Select the name of the next sound
*/
if (soundName != "")
newSoundPath = soundName;
else { // if no soundName, select the next sound to play
if (!chooseNextSoundRandomly) {
if (currentIndex == nbSounds - 1) { // end of playlist
newSoundPath = tabOfSounds[0];
currentIndex = 0;
}
else {
newSoundPath = tabOfSounds[currentIndex+1];
currentIndex++;
}
} // end if not random
else { // chosoe next sound randomly
var randomIndex = Mathf.Floor(Random.Range(0, nbSounds - 0.01));
newSoundPath = tabOfSounds[randomIndex];
}
}
/*
* Load and play the selected sound
*/
audio.clip = Resources.Load(newSoundPath) as AudioClip;
if (displaySongName)
soundNameStr = fileSystem.getName(newSoundPath);
if (displayPlayButton) {
if (audioIsPlaying) {
audio.Play();
currentBtn = pauseBtn;
}
else {
audio.Pause();
currentBtn = playBtn;
}
}
}
function removeMusic() {
disableAll();
if(audio)
audio.Stop();
Destroy(GetComponent(GUIText));
Destroy(GetComponent("AudioSource"));
}
/*******************************************************
**** Cacher / desactiver les evennements de l'objet ****
********************************************************/
/*
* Affiche l'objet et active les evenements
*/
public function enableAll() {
show() ;
enableEvents() ;
if ((currentBtn == pauseBtn || !displayPlayButton) && audio )
audio.Play();
}
/*
* Cache l'objet et desactive les evenements
*/
public function disableAll() {
hide() ;
disableEvents();
if( audio )
audio.Pause();
}
/*
* Active les evenements
*/
public function enableEvents() {
eventEnable = true ;
}
/*
* Desactive les evenements
*/
public function disableEvents() {
eventEnable = false ;
}
/*
* Affiche l'objet
*/
public function show() {
soundIsHidden = false ;
}
/*
* Cache l'objet
*/
public function hide() {
soundIsHidden = true ;
}
/*
* Getters
*/
public function areEventEnabled() : boolean {
return eventEnable ;
}
public function isHidden() : boolean {
return soundIsHidden ;
}
function isOnIpad() : boolean {
return ( SystemInfo.deviceType == DeviceType.Handheld );
}