/libs/ObjectAL/Actions/OALAction.h

http://github.com/kstenerud/ObjectAL-for-iPhone · C Header · 310 lines · 123 code · 59 blank · 128 comment · 1 complexity · ae9318176f3e8c9bdd45a3fd0973096a MD5 · raw file

  1. //
  2. // OALAction.h
  3. // ObjectAL
  4. //
  5. // Created by Karl Stenerud on 10-09-18.
  6. //
  7. // Copyright 2009 Karl Stenerud
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //
  21. // Note: You are NOT required to make the license available from within your
  22. // iOS application. Including it in your project is sufficient.
  23. //
  24. // Attribution is not required, but appreciated :)
  25. //
  26. #import <Foundation/Foundation.h>
  27. #import "OALFunction.h"
  28. #import "ObjectALConfig.h"
  29. #if OBJECTAL_USE_COCOS2D_ACTIONS
  30. #pragma mark Cocos2d Subclassing
  31. #import "cocos2d.h"
  32. /** Generates common code required to subclass from a cocos2d action
  33. * while maintaining the functionality of an OALAction.
  34. */
  35. #define COCOS2D_SUBCLASS_HEADER(CLASS_A,CLASS_B) \
  36. @interface CLASS_A: CLASS_B \
  37. { \
  38. bool started; \
  39. } \
  40. \
  41. @property(readonly,nonatomic) bool running; \
  42. - (void) runWithTarget:(id) target; \
  43. - (void) prepareWithTarget:(id) target; \
  44. - (void) stopAction; \
  45. - (void) updateCompletion:(float) proportionComplete; \
  46. \
  47. @end
  48. /** Generates common code required to subclass from a cocos2d action
  49. * while maintaining the functionality of an OALAction.
  50. */
  51. #define COCOS2D_SUBCLASS(CLASS_A) \
  52. @implementation CLASS_A \
  53. \
  54. - (id) init \
  55. { \
  56. return [self initWithDuration:0]; \
  57. } \
  58. \
  59. -(void) startWithTarget:(id) targetIn \
  60. { \
  61. [super startWithTarget:targetIn]; \
  62. [self prepareWithTarget:targetIn]; \
  63. started = YES; \
  64. [self runWithTarget:targetIn]; \
  65. } \
  66. \
  67. - (void) update:(float) proportionComplete \
  68. { \
  69. [super update:proportionComplete]; \
  70. [self updateCompletion:proportionComplete]; \
  71. } \
  72. \
  73. - (bool) running \
  74. { \
  75. return !self.isDone; \
  76. } \
  77. \
  78. - (void) runWithTarget:(id) targetIn \
  79. { \
  80. if(!started) \
  81. { \
  82. [[CCActionManager sharedManager] addAction:self target:targetIn paused:NO]; \
  83. } \
  84. } \
  85. \
  86. - (void) stopAction \
  87. { \
  88. [[CCActionManager sharedManager] removeAction:self]; \
  89. } \
  90. #endif /* OBJECTAL_USE_COCOS2D_ACTIONS */
  91. /* There are two versions of the actions which can be used: ObjectAL and Cocos2d.
  92. * It's usually more convenient when using Cocos2d to have all actions as part of
  93. * the Cocos2d action system. You can set this in ObjectALConfig.h
  94. */
  95. #if !OBJECTAL_USE_COCOS2D_ACTIONS
  96. #pragma mark -
  97. #pragma mark OALAction (ObjectAL version)
  98. /**
  99. * Represents an action that can be performed on an object.
  100. */
  101. @interface OALAction : NSObject
  102. {
  103. /** The target to perform the action on */
  104. id target;
  105. float duration;
  106. float elapsed;
  107. bool running;
  108. /** If TRUE, this action is running via OALActionManager. */
  109. bool runningInManager;
  110. }
  111. #pragma mark Properties
  112. /** The target to perform the action on. WEAK REFERENCE. */
  113. @property(readonly,nonatomic) id target;
  114. /** The duration of the action, in seconds. */
  115. @property(readonly,nonatomic) float duration;
  116. /** The amount of time that has elapsed for this action, in seconds. */
  117. @property(readwrite,nonatomic) float elapsed;
  118. /** If true, the action is currently running. */
  119. @property(readonly,nonatomic) bool running;
  120. #pragma mark Object Management
  121. /** Initialize an action.
  122. *
  123. * @param duration The duration of this action in seconds.
  124. * @return The initialized action.
  125. */
  126. - (id) initWithDuration:(float) duration;
  127. #pragma mark Functions
  128. /** Run this action on a target.
  129. *
  130. * @param target The target to run the action on.
  131. */
  132. - (void) runWithTarget:(id) target;
  133. /** Called by runWithTraget to do any final preparations before running.
  134. * Subclasses must ensure that duration is valid when this method returns.
  135. *
  136. * @param target The target to run the action on.
  137. */
  138. - (void) prepareWithTarget:(id) target;
  139. /** Called by runWithTarget to start the action running.
  140. */
  141. - (void) startAction;
  142. /** Called by OALActionManager to update this action's progress.
  143. *
  144. * @param proportionComplete The proportion of this action's duration that has elapsed.
  145. */
  146. - (void) updateCompletion:(float) proportionComplete;
  147. /** Stop this action.
  148. */
  149. - (void) stopAction;
  150. @end
  151. #else /* !OBJECTAL_USE_COCOS2D_ACTIONS */
  152. COCOS2D_SUBCLASS_HEADER(OALAction, CCIntervalAction);
  153. #endif /* !OBJECTAL_USE_COCOS2D_ACTIONS */
  154. #pragma mark -
  155. #pragma mark OALFunctionAction
  156. /**
  157. * An action that applies a function to the proportionComplete parameter in
  158. * [update] before applying the result to the target.
  159. * This allows things like exponential and s-curve functions when applying gain
  160. * transitions, for example.
  161. */
  162. @interface OALFunctionAction: OALAction
  163. {
  164. float startValue;
  165. float endValue;
  166. /** The lowest value that will ever be set over the course of this function. */
  167. float lowValue;
  168. /** The difference between the lowest and highest value. */
  169. float delta;
  170. id<OALFunction,NSObject> function;
  171. /** The reverse function, if any. When this is not null, the reverse function is used. */
  172. OALReverseFunction* reverseFunction;
  173. /** The basic function that will be applied normally, or reversed. */
  174. id<OALFunction,NSObject> realFunction;
  175. }
  176. #pragma mark Properties
  177. /** The function that will be applied. */
  178. @property(readwrite,retain,nonatomic) id<OALFunction,NSObject> function;
  179. /** The value that the property in the target will hold at the start of the action. */
  180. @property(readwrite,assign,nonatomic) float startValue;
  181. /** The value that the property in the target will hold at the end of the action. */
  182. @property(readwrite,assign,nonatomic) float endValue;
  183. #pragma mark Object Management
  184. /** Create a new action using the default function.
  185. * The start value will be the current value of the target this action is applied to.
  186. *
  187. * @param duration The duration of this action in seconds.
  188. * @param endValue The "ending" value that this action will converge upon when setting the target's property.
  189. * @return A new action.
  190. */
  191. + (id) actionWithDuration:(float) duration endValue:(float) endValue;
  192. /** Create a new action.
  193. * The start value will be the current value of the target this action is applied to.
  194. *
  195. * @param duration The duration of this action in seconds.
  196. * @param endValue The "ending" value that this action will converge upon when setting the target's property.
  197. * @param function The function to apply in this action's update method.
  198. * @return A new action.
  199. */
  200. + (id) actionWithDuration:(float) duration
  201. endValue:(float) endValue
  202. function:(id<OALFunction,NSObject>) function;
  203. /** Create a new action.
  204. *
  205. * @param duration The duration of this action in seconds.
  206. * @param startValue The "starting" value that this action will diverge from when setting the target's
  207. * property. If NAN, use the current value from the target.
  208. * @param endValue The "ending" value that this action will converge upon when setting the target's property.
  209. * @param function The function to apply in this action's update method.
  210. * @return A new action.
  211. */
  212. + (id) actionWithDuration:(float) duration
  213. startValue:(float) startValue
  214. endValue:(float) endValue
  215. function:(id<OALFunction,NSObject>) function;
  216. /** Initialize an action using the default function.
  217. * The start value will be the current value of the target this action is applied to.
  218. *
  219. * @param duration The duration of this action in seconds.
  220. * @param endValue The "ending" value that this action will converge upon when setting the target's property.
  221. * @return The initialized action.
  222. */
  223. - (id) initWithDuration:(float) duration endValue:(float) endValue;
  224. /** Initialize an action.
  225. * The start value will be the current value of the target this action is applied to.
  226. *
  227. * @param duration The duration of this action in seconds.
  228. * @param endValue The "ending" value that this action will converge upon when setting the target's property.
  229. * @param function The function to apply in this action's update method.
  230. * @return The initialized action.
  231. */
  232. - (id) initWithDuration:(float) duration
  233. endValue:(float) endValue
  234. function:(id<OALFunction,NSObject>) function;
  235. /** Initialize an action.
  236. *
  237. * @param duration The duration of this action in seconds.
  238. * @param startValue The "starting" value that this action will diverge from when setting the target's
  239. * property. If NAN, use the current value from the target.
  240. * @param endValue The "ending" value that this action will converge upon when setting the target's property.
  241. * @param function The function to apply in this action's update method.
  242. * @return The initialized action.
  243. */
  244. - (id) initWithDuration:(float) duration
  245. startValue:(float) startValue
  246. endValue:(float) endValue
  247. function:(id<OALFunction,NSObject>) function;
  248. #pragma mark Utility
  249. /** Get the function that this action would use by default if none was specified. */
  250. + (id<OALFunction,NSObject>) defaultFunction;
  251. @end