VLCHUDSliderCell.m 12.3 KB
Newer Older
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
//
//  VLCHUDSliderCell.m
//  BGHUDAppKit
//
//  Created by BinaryGod on 5/30/08.
//
//  Copyright (c) 2008, Tim Davis (BinaryMethod.com, binary.god@gmail.com)
//  All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without modification,
//  are permitted provided that the following conditions are met:
//
//		Redistributions of source code must retain the above copyright notice, this
//	list of conditions and the following disclaimer.
//
//		Redistributions in binary form must reproduce the above copyright notice,
//	this list of conditions and the following disclaimer in the documentation and/or
//	other materials provided with the distribution.
//
//		Neither the name of the BinaryMethod.com nor the names of its contributors
//	may be used to endorse or promote products derived from this software without
//	specific prior written permission.
//
//	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
//	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//	IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
//	INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//	BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
//	OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
//	WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//	POSSIBILITY OF SUCH DAMAGE.
34 35

#import "VLCHUDSliderCell.h"
36
#import "CompatibilityFixes.h"
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

@implementation VLCHUDSliderCell

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        // Custom colors for the slider
        _sliderColor            = [NSColor colorWithCalibratedRed:0.318 green:0.318 blue:0.318 alpha:0.6];
        _disabledSliderColor    = [NSColor colorWithCalibratedRed:0.318 green:0.318 blue:0.318 alpha:0.2];
        _strokeColor            = [NSColor colorWithCalibratedRed:0.749 green:0.761 blue:0.788 alpha:1.0];
        _disabledStrokeColor    = [NSColor colorWithCalibratedRed:0.749 green:0.761 blue:0.788 alpha:0.2];

        // Custom knob gradients
        _knobGradient           = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251 green:0.251 blue:0.255 alpha:1.0]
                                                                endingColor:[NSColor colorWithDeviceRed:0.118 green:0.118 blue:0.118 alpha:1.0]];
        _disableKnobGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251 green:0.251 blue:0.255 alpha:1.0]
                                                                endingColor:[NSColor colorWithDeviceRed:0.118 green:0.118 blue:0.118 alpha:1.0]];
    }
    return self;
}

NSAffineTransform* RotationTransform(const CGFloat angle, const NSPoint point)
{
    NSAffineTransform*	transform = [NSAffineTransform transform];
    [transform translateXBy:point.x yBy:point.y];
    [transform rotateByRadians:angle];
    [transform translateXBy:-(point.x) yBy:-(point.y)];
    return transform;
}

68 69
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
70
- (void)drawKnob:(NSRect)smallRect
71
{
72 73 74
    if (OSX_YOSEMITE_AND_HIGHER) {
        return [super drawKnob:smallRect];
    }
75 76
    NSBezierPath *path = [NSBezierPath bezierPath];
    // Inset rect to have enough room for the stroke
77
    smallRect = NSInsetRect(smallRect, 1.0, 1.0);
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

    // Get min/max/mid coords for shape calculations
    CGFloat minX = NSMinX(smallRect);
    CGFloat minY = NSMinY(smallRect);
    CGFloat maxX = NSMaxX(smallRect);
    CGFloat maxY = NSMaxY(smallRect);
    CGFloat midX = NSMidX(smallRect);
    CGFloat midY = NSMidY(smallRect);

    // Draw the knobs shape
    if (self.numberOfTickMarks > 0) {
        // We have tickmarks, draw an arrow-like shape
        if (self.isVertical) {
            // For some reason the rect is not alligned correctly at
            // tickmarks and clipped, so this ugly thing is necessary:
            maxY = maxY - 2;
            midY = midY - 1;

            // Right pointing arrow
            [path moveToPoint:NSMakePoint(minX + 3, minY)];
            [path lineToPoint:NSMakePoint(midX + 2, minY)];
            [path lineToPoint:NSMakePoint(maxX, midY)];
            [path lineToPoint:NSMakePoint(midX + 2, maxY)];
            [path lineToPoint:NSMakePoint(minX + 3, maxY)];
            [path appendBezierPathWithArcFromPoint:NSMakePoint(minX, maxY)
                                           toPoint:NSMakePoint(minX, maxY - 3)
                                            radius:2.5f];
            [path lineToPoint:NSMakePoint(minX, maxY - 3)];
            [path lineToPoint:NSMakePoint(minX, minY + 3)];
            [path appendBezierPathWithArcFromPoint:NSMakePoint(minX, minY)
                                           toPoint:NSMakePoint(minX + 3, minY)
                                            radius:2.5f];
            [path closePath];
        } else {
            // Down pointing arrow
            [path moveToPoint:NSMakePoint(minX + 3, minY)];
            [path lineToPoint:NSMakePoint(maxX - 3, minY)];
            [path appendBezierPathWithArcFromPoint:NSMakePoint(maxX, minY)
                                           toPoint:NSMakePoint(maxX, minY + 3)
                                            radius:2.5f];
            [path lineToPoint:NSMakePoint(maxX, minY + 3)];
            [path lineToPoint:NSMakePoint(maxX, midY + 2)];
            [path lineToPoint:NSMakePoint(midX, maxY)];
            [path lineToPoint:NSMakePoint(minX, midY + 2)];
            [path lineToPoint:NSMakePoint(minX, minY + 3)];
            [path appendBezierPathWithArcFromPoint:NSMakePoint(minX, minY)
                                           toPoint:NSMakePoint(minX + 3, minY)
                                            radius:2.5f];
            [path closePath];
        }

        // Rotate our knob if needed to the correct position
        if (self.tickMarkPosition == NSTickMarkAbove) {
            NSAffineTransform *transform = nil;
            transform = RotationTransform(M_PI, NSMakePoint(midX, midY));
            [path transformUsingAffineTransform:transform];
        }
    } else {
        // We have no tickmarks, draw a round knob
        [path appendBezierPathWithOvalInRect:NSInsetRect(smallRect, 0.5, 0.5)];
    }

    // Draw the knobs background
    if (self.isEnabled && self.isHighlighted) {
        [_knobGradient drawInBezierPath:path angle:270.0f];
    } else if (self.isEnabled) {
        [_knobGradient drawInBezierPath:path angle:90.0f];
    } else {
        [_disableKnobGradient drawInBezierPath:path angle:90.0f];
    }

    // Draw white stroke around the knob
    if (self.isEnabled) {
        [_strokeColor setStroke];
    } else {
        [_disabledStrokeColor setStroke];
    }

156
    [path setLineWidth:1.0];
157 158 159
    [path stroke];
}

160 161
- (void)drawBarInside:(NSRect)fullRect flipped:(BOOL)flipped
{
162 163 164
    if (OSX_YOSEMITE_AND_HIGHER) {
        return [super drawBarInside:fullRect flipped:flipped];
    }
165 166 167 168 169 170 171
    if (self.isVertical) {
        return [self drawVerticalBarInFrame:fullRect];
    } else {
        return [self drawHorizontalBarInFrame:fullRect];
    }
}
#pragma clang diagnostic pop
172

173 174 175 176
- (void)drawVerticalBarInFrame:(NSRect)frame
{
    // Adjust frame based on ControlSize
    switch ([self controlSize]) {
177

178
        case NSRegularControlSize:
179

180 181 182 183 184 185
            if ([self numberOfTickMarks] != 0) {
                if ([self tickMarkPosition] == NSTickMarkRight) {
                    frame.origin.x += 4;
                } else {
                    frame.origin.x += frame.size.width - 9;
                }
186
            } else {
187
                frame.origin.x = frame.origin.x + (((frame.origin.x + frame.size.width) /2) - 2.5f);
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
            frame.origin.x += 0.5f;
            frame.origin.y += 2.5f;
            frame.size.height -= 6;
            frame.size.width = 5;
            break;

        case NSSmallControlSize:

            if ([self numberOfTickMarks] != 0) {
                if ([self tickMarkPosition] == NSTickMarkRight) {
                    frame.origin.x += 3;
                } else {
                    frame.origin.x += frame.size.width - 8;
                }
            } else {
                frame.origin.x = frame.origin.x + (((frame.origin.x + frame.size.width) /2) - 2.5f);
            }
            frame.origin.y += 0.5f;
            frame.size.height -= 1;
            frame.origin.x += 0.5f;
            frame.size.width = 5;
            break;

        case NSMiniControlSize:

            if ([self numberOfTickMarks] != 0) {
                if ([self tickMarkPosition] == NSTickMarkRight) {
                    frame.origin.x += 2.5f;
                } else {
                    frame.origin.x += frame.size.width - 6.5f;
                }
            } else {
                frame.origin.x = frame.origin.x + (((frame.origin.x + frame.size.width) /2) - 2);
            }
            frame.origin.x += 1;
            frame.origin.y += 0.5f;
            frame.size.height -= 1;
            frame.size.width = 3;
            break;
    }
    
    // Draw Bar
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect: frame xRadius: 2 yRadius: 2];
    
    if ([self isEnabled]) {
        [_sliderColor set];
        [path fill];
236

237 238 239 240
        [_strokeColor set];
        [path stroke];
    } else {
        [_disabledSliderColor set];
241
        [path fill];
242 243 244

        [_disabledStrokeColor set];
        [path stroke];
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 284 285 286 287
}

- (void)drawHorizontalBarInFrame:(NSRect)frame
{
    // Adjust frame based on ControlSize
    switch ([self controlSize]) {

        case NSRegularControlSize:

            if ([self numberOfTickMarks] != 0) {
                if ([self tickMarkPosition] == NSTickMarkBelow) {
                    frame.origin.y += 4;
                } else {
                    frame.origin.y += frame.size.height - 10;
                }
            } else {
                frame.origin.y = frame.origin.y + (((frame.origin.y + frame.size.height) /2) - 2.5f);
            }
            frame.origin.x += 2.5f;
            frame.origin.y += 0.5f;
            frame.size.width -= 5;
            frame.size.height = 5;
            break;

        case NSSmallControlSize:

            if ([self numberOfTickMarks] != 0) {
                if ([self tickMarkPosition] == NSTickMarkBelow) {
                    frame.origin.y += 2;
                } else {
                    frame.origin.y += frame.size.height - 8;
                }
            } else {
                frame.origin.y = frame.origin.y + (((frame.origin.y + frame.size.height) /2) - 2.5f);
            }
            frame.origin.x += 0.5f;
            frame.origin.y += 0.5f;
            frame.size.width -= 1;
            frame.size.height = 5;
            break;

        case NSMiniControlSize:
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
            if ([self numberOfTickMarks] != 0) {
                if ([self tickMarkPosition] == NSTickMarkBelow) {
                    frame.origin.y += 2;
                } else {
                    frame.origin.y += frame.size.height - 6;
                }
            } else {
                frame.origin.y = frame.origin.y + (((frame.origin.y + frame.size.height) /2) - 2);
            }
            frame.origin.x += 0.5f;
            frame.origin.y += 0.5f;
            frame.size.width -= 1;
            frame.size.height = 3;
            break;
    }
    
    // Draw Bar
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:2 yRadius:2];
    
    if ([self isEnabled]) {
        [_sliderColor set];
        [path fill];
        
        [_strokeColor set];
        [path stroke];
    } else {
        [_disabledSliderColor set];
        [path fill];
        
        [_disabledStrokeColor set];
        [path stroke];
    }
321 322
}

323 324
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
325 326
- (void)drawTickMarks
{
327 328 329
    if (OSX_YOSEMITE_AND_HIGHER) {
        return [super drawTickMarks];
    }
330 331 332 333 334 335 336 337 338 339
    for (int i = 0; i < self.numberOfTickMarks; i++) {
        NSRect tickMarkRect = [self rectOfTickMarkAtIndex:i];
        if (self.isEnabled) {
            [_strokeColor setFill];
        } else {
            [_disabledStrokeColor setFill];
        }
        NSRectFill(tickMarkRect);
    }
}
340
#pragma clang diagnostic pop
341

342
@end