Browse Source

Read day-of-week sprite atlas and display correctly

Thomas Buck 9 years ago
parent
commit
b8dd6ceeaf

+ 1
- 1
OtaClock/Images.xcassets/bubble.imageset/Contents.json View File

3
     {
3
     {
4
       "idiom" : "universal",
4
       "idiom" : "universal",
5
       "scale" : "1x",
5
       "scale" : "1x",
6
-      "filename" : "bubble.png"
6
+      "filename" : "bubbleTest.png"
7
     },
7
     },
8
     {
8
     {
9
       "idiom" : "universal",
9
       "idiom" : "universal",

BIN
OtaClock/Images.xcassets/bubble.imageset/bubble.png → OtaClock/Images.xcassets/bubble.imageset/bubbleTest.png View File


+ 1
- 1
OtaClock/Info.plist View File

19
 	<key>CFBundleSignature</key>
19
 	<key>CFBundleSignature</key>
20
 	<string>????</string>
20
 	<string>????</string>
21
 	<key>CFBundleVersion</key>
21
 	<key>CFBundleVersion</key>
22
-	<string>147</string>
22
+	<string>162</string>
23
 	<key>LSApplicationCategoryType</key>
23
 	<key>LSApplicationCategoryType</key>
24
 	<string>public.app-category.utilities</string>
24
 	<string>public.app-category.utilities</string>
25
 	<key>LSMinimumSystemVersion</key>
25
 	<key>LSMinimumSystemVersion</key>

+ 2
- 1
OtaClock/MainView.m View File

6
 //  Copyright (c) 2015 xythobuz. All rights reserved.
6
 //  Copyright (c) 2015 xythobuz. All rights reserved.
7
 //
7
 //
8
 
8
 
9
+#import "Render.h"
9
 #import "MainWindow.h"
10
 #import "MainWindow.h"
10
 #import "MainView.h"
11
 #import "MainView.h"
11
 
12
 
18
 @synthesize render;
19
 @synthesize render;
19
 
20
 
20
 -(void)awakeFromNib {
21
 -(void)awakeFromNib {
21
-    render = [[Render alloc] init];
22
+    render = [[Render alloc] initWithParent:self];
22
     [(MainWindow*)[self window] setDefaultBackgroundSize:[render baseSize]]; // Set window to a useful default size
23
     [(MainWindow*)[self window] setDefaultBackgroundSize:[render baseSize]]; // Set window to a useful default size
23
 }
24
 }
24
 
25
 

+ 12
- 4
OtaClock/MainWindow.m View File

117
     if (startScale == 4) [self.changeSize4 setState:NSOnState];
117
     if (startScale == 4) [self.changeSize4 setState:NSOnState];
118
     if (startScale == 5) [self.changeSize5 setState:NSOnState];
118
     if (startScale == 5) [self.changeSize5 setState:NSOnState];
119
     
119
     
120
-    [[self.mainView render] drawWith:lastEyeState]; // Initialize render image
120
+    [[self.mainView render] drawWithEye:lastEyeState]; // Initialize render image
121
     [self unblink]; // Schedule next blinking
121
     [self unblink]; // Schedule next blinking
122
     
122
     
123
+    // Start time keeping
124
+    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
125
+    [self updateTime:nil];
126
+    
123
     [self setFrame:frame display:YES];
127
     [self setFrame:frame display:YES];
124
 }
128
 }
125
 
129
 
130
+- (void)updateTime:(id)sender {
131
+    [[self.mainView render] drawWithDate:[NSDate date]];
132
+}
133
+
126
 - (void)blink {
134
 - (void)blink {
127
     if (currentlyBlinking == NO) {
135
     if (currentlyBlinking == NO) {
128
         currentlyBlinking = YES;
136
         currentlyBlinking = YES;
129
-        [[self.mainView render] drawWith:EYE_BLINK];
137
+        [[self.mainView render] drawWithEye:EYE_BLINK];
130
         self.mainView.needsDisplay = YES;
138
         self.mainView.needsDisplay = YES;
131
     }
139
     }
132
     
140
     
136
 - (void)unblink {
144
 - (void)unblink {
137
     if (currentlyBlinking == YES) {
145
     if (currentlyBlinking == YES) {
138
         currentlyBlinking = NO;
146
         currentlyBlinking = NO;
139
-        [[self.mainView render] drawWith:lastEyeState];
147
+        [[self.mainView render] drawWithEye:lastEyeState];
140
         self.mainView.needsDisplay = YES;
148
         self.mainView.needsDisplay = YES;
141
     }
149
     }
142
     
150
     
271
     if (eyeState != lastEyeState) {
279
     if (eyeState != lastEyeState) {
272
         lastEyeState = eyeState;
280
         lastEyeState = eyeState;
273
         if (currentlyBlinking == NO) {
281
         if (currentlyBlinking == NO) {
274
-            [[self.mainView render] drawWith:lastEyeState];
282
+            [[self.mainView render] drawWithEye:lastEyeState];
275
             self.mainView.needsDisplay = YES;
283
             self.mainView.needsDisplay = YES;
276
         }
284
         }
277
     }
285
     }

+ 5
- 2
OtaClock/Render.h View File

8
 
8
 
9
 #import <Cocoa/Cocoa.h>
9
 #import <Cocoa/Cocoa.h>
10
 
10
 
11
+@class MainView;
12
+
11
 @interface Render : NSObject
13
 @interface Render : NSObject
12
 
14
 
13
-- (id)init;
15
+- (id)initWithParent:(MainView *)par;
14
 - (NSSize)baseSize;
16
 - (NSSize)baseSize;
15
 
17
 
16
-- (void)drawWith:(NSInteger)eyeIndex;
18
+- (void)drawWithDate:(NSDate *)date;
19
+- (void)drawWithEye:(NSInteger)eyeIndex;
17
 - (void)drawInto:(NSView *)view;
20
 - (void)drawInto:(NSView *)view;
18
 
21
 
19
 @end
22
 @end

+ 90
- 13
OtaClock/Render.m View File

6
 //  Copyright (c) 2015 xythobuz. All rights reserved.
6
 //  Copyright (c) 2015 xythobuz. All rights reserved.
7
 //
7
 //
8
 
8
 
9
+#import "MainView.h"
9
 #import "Render.h"
10
 #import "Render.h"
10
 
11
 
11
 #define FULL_IMAGE_WIDTH 86
12
 #define FULL_IMAGE_WIDTH 86
15
 #define EYE_X_OFFSET 60
16
 #define EYE_X_OFFSET 60
16
 #define EYE_Y_OFFSET 45
17
 #define EYE_Y_OFFSET 45
17
 
18
 
19
+#define FONT_DAYS_WIDTH 17
20
+#define FONT_DAYS_HEIGHT 3
21
+#define FONT_DAYS_PADDING 1
22
+#define FONT_DAYS_X_OFFSET 31
23
+#define FONT_DAYS_Y_OFFSET 72
24
+
18
 @interface Render ()
25
 @interface Render ()
19
 
26
 
20
-@property (assign) CGImageRef otaconGraphic;
21
-@property (assign) CGImageRef bubbleGraphic;
27
+@property (assign) CGImageRef otaconGraphic, bubbleGraphic;
22
 @property (assign) CGImageRef eye0, eye1, eye2, eye3, eye4;
28
 @property (assign) CGImageRef eye0, eye1, eye2, eye3, eye4;
29
+@property (assign) CGImageRef fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday;
30
+@property (assign) NSInteger eyeToDraw, dayOfWeek;
23
 
31
 
24
 @property (assign) NSSize fullSize;
32
 @property (assign) NSSize fullSize;
25
 @property (assign) CGContextRef drawContext;
33
 @property (assign) CGContextRef drawContext;
26
 
34
 
35
+@property (weak) MainView *parent;
36
+
27
 @end
37
 @end
28
 
38
 
29
 @implementation Render
39
 @implementation Render
30
 
40
 
31
-@synthesize otaconGraphic;
32
-@synthesize bubbleGraphic;
41
+@synthesize otaconGraphic, bubbleGraphic;
33
 @synthesize eye0, eye1, eye2, eye3, eye4;
42
 @synthesize eye0, eye1, eye2, eye3, eye4;
43
+@synthesize fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday;
44
+@synthesize eyeToDraw, dayOfWeek;
34
 
45
 
35
 @synthesize fullSize;
46
 @synthesize fullSize;
36
 @synthesize drawContext;
47
 @synthesize drawContext;
48
+@synthesize parent;
37
 
49
 
38
 - (CGImageRef)eyeHelper:(NSInteger)index {
50
 - (CGImageRef)eyeHelper:(NSInteger)index {
39
     if (index == 0) return eye0;
51
     if (index == 0) return eye0;
46
     return eye0;
58
     return eye0;
47
 }
59
 }
48
 
60
 
49
-- (id)init {
61
+- (CGImageRef)dayHelper:(NSInteger)index {
62
+    if (index == -1) return fontSunday;
63
+    if (index == 0) return fontMonday;
64
+    if (index == 1) return fontTuesday;
65
+    if (index == 2) return fontWednesday;
66
+    if (index == 3) return fontThursday;
67
+    if (index == 4) return fontFriday;
68
+    if (index == 5) return fontSaturday;
69
+    if (index == 6) return fontSunday;
70
+    
71
+    NSLog(@"Render:dayHelper:%ld unknown index!", (long)index);
72
+    return fontMonday;
73
+}
74
+
75
+- (id)initWithParent:(MainView *)par {
50
     self = [super init];
76
     self = [super init];
51
     if (self == nil) return nil;
77
     if (self == nil) return nil;
52
     
78
     
79
+    parent = par;
80
+    
53
     NSImage *otaconImage = [NSImage imageNamed:@"otacon"];
81
     NSImage *otaconImage = [NSImage imageNamed:@"otacon"];
54
     NSImage *bubbleImage = [NSImage imageNamed:@"bubble"];
82
     NSImage *bubbleImage = [NSImage imageNamed:@"bubble"];
55
     NSImage *eye0Image = [NSImage imageNamed:@"eyes_0"];
83
     NSImage *eye0Image = [NSImage imageNamed:@"eyes_0"];
57
     NSImage *eye2Image = [NSImage imageNamed:@"eyes_2"];
85
     NSImage *eye2Image = [NSImage imageNamed:@"eyes_2"];
58
     NSImage *eye3Image = [NSImage imageNamed:@"eyes_3"];
86
     NSImage *eye3Image = [NSImage imageNamed:@"eyes_3"];
59
     NSImage *eye4Image = [NSImage imageNamed:@"eyes_4"];
87
     NSImage *eye4Image = [NSImage imageNamed:@"eyes_4"];
88
+    NSImage *fontDaysImage = [NSImage imageNamed:@"font_days"];
60
     
89
     
61
     NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithAttributes:nil];
90
     NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithAttributes:nil];
62
     
91
     
68
     eye3 = [eye3Image CGImageForProposedRect:nil context:context hints:nil];
97
     eye3 = [eye3Image CGImageForProposedRect:nil context:context hints:nil];
69
     eye4 = [eye4Image CGImageForProposedRect:nil context:context hints:nil];
98
     eye4 = [eye4Image CGImageForProposedRect:nil context:context hints:nil];
70
     
99
     
100
+    CGImageRef fontDays = [fontDaysImage CGImageForProposedRect:nil context:context hints:nil];
101
+    NSRect rectDay;
102
+    rectDay.size.width = FONT_DAYS_WIDTH;
103
+    rectDay.size.height = FONT_DAYS_HEIGHT;
104
+    rectDay.origin.x = 0;
105
+    rectDay.origin.y = 0;
106
+    fontMonday = CGImageCreateWithImageInRect(fontDays, rectDay);
107
+    rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
108
+    fontTuesday = CGImageCreateWithImageInRect(fontDays, rectDay);
109
+    rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
110
+    fontWednesday = CGImageCreateWithImageInRect(fontDays, rectDay);
111
+    rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
112
+    fontThursday = CGImageCreateWithImageInRect(fontDays, rectDay);
113
+    rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
114
+    fontFriday = CGImageCreateWithImageInRect(fontDays, rectDay);
115
+    rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
116
+    fontSaturday = CGImageCreateWithImageInRect(fontDays, rectDay);
117
+    rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
118
+    fontSunday = CGImageCreateWithImageInRect(fontDays, rectDay);
119
+    
71
     fullSize.width = FULL_IMAGE_WIDTH;
120
     fullSize.width = FULL_IMAGE_WIDTH;
72
     fullSize.height = FULL_IMAGE_HEIGHT;
121
     fullSize.height = FULL_IMAGE_HEIGHT;
73
     
122
     
74
     drawContext = CGBitmapContextCreate(NULL, fullSize.width, fullSize.height, 8, 0, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
123
     drawContext = CGBitmapContextCreate(NULL, fullSize.width, fullSize.height, 8, 0, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
75
     
124
     
125
+    eyeToDraw = 0;
126
+    dayOfWeek = 0;
127
+    
76
     return self;
128
     return self;
77
 }
129
 }
78
 
130
 
80
     return fullSize;
132
     return fullSize;
81
 }
133
 }
82
 
134
 
83
-- (void)drawWith:(NSInteger)eyeIndex {
84
-    CGRect size;
135
+- (void)drawWithDate:(NSDate *)date {
136
+    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date];
137
+    dayOfWeek = [components weekday] - 2;
85
     
138
     
139
+    
140
+    // Check if something changed, if so, set needsDisplay
141
+    // if (bla) {
142
+    //     parent.needsDisplay = YES;
143
+    // }
144
+}
145
+
146
+- (void)drawWithEye:(NSInteger)eyeIndex {
147
+    eyeToDraw = eyeIndex;
148
+}
149
+
150
+- (void)drawInto:(NSView *)view {
86
     // Clear entire canvas
151
     // Clear entire canvas
152
+    CGRect size;
87
     size.size = fullSize;
153
     size.size = fullSize;
88
     size.origin = NSZeroPoint;
154
     size.origin = NSZeroPoint;
89
     CGContextClearRect(drawContext, size);
155
     CGContextClearRect(drawContext, size);
94
     size.origin.y = BUBBLE_Y_OFFSET;
160
     size.origin.y = BUBBLE_Y_OFFSET;
95
     CGContextDrawImage(drawContext, size, bubbleGraphic);
161
     CGContextDrawImage(drawContext, size, bubbleGraphic);
96
     
162
     
163
+    // Draw Date
164
+
165
+    // Draw Day of Week
166
+    CGImageRef day = [self dayHelper:dayOfWeek];
167
+    size.size.width = FONT_DAYS_WIDTH;
168
+    size.size.height = FONT_DAYS_HEIGHT;
169
+    size.origin.x = FONT_DAYS_X_OFFSET;
170
+    size.origin.y = FONT_DAYS_Y_OFFSET;
171
+    CGContextDrawImage(drawContext, size, day);
172
+    
173
+    // Draw Time
174
+    
97
     // Draw Otacon
175
     // Draw Otacon
98
     size.size.width = CGImageGetWidth(otaconGraphic);
176
     size.size.width = CGImageGetWidth(otaconGraphic);
99
     size.size.height = CGImageGetHeight(otaconGraphic);
177
     size.size.height = CGImageGetHeight(otaconGraphic);
102
     CGContextDrawImage(drawContext, size, otaconGraphic);
180
     CGContextDrawImage(drawContext, size, otaconGraphic);
103
     
181
     
104
     // Draw eyes
182
     // Draw eyes
105
-    size.size.width = CGImageGetWidth([self eyeHelper:eyeIndex]);
106
-    size.size.height = CGImageGetHeight([self eyeHelper:eyeIndex]);
183
+    size.size.width = CGImageGetWidth([self eyeHelper:eyeToDraw]);
184
+    size.size.height = CGImageGetHeight([self eyeHelper:eyeToDraw]);
107
     size.origin.x = EYE_X_OFFSET;
185
     size.origin.x = EYE_X_OFFSET;
108
     size.origin.y = EYE_Y_OFFSET;
186
     size.origin.y = EYE_Y_OFFSET;
109
-    CGContextDrawImage(drawContext, size, [self eyeHelper:eyeIndex]);
110
-}
111
-
112
-- (void)drawInto:(NSView *)view {
187
+    CGContextDrawImage(drawContext, size, [self eyeHelper:eyeToDraw]);
188
+    
189
+    // Render resulting canvas into bitmap and scale this into our window
113
     CGImageRef drawnImage = CGBitmapContextCreateImage(drawContext);
190
     CGImageRef drawnImage = CGBitmapContextCreateImage(drawContext);
114
     NSImage *result = [[NSImage alloc] initWithCGImage:drawnImage size:fullSize];
191
     NSImage *result = [[NSImage alloc] initWithCGImage:drawnImage size:fullSize];
115
     [result drawInRect:[view bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:NO hints:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:NSImageInterpolationNone] forKey:NSImageHintInterpolation]];
192
     [result drawInRect:[view bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:NO hints:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:NSImageInterpolationNone] forKey:NSImageHintInterpolation]];

Loading…
Cancel
Save