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