Browse Source

Read day-of-week sprite atlas and display correctly

Thomas Buck 8 years ago
parent
commit
b8dd6ceeaf

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

@@ -3,7 +3,7 @@
3 3
     {
4 4
       "idiom" : "universal",
5 5
       "scale" : "1x",
6
-      "filename" : "bubble.png"
6
+      "filename" : "bubbleTest.png"
7 7
     },
8 8
     {
9 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,7 +19,7 @@
19 19
 	<key>CFBundleSignature</key>
20 20
 	<string>????</string>
21 21
 	<key>CFBundleVersion</key>
22
-	<string>147</string>
22
+	<string>162</string>
23 23
 	<key>LSApplicationCategoryType</key>
24 24
 	<string>public.app-category.utilities</string>
25 25
 	<key>LSMinimumSystemVersion</key>

+ 2
- 1
OtaClock/MainView.m View File

@@ -6,6 +6,7 @@
6 6
 //  Copyright (c) 2015 xythobuz. All rights reserved.
7 7
 //
8 8
 
9
+#import "Render.h"
9 10
 #import "MainWindow.h"
10 11
 #import "MainView.h"
11 12
 
@@ -18,7 +19,7 @@
18 19
 @synthesize render;
19 20
 
20 21
 -(void)awakeFromNib {
21
-    render = [[Render alloc] init];
22
+    render = [[Render alloc] initWithParent:self];
22 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,16 +117,24 @@
117 117
     if (startScale == 4) [self.changeSize4 setState:NSOnState];
118 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 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 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 134
 - (void)blink {
127 135
     if (currentlyBlinking == NO) {
128 136
         currentlyBlinking = YES;
129
-        [[self.mainView render] drawWith:EYE_BLINK];
137
+        [[self.mainView render] drawWithEye:EYE_BLINK];
130 138
         self.mainView.needsDisplay = YES;
131 139
     }
132 140
     
@@ -136,7 +144,7 @@
136 144
 - (void)unblink {
137 145
     if (currentlyBlinking == YES) {
138 146
         currentlyBlinking = NO;
139
-        [[self.mainView render] drawWith:lastEyeState];
147
+        [[self.mainView render] drawWithEye:lastEyeState];
140 148
         self.mainView.needsDisplay = YES;
141 149
     }
142 150
     
@@ -271,7 +279,7 @@
271 279
     if (eyeState != lastEyeState) {
272 280
         lastEyeState = eyeState;
273 281
         if (currentlyBlinking == NO) {
274
-            [[self.mainView render] drawWith:lastEyeState];
282
+            [[self.mainView render] drawWithEye:lastEyeState];
275 283
             self.mainView.needsDisplay = YES;
276 284
         }
277 285
     }

+ 5
- 2
OtaClock/Render.h View File

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

+ 90
- 13
OtaClock/Render.m View File

@@ -6,6 +6,7 @@
6 6
 //  Copyright (c) 2015 xythobuz. All rights reserved.
7 7
 //
8 8
 
9
+#import "MainView.h"
9 10
 #import "Render.h"
10 11
 
11 12
 #define FULL_IMAGE_WIDTH 86
@@ -15,25 +16,36 @@
15 16
 #define EYE_X_OFFSET 60
16 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 25
 @interface Render ()
19 26
 
20
-@property (assign) CGImageRef otaconGraphic;
21
-@property (assign) CGImageRef bubbleGraphic;
27
+@property (assign) CGImageRef otaconGraphic, bubbleGraphic;
22 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 32
 @property (assign) NSSize fullSize;
25 33
 @property (assign) CGContextRef drawContext;
26 34
 
35
+@property (weak) MainView *parent;
36
+
27 37
 @end
28 38
 
29 39
 @implementation Render
30 40
 
31
-@synthesize otaconGraphic;
32
-@synthesize bubbleGraphic;
41
+@synthesize otaconGraphic, bubbleGraphic;
33 42
 @synthesize eye0, eye1, eye2, eye3, eye4;
43
+@synthesize fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday;
44
+@synthesize eyeToDraw, dayOfWeek;
34 45
 
35 46
 @synthesize fullSize;
36 47
 @synthesize drawContext;
48
+@synthesize parent;
37 49
 
38 50
 - (CGImageRef)eyeHelper:(NSInteger)index {
39 51
     if (index == 0) return eye0;
@@ -46,10 +58,26 @@
46 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 76
     self = [super init];
51 77
     if (self == nil) return nil;
52 78
     
79
+    parent = par;
80
+    
53 81
     NSImage *otaconImage = [NSImage imageNamed:@"otacon"];
54 82
     NSImage *bubbleImage = [NSImage imageNamed:@"bubble"];
55 83
     NSImage *eye0Image = [NSImage imageNamed:@"eyes_0"];
@@ -57,6 +85,7 @@
57 85
     NSImage *eye2Image = [NSImage imageNamed:@"eyes_2"];
58 86
     NSImage *eye3Image = [NSImage imageNamed:@"eyes_3"];
59 87
     NSImage *eye4Image = [NSImage imageNamed:@"eyes_4"];
88
+    NSImage *fontDaysImage = [NSImage imageNamed:@"font_days"];
60 89
     
61 90
     NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithAttributes:nil];
62 91
     
@@ -68,11 +97,34 @@
68 97
     eye3 = [eye3Image CGImageForProposedRect:nil context:context hints:nil];
69 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 120
     fullSize.width = FULL_IMAGE_WIDTH;
72 121
     fullSize.height = FULL_IMAGE_HEIGHT;
73 122
     
74 123
     drawContext = CGBitmapContextCreate(NULL, fullSize.width, fullSize.height, 8, 0, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
75 124
     
125
+    eyeToDraw = 0;
126
+    dayOfWeek = 0;
127
+    
76 128
     return self;
77 129
 }
78 130
 
@@ -80,10 +132,24 @@
80 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 151
     // Clear entire canvas
152
+    CGRect size;
87 153
     size.size = fullSize;
88 154
     size.origin = NSZeroPoint;
89 155
     CGContextClearRect(drawContext, size);
@@ -94,6 +160,18 @@
94 160
     size.origin.y = BUBBLE_Y_OFFSET;
95 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 175
     // Draw Otacon
98 176
     size.size.width = CGImageGetWidth(otaconGraphic);
99 177
     size.size.height = CGImageGetHeight(otaconGraphic);
@@ -102,14 +180,13 @@
102 180
     CGContextDrawImage(drawContext, size, otaconGraphic);
103 181
     
104 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 185
     size.origin.x = EYE_X_OFFSET;
108 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 190
     CGImageRef drawnImage = CGBitmapContextCreateImage(drawContext);
114 191
     NSImage *result = [[NSImage alloc] initWithCGImage:drawnImage size:fullSize];
115 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