CCScrollLayer.cpp

leo leo, 2011-06-27 12:05

Download (3.3 kB)

 
1
//  CCScrollLayer.cpp
2
//  Museum
3
//
4
//  Created by GParvaneh on 29/12/2010.
5
//  Copyright 2010. All rights reserved.
6
//  Ported to C++ by Lior Tamam on 03/04/2011
7
#include "CCScrollLayer.h"
8

    
9
CCScrollLayer* CCScrollLayer::layerWithLayers(CCMutableArray<CCLayer*> *layers, int widthOffset)
10
{        
11
        CCScrollLayer *pRet = new CCScrollLayer();
12
        if (pRet && pRet->initWithLayers(layers, widthOffset))
13
        {
14
                pRet->autorelease();
15
                return pRet;
16
        }
17
        CC_SAFE_DELETE(pRet)
18
        return NULL;
19
}
20

    
21
bool CCScrollLayer::initWithLayers(CCMutableArray<CCLayer*> *layers, int widthOffset)
22
{        
23
        if (CCLayer::init())
24
        {                
25
                // Make sure the layer accepts touches
26
                CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true);
27
                
28
                // Set up the starting variables
29
                if(!widthOffset)
30
                {
31
                        widthOffset = 0;
32
                }        
33
                currentScreen = 1;
34
                
35
                // offset added to show preview of next/previous screens
36
                scrollWidth  = (int)CCDirector::sharedDirector()->getWinSize().width - widthOffset;
37
                scrollHeight = (int)CCDirector::sharedDirector()->getWinSize().height;
38
                startWidth = scrollWidth;
39
                startHeight = scrollHeight;
40
                
41
                // Loop through the array and add the screens
42
                unsigned int i;
43
                for (i=0; i<layers->count(); i++)
44
                {
45
                        CCLayer* l = layers->getObjectAtIndex(i);
46
                        l->setAnchorPoint(ccp(0,0));
47
                        l->setPosition(ccp((i*scrollWidth),0));
48
                        addChild(l);                        
49
                }
50
                
51
                // Setup a count of the available screens
52
                totalScreens = layers->count();
53
                return true;        
54
        }
55
        else
56
        {
57
                return false;
58
        }        
59
}
60

    
61
void CCScrollLayer::moveToPage(int page)
62
{        
63
        CCEaseBounce* changePage = CCEaseBounce::actionWithAction(CCMoveTo::actionWithDuration(0.3f, ccp(-((page-1)*scrollWidth),0)));
64
        this->runAction(changePage);
65
        currentScreen = page;        
66
}
67

    
68
void CCScrollLayer::moveToNextPage()
69
{        
70
        CCEaseBounce* changePage = CCEaseBounce::actionWithAction(CCMoveTo::actionWithDuration(0.3f, ccp(-(((currentScreen+1)-1)*scrollWidth),0)));
71
        
72
        this->runAction(changePage);
73
        currentScreen = currentScreen+1;        
74
}
75

    
76
void CCScrollLayer::moveToPreviousPage()
77
{        
78
        CCEaseBounce* changePage =CCEaseBounce::actionWithAction(CCMoveTo::actionWithDuration(0.3f, ccp(-(((currentScreen-1)-1)*scrollWidth),0)));
79
        this->runAction(changePage);
80
        currentScreen = currentScreen-1;        
81
}
82

    
83
void CCScrollLayer::onExit()
84
{
85
        CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
86
        CCLayer::onExit();
87
}
88

    
89
bool CCScrollLayer::ccTouchBegan(CCTouch *touch, CCEvent *withEvent)
90
{
91
        
92
        CCPoint touchPoint = touch->locationInView(touch->view());
93
        touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
94
        
95
        startSwipe = (int)touchPoint.x;
96
        return true;
97
}
98

    
99
void CCScrollLayer::ccTouchMoved(CCTouch *touch, CCEvent *withEvent)
100
{        
101
        CCPoint touchPoint = touch->locationInView(touch->view());
102
        touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
103
        
104
        this->setPosition(ccp((-(currentScreen-1)*scrollWidth)+(touchPoint.x-startSwipe),0));
105
}
106

    
107
void CCScrollLayer::ccTouchEnded(CCTouch *touch, CCEvent *withEvent)
108
{
109
        
110
        CCPoint touchPoint = touch->locationInView(touch->view());
111
        touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
112
        
113
        int newX = (int)touchPoint.x;
114
                
115
        if ( (newX - startSwipe) < -scrollWidth / 3 && (currentScreen+1) <= totalScreens )
116
        {
117
                this->moveToNextPage();
118
        }
119
        else if ( (newX - startSwipe) > scrollWidth / 3 && (currentScreen-1) > 0 )
120
        {
121
                this->moveToPreviousPage();
122
        }
123
        else
124
        {
125
                this->moveToPage(currentScreen);                
126
        }                
127
}