-
Notifications
You must be signed in to change notification settings - Fork 5
/
TMSliderControl.m
254 lines (218 loc) · 5.99 KB
/
TMSliderControl.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
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
//
// TMSliderControl.m
// TMSliderControl
//
// Created by Rick Fillion on 01/02/09.
// All code is provided under the New BSD license.
//
#import "TMSliderControl.h"
/*
* Basic NSImageView subclass for handle
*/
@interface TMSliderControlHandle : NSImageView {
}
@end
@implementation TMSliderControlHandle
- (void)mouseDown:(NSEvent *)theEvent { [[self superview] mouseDown:theEvent]; }
- (void)mouseDragged:(NSEvent*)theEvent { [[self superview] mouseDragged:theEvent]; }
- (void)mouseUp:(NSEvent*)theEvent { [[self superview] mouseUp:theEvent]; }
@end
/*
* Private control methods
*/
@interface TMSliderControl (Private)
- (void)drawBacking;
- (void)drawOverlay;
- (void)drawHandle;
@end
@implementation TMSliderControl (Private)
- (void)drawBacking
{
[sliderWell drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}
- (void)drawOverlay
{
[overlayMask drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}
- (void)drawHandle
{
NSImage *sliderImage;
if (controlState == kTMSliderControlState_Active)
{
sliderImage = sliderHandleDown;
}
else
{
sliderImage = sliderHandle;
}
[sliderHandleView setImage: sliderImage];
}
- (void)layoutHandle
{
handleControlRectOff = NSMakeRect(-2,1, 44, 27);
handleControlRectOn = NSMakeRect([self bounds].size.width - 42,1, 44, 27);
handleControlRect = handleControlRectOff;
}
@end
@implementation TMSliderControl
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
sliderWell = [[NSImage imageNamed:@"SliderWell"] retain];
overlayMask = [[NSImage imageNamed:@"OverlayMask"] retain];
sliderHandle = [[NSImage imageNamed:@"SliderHandle"] retain];
sliderHandleDown = [[NSImage imageNamed:@"SliderHandleDown"] retain];
[self layoutHandle];
sliderHandleView = [[TMSliderControlHandle alloc] initWithFrame: handleControlRectOff];
[sliderHandleView setWantsLayer: YES];
[self addSubview: sliderHandleView];
state = NO;
//[self setWantsLayer:YES];
}
return self;
}
- (void)dealloc
{
[sliderWell release];
[overlayMask release];
[sliderHandle release];
[sliderHandleDown release];
[sliderHandleView release];
[super dealloc];
}
- (void)awakeFromNib
{
[self setWantsLayer:YES];
}
- (void)drawRect:(NSRect)rect {
[self drawBacking];
[self drawHandle];
[self drawOverlay];
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void)mouseDown:(NSEvent*)theEvent
{
NSPoint mousePoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
hasDragged = NO;
// down on the position control rect
if(NSPointInRect(mousePoint,handleControlRect))
{
controlState = kTMSliderControlState_Active;
mouseDownPosition = NSMakePoint(mousePoint.x - handleControlRect.origin.x, 0);
[self setNeedsDisplay:YES];
}
}
- (void)mouseDragged:(NSEvent*)theEvent
{
NSPoint mousePoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
hasDragged = YES;
// dragging the position control rect
if(controlState == kTMSliderControlState_Active)
{
// center the rect around the mouse point
float newXPosition = mousePoint.x - mouseDownPosition.x;
// clamp the position .
if (newXPosition < handleControlRectOff.origin.x)
newXPosition = handleControlRectOff.origin.x;
if (newXPosition > handleControlRectOn.origin.x)
newXPosition = handleControlRectOn.origin.x;
handleControlRect.origin.x = newXPosition;
[sliderHandleView setFrame: handleControlRect];
[self setNeedsDisplay:YES];
}
}
- (void)mouseUp:(NSEvent*)theEvent
{
float minimumMovement = 10.0;
if(controlState != kTMSliderControlState_Inactive)
{
// switch the state to inactive and redraw
controlState = kTMSliderControlState_Inactive;
if (!hasDragged)
{
// if they never dragged, but clicked/released in place on the handle, flip it over.
[self setState:![self state]];
}
else if (state == NSOffState)
{
if (handleControlRect.origin.x > minimumMovement)
{
// moved it enough to set it
[self setState: NSOnState];
}
else
{
// put it back where it was.
[self setState: NSOffState];
}
}
else
{
if (handleControlRect.origin.x < [self bounds].size.width - handleControlRect.size.width - minimumMovement)
{
// moved it enough to set it
[self setState: NSOffState];
}
else
{
// put it back where it was.
[self setState: NSOnState];
}
}
}
else
{
[self setState:![self state]];
}
}
- (BOOL)state
{
return state;
}
- (void)setState:(NSInteger)newState
{
[super setState:newState];
if ([self state] == NSOffState)
{
handleControlRect = handleControlRectOff;
}
else
{
handleControlRect = handleControlRectOn;
}
if ([sliderHandleView window])
{
// It's in a window, we can use CoreAnimation
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration: 0.15];
[[sliderHandleView animator] setFrameOrigin: handleControlRect.origin];
[NSAnimationContext endGrouping];
}
else {
// It's not in a window, just set it.
[sliderHandleView setFrameOrigin:handleControlRect.origin];
}
[self sendAction:[self action] to:[self target]];
[self setNeedsDisplay: YES];
}
- (void)setTarget:(id)anObject
{
target = anObject;
}
- (id)target
{
return target;
}
- (void)setAction:(SEL)aSelector
{
action = aSelector;
}
- (SEL)action
{
return action;
}
@end