-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMTILcdView.m
235 lines (190 loc) · 6.72 KB
/
MTILcdView.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
//
// MTILcdView.m
// Copyright 2009 Ryan Morlok
// http://softwareblog.morlok.net/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "MTILcdView.h"
#define LCD_DRAWING_UNSCALED_HEIGHT 90.0
#define LCD_DRAWING_UNSCALED_WIDTH 50.0
#define LCD_DRAWING_BETWEEN_DIGIT_SPACE 10.0
// The elements of the LCD digit are numbered as follows:
//
// /----3----\
// | |
// 2 4
// | |
// \ /
// ----7----
// / \
// | |
// 1 5
// | |
// \----6----/
//
// The following constants translate these positions into a bitmask
#define LCD_POS_1 0x01
#define LCD_POS_2 0x02
#define LCD_POS_3 0x04
#define LCD_POS_4 0x08
#define LCD_POS_5 0x10
#define LCD_POS_6 0x20
#define LCD_POS_7 0x40
// Translates a character into a bitmask for the LCD elements
NSUInteger BitmaskForDigit(char c)
{
switch (c) {
case '0' : return LCD_POS_1 | LCD_POS_2 | LCD_POS_3 | LCD_POS_4 | LCD_POS_5 | LCD_POS_6;
case '1' : return LCD_POS_4 | LCD_POS_5;
case '2' : return LCD_POS_3 | LCD_POS_4 | LCD_POS_7 | LCD_POS_1 | LCD_POS_6;
case '3' : return LCD_POS_3 | LCD_POS_4 | LCD_POS_7 | LCD_POS_5 | LCD_POS_6;
case '4' : return LCD_POS_2 | LCD_POS_7 | LCD_POS_4 | LCD_POS_5;
case '5' : return LCD_POS_3 | LCD_POS_2 | LCD_POS_7 | LCD_POS_5 | LCD_POS_6;
case '6' : return LCD_POS_3 | LCD_POS_2 | LCD_POS_7 | LCD_POS_1 | LCD_POS_6 | LCD_POS_5;
case '7' : return LCD_POS_3 | LCD_POS_4 | LCD_POS_5;
case '8' : return LCD_POS_1 | LCD_POS_2 | LCD_POS_3 | LCD_POS_4 | LCD_POS_5 | LCD_POS_6 | LCD_POS_7;
case '9' : return LCD_POS_2 | LCD_POS_3 | LCD_POS_4 | LCD_POS_5 | LCD_POS_6 | LCD_POS_7;
default : return 0;
}
}
@implementation MTILcdView
#pragma mark Private Methods
- (void) drawColonToContext:(CGContextRef)context
{
CGFloat size = 0.85 * LCD_DRAWING_BETWEEN_DIGIT_SPACE;
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, litColor);
CGContextAddEllipseInRect(context, CGRectMake((LCD_DRAWING_BETWEEN_DIGIT_SPACE - size)/2.0, 25.0 - (size/2.0), size, size));
CGContextAddEllipseInRect(context, CGRectMake((LCD_DRAWING_BETWEEN_DIGIT_SPACE - size)/2.0, 65.0 - (size/2.0), size, size));
CGContextFillPath(context);
CGContextRestoreGState(context);
}
- (void) drawDigit:(NSUInteger)digitDef toContext:(CGContextRef)context
{
CGContextSaveGState(context);
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathMoveToPoint(myPath, NULL, 5.0, 1.0);
CGPathAddLineToPoint(myPath, NULL, 0.0, 6.0);
CGPathAddLineToPoint(myPath, NULL, 0.0, 34.0);
CGPathAddLineToPoint(myPath, NULL, 5.0, 39.0);
CGPathAddLineToPoint(myPath, NULL, 10.0, 34.0);
CGPathAddLineToPoint(myPath, NULL, 10.0, 6.0);
CGPathCloseSubpath(myPath);
CGContextSetFillColorWithColor(context, (digitDef & LCD_POS_1 ? litColor : dimColor));
CGContextTranslateCTM(context, 0.0, 5.0);
CGContextAddPath(context, myPath);
CGContextFillPath(context);
CGContextSetFillColorWithColor(context, (digitDef & LCD_POS_2 ? litColor : dimColor));
CGContextTranslateCTM(context, 0.0, 40.0);
CGContextAddPath(context, myPath);
CGContextFillPath(context);
CGContextSetFillColorWithColor(context, (digitDef & LCD_POS_3 ? litColor : dimColor));
CGContextTranslateCTM(context, 5.0, 45.0);
CGContextRotateCTM(context, -pi / 2.0);
CGContextAddPath(context, myPath);
CGContextFillPath(context);
CGContextSetFillColorWithColor(context, (digitDef & LCD_POS_4 ? litColor : dimColor));
CGContextTranslateCTM(context, 5.0, 45.0);
CGContextRotateCTM(context, -pi / 2.0);
CGContextAddPath(context, myPath);
CGContextFillPath(context);
CGContextSetFillColorWithColor(context, (digitDef & LCD_POS_5 ? litColor : dimColor));
CGContextTranslateCTM(context, 0.0, 40.0);
CGContextAddPath(context, myPath);
CGContextFillPath(context);
CGContextSetFillColorWithColor(context, (digitDef & LCD_POS_6 ? litColor : dimColor));
CGContextTranslateCTM(context, 5.0, 45.0);
CGContextRotateCTM(context, -pi / 2.0);
CGContextAddPath(context, myPath);
CGContextFillPath(context);
CGContextSetFillColorWithColor(context, (digitDef & LCD_POS_7 ? litColor : dimColor));
CGContextTranslateCTM(context, 40.0, 0.0);
CGContextAddPath(context, myPath);
CGContextFillPath(context);
CGPathRelease(myPath);
CGContextRestoreGState(context);
}
- (CGAffineTransform) scaleTranformForFrame
{
// Compute how much we should scale our drawing based on the
// needed height versus the height that is available.
CGFloat scale = [self bounds].size.height / LCD_DRAWING_UNSCALED_HEIGHT;
return CGAffineTransformMakeScale(scale, scale);
}
#pragma mark Public Methods
@synthesize litColor;
@synthesize dimColor;
@synthesize text;
- (void) setLitColor:(CGColorRef)color
{
if( color == litColor )
return;
CGColorRelease(litColor);
litColor = CGColorRetain(color);
[self setNeedsDisplay:YES];
}
- (void) setDimColor:(CGColorRef)color
{
if( color == dimColor )
return;
CGColorRelease(dimColor);
dimColor = CGColorRetain(color);
[self setNeedsDisplay:YES];
}
- (void) setText:(NSString*)t
{
if( text == t )
return;
[text release];
text = [t retain];
[self setNeedsDisplay:YES];
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Nothing to see here...
}
return self;
}
- (void)drawRect:(NSRect)rect {
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
// Scale the drawing for the current size of the view
CGContextConcatCTM(myContext, [self scaleTranformForFrame]);
// Draw the digits
for(NSUInteger i = 0; i < text.length; i++) {
if( ':' == [text characterAtIndex:i] ) {
// Colon is special case that fits in the normal space between two numbers
CGContextSaveGState(myContext);
if( i > 0 )
CGContextTranslateCTM(myContext, LCD_DRAWING_UNSCALED_WIDTH, 0);
[self drawColonToContext:myContext];
CGContextRestoreGState(myContext);
} else {
if( i > 0 )
CGContextTranslateCTM(myContext, LCD_DRAWING_UNSCALED_WIDTH + LCD_DRAWING_BETWEEN_DIGIT_SPACE, 0);
[self drawDigit:BitmaskForDigit([text characterAtIndex:i]) toContext:myContext];
}
}
}
- (void)dealloc
{
[text release];
CGColorRelease(litColor);
CGColorRelease(dimColor);
text = nil;
litColor = NULL;
dimColor = NULL;
[super dealloc];
}
@end