summary refs log tree commit diff stats
path: root/Classes/CocosOverlayScrollView.m
blob: 9db0575033743d0671df1bd82ba2f4797ed886da (plain) (blame)
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
//
//  CocosOverlayScrollView.m
//  shapes
//
//  Created by Nate Murray on 8/23/10.
//  Copyright 2010 LittleHiccup. All rights reserved.
//

#import "CocosOverlayScrollView.h"

@implementation CocosOverlayScrollView
@synthesize targetLayer;

// Configure your favorite UIScrollView options here
-(id) initWithFrame: (CGRect) frameRect numPages: (int) numPages width: (float) width layer: (CCNode*) layer pageControl:(UIPageControl *)m_pageControl{
    if ((self = [super initWithFrame: frameRect])){ 
        self.contentSize = CGSizeMake(width*numPages, 320);
        self.bounces = YES;
        self.delaysContentTouches = NO;
        self.delegate = self;
        self.pagingEnabled = YES;
        self.scrollsToTop = NO;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        [self setUserInteractionEnabled:TRUE];
        [self setScrollEnabled:TRUE];
        self.targetLayer = layer;
        // self.canCancelContentTouches  = YES;
        pageControl = m_pageControl;
    }
    return self;
}

-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
    if (!self.dragging)
    {
        // UITouch* touch = [[touches allObjects] objectAtIndex:0];
        // CGPoint location = [touch locationInView: [[touch view] superview]];
        // CCLOG(@"touch at l.x:%f l.y:%f", location.x, location.y);

        [self.nextResponder touchesBegan: touches withEvent:event];
        [[[CCDirector sharedDirector] openGLView] touchesBegan:touches withEvent:event];
    }

    [super touchesBegan: touches withEvent: event];
}

-(void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
    if (!self.dragging)
    {
        [self.nextResponder touchesEnded: touches withEvent:event];
        [[[CCDirector sharedDirector] openGLView] touchesEnded:touches withEvent:event];
    }

    [super touchesEnded: touches withEvent: event];
}

-(void) touchesCancelled: (NSSet *) touches withEvent: (UIEvent *) event
{
    // if (!self.dragging)
    // {
        // CCLOG(@"CocosOverlayScrollView touchesEnded not dragging");
        [self.nextResponder touchesCancelled: touches withEvent:event];
        [[[CCDirector sharedDirector] openGLView] touchesCancelled:touches withEvent:event];
    // }
    [super touchesCancelled: touches withEvent: event];
}


- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
  // TODO - Custom code for handling deceleration of the scroll view
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint dragPt = [scrollView contentOffset];
    dragPt = [[CCDirector sharedDirector] convertToGL:dragPt];

    dragPt.y = dragPt.y * -1;
    dragPt.x = dragPt.x * -1;

    CGPoint newLayerPosition = CGPointMake(dragPt.x, dragPt.y);

    [targetLayer setPosition:newLayerPosition];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // CGPoint dragPt = [scrollView contentOffset];
    // etc.
}

-(void) dealloc {
    self.targetLayer = nil;
    [super dealloc];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int currentPage = (NSUInteger)(scrollView.contentOffset.x / scrollView.frame.size.width);
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:currentPage forKey:@"lastSelectedMode"];
    
    [pageControl setCurrentPage:currentPage];
}

@end