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

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