| 1 | /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | * SOFTWARE. |
| 20 | */ |
| 21 | |
| 22 | #import "ObjectiveChipmunk/ObjectiveChipmunk.h" |
| 23 | |
| 24 | @interface ChipmunkGrab : NSObject<ChipmunkObject> { |
| 25 | NSArray *_chipmunkObjects; |
| 26 | |
| 27 | cpVect _pos; |
| 28 | cpFloat _smoothing; |
| 29 | |
| 30 | ChipmunkShape *_grabbedShape; |
| 31 | |
| 32 | id _data; |
| 33 | } |
| 34 | |
| 35 | /// Last touch location of the grab. |
| 36 | @property(nonatomic, readonly) cpVect pos; |
| 37 | |
| 38 | /// The ChipmunkShape that this grab was created for. |
| 39 | @property(nonatomic, readonly) ChipmunkShape *grabbedShape; |
| 40 | |
| 41 | /// User definable pointer |
| 42 | @property(nonatomic, retain) id data; |
| 43 | |
| 44 | @end |
| 45 | |
| 46 | |
| 47 | /// Simple class to implement multitouch grabbing of physics objects. |
| 48 | @interface ChipmunkMultiGrab : NSObject { |
| 49 | ChipmunkSpace *_space; |
| 50 | NSMutableArray *_grabs; |
| 51 | |
| 52 | cpFloat _smoothing; |
| 53 | cpFloat _grabForce; |
| 54 | |
| 55 | cpFloat _grabFriction; |
| 56 | cpFloat _grabRotaryFriction; |
| 57 | cpFloat _grabRadius; |
| 58 | |
| 59 | cpShapeFilter filter; |
| 60 | bool (^_grabFilter)(ChipmunkShape *shape); |
| 61 | cpFloat (^_grabSort)(ChipmunkShape *shape, cpFloat depth); |
| 62 | |
| 63 | bool _pushMode, _pullMode; |
| 64 | |
| 65 | cpFloat _pushMass; |
| 66 | cpFloat _pushFriction; |
| 67 | cpFloat _pushElasticity; |
| 68 | cpCollisionType _pushCollisionType; |
| 69 | } |
| 70 | |
| 71 | @property(nonatomic, assign) cpFloat smoothing; |
| 72 | @property(nonatomic, assign) cpFloat grabForce; |
| 73 | |
| 74 | /// Layers used for the point query when grabbing objects. |
| 75 | @property(nonatomic, assign) cpShapeFilter filter; |
| 76 | |
| 77 | /// Group used for the point query when grabbing objects |
| 78 | @property(nonatomic, assign) cpGroup group; |
| 79 | |
| 80 | /// Gives you the opportunity to further filter shapes. Return FALSE to ignore a shape. |
| 81 | /// The default implementation always returns TRUE. |
| 82 | @property(nonatomic, copy) bool (^grabFilter)(ChipmunkShape *shape); |
| 83 | |
| 84 | /// When clicking on a spot where two shapes overlap, the default behavior is to grab the shape that |
| 85 | /// overlaps the grab point the most. It's possible to use a custom sorting order instead however. |
| 86 | /// The block is called with each shape and the grab depth. |
| 87 | /// It should return a positive float. The shape with the highest value is grabbed. |
| 88 | /// The block is only called if the touch location is within a shape. |
| 89 | @property(nonatomic, copy) cpFloat (^grabSort)(ChipmunkShape *shape, cpFloat depth); |
| 90 | |
| 91 | /// Amount of friction applied by the touch. |
| 92 | /// Should be less than the grabForce. Defaults to 0.0. |
| 93 | @property(nonatomic, assign) cpFloat grabFriction; |
| 94 | |
| 95 | /// The amount torque to apply to the grab to keep it from spinning. |
| 96 | /// Defaults to 0.0. |
| 97 | @property(nonatomic, assign) cpFloat grabRotaryFriction; |
| 98 | |
| 99 | /// On a touch screen, a single point query can make it really hard to grab small objects with a fat finger. |
| 100 | /// By providing a radius, it will make it much easier for users to grab objects. |
| 101 | /// Defaults to 0.0. |
| 102 | @property(nonatomic, assign) cpFloat grabRadius; |
| 103 | |
| 104 | @property(nonatomic, assign) bool pullMode; |
| 105 | @property(nonatomic, assign) bool pushMode; |
| 106 | |
| 107 | @property(nonatomic, assign) cpFloat pushMass; |
| 108 | @property(nonatomic, assign) cpFloat pushFriction; |
| 109 | @property(nonatomic, assign) cpFloat pushElasticity; |
| 110 | @property(nonatomic, assign) cpCollisionType pushCollisionType; |
| 111 | |
| 112 | @property(nonatomic, readonly) NSArray *grabs; |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | @c space is the space to grab shapes in. |
| 117 | @c smoothing is the amount of mouse smoothing to apply as percentage of remaining error per second. |
| 118 | cpfpow(0.8, 60) is a good starting point that provides fast response, but smooth mouse updates. |
| 119 | @c force is the force the grab points can apply. |
| 120 | */ |
| 121 | -(id)initForSpace:(ChipmunkSpace *)space withSmoothing:(cpFloat)smoothing withGrabForce:(cpFloat)grabForce; |
| 122 | |
| 123 | /// Start tracking a new grab point |
| 124 | /// Returns the ChipmunkGrab that is tracking the touch, but only if a shape was grabbed. |
| 125 | /// Returns nil when creating a push shape (if push mode is enabled), or when no shape is grabbed. |
| 126 | -(ChipmunkGrab *)beginLocation:(cpVect)pos; |
| 127 | |
| 128 | /// Update a grab point. |
| 129 | /// Returns the ChipmunkGrab that is tracking the touch, but only if the grab is tracking a shape. |
| 130 | -(ChipmunkGrab *)updateLocation:(cpVect)pos; |
| 131 | |
| 132 | /// End a grab point. |
| 133 | /// Returns the ChipmunkGrab that was tracking the touch, but only if the grab was tracking a shape. |
| 134 | -(ChipmunkGrab *)endLocation:(cpVect)pos; |
| 135 | |
| 136 | @end |