-
Notifications
You must be signed in to change notification settings - Fork 115
/
SFMPieceView.mm
68 lines (56 loc) · 2.34 KB
/
SFMPieceView.mm
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
//
// SFMPieceView.m
// Stockfish
//
// Created by Daylen Yang on 1/11/14.
// Copyright (c) 2014 Daylen Yang. All rights reserved.
//
#import "SFMPieceView.h"
#import <QuartzCore/CAMediaTimingFunction.h>
@interface SFMPieceView()
@property SFMBoardView* boardView;
@end
#define PIECE_SHADOW_BLUR_RADIUS 5
@implementation SFMPieceView
- (id)initWithPieceType:(Chess::Piece)pieceType
onSquare:(Chess::Square)square
boardView:(SFMBoardView *)boardView
{
self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)];
if (self) {
NSString *name = [SFMPieceView fileNameForPieceType:pieceType];
[self setImage:[NSImage imageNamed:name]];
self.square = square;
self.boardView = boardView;
NSShadow *shadow = [NSShadow new];
[shadow setShadowBlurRadius:PIECE_SHADOW_BLUR_RADIUS];
[shadow setShadowColor:[NSColor colorWithWhite:0 alpha:0.5]]; // Gray
[self setShadow:shadow];
}
return self;
}
+ (NSString *)fileNameForPieceType:(Chess::Piece)pieceType
{
static NSDictionary *names = @{[NSNumber numberWithInt:WP]: @"pawn_w",
[NSNumber numberWithInt:WN]: @"knight_w",
[NSNumber numberWithInt:WB]: @"bishop_w",
[NSNumber numberWithInt:WR]: @"rook_w",
[NSNumber numberWithInt:WQ]: @"queen_w",
[NSNumber numberWithInt:WK]: @"king_w",
[NSNumber numberWithInt:BP]: @"pawn_b",
[NSNumber numberWithInt:BN]: @"knight_b",
[NSNumber numberWithInt:BB]: @"bishop_b",
[NSNumber numberWithInt:BR]: @"rook_b",
[NSNumber numberWithInt:BQ]: @"queen_b",
[NSNumber numberWithInt:BK]: @"king_b"};
return names[[NSNumber numberWithInt:pieceType]];
}
- (void)moveTo:(NSPoint)point
{
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.5];
[[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0.23 :1 :0.32 :1]];
[[self animator] setFrameOrigin:point];
[NSAnimationContext endGrouping];
}
@end