forked from joshclick/CanaClone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSneakyButton.m
executable file
·125 lines (105 loc) · 2.83 KB
/
SneakyButton.m
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
//
// button.m
// Classroom Demo
//
// Created by Nick Pannuto on 2/10/10.
// Copyright 2010 Sneakyness, llc.. All rights reserved.
//
#import "SneakyButton.h"
@implementation SneakyButton
@synthesize status, value, active, isHoldable, isToggleable, rateLimit, radius;
- (void) onEnterTransitionDidFinish
{
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:1 swallowsTouches:YES];
}
- (void) onExit
{
[[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
}
-(id)initWithRect:(CGRect)rect{
self = [super init];
if(self){
bounds = CGRectMake(0, 0, rect.size.width, rect.size.height);
center = CGPointMake(rect.size.width/2, rect.size.height/2);
status = 1; //defaults to enabled
active = NO;
value = 0;
isHoldable = 0;
isToggleable = 0;
radius = 32.0f;
rateLimit = 1.0f/120.0f;
position_ = rect.origin;
}
return self;
}
+(id) button
{
return [[[SneakyButton alloc] initWithRect:CGRectZero] autorelease];
}
+(id) buttonWithRect:(CGRect)rect
{
return [[[SneakyButton alloc] initWithRect:rect] autorelease];
}
-(void)limiter:(float)delta{
value = 0;
[self unschedule: @selector(limiter:)];
active = NO;
}
- (void) setRadius:(float)r
{
radius = r;
radiusSq = r*r;
}
#pragma mark Touch Delegate
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
if (active) return NO;
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
location = [self convertToNodeSpace:location];
//Do a fast rect check before doing a circle hit check:
if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){
return NO;
}else{
float dSq = location.x*location.x + location.y*location.y;
if(radiusSq > dSq){
active = YES;
if (!isHoldable && !isToggleable){
value = 1;
[self schedule: @selector(limiter:) interval:rateLimit];
}
if (isHoldable) value = 1;
if (isToggleable) value = !value;
return YES;
}
}
return NO;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
if (!active) return;
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
location = [self convertToNodeSpace:location];
//Do a fast rect check before doing a circle hit check:
if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){
return;
}else{
float dSq = location.x*location.x + location.y*location.y;
if(radiusSq > dSq){
if (isHoldable) value = 1;
}
else {
if (isHoldable) value = 0; active = NO;
}
}
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
if (!active) return;
if (isHoldable) value = 0;
if (isHoldable||isToggleable) active = NO;
}
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
[self ccTouchEnded:touch withEvent:event];
}
@end