Native Mac OS X OtaClock replica
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Render.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. //
  2. // Render.m
  3. // OtaClock
  4. //
  5. // Created by Thomas Buck on 17.08.15.
  6. // Copyright (c) 2015 xythobuz. All rights reserved.
  7. //
  8. #import "MainView.h"
  9. #import "Render.h"
  10. #define FULL_IMAGE_WIDTH 86
  11. #define FULL_IMAGE_HEIGHT 80
  12. #define BUBBLE_Y_OFFSET 45
  13. #define OTACON_X_OFFSET 1
  14. #define EYE_X_OFFSET 60
  15. #define EYE_Y_OFFSET 45
  16. #define FONT_DAYS_WIDTH 17
  17. #define FONT_DAYS_HEIGHT 3
  18. #define FONT_DAYS_PADDING 1
  19. #define FONT_DAYS_X_OFFSET 31
  20. #define FONT_DAYS_Y_OFFSET 72
  21. #define FONT_LARGE_WIDTH 6
  22. #define FONT_LARGE_HEIGHT 7
  23. #define FONT_LARGE_PADDING 1
  24. #define FONT_SMALL_WIDTH 4
  25. #define FONT_SMALL_HEIGHT 5
  26. #define FONT_SMALL_PADDING 1
  27. #define FONT_SMALL_DATE_Y_OFFSET 71
  28. #define FONT_SMALL_DATE_X0_OFFSET 3
  29. #define FONT_SMALL_DATE_X1_OFFSET 8
  30. #define FONT_SMALL_DATE_X2_OFFSET 17
  31. #define FONT_SMALL_DATE_X3_OFFSET 22
  32. #define FONT_SMALL_ALARM_Y_OFFSET 57
  33. #define FONT_SMALL_ALARM_X0_OFFSET 29
  34. #define FONT_SMALL_ALARM_X1_OFFSET 34
  35. #define FONT_SMALL_ALARM_X2_OFFSET 41
  36. #define FONT_SMALL_ALARM_X3_OFFSET 46
  37. @interface Render ()
  38. @property (assign) CGImageRef otaconGraphic, bubbleGraphic;
  39. @property (assign) CGImageRef eye0, eye1, eye2, eye3, eye4;
  40. @property (assign) CGImageRef fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday;
  41. @property (assign) CGImageRef fontSmall1, fontSmall2, fontSmall3, fontSmall4, fontSmall5, fontSmall6, fontSmall7, fontSmall8, fontSmall9, fontSmall0;
  42. @property (assign) CGImageRef fontLarge1, fontLarge2, fontLarge3, fontLarge4, fontLarge5, fontLarge6, fontLarge7, fontLarge8, fontLarge9, fontLarge0;
  43. @property (assign) NSInteger eyeToDraw, dayOfWeek;
  44. @property (assign) NSInteger dateDigit0, dateDigit1, dateDigit2, dateDigit3;
  45. @property (assign) NSInteger alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
  46. @property (assign) NSSize fullSize;
  47. @property (assign) CGContextRef drawContext;
  48. @property (weak) MainView *parent;
  49. @end
  50. @implementation Render
  51. @synthesize otaconGraphic, bubbleGraphic;
  52. @synthesize eye0, eye1, eye2, eye3, eye4;
  53. @synthesize fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday;
  54. @synthesize fontSmall1, fontSmall2, fontSmall3, fontSmall4, fontSmall5, fontSmall6, fontSmall7, fontSmall8, fontSmall9, fontSmall0;
  55. @synthesize fontLarge1, fontLarge2, fontLarge3, fontLarge4, fontLarge5, fontLarge6, fontLarge7, fontLarge8, fontLarge9, fontLarge0;
  56. @synthesize eyeToDraw, dayOfWeek;
  57. @synthesize dateDigit0, dateDigit1, dateDigit2, dateDigit3;
  58. @synthesize alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
  59. @synthesize fullSize;
  60. @synthesize drawContext;
  61. @synthesize parent;
  62. - (CGImageRef)eyeHelper:(NSInteger)index {
  63. if (index == 0) return eye0;
  64. if (index == 1) return eye1;
  65. if (index == 2) return eye2;
  66. if (index == 3) return eye3;
  67. if (index == 4) return eye4;
  68. NSLog(@"Render:eyeHelper:%ld unknown index!", (long)index);
  69. return eye0;
  70. }
  71. - (CGImageRef)dayHelper:(NSInteger)index {
  72. if (index == -1) return fontSunday;
  73. if (index == 0) return fontMonday;
  74. if (index == 1) return fontTuesday;
  75. if (index == 2) return fontWednesday;
  76. if (index == 3) return fontThursday;
  77. if (index == 4) return fontFriday;
  78. if (index == 5) return fontSaturday;
  79. if (index == 6) return fontSunday;
  80. NSLog(@"Render:dayHelper:%ld unknown index!", (long)index);
  81. return fontMonday;
  82. }
  83. - (CGImageRef)smallHelper:(NSInteger)index {
  84. if (index == 0) return fontSmall0;
  85. if (index == 1) return fontSmall1;
  86. if (index == 2) return fontSmall2;
  87. if (index == 3) return fontSmall3;
  88. if (index == 4) return fontSmall4;
  89. if (index == 5) return fontSmall5;
  90. if (index == 6) return fontSmall6;
  91. if (index == 7) return fontSmall7;
  92. if (index == 8) return fontSmall8;
  93. if (index == 9) return fontSmall9;
  94. NSLog(@"Render:smallHelper:%ld unknown index!", (long)index);
  95. return fontSmall0;
  96. }
  97. - (CGImageRef)largeHelper:(NSInteger)index {
  98. if (index == 0) return fontLarge0;
  99. if (index == 1) return fontLarge1;
  100. if (index == 2) return fontLarge2;
  101. if (index == 3) return fontLarge3;
  102. if (index == 4) return fontLarge4;
  103. if (index == 5) return fontLarge5;
  104. if (index == 6) return fontLarge6;
  105. if (index == 7) return fontLarge7;
  106. if (index == 8) return fontLarge8;
  107. if (index == 9) return fontLarge9;
  108. NSLog(@"Render:largeHelper:%ld unknown index!", (long)index);
  109. return fontLarge0;
  110. }
  111. - (id)initWithParent:(MainView *)par {
  112. self = [super init];
  113. if (self == nil) return nil;
  114. parent = par;
  115. NSImage *otaconImage = [NSImage imageNamed:@"otacon"];
  116. NSImage *bubbleImage = [NSImage imageNamed:@"bubble"];
  117. NSImage *eye0Image = [NSImage imageNamed:@"eyes_0"];
  118. NSImage *eye1Image = [NSImage imageNamed:@"eyes_1"];
  119. NSImage *eye2Image = [NSImage imageNamed:@"eyes_2"];
  120. NSImage *eye3Image = [NSImage imageNamed:@"eyes_3"];
  121. NSImage *eye4Image = [NSImage imageNamed:@"eyes_4"];
  122. NSImage *fontDaysImage = [NSImage imageNamed:@"font_days"];
  123. NSImage *fontSmallImage = [NSImage imageNamed:@"font_small"];
  124. NSImage *fontLargeImage = [NSImage imageNamed:@"font_large"];
  125. NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithAttributes:nil];
  126. otaconGraphic = [otaconImage CGImageForProposedRect:nil context:context hints:nil];
  127. bubbleGraphic = [bubbleImage CGImageForProposedRect:nil context:context hints:nil];
  128. eye0 = [eye0Image CGImageForProposedRect:nil context:context hints:nil];
  129. eye1 = [eye1Image CGImageForProposedRect:nil context:context hints:nil];
  130. eye2 = [eye2Image CGImageForProposedRect:nil context:context hints:nil];
  131. eye3 = [eye3Image CGImageForProposedRect:nil context:context hints:nil];
  132. eye4 = [eye4Image CGImageForProposedRect:nil context:context hints:nil];
  133. CGImageRef fontDays = [fontDaysImage CGImageForProposedRect:nil context:context hints:nil];
  134. NSRect rectDay;
  135. rectDay.size.width = FONT_DAYS_WIDTH;
  136. rectDay.size.height = FONT_DAYS_HEIGHT;
  137. rectDay.origin.x = 0;
  138. rectDay.origin.y = 0;
  139. fontMonday = CGImageCreateWithImageInRect(fontDays, rectDay);
  140. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  141. fontTuesday = CGImageCreateWithImageInRect(fontDays, rectDay);
  142. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  143. fontWednesday = CGImageCreateWithImageInRect(fontDays, rectDay);
  144. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  145. fontThursday = CGImageCreateWithImageInRect(fontDays, rectDay);
  146. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  147. fontFriday = CGImageCreateWithImageInRect(fontDays, rectDay);
  148. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  149. fontSaturday = CGImageCreateWithImageInRect(fontDays, rectDay);
  150. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  151. fontSunday = CGImageCreateWithImageInRect(fontDays, rectDay);
  152. NSRect rect;
  153. CGImageRef fontSmall = [fontSmallImage CGImageForProposedRect:nil context:context hints:nil];
  154. rect.size.width = FONT_SMALL_WIDTH;
  155. rect.size.height = FONT_SMALL_HEIGHT;
  156. rect.origin.x = 0;
  157. rect.origin.y = 0;
  158. fontSmall0 = CGImageCreateWithImageInRect(fontSmall, rect);
  159. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  160. fontSmall1 = CGImageCreateWithImageInRect(fontSmall, rect);
  161. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  162. fontSmall2 = CGImageCreateWithImageInRect(fontSmall, rect);
  163. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  164. fontSmall3 = CGImageCreateWithImageInRect(fontSmall, rect);
  165. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  166. fontSmall4 = CGImageCreateWithImageInRect(fontSmall, rect);
  167. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  168. fontSmall5 = CGImageCreateWithImageInRect(fontSmall, rect);
  169. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  170. fontSmall6 = CGImageCreateWithImageInRect(fontSmall, rect);
  171. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  172. fontSmall7 = CGImageCreateWithImageInRect(fontSmall, rect);
  173. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  174. fontSmall8 = CGImageCreateWithImageInRect(fontSmall, rect);
  175. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  176. fontSmall9 = CGImageCreateWithImageInRect(fontSmall, rect);
  177. CGImageRef fontLarge = [fontLargeImage CGImageForProposedRect:nil context:context hints:nil];
  178. rect.size.width = FONT_LARGE_WIDTH;
  179. rect.size.height = FONT_LARGE_HEIGHT;
  180. rect.origin.x = 0;
  181. rect.origin.y = 0;
  182. fontLarge0 = CGImageCreateWithImageInRect(fontLarge, rect);
  183. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  184. fontLarge1 = CGImageCreateWithImageInRect(fontLarge, rect);
  185. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  186. fontLarge2 = CGImageCreateWithImageInRect(fontLarge, rect);
  187. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  188. fontLarge3 = CGImageCreateWithImageInRect(fontLarge, rect);
  189. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  190. fontLarge4 = CGImageCreateWithImageInRect(fontLarge, rect);
  191. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  192. fontLarge5 = CGImageCreateWithImageInRect(fontLarge, rect);
  193. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  194. fontLarge6 = CGImageCreateWithImageInRect(fontLarge, rect);
  195. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  196. fontLarge7 = CGImageCreateWithImageInRect(fontLarge, rect);
  197. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  198. fontLarge8 = CGImageCreateWithImageInRect(fontLarge, rect);
  199. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  200. fontLarge9 = CGImageCreateWithImageInRect(fontLarge, rect);
  201. fullSize.width = FULL_IMAGE_WIDTH;
  202. fullSize.height = FULL_IMAGE_HEIGHT;
  203. drawContext = CGBitmapContextCreate(NULL, fullSize.width, fullSize.height, 8, 0, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
  204. eyeToDraw = 0;
  205. dayOfWeek = 0;
  206. dateDigit0 = 1;
  207. dateDigit1 = 8;
  208. dateDigit2 = 8;
  209. dateDigit3 = 8;
  210. alarmDigit0 = 8;
  211. alarmDigit1 = 8;
  212. alarmDigit2 = 8;
  213. alarmDigit3 = 8;
  214. return self;
  215. }
  216. - (NSSize)baseSize {
  217. return fullSize;
  218. }
  219. - (void)drawWithDate:(NSDate *)date {
  220. NSCalendarUnit comps = NSWeekdayCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
  221. NSDateComponents *components = [[NSCalendar currentCalendar] components:comps fromDate:date];
  222. dayOfWeek = [components weekday] - 2; // map sun=1 to sun=-1
  223. if ([components month] >= 10) {
  224. dateDigit0 = 1;
  225. dateDigit1 = [components month] - 10;
  226. } else {
  227. dateDigit0 = 0;
  228. dateDigit1 = [components month];
  229. }
  230. if ([components day] >= 30) {
  231. dateDigit2 = 3;
  232. dateDigit3 = [components day] - 30;
  233. } else if ([components day] >= 20) {
  234. dateDigit2 = 2;
  235. dateDigit3 = [components day] - 20;
  236. } else if ([components day] >= 10) {
  237. dateDigit2 = 1;
  238. dateDigit3 = [components day] - 10;
  239. } else {
  240. dateDigit2 = 0;
  241. dateDigit3 = [components day];
  242. }
  243. }
  244. - (void)drawWithEye:(NSInteger)eyeIndex {
  245. eyeToDraw = eyeIndex;
  246. }
  247. - (void)drawInto:(NSView *)view {
  248. // Clear entire canvas
  249. CGRect size;
  250. size.size = fullSize;
  251. size.origin = NSZeroPoint;
  252. CGContextClearRect(drawContext, size);
  253. // Draw Speech Bubble
  254. size.size.width = CGImageGetWidth(bubbleGraphic);
  255. size.size.height = CGImageGetHeight(bubbleGraphic);
  256. size.origin.y = BUBBLE_Y_OFFSET;
  257. CGContextDrawImage(drawContext, size, bubbleGraphic);
  258. // Draw Date
  259. size.size.width = FONT_SMALL_WIDTH;
  260. size.size.height = FONT_SMALL_HEIGHT;
  261. size.origin.y = FONT_SMALL_DATE_Y_OFFSET;
  262. if (dateDigit0 == 1) {
  263. size.origin.x = FONT_SMALL_DATE_X0_OFFSET;
  264. CGContextDrawImage(drawContext, size, fontSmall1);
  265. }
  266. if ((dateDigit1 >= 0) && (dateDigit1 <= 9)) {
  267. size.origin.x = FONT_SMALL_DATE_X1_OFFSET;
  268. CGContextDrawImage(drawContext, size, [self smallHelper:dateDigit1]);
  269. }
  270. if ((dateDigit2 >= 0) && (dateDigit2 <= 9)) {
  271. size.origin.x = FONT_SMALL_DATE_X2_OFFSET;
  272. CGContextDrawImage(drawContext, size, [self smallHelper:dateDigit2]);
  273. }
  274. if ((dateDigit3 >= 0) && (dateDigit3 <= 9)) {
  275. size.origin.x = FONT_SMALL_DATE_X3_OFFSET;
  276. CGContextDrawImage(drawContext, size, [self smallHelper:dateDigit3]);
  277. }
  278. // Draw Day of Week
  279. CGImageRef day = [self dayHelper:dayOfWeek];
  280. size.size.width = FONT_DAYS_WIDTH;
  281. size.size.height = FONT_DAYS_HEIGHT;
  282. size.origin.x = FONT_DAYS_X_OFFSET;
  283. size.origin.y = FONT_DAYS_Y_OFFSET;
  284. CGContextDrawImage(drawContext, size, day);
  285. // Draw Time
  286. // Draw Alarm Time
  287. size.size.width = FONT_SMALL_WIDTH;
  288. size.size.height = FONT_SMALL_HEIGHT;
  289. size.origin.y = FONT_SMALL_ALARM_Y_OFFSET;
  290. if ((alarmDigit0 >= 0) && (alarmDigit0 <= 9)) {
  291. size.origin.x = FONT_SMALL_ALARM_X0_OFFSET;
  292. CGContextDrawImage(drawContext, size, [self smallHelper:alarmDigit0]);
  293. }
  294. if ((alarmDigit1 >= 0) && (alarmDigit1 <= 9)) {
  295. size.origin.x = FONT_SMALL_ALARM_X1_OFFSET;
  296. CGContextDrawImage(drawContext, size, [self smallHelper:alarmDigit1]);
  297. }
  298. if ((alarmDigit2 >= 0) && (alarmDigit2 <= 9)) {
  299. size.origin.x = FONT_SMALL_ALARM_X2_OFFSET;
  300. CGContextDrawImage(drawContext, size, [self smallHelper:alarmDigit2]);
  301. }
  302. if ((alarmDigit3 >= 0) && (alarmDigit3 <= 9)) {
  303. size.origin.x = FONT_SMALL_ALARM_X3_OFFSET;
  304. CGContextDrawImage(drawContext, size, [self smallHelper:alarmDigit3]);
  305. }
  306. // Draw Otacon
  307. size.size.width = CGImageGetWidth(otaconGraphic);
  308. size.size.height = CGImageGetHeight(otaconGraphic);
  309. size.origin = NSZeroPoint;
  310. size.origin.x = CGImageGetWidth(bubbleGraphic) + OTACON_X_OFFSET;
  311. CGContextDrawImage(drawContext, size, otaconGraphic);
  312. // Draw eyes
  313. size.size.width = CGImageGetWidth([self eyeHelper:eyeToDraw]);
  314. size.size.height = CGImageGetHeight([self eyeHelper:eyeToDraw]);
  315. size.origin.x = EYE_X_OFFSET;
  316. size.origin.y = EYE_Y_OFFSET;
  317. CGContextDrawImage(drawContext, size, [self eyeHelper:eyeToDraw]);
  318. // Render resulting canvas into bitmap and scale this into our window
  319. CGImageRef drawnImage = CGBitmapContextCreateImage(drawContext);
  320. NSImage *result = [[NSImage alloc] initWithCGImage:drawnImage size:fullSize];
  321. [result drawInRect:[view bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:NO hints:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:NSImageInterpolationNone] forKey:NSImageHintInterpolation]];
  322. CGImageRelease(drawnImage);
  323. }
  324. @end