PageRenderTime 49ms CodeModel.GetById 1ms app.highlight 44ms RepoModel.GetById 0ms app.codeStats 1ms

/Classes/FadeDemo.m

http://github.com/kstenerud/ObjectAL-for-iPhone
Objective C | 354 lines | 237 code | 61 blank | 56 comment | 18 complexity | e570ec9bc39bce7d57f640695c82b02c MD5 | raw file
  1//
  2//  FadeDemo.m
  3//  ObjectAL
  4//
  5//  Created by Karl Stenerud on 10-08-17.
  6//
  7
  8#import "FadeDemo.h"
  9#import "MainScene.h"
 10#import "CCLayer+Scene.h"
 11#import "ImageButton.h"
 12#import "ImageAndLabelButton.h"
 13#import "ObjectAL.h"
 14#import "CCLayer+AudioPanel.h"
 15
 16#define kSpaceBetweenButtons 40
 17#define kStartY 160
 18
 19@interface FadeDemo (Private)
 20
 21/** Build the user interface. */
 22- (void) buildUI;
 23
 24/** Start/stop the background track. */
 25- (void) onBackgroundPlayStop:(LampButton*) button;
 26
 27/** Fade out the background track. */
 28- (void) onBackgroundFadeOut:(LampButton*) button;
 29
 30/** Fade in the background track. */
 31- (void) onBackgroundFadeIn:(LampButton*) button;
 32
 33/** Called when the fade completes. */
 34- (void) onBackgroundFadeComplete:(id) sender;
 35
 36/** Start/stop the OpenAL source. */
 37- (void) onObjectALPlayStop:(LampButton*) button;
 38
 39/** Fade the source out. */
 40- (void) onObjectALFadeOut:(LampButton*) button;
 41
 42/** Fade the source in. */
 43- (void) onObjectALFadeIn:(LampButton*) button;
 44
 45/** Called when the fade completes. */
 46- (void) onObjectALFadeComplete:(id) sender;
 47
 48/** Exit the demo. */
 49- (void) onExitPressed;
 50
 51@end
 52
 53@implementation FadeDemo
 54
 55- (id) init
 56{
 57	if(nil != (self = [super initWithColor:ccc4(0, 0, 0, 0)]))
 58	{
 59		[self buildUI];
 60	}
 61	return self;
 62}
 63
 64- (void) buildUI
 65{
 66	[self buildAudioPanelWithTSeparator];
 67	[self addPanelTitle:@"Fading"];
 68	[self addPanelLine1:@"Click Start, then use fade buttons"];
 69	[self addPanelLine2:@"to start or cancel a fade."];
 70	
 71	CGSize screenSize = [[CCDirector sharedDirector] winSize];
 72	CGPoint center = ccp(screenSize.width/2, screenSize.height/2);
 73
 74	LampButton* button;
 75	CCLabel* label;
 76	
 77	CGPoint pos = ccp(60, screenSize.height - kStartY);
 78	
 79	label = [CCLabel labelWithString:@"ALSource" fontName:@"Helvetica-Bold" fontSize:24];
 80	label.anchorPoint = ccp(0, 0.5f);
 81	label.position = ccp(pos.x+10, pos.y);
 82	[self addChild:label];
 83	
 84	pos.y -= kSpaceBetweenButtons;
 85	
 86	button = [LampButton buttonWithText:@"Start/Stop"
 87								   font:@"Helvetica"
 88								   size:20
 89							 lampOnLeft:YES
 90								 target:self
 91							   selector:@selector(onObjectALPlayStop:)];
 92	button.anchorPoint = ccp(0, 0.5f);
 93	button.position = pos;
 94	[self addChild:button];
 95	startStopSourceButton = button;
 96	
 97	pos.y -= kSpaceBetweenButtons;
 98	
 99	button = [LampButton buttonWithText:@"Fade Out"
100								   font:@"Helvetica"
101								   size:20
102							 lampOnLeft:YES
103								 target:self
104							   selector:@selector(onObjectALFadeOut:)];
105	button.anchorPoint = ccp(0, 0.5f);
106	button.position = pos;
107	[self addChild:button];
108	fadeOutSourceButton = button;
109	
110	pos.y -= kSpaceBetweenButtons;
111	
112	button = [LampButton buttonWithText:@"Fade In"
113								   font:@"Helvetica"
114								   size:20
115							 lampOnLeft:YES
116								 target:self
117							   selector:@selector(onObjectALFadeIn:)];
118	button.anchorPoint = ccp(0, 0.5f);
119	button.position = pos;
120	[self addChild:button];
121	fadeInSourceButton = button;
122
123
124	pos = ccp(center.x+40, screenSize.height - kStartY);
125
126	label = [CCLabel labelWithString:@"AudioTrack" fontName:@"Helvetica-Bold" fontSize:24];
127	label.anchorPoint = ccp(0, 0.5f);
128	label.position = ccp(pos.x+10, pos.y);
129	[self addChild:label];
130	
131	pos.y -= kSpaceBetweenButtons;
132	
133	button = [LampButton buttonWithText:@"Start/Stop"
134								   font:@"Helvetica"
135								   size:20
136							 lampOnLeft:YES
137								 target:self
138							   selector:@selector(onBackgroundPlayStop:)];
139	button.anchorPoint = ccp(0, 0.5f);
140	button.position = pos;
141	[self addChild:button];
142	startStopTrackButton = button;
143	
144	pos.y -= kSpaceBetweenButtons;
145	
146	button = [LampButton buttonWithText:@"Fade Out"
147								   font:@"Helvetica"
148								   size:20
149							 lampOnLeft:YES
150								 target:self
151							   selector:@selector(onBackgroundFadeOut:)];
152	button.anchorPoint = ccp(0, 0.5f);
153	button.position = pos;
154	[self addChild:button];
155	fadeOutTrackButton = button;
156	
157	pos.y -= kSpaceBetweenButtons;
158	
159	button = [LampButton buttonWithText:@"Fade In"
160								   font:@"Helvetica"
161								   size:20
162							 lampOnLeft:YES
163								 target:self
164							   selector:@selector(onBackgroundFadeIn:)];
165	button.anchorPoint = ccp(0, 0.5f);
166	button.position = pos;
167	[self addChild:button];
168	fadeInTrackButton = button;
169	
170
171	// Exit button
172	button = [ImageButton buttonWithImageFile:@"Exit.png" target:self selector:@selector(onExitPressed)];
173	button.anchorPoint = ccp(1,1);
174	button.position = ccp(screenSize.width, screenSize.height);
175	[self addChild:button z:250];
176}
177
178- (void) onEnterTransitionDidFinish
179{
180	// Initialize the OpenAL device and context here instead of in init so that
181	// it doesn't happen prematurely.
182	[OALSimpleAudio sharedInstance];
183}
184
185- (void) onBackgroundPlayStop:(LampButton*) button
186{
187	[[OALSimpleAudio sharedInstance].backgroundTrack stopFade];
188	fadeInTrackButton.isOn = NO;
189	fadeOutTrackButton.isOn = NO;
190
191	if(button.isOn)
192	{
193		[OALSimpleAudio sharedInstance].bgVolume = 1.0f;
194		[[OALSimpleAudio sharedInstance] playBg:@"ColdFunk.caf" loop:YES];
195	}
196	else
197	{
198		[[OALSimpleAudio sharedInstance] stopBg];
199	}
200}
201
202- (void) onBackgroundFadeOut:(LampButton*) button
203{
204	fadeInTrackButton.isOn = NO;
205	[[OALSimpleAudio sharedInstance].backgroundTrack stopFade];
206
207	if([OALSimpleAudio sharedInstance].bgPlaying && [OALSimpleAudio sharedInstance].bgVolume > 0.0f)
208	{
209		if(button.isOn)
210		{
211			[[OALSimpleAudio sharedInstance].backgroundTrack fadeTo:0.0f duration:1.0f target:self selector:@selector(onBackgroundFadeComplete:)];
212		}
213
214		// Alternatively, you could do this:
215		//   OALAction* action = [OALSequentialActions actions:
216		//						[OALGainAction actionWithDuration:1.0 endValue:0.0],
217		//						[OALCall actionWithCallTarget:self selector:@selector(onBackgroundFadeComplete:)],
218		//						nil];
219		//   [action runWithTarget:[OALSimpleAudio sharedInstance].backgroundTrack];
220		//
221		// You could also specify a function like this:
222		//   [OALGainAction actionWithDuration:1.0 endValue:0.0 function:[OALLogarithmicFunction function]];
223	}
224	else
225	{
226		button.isOn = NO;
227	}
228}
229
230- (void) onBackgroundFadeIn:(LampButton*) button
231{
232	fadeOutTrackButton.isOn = NO;
233	[[OALSimpleAudio sharedInstance].backgroundTrack stopFade];
234	
235	if([OALSimpleAudio sharedInstance].bgPlaying && [OALSimpleAudio sharedInstance].bgVolume < 1.0f)
236	{
237		if(button.isOn)
238		{
239			[[OALSimpleAudio sharedInstance].backgroundTrack fadeTo:1.0f duration:1.0f target:self selector:@selector(onBackgroundFadeComplete:)];
240		}
241
242		// Alternatively, you could do this:
243		//   OALAction* action = [OALSequentialActions actions:
244		//						[OALGainAction actionWithDuration:1.0 endValue:1.0],
245		//						[OALCall actionWithCallTarget:self selector:@selector(onBackgroundFadeComplete:)],
246		//						nil];
247		//   [action runWithTarget:[OALSimpleAudio sharedInstance].backgroundTrack];
248		//
249		// You could also specify a function like this:
250		//   [OALGainAction actionWithDuration:1.0 endValue:1.0 function:[OALLogarithmicFunction function]];
251	}
252	else
253	{
254		button.isOn = NO;
255	}
256}
257
258- (void) onBackgroundFadeComplete:(id) sender
259{
260	fadeInTrackButton.isOn = NO;
261	fadeOutTrackButton.isOn = NO;
262}
263
264
265- (void) onObjectALPlayStop:(LampButton*) button
266{
267	[source stopFade];
268	fadeInSourceButton.isOn = NO;
269	fadeOutSourceButton.isOn = NO;
270	
271	if(button.isOn)
272	{
273		source = [[OALSimpleAudio sharedInstance] playEffect:@"HappyAlley.caf" loop:YES];
274	}
275	else
276	{
277		[source stop];
278		source = nil;
279	}
280}
281
282- (void) onObjectALFadeOut:(LampButton*) button
283{
284	fadeInSourceButton.isOn = NO;
285	[source stopFade];
286	
287	if(nil != source && source.volume > 0.0f)
288	{
289		if(button.isOn)
290		{
291			[source fadeTo:0.0f duration:1.0f target:self selector:@selector(onObjectALFadeComplete:)];
292		}
293
294		// Alternatively, you could do this:
295		//   OALAction* action = [OALSequentialActions actions:
296		//						[OALGainAction actionWithDuration:1.0 endValue:0.0],
297		//						[OALCall actionWithCallTarget:self selector:@selector(onObjectALFadeComplete:)],
298		//						nil];
299		//   [action runWithTarget:source];
300		//
301		// You could also specify a function like this:
302		//   [OALGainAction actionWithDuration:1.0 endValue:0.0 function:[OALLogarithmicFunction function]];
303	}
304	else
305	{
306		button.isOn = NO;
307	}
308}
309
310- (void) onObjectALFadeIn:(LampButton*) button
311{
312	fadeOutSourceButton.isOn = NO;
313	[source stopFade];
314	
315	if(nil != source && source.volume < 1.0f)
316	{
317		if(button.isOn)
318		{
319			[source fadeTo:1.0f duration:1.0f target:self selector:@selector(onObjectALFadeComplete:)];
320		}
321		
322		// Alternatively, you could do this:
323		//   OALAction* action = [OALSequentialActions actions:
324		//						[OALGainAction actionWithDuration:1.0 endValue:1.0],
325		//						[OALCall actionWithCallTarget:self selector:@selector(onObjectALFadeComplete:)],
326		//						nil];
327		//   [action runWithTarget:source];
328		//
329		// You could also specify a function like this:
330		//   [OALGainAction actionWithDuration:1.0 endValue:1.0 function:[OALLogarithmicFunction function]];
331	}
332	else
333	{
334		button.isOn = NO;
335	}
336}
337
338- (void) onObjectALFadeComplete:(id) sender
339{
340	fadeInSourceButton.isOn = NO;
341	fadeOutSourceButton.isOn = NO;
342}
343
344- (void) onExitPressed
345{
346	// These are needed when using cocos2d actions since the its action engine is less forgiving.
347	[[OALSimpleAudio sharedInstance].backgroundTrack stopActions];
348	[source stopActions];
349	
350	self.isTouchEnabled = NO;
351	[[CCDirector sharedDirector] replaceScene:[MainLayer scene]];
352}
353
354@end