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 13KB

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