-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYStarRatingView.m
283 lines (236 loc) · 8.86 KB
/
SSYStarRatingView.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// SSYStarRatingViewView.
//
// Created by Ernesto Garcia on 26/02/12.
// Copyright (c) 2012 cocoawithchurros.com All rights reserved.
// Distributed under MIT license
#import "SSYStarRatingView.h"
#import "SSYVectorImages.h"
#import "NSObject+SSYBindingsHelp.h"
#define SSY_STAR_RATING_VIEW_DEFAULT_HALFSTAR_THRESHOLD 0.6
NSString* const constKeyRating = @"rating" ;
@implementation SSYStarRatingView
@synthesize starImage = m_starImage;
@synthesize removeXImage = m_removeXImage ;
@synthesize starHighlightedImage = m_starHighlightedImage ;
@synthesize rating = m_rating;
@synthesize maxRating = m_maxRating ;
@synthesize backgroundColor = m_backgroundColor;
@synthesize editable = m_editable ;
@synthesize delegate = m_delegate ;
@synthesize horizontalMargin = m_horizontalMargin;
//@synthesize drawHalfStars;
@synthesize halfStarThreshold = m_halfStarThreshold ;
@synthesize displayMode = m_displayMode ;
+ (void)initialize {
[self exposeBinding:constKeyRating] ;
}
#pragma mark -
#pragma mark Init & dealloc
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGFloat starHeight = frame.size.height ;
[self setMaxRating:5.0] ;
[self setHorizontalMargin:0.0] ;
[self setDisplayMode:SSYStarRatingViewDisplayFull] ;
[self setHalfStarThreshold:SSY_STAR_RATING_VIEW_DEFAULT_HALFSTAR_THRESHOLD] ;
[self setStarImage:[SSYVectorImages imageStyle:SSYVectorImageStyleStar
wength:starHeight
color:[NSColor lightGrayColor]
darkModeView:self
rotateDegrees:0.0
inset:0.0]] ;
[self setStarHighlightedImage:[SSYVectorImages imageStyle:SSYVectorImageStyleStar
wength:starHeight
color:[NSColor blueColor]
darkModeView:self
rotateDegrees:0.0
inset:0.0]] ;
[self setRemoveXImage:[SSYVectorImages imageStyle:SSYVectorImageStyleRemoveX
wength:(0.9 * starHeight)
color:nil
darkModeView:self
rotateDegrees:0.0
inset:0.0]] ;
[self setEditable:YES] ;
}
return self;
}
#if (__ppc__)
#define NO_ARC 1
#else
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1060
#define NO_ARC 1
#else
#if __has_feature(objc_arc)
#define NO_ARC 0
#else
#define NO_ARC 1
#endif
#endif
#endif
#if !__has_feature(objc_arc)
-(void)dealloc
{
[m_starImage release] ;
[m_starHighlightedImage release] ;
[m_removeXImage release] ;
[m_backgroundColor release] ;
[m_rating release] ;
[super dealloc] ;
}
#endif
#pragma mark -
#pragma mark Setters
-(void)setRating:(NSNumber*)rating
{
[m_rating release] ;
m_rating = rating ;
[m_rating retain] ;
self.needsDisplay = YES;
}
- (NSNumber*)rating {
return [[m_rating copy] autorelease] ;
}
-(void)setDisplayMode:(SSYStarRatingViewDisplayMode)dispMode
{
m_displayMode = dispMode ;
self.needsDisplay = YES;
}
#pragma mark -
#pragma mark Drawing
-(NSPoint)pointOfStarAtPosition:(NSInteger)position highlighted:(BOOL)hightlighted
{
NSSize size = hightlighted ? [self starHighlightedImage].size : [self starImage].size;
NSInteger starsSpace = self.bounds.size.width - 2*[self horizontalMargin] - [[self removeXImage] size].width ;
CGFloat removeXImageWidth = [[self removeXImage] size].width ;
BOOL hasRemoveXButton = (removeXImageWidth > 0 ) ;
NSInteger numberOfSpaces = [self maxRating] - 1 + (hasRemoveXButton ? 1 : 0) ;
NSInteger numberOfStars = [self maxRating] ;
NSInteger interSpace = 0 ;
interSpace = numberOfSpaces > 0 ? (starsSpace - numberOfStars*size.width)/numberOfSpaces : 0 ;
if(interSpace < 0) {
interSpace = 0 ;
}
CGFloat x = [self horizontalMargin] + removeXImageWidth + size.width*position ;
if((position > 0) || hasRemoveXButton) {
// This star is either not the first star, or it is
// the first star but is preceded by the Remove (X)
// button.
CGFloat extraMarginForRemoveX = hasRemoveXButton ? 1 : 0 ;
x += interSpace*(position + extraMarginForRemoveX) ;
}
CGFloat y = (self.bounds.size.height - size.height)/2.0 ;
return NSMakePoint(x,y) ;
}
- (void)drawRect:(NSRect)dirtyRect
{
#if 0
NSBezierPath *p = [NSBezierPath bezierPathWithRect:self.bounds];
[[NSColor redColor] set] ;
[p stroke] ;
#endif
// Draw background color
NSColor *colorToDraw = [self backgroundColor]==nil?[NSColor clearColor]:[self backgroundColor];
[colorToDraw set];
NSBezierPath *path = [NSBezierPath bezierPathWithRect:self.bounds];
[path fill] ;
// Draw the Remove(X) Circle
NSSize removeXSize = [[self removeXImage] size] ;
CGFloat removeXTweak = -1.0 ;
[[self removeXImage] drawAtPoint:NSMakePoint([self horizontalMargin], (self.bounds.size.height - removeXSize.height)/2.0 + removeXTweak)
fromRect:NSMakeRect(0.0, 0.0, removeXSize.width, removeXSize.height)
operation:NSCompositingOperationSourceOver
fraction:1.0] ;
// Draw stars
NSSize starSize = [[self starImage] size] ;
NSSize starHighlightedSize = [[self starHighlightedImage] size] ;
for( NSInteger i=0 ; i<[self maxRating]; i++ )
{
[[self starImage] drawAtPoint:[self pointOfStarAtPosition:i
highlighted:YES]
fromRect:NSMakeRect(0.0, 0.0, starSize.width, starSize.height)
operation:NSCompositingOperationSourceOver
fraction:1.0] ;
if( i < [[self rating] floatValue])
{
[NSGraphicsContext saveGraphicsState];
NSBezierPath *pathClip=nil;
NSPoint starPoint = [self pointOfStarAtPosition:i highlighted:NO];
if( i< [[self rating] floatValue] && [[self rating] floatValue] < i+1 )
{
float difference = [[self rating] floatValue] - i;
NSRect rectClip;
rectClip.origin = starPoint;
rectClip.size = starSize;
if([self displayMode] == SSYStarRatingViewDisplayHalf && difference < [self halfStarThreshold]) // Draw half star image
{
rectClip.size.width/=2.0;
pathClip = [NSBezierPath bezierPathWithRect:rectClip];
}
else if( [self displayMode] == SSYStarRatingViewDisplayAccurate )
{
rectClip.size.width*=difference;
pathClip = [NSBezierPath bezierPathWithRect:rectClip];
}
if( pathClip)
[pathClip addClip];
}
[[self starHighlightedImage] drawAtPoint:starPoint fromRect:NSMakeRect(0.0, 0.0, starHighlightedSize.width, starHighlightedSize.height) operation:NSCompositingOperationSourceOver fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
}
}
if ([[self rating] floatValue] > 0.0) {
CGFloat removeXImageWidth = [[self removeXImage] size].width ;
BOOL hasRemoveXButton = (removeXImageWidth > 0 ) ;
if (hasRemoveXButton) {
[self addToolTipRect:NSMakeRect(0.0, 0.0, removeXImageWidth, [self bounds].size.height)
owner:@"Reset to 'Unrated'" // We can get away with not retaining this because it is a constant string.
userData:nil] ;
}
}
}
#pragma mark -
#pragma mark Mouse Interaction
-(NSInteger) starsForPoint:(NSPoint)point
{
NSInteger stars=0;
for( NSInteger i=0; i<[self maxRating]; i++ )
{
NSPoint p =[self pointOfStarAtPosition:i highlighted:NO];
if( point.x > p.x )
stars=i+1;
}
return stars;
}
-(void)mouseDown:(NSEvent *)theEvent {
if( ![self editable] )
return;
if ([theEvent type] == NSEventTypeLeftMouseDown) {
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
NSNumber* rating = [NSNumber numberWithInteger:[self starsForPoint:pointInView]] ;
[self setRating:rating] ;
// This was moved here from -setRating: in BookMacster 1.11.6
[self pushBindingValue:rating
forKey:constKeyRating] ;
self.needsDisplay = YES;
}
}
-(void)mouseDragged:(NSEvent *)theEvent
{
if( ![self editable] )
return;
NSPoint pointInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
[self setRating:[NSNumber numberWithInteger:[self starsForPoint:pointInView]]] ;
self.needsDisplay = YES;
}
-(void)mouseUp:(NSEvent *)theEvent
{
if( ![self editable] )
return;
if( [[self delegate] respondsToSelector:@selector(starsSelectionChanged:rating:)] )
[[self delegate] starsSelectionChanged:self rating:[[self rating] floatValue]];
}
@end