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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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_0_X_OFFSET 1
  14. #define OTACON_1_X_OFFSET 0
  15. #define OTACON_2_X_OFFSET -6
  16. #define OTACON_3_X_OFFSET -6
  17. #define EYE_X_OFFSET 60
  18. #define EYE_Y_OFFSET 45
  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. #define FONT_LARGE_WIDTH 6
  25. #define FONT_LARGE_HEIGHT 7
  26. #define FONT_LARGE_PADDING 1
  27. #define FONT_LARGE_Y_OFFSET 63
  28. #define FONT_LARGE_X0_OFFSET 5
  29. #define FONT_LARGE_X1_OFFSET 12
  30. #define FONT_LARGE_X2_OFFSET 21
  31. #define FONT_LARGE_X3_OFFSET 28
  32. #define FONT_LARGE_X4_OFFSET 37
  33. #define FONT_LARGE_X5_OFFSET 44
  34. #define FONT_SMALL_WIDTH 4
  35. #define FONT_SMALL_HEIGHT 5
  36. #define FONT_SMALL_PADDING 1
  37. #define FONT_SMALL_DATE_Y_OFFSET 71
  38. #define FONT_SMALL_DATE_X0_OFFSET 3
  39. #define FONT_SMALL_DATE_X1_OFFSET 8
  40. #define FONT_SMALL_DATE_X2_OFFSET 17
  41. #define FONT_SMALL_DATE_X3_OFFSET 22
  42. #define FONT_SMALL_ALARM_Y_OFFSET 57
  43. #define FONT_SMALL_ALARM_X0_OFFSET 29
  44. #define FONT_SMALL_ALARM_X1_OFFSET 34
  45. #define FONT_SMALL_ALARM_X2_OFFSET 41
  46. #define FONT_SMALL_ALARM_X3_OFFSET 46
  47. #define FONT_TIME_WIDTH 11
  48. #define FONT_TIME_HEIGHT 3
  49. #define FONT_TIME_PADDING 1
  50. #define ALARM_X_OFFSET 5
  51. #define ALARM_Y_OFFSET 57
  52. #define DOTS_TIME_Y_OFFSET 63
  53. #define DOTS_TIME_X0_OFFSET 18
  54. #define DOTS_TIME_X1_OFFSET 34
  55. #define DOTS_ALARM_X_OFFSET 38
  56. #define DOTS_ALARM_Y_OFFSET 57
  57. #define BLANK_OFFSET_X 13
  58. #define BLANK_OFFSET_Y 71
  59. @interface Render ()
  60. @property (assign) CGImageRef otaconGraphic, bubbleGraphic, alarmGraphic, blankGraphic;
  61. @property (assign) CGImageRef otacon1Graphic, otacon2Graphic, otacon3Graphic;
  62. @property (assign) CGImageRef eye0, eye1, eye2, eye3, eye4, dotSmallGraphic, dotLargeGraphic;
  63. @property (assign) CGImageRef fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday, fontAM, fontPM;
  64. @property (assign) CGImageRef fontSmall1, fontSmall2, fontSmall3, fontSmall4, fontSmall5, fontSmall6, fontSmall7, fontSmall8, fontSmall9, fontSmall0;
  65. @property (assign) CGImageRef fontLarge1, fontLarge2, fontLarge3, fontLarge4, fontLarge5, fontLarge6, fontLarge7, fontLarge8, fontLarge9, fontLarge0;
  66. @property (assign) NSInteger eyeToDraw, dayOfWeek, animState;
  67. @property (assign) NSInteger dateDigit0, dateDigit1, dateDigit2, dateDigit3;
  68. @property (assign) NSInteger alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
  69. @property (assign) NSInteger timeDigit0, timeDigit1, timeDigit2, timeDigit3, timeDigit4, timeDigit5;
  70. @property (assign) BOOL alarmSign, alarmDots, timeDots, drawDate, militaryTime, isAfternoon;
  71. @property (assign) NSSize fullSize;
  72. @property (assign) CGContextRef drawContext;
  73. @property (weak) MainView *parent;
  74. @end
  75. @implementation Render
  76. @synthesize otaconGraphic, bubbleGraphic, alarmGraphic, blankGraphic;
  77. @synthesize otacon1Graphic, otacon2Graphic, otacon3Graphic;
  78. @synthesize eye0, eye1, eye2, eye3, eye4, dotSmallGraphic, dotLargeGraphic;
  79. @synthesize fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday, fontAM, fontPM;
  80. @synthesize fontSmall1, fontSmall2, fontSmall3, fontSmall4, fontSmall5, fontSmall6, fontSmall7, fontSmall8, fontSmall9, fontSmall0;
  81. @synthesize fontLarge1, fontLarge2, fontLarge3, fontLarge4, fontLarge5, fontLarge6, fontLarge7, fontLarge8, fontLarge9, fontLarge0;
  82. @synthesize eyeToDraw, dayOfWeek, animState;
  83. @synthesize dateDigit0, dateDigit1, dateDigit2, dateDigit3;
  84. @synthesize alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
  85. @synthesize timeDigit0, timeDigit1, timeDigit2, timeDigit3, timeDigit4, timeDigit5;
  86. @synthesize alarmSign, alarmDots, timeDots, drawDate, militaryTime, isAfternoon;
  87. @synthesize fullSize;
  88. @synthesize drawContext;
  89. @synthesize parent;
  90. - (CGImageRef)eyeHelper:(NSInteger)index {
  91. if (index == 0) return eye0;
  92. if (index == 1) return eye1;
  93. if (index == 2) return eye2;
  94. if (index == 3) return eye3;
  95. if (index == 4) return eye4;
  96. NSLog(@"Render:eyeHelper:%ld unknown index!", (long)index);
  97. return eye0;
  98. }
  99. - (CGImageRef)dayHelper:(NSInteger)index {
  100. if (index == -1) return fontSunday;
  101. if (index == 0) return fontMonday;
  102. if (index == 1) return fontTuesday;
  103. if (index == 2) return fontWednesday;
  104. if (index == 3) return fontThursday;
  105. if (index == 4) return fontFriday;
  106. if (index == 5) return fontSaturday;
  107. if (index == 6) return fontSunday;
  108. NSLog(@"Render:dayHelper:%ld unknown index!", (long)index);
  109. return fontMonday;
  110. }
  111. - (CGImageRef)smallHelper:(NSInteger)index {
  112. if (index == 0) return fontSmall0;
  113. if (index == 1) return fontSmall1;
  114. if (index == 2) return fontSmall2;
  115. if (index == 3) return fontSmall3;
  116. if (index == 4) return fontSmall4;
  117. if (index == 5) return fontSmall5;
  118. if (index == 6) return fontSmall6;
  119. if (index == 7) return fontSmall7;
  120. if (index == 8) return fontSmall8;
  121. if (index == 9) return fontSmall9;
  122. NSLog(@"Render:smallHelper:%ld unknown index!", (long)index);
  123. return fontSmall0;
  124. }
  125. - (CGImageRef)largeHelper:(NSInteger)index {
  126. if (index == 0) return fontLarge0;
  127. if (index == 1) return fontLarge1;
  128. if (index == 2) return fontLarge2;
  129. if (index == 3) return fontLarge3;
  130. if (index == 4) return fontLarge4;
  131. if (index == 5) return fontLarge5;
  132. if (index == 6) return fontLarge6;
  133. if (index == 7) return fontLarge7;
  134. if (index == 8) return fontLarge8;
  135. if (index == 9) return fontLarge9;
  136. NSLog(@"Render:largeHelper:%ld unknown index!", (long)index);
  137. return fontLarge0;
  138. }
  139. - (id)initWithParent:(MainView *)par {
  140. self = [super init];
  141. if (self == nil) return nil;
  142. parent = par;
  143. NSImage *otaconImage = [NSImage imageNamed:@"otacon"];
  144. NSImage *bubbleImage = [NSImage imageNamed:@"bubble"];
  145. NSImage *eye0Image = [NSImage imageNamed:@"eyes_0"];
  146. NSImage *eye1Image = [NSImage imageNamed:@"eyes_1"];
  147. NSImage *eye2Image = [NSImage imageNamed:@"eyes_2"];
  148. NSImage *eye3Image = [NSImage imageNamed:@"eyes_3"];
  149. NSImage *eye4Image = [NSImage imageNamed:@"eyes_4"];
  150. NSImage *fontDaysImage = [NSImage imageNamed:@"font_days"];
  151. NSImage *fontSmallImage = [NSImage imageNamed:@"font_small"];
  152. NSImage *fontLargeImage = [NSImage imageNamed:@"font_large"];
  153. NSImage *fontTimeImage = [NSImage imageNamed:@"font_time"];
  154. NSImage *alarmImage = [NSImage imageNamed:@"alarm"];
  155. NSImage *dotsSmallImage = [NSImage imageNamed:@"dots_small"];
  156. NSImage *dotsLargeImage = [NSImage imageNamed:@"dots_large"];
  157. NSImage *blankImage = [NSImage imageNamed:@"blank"];
  158. NSImage *otacon1Image = [NSImage imageNamed:@"otacon_1"];
  159. NSImage *otacon2Image = [NSImage imageNamed:@"otacon_2"];
  160. NSImage *otacon3Image = [NSImage imageNamed:@"otacon_3"];
  161. NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithAttributes:nil];
  162. otaconGraphic = [otaconImage CGImageForProposedRect:nil context:context hints:nil];
  163. bubbleGraphic = [bubbleImage CGImageForProposedRect:nil context:context hints:nil];
  164. alarmGraphic = [alarmImage CGImageForProposedRect:nil context:context hints:nil];
  165. dotSmallGraphic = [dotsSmallImage CGImageForProposedRect:nil context:context hints:nil];
  166. dotLargeGraphic = [dotsLargeImage CGImageForProposedRect:nil context:context hints:nil];
  167. blankGraphic = [blankImage CGImageForProposedRect:nil context:context hints:nil];
  168. eye0 = [eye0Image CGImageForProposedRect:nil context:context hints:nil];
  169. eye1 = [eye1Image CGImageForProposedRect:nil context:context hints:nil];
  170. eye2 = [eye2Image CGImageForProposedRect:nil context:context hints:nil];
  171. eye3 = [eye3Image CGImageForProposedRect:nil context:context hints:nil];
  172. eye4 = [eye4Image CGImageForProposedRect:nil context:context hints:nil];
  173. otacon1Graphic = [otacon1Image CGImageForProposedRect:nil context:context hints:nil];
  174. otacon2Graphic = [otacon2Image CGImageForProposedRect:nil context:context hints:nil];
  175. otacon3Graphic = [otacon3Image CGImageForProposedRect:nil context:context hints:nil];
  176. CGImageRef fontDays = [fontDaysImage CGImageForProposedRect:nil context:context hints:nil];
  177. NSRect rectDay;
  178. rectDay.size.width = FONT_DAYS_WIDTH;
  179. rectDay.size.height = FONT_DAYS_HEIGHT;
  180. rectDay.origin.x = 0;
  181. rectDay.origin.y = 0;
  182. fontMonday = CGImageCreateWithImageInRect(fontDays, rectDay);
  183. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  184. fontTuesday = CGImageCreateWithImageInRect(fontDays, rectDay);
  185. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  186. fontWednesday = CGImageCreateWithImageInRect(fontDays, rectDay);
  187. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  188. fontThursday = CGImageCreateWithImageInRect(fontDays, rectDay);
  189. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  190. fontFriday = CGImageCreateWithImageInRect(fontDays, rectDay);
  191. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  192. fontSaturday = CGImageCreateWithImageInRect(fontDays, rectDay);
  193. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  194. fontSunday = CGImageCreateWithImageInRect(fontDays, rectDay);
  195. NSRect rect;
  196. CGImageRef fontSmall = [fontSmallImage CGImageForProposedRect:nil context:context hints:nil];
  197. rect.size.width = FONT_SMALL_WIDTH;
  198. rect.size.height = FONT_SMALL_HEIGHT;
  199. rect.origin.x = 0;
  200. rect.origin.y = 0;
  201. fontSmall0 = CGImageCreateWithImageInRect(fontSmall, rect);
  202. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  203. fontSmall1 = CGImageCreateWithImageInRect(fontSmall, rect);
  204. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  205. fontSmall2 = CGImageCreateWithImageInRect(fontSmall, rect);
  206. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  207. fontSmall3 = CGImageCreateWithImageInRect(fontSmall, rect);
  208. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  209. fontSmall4 = CGImageCreateWithImageInRect(fontSmall, rect);
  210. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  211. fontSmall5 = CGImageCreateWithImageInRect(fontSmall, rect);
  212. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  213. fontSmall6 = CGImageCreateWithImageInRect(fontSmall, rect);
  214. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  215. fontSmall7 = CGImageCreateWithImageInRect(fontSmall, rect);
  216. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  217. fontSmall8 = CGImageCreateWithImageInRect(fontSmall, rect);
  218. rect.origin.x += FONT_SMALL_WIDTH + FONT_SMALL_PADDING;
  219. fontSmall9 = CGImageCreateWithImageInRect(fontSmall, rect);
  220. CGImageRef fontLarge = [fontLargeImage CGImageForProposedRect:nil context:context hints:nil];
  221. rect.size.width = FONT_LARGE_WIDTH;
  222. rect.size.height = FONT_LARGE_HEIGHT;
  223. rect.origin.x = 0;
  224. rect.origin.y = 0;
  225. fontLarge0 = CGImageCreateWithImageInRect(fontLarge, rect);
  226. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  227. fontLarge1 = CGImageCreateWithImageInRect(fontLarge, rect);
  228. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  229. fontLarge2 = CGImageCreateWithImageInRect(fontLarge, rect);
  230. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  231. fontLarge3 = CGImageCreateWithImageInRect(fontLarge, rect);
  232. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  233. fontLarge4 = CGImageCreateWithImageInRect(fontLarge, rect);
  234. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  235. fontLarge5 = CGImageCreateWithImageInRect(fontLarge, rect);
  236. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  237. fontLarge6 = CGImageCreateWithImageInRect(fontLarge, rect);
  238. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  239. fontLarge7 = CGImageCreateWithImageInRect(fontLarge, rect);
  240. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  241. fontLarge8 = CGImageCreateWithImageInRect(fontLarge, rect);
  242. rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
  243. fontLarge9 = CGImageCreateWithImageInRect(fontLarge, rect);
  244. CGImageRef fontTime = [fontTimeImage CGImageForProposedRect:nil context:context hints:nil];
  245. rect.size.width = FONT_TIME_WIDTH;
  246. rect.size.height = FONT_TIME_HEIGHT;
  247. rect.origin.x = 0;
  248. rect.origin.y = 0;
  249. fontAM = CGImageCreateWithImageInRect(fontTime, rect);
  250. rect.origin.y += FONT_TIME_HEIGHT + FONT_TIME_PADDING;
  251. fontPM = CGImageCreateWithImageInRect(fontTime, rect);
  252. fullSize.width = FULL_IMAGE_WIDTH;
  253. fullSize.height = FULL_IMAGE_HEIGHT;
  254. drawContext = CGBitmapContextCreate(NULL, fullSize.width, fullSize.height, 8, 0, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
  255. animState = 0;
  256. eyeToDraw = 0;
  257. dayOfWeek = 0;
  258. dateDigit0 = 1;
  259. dateDigit1 = 8;
  260. dateDigit2 = 8;
  261. dateDigit3 = 8;
  262. timeDigit0 = 8;
  263. timeDigit1 = 8;
  264. timeDigit2 = 8;
  265. timeDigit3 = 8;
  266. timeDigit4 = 8;
  267. timeDigit5 = 8;
  268. timeDots = YES;
  269. militaryTime = NO;
  270. isAfternoon = NO;
  271. alarmDigit0 = -1;
  272. alarmDigit1 = -1;
  273. alarmDigit2 = -1;
  274. alarmDigit3 = -1;
  275. alarmSign = NO;
  276. alarmDots = NO;
  277. return self;
  278. }
  279. - (NSSize)baseSize {
  280. return fullSize;
  281. }
  282. #define CONVERT_DECIMAL(num, ten, one) do { \
  283. if (num >= 50) { \
  284. ten = 5; \
  285. one = num - 50; \
  286. } else if (num >= 40) { \
  287. ten = 4; \
  288. one = num - 40; \
  289. } else if (num >= 30) { \
  290. ten = 3; \
  291. one = num - 30; \
  292. } else if (num >= 20) { \
  293. ten = 2; \
  294. one = num - 20; \
  295. } else if (num >= 10) { \
  296. ten = 1; \
  297. one = num - 10; \
  298. } else { \
  299. ten = 0; \
  300. one = num; \
  301. } \
  302. } while (0);
  303. - (void)blinkDots {
  304. timeDots = !timeDots;
  305. if ((alarmDigit0 != -1) || (alarmDigit1 != -1) || (alarmDigit2 != -1) || (alarmDigit3 != -1)) {
  306. alarmDots = timeDots;
  307. }
  308. }
  309. - (void)drawMilitaryTime:(BOOL)mil {
  310. militaryTime = mil;
  311. [self drawWithDate:[NSDate date]];
  312. }
  313. - (void)drawAnimation:(NSInteger)state {
  314. if (animState != state) {
  315. [[self.parent window] invalidateShadow];
  316. }
  317. animState = state;
  318. }
  319. - (void)drawAlarmDate:(NSDate *)alarm {
  320. if (alarm == nil) {
  321. alarmDigit0 = -1;
  322. alarmDigit1 = -1;
  323. alarmDigit2 = -1;
  324. alarmDigit3 = -1;
  325. alarmSign = NO;
  326. alarmDots = NO;
  327. return;
  328. }
  329. alarmSign = YES;
  330. alarmDots = YES;
  331. NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:alarm];
  332. CONVERT_DECIMAL([components hour], alarmDigit0, alarmDigit1);
  333. CONVERT_DECIMAL([components minute], alarmDigit2, alarmDigit3);
  334. }
  335. - (void)drawDate:(BOOL)draw {
  336. drawDate = draw;
  337. }
  338. - (void)drawWithDate:(NSDate *)date {
  339. NSCalendarUnit comps = NSWeekdayCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
  340. NSDateComponents *components = [[NSCalendar currentCalendar] components:comps fromDate:date];
  341. dayOfWeek = [components weekday] - 2; // map sun=1 to sun=-1
  342. // Convert from military time to am/pm format if neccessary
  343. NSInteger hour = [components hour];
  344. if (militaryTime == NO) {
  345. if (hour == 0) {
  346. hour = 12;
  347. isAfternoon = false;
  348. } else if (hour == 12) {
  349. isAfternoon = true;
  350. } else if (hour > 12) {
  351. isAfternoon = true;
  352. hour -= 12;
  353. } else if (hour < 12) {
  354. isAfternoon = false;
  355. }
  356. }
  357. CONVERT_DECIMAL([components month], dateDigit0, dateDigit1);
  358. CONVERT_DECIMAL([components day], dateDigit2, dateDigit3);
  359. CONVERT_DECIMAL(hour, timeDigit0, timeDigit1);
  360. CONVERT_DECIMAL([components minute], timeDigit2, timeDigit3);
  361. CONVERT_DECIMAL([components second], timeDigit4, timeDigit5);
  362. // Remove leading hour zero when drawing in am/pm mode
  363. if (militaryTime == NO) {
  364. if (timeDigit0 == 0) {
  365. timeDigit0 = -1;
  366. }
  367. }
  368. }
  369. - (void)drawWithEye:(NSInteger)eyeIndex {
  370. eyeToDraw = eyeIndex;
  371. }
  372. - (void)drawInto:(NSView *)view {
  373. // Clear entire canvas
  374. CGRect size;
  375. size.size = fullSize;
  376. size.origin = NSZeroPoint;
  377. CGContextClearRect(drawContext, size);
  378. // Draw Speech Bubble
  379. size.size.width = CGImageGetWidth(bubbleGraphic);
  380. size.size.height = CGImageGetHeight(bubbleGraphic);
  381. size.origin.y = BUBBLE_Y_OFFSET;
  382. CGContextDrawImage(drawContext, size, bubbleGraphic);
  383. if (drawDate == YES) {
  384. // Draw Date
  385. size.size.width = FONT_SMALL_WIDTH;
  386. size.size.height = FONT_SMALL_HEIGHT;
  387. size.origin.y = FONT_SMALL_DATE_Y_OFFSET;
  388. if (dateDigit0 == 1) {
  389. size.origin.x = FONT_SMALL_DATE_X0_OFFSET;
  390. CGContextDrawImage(drawContext, size, fontSmall1);
  391. }
  392. if ((dateDigit1 >= 0) && (dateDigit1 <= 9)) {
  393. size.origin.x = FONT_SMALL_DATE_X1_OFFSET;
  394. CGContextDrawImage(drawContext, size, [self smallHelper:dateDigit1]);
  395. }
  396. if ((dateDigit2 >= 0) && (dateDigit2 <= 9)) {
  397. size.origin.x = FONT_SMALL_DATE_X2_OFFSET;
  398. CGContextDrawImage(drawContext, size, [self smallHelper:dateDigit2]);
  399. }
  400. if ((dateDigit3 >= 0) && (dateDigit3 <= 9)) {
  401. size.origin.x = FONT_SMALL_DATE_X3_OFFSET;
  402. CGContextDrawImage(drawContext, size, [self smallHelper:dateDigit3]);
  403. }
  404. // Draw Day of Week
  405. CGImageRef day = [self dayHelper:dayOfWeek];
  406. size.size.width = FONT_DAYS_WIDTH;
  407. size.size.height = FONT_DAYS_HEIGHT;
  408. size.origin.x = FONT_DAYS_X_OFFSET;
  409. size.origin.y = FONT_DAYS_Y_OFFSET;
  410. CGContextDrawImage(drawContext, size, day);
  411. } else {
  412. // Block unneeded date elements
  413. size.size.width = CGImageGetWidth(blankGraphic);
  414. size.size.height = CGImageGetHeight(blankGraphic);
  415. size.origin.x = BLANK_OFFSET_X;
  416. size.origin.y = BLANK_OFFSET_Y;
  417. CGContextDrawImage(drawContext, size, blankGraphic);
  418. }
  419. // Draw Time
  420. size.size.width = FONT_LARGE_WIDTH;
  421. size.size.height = FONT_LARGE_HEIGHT;
  422. size.origin.y = FONT_LARGE_Y_OFFSET;
  423. if ((timeDigit0 >= 0) && (timeDigit0 <= 9)) {
  424. size.origin.x = FONT_LARGE_X0_OFFSET;
  425. CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit0]);
  426. }
  427. if ((timeDigit1 >= 0) && (timeDigit1 <= 9)) {
  428. size.origin.x = FONT_LARGE_X1_OFFSET;
  429. CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit1]);
  430. }
  431. if ((timeDigit2 >= 0) && (timeDigit2 <= 9)) {
  432. size.origin.x = FONT_LARGE_X2_OFFSET;
  433. CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit2]);
  434. }
  435. if ((timeDigit3 >= 0) && (timeDigit3 <= 9)) {
  436. size.origin.x = FONT_LARGE_X3_OFFSET;
  437. CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit3]);
  438. }
  439. if (militaryTime == YES) {
  440. if ((timeDigit4 >= 0) && (timeDigit4 <= 9)) {
  441. size.origin.x = FONT_LARGE_X4_OFFSET;
  442. CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit4]);
  443. }
  444. if ((timeDigit5 >= 0) && (timeDigit5 <= 9)) {
  445. size.origin.x = FONT_LARGE_X5_OFFSET;
  446. CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit5]);
  447. }
  448. } else {
  449. // Draw AM/PM marker
  450. size.size.width = FONT_TIME_WIDTH;
  451. size.size.height = FONT_TIME_HEIGHT;
  452. size.origin.x = 36;
  453. size.origin.y = 63;
  454. CGContextDrawImage(drawContext, size, isAfternoon ? fontPM : fontAM);
  455. }
  456. // Draw dots between hours, minutes and seconds
  457. if (timeDots == YES) {
  458. size.size.width = CGImageGetWidth(dotLargeGraphic);
  459. size.size.height = CGImageGetHeight(dotLargeGraphic);
  460. size.origin.y = DOTS_TIME_Y_OFFSET;
  461. size.origin.x = DOTS_TIME_X0_OFFSET;
  462. CGContextDrawImage(drawContext, size, dotLargeGraphic);
  463. if (militaryTime == YES) {
  464. size.origin.x = DOTS_TIME_X1_OFFSET;
  465. CGContextDrawImage(drawContext, size, dotLargeGraphic);
  466. }
  467. }
  468. // Draw Alarm Graphic
  469. if (alarmSign == YES) {
  470. size.size.width = CGImageGetWidth(alarmGraphic);
  471. size.size.height = CGImageGetHeight(alarmGraphic);
  472. size.origin.x = ALARM_X_OFFSET;
  473. size.origin.y = ALARM_Y_OFFSET;
  474. CGContextDrawImage(drawContext, size, alarmGraphic);
  475. }
  476. // Draw dots between alarm hours and minutes
  477. if (alarmDots == YES) {
  478. size.size.width = CGImageGetWidth(dotSmallGraphic);
  479. size.size.height = CGImageGetHeight(dotSmallGraphic);
  480. size.origin.x = DOTS_ALARM_X_OFFSET;
  481. size.origin.y = DOTS_ALARM_Y_OFFSET;
  482. CGContextDrawImage(drawContext, size, dotSmallGraphic);
  483. }
  484. // Draw Alarm Time
  485. size.size.width = FONT_SMALL_WIDTH;
  486. size.size.height = FONT_SMALL_HEIGHT;
  487. size.origin.y = FONT_SMALL_ALARM_Y_OFFSET;
  488. if ((alarmDigit0 >= 0) && (alarmDigit0 <= 9)) {
  489. size.origin.x = FONT_SMALL_ALARM_X0_OFFSET;
  490. CGContextDrawImage(drawContext, size, [self smallHelper:alarmDigit0]);
  491. }
  492. if ((alarmDigit1 >= 0) && (alarmDigit1 <= 9)) {
  493. size.origin.x = FONT_SMALL_ALARM_X1_OFFSET;
  494. CGContextDrawImage(drawContext, size, [self smallHelper:alarmDigit1]);
  495. }
  496. if ((alarmDigit2 >= 0) && (alarmDigit2 <= 9)) {
  497. size.origin.x = FONT_SMALL_ALARM_X2_OFFSET;
  498. CGContextDrawImage(drawContext, size, [self smallHelper:alarmDigit2]);
  499. }
  500. if ((alarmDigit3 >= 0) && (alarmDigit3 <= 9)) {
  501. size.origin.x = FONT_SMALL_ALARM_X3_OFFSET;
  502. CGContextDrawImage(drawContext, size, [self smallHelper:alarmDigit3]);
  503. }
  504. size.origin = NSZeroPoint;
  505. if (animState == 0) {
  506. // Draw Otacon
  507. size.size.width = CGImageGetWidth(otaconGraphic);
  508. size.size.height = CGImageGetHeight(otaconGraphic);
  509. size.origin.x = CGImageGetWidth(bubbleGraphic) + OTACON_0_X_OFFSET;
  510. CGContextDrawImage(drawContext, size, otaconGraphic);
  511. // Draw eyes
  512. size.size.width = CGImageGetWidth([self eyeHelper:eyeToDraw]);
  513. size.size.height = CGImageGetHeight([self eyeHelper:eyeToDraw]);
  514. size.origin.x = EYE_X_OFFSET;
  515. size.origin.y = EYE_Y_OFFSET;
  516. CGContextDrawImage(drawContext, size, [self eyeHelper:eyeToDraw]);
  517. } else if (animState == 1) {
  518. size.size.width = CGImageGetWidth(otacon1Graphic);
  519. size.size.height = CGImageGetHeight(otacon1Graphic);
  520. size.origin.x = CGImageGetWidth(bubbleGraphic) + OTACON_1_X_OFFSET;
  521. CGContextDrawImage(drawContext, size, otacon1Graphic);
  522. } else if (animState == 2) {
  523. size.size.width = CGImageGetWidth(otacon2Graphic);
  524. size.size.height = CGImageGetHeight(otacon2Graphic);
  525. size.origin.x = CGImageGetWidth(bubbleGraphic) + OTACON_2_X_OFFSET;
  526. CGContextDrawImage(drawContext, size, otacon2Graphic);
  527. } else {
  528. size.size.width = CGImageGetWidth(otacon3Graphic);
  529. size.size.height = CGImageGetHeight(otacon3Graphic);
  530. size.origin.x = CGImageGetWidth(bubbleGraphic) + OTACON_3_X_OFFSET;
  531. CGContextDrawImage(drawContext, size, otacon3Graphic);
  532. }
  533. // Render resulting canvas into bitmap and scale this into our window
  534. CGImageRef drawnImage = CGBitmapContextCreateImage(drawContext);
  535. NSImage *result = [[NSImage alloc] initWithCGImage:drawnImage size:fullSize];
  536. [result drawInRect:[view bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:NO hints:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:NSImageInterpolationNone] forKey:NSImageHintInterpolation]];
  537. CGImageRelease(drawnImage);
  538. }
  539. @end