ToyClipToAction.cpp
1.99 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
//
// ToyClipToAction.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 24.06.2017.
//
//
#include <stdio.h>
#include "ToyClipToAction.h"
ClipTo* ClipTo::create(float duration, const cocos2d::Rect& newRect)
{
ClipTo *ret = new (std::nothrow) ClipTo();
if (ret && ret->initWithDuration(duration, newRect))
{
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
bool ClipTo::initWithDuration(float duration, const cocos2d::Rect& newRect)
{
bool ret = false;
if (ActionInterval::initWithDuration(duration))
{
_toRect = newRect;
ret = true;
}
return ret;
}
ClipTo* ClipTo::clone() const
{
// no copy constructor
return ClipTo::create(_duration, _toRect);
}
void ClipTo::startWithTarget(cocos2d::Node *target)
{
ActionInterval::startWithTarget(target);
auto targetSprite = dynamic_cast<cocos2d::Sprite*>(target);
if(targetSprite){
_startRect = targetSprite->getTextureRect();
_deltaX = _toRect.origin.x - _startRect.origin.x;
_deltaY = _toRect.origin.y - _startRect.origin.y;
_deltaW = _toRect.size.width - _startRect.size.width;
_deltaH = _toRect.size.height - _startRect.size.height;
}
}
ClipTo* ClipTo::reverse() const
{
CCASSERT(false, "reverse() not supported in ClipTo");
return nullptr;
}
void ClipTo::update(float t)
{
if (_target)
{
auto targetSprite = dynamic_cast<cocos2d::Sprite*>(_target);
if(targetSprite){
auto dx = _deltaX*t;
auto dy = _deltaY*t;
auto dw = _deltaW*t;
auto dh = _deltaH*t;
cocos2d::Rect newRect = cocos2d::Rect(MAX(_startRect.origin.x + dx,0), MAX(_startRect.origin.y + dy,0), MAX(_startRect.size.width + dw,0), MAX(_startRect.size.height + dh,0));
targetSprite->setTextureRect(newRect);
targetSprite->setContentSize(newRect.size);
}
}
}