This repository was archived by the owner on Jun 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRPScrollingNode.m
More file actions
334 lines (246 loc) · 9.82 KB
/
RPScrollingNode.m
File metadata and controls
334 lines (246 loc) · 9.82 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
// RPScrollingNode.m
//
// Copyright (c) 2013 Robots and Pencils ( http://robotsandpencils.com/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "RPScrollingNode.h"
#define kRPScrollingNodeRowTag 29876
typedef enum {
kRPScrollStateTouched = 0,
kRPScrollStateScrolling,
kRPScrollStateEnding,
} RPScrollState;
@interface RPScrollingNode () {
}
@property (nonatomic) RPScrollState scrollState;
@property (nonatomic) CGPoint touchBeganPos;
@end
@implementation RPScrollingNode
@synthesize nodes = nodes_;
@synthesize isEnabled = isEnabled_;
@synthesize minVerticalScrollDistance = _minVerticalScrollDistance;
@synthesize scrollState = _scrollState;
@synthesize touchBeganPos = _touchBeganPos;
@synthesize delegate = _delegate;
+(id) scrollingNodeWithNodes:(NSArray *)nodes height:(NSInteger)height
{
return [[[self alloc] initWithNodes:nodes height:height] autorelease];
}
- (id)initWithNodes:(NSArray *)nodes height:(NSInteger)height{
self = [super init];
if (self) {
self.isTouchEnabled = YES;
self.isRelativeAnchorPoint = YES;
viewableHeight_ = height;
slidingNode_ = [CCNode node];
[self addChild:slidingNode_];
self.nodes = nodes;
self.isEnabled = YES;
// default min distance moved before scrolling
self.minVerticalScrollDistance = 15.0;
}
return self;
}
- (void)loadNodes{
CGSize size = CGSizeZero;
for (CCNode *node in nodes_) {
size.width = (MAX([node contentSize].width, size.width));
size.height += [node contentSize].height;
}
[slidingNode_ setContentSize:size];
slidingNode_.anchorPoint = ccp(0.5f, 0.5f);
[self setContentSize:CGSizeMake(size.width, viewableHeight_)];
NSInteger nodeIndex = 0;
for (CCNode *node in nodes_) {
CGSize nodeSize = [node contentSize];
NSInteger nodeHeight = ([nodes_ count] - 1 - nodeIndex ) * nodeSize.height;
node.position = ccp(size.width * .5, nodeHeight + nodeSize.height * .5);
node.tag = kRPScrollingNodeRowTag;
[slidingNode_ addChild:node];
nodeIndex++;
}
slidingNode_.position = ccp(size.width * .5, viewableHeight_ - ([slidingNode_ contentSize].height * .5));
}
- (id)init
{
self = [self initWithNodes:nil height:0.0f];
if (self) {
}
return self;
}
- (void)dealloc {
[nodes_ release];
[super dealloc];
}
- (void)setNodes:(NSArray *)nodes{
[slidingNode_ removeAllChildrenWithCleanup:YES];
[nodes_ release];
nodes_ = [nodes retain];
[self loadNodes];
// visual indicator of refresh
[self runSlideUpAnimation];
}
- (void)runSlideUpAnimation{
CGPoint newPosition = ccpAdd(slidingNode_.position, ccp(0.0f, -viewableHeight_));
slidingNode_.position = newPosition;
[self bounceBackIfNeeded];
}
#pragma mark Touch Handling
-(void) registerWithTouchDispatcher
{
CCTouchDispatcher *dispatcher = [CCTouchDispatcher sharedDispatcher];
// make we have touch priority over any CCMenu and don't swallow touches
[dispatcher addTargetedDelegate: self priority:kCCMenuTouchPriority-1 swallowsTouches:NO];
}
// returns YES if touch is inside our boundingBox
-(BOOL) isTouchForMe:(UITouch *) touch
{
CGPoint point = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[touch locationInView: [touch view]]]];
CGPoint prevPoint = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[touch previousLocationInView: [touch view]]]];
CGRect rect = CGRectMake(0, 0, self.contentSize.width, self.contentSize.height);
if ( CGRectContainsPoint(rect, point) || CGRectContainsPoint(rect, prevPoint) )
return YES;
return NO;
}
-(CCMenuItem *) itemForTouch: (UITouch *) touch
{
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
CCMenuItem* item;
CCARRAY_FOREACH(children_, item){
if ( [item visible] ) {
CGPoint local = [item convertToNodeSpace:touchLocation];
CGRect r = [item boundingBox];
r.origin = CGPointZero;
if( CGRectContainsPoint( r, local ) )
return item;
}
}
return nil;
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
if (!isEnabled_)
return NO;
if( !visible_ )
return NO;
curTouchLength_ = 0; //< every new touch should reset previous touch length
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
// save the touch position before converting to node space
self.touchBeganPos = touchPoint;
// convert parent's node space
touchPoint = [self.parent convertToNodeSpace:touchPoint];
if (!CGRectContainsPoint([self boundingBox], touchPoint)) {
return NO;
}
// start slide even if touch began outside of menuitems, but inside menu rect
if ([self isTouchForMe: touch] ){
self.scrollState = kRPScrollStateTouched;
if ([self.delegate respondsToSelector:@selector(rpScrollLayerTouchBegan:)]){
[self.delegate rpScrollLayerTouchBegan:self];
}
return YES;
}
return NO;
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
// get touch move delta
CGPoint point = [touch locationInView: [touch view]];
point = [ [CCDirector sharedDirector] convertToGL: point ];
if (self.scrollState == kRPScrollStateTouched) {
CGPoint testDelta = ccpSub(point, self.touchBeganPos);
// check to see if changes in total distance is greater then min distance to scroll
if (ccpLength(testDelta) > self.minVerticalScrollDistance){
self.scrollState = kRPScrollStateScrolling;
if ([self.delegate respondsToSelector:@selector(rpScrollLayerScrollingStarted:)]){
[self.delegate rpScrollLayerScrollingStarted:self];
}
}
else {
return;
}
}
CGPoint prevPoint = [touch previousLocationInView:[touch view]];
prevPoint = [ [CCDirector sharedDirector] convertToGL: prevPoint ];
CGPoint delta = ccpSub(point, prevPoint);
curTouchLength_ += ccpLength( delta );
// cancel out sideways movement
delta.x = 0;
// can never let the top item go below the top of the content
CGPoint newPosition = ccpAdd(slidingNode_.position, delta );
slidingNode_.position = newPosition;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
[self bounceBackIfNeeded];
self.scrollState = kRPScrollStateEnding;
if ([self.delegate respondsToSelector:@selector(rpScrollLayerTouchEnded:)]){
[self.delegate rpScrollLayerTouchEnded:self];
}
}
-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
[self bounceBackIfNeeded];
self.scrollState = kRPScrollStateEnding;
if ([self.delegate respondsToSelector:@selector(rpScrollLayerTouchCancelled:)]){
[self.delegate rpScrollLayerTouchCancelled:self];
}
}
- (void) visit
{
if (!self.visible) {
return;
}
// clip outside bounding box
glEnable(GL_SCISSOR_TEST);
CGRect box = [self boundingBox];
// convert bounding box to world space
CGPoint worldSpaceOrigin = [self.parent convertToWorldSpace:box.origin];
CGRect scissorRect = CGRectMake(worldSpaceOrigin.x, worldSpaceOrigin.y, box.size.width, box.size.height);
// convert to pixel space
scissorRect = CC_RECT_POINTS_TO_PIXELS(scissorRect);
glScissor(scissorRect.origin.x, scissorRect.origin.y, scissorRect.size.width, scissorRect.size.height);
[super visit];
glDisable(GL_SCISSOR_TEST);
}
- (void)bounceBackIfNeeded{
CGRect box = [self boundingBox];
CGFloat topOfSlidingNode = slidingNode_.position.y + ([slidingNode_ boundingBox].size.height * .5);
CGPoint adjustPoint = CGPointMake(slidingNode_.position.x, 0.0f);
if (topOfSlidingNode < box.size.height ) {
adjustPoint.y = box.size.height - ([slidingNode_ boundingBox].size.height * .5);
}
CGFloat bottomOfSlidingNode = slidingNode_.position.y - ([slidingNode_ boundingBox].size.height * .5);
// this is for when the sliding node height is greater than content size
if (bottomOfSlidingNode > 0 && [slidingNode_ contentSize].height >= box.size.height) {
adjustPoint.y = ([slidingNode_ boundingBox].size.height * .5);
}else if (bottomOfSlidingNode > 0 && [slidingNode_ contentSize].height < box.size.height) {
adjustPoint.y = box.size.height - ([slidingNode_ boundingBox].size.height * .5);
}
if (adjustPoint.y != 0.0f) {
CCMoveTo *moveTo = [CCMoveTo actionWithDuration:0.3f position:adjustPoint];
CCEaseElastic *ease = [CCEaseElastic actionWithAction:moveTo];
[slidingNode_ runAction:ease];
}
}
@end