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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. @interface Render ()
  22. @property (assign) CGImageRef otaconGraphic, bubbleGraphic;
  23. @property (assign) CGImageRef eye0, eye1, eye2, eye3, eye4;
  24. @property (assign) CGImageRef fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday;
  25. @property (assign) NSInteger eyeToDraw, dayOfWeek;
  26. @property (assign) NSSize fullSize;
  27. @property (assign) CGContextRef drawContext;
  28. @property (weak) MainView *parent;
  29. @end
  30. @implementation Render
  31. @synthesize otaconGraphic, bubbleGraphic;
  32. @synthesize eye0, eye1, eye2, eye3, eye4;
  33. @synthesize fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday;
  34. @synthesize eyeToDraw, dayOfWeek;
  35. @synthesize fullSize;
  36. @synthesize drawContext;
  37. @synthesize parent;
  38. - (CGImageRef)eyeHelper:(NSInteger)index {
  39. if (index == 0) return eye0;
  40. if (index == 1) return eye1;
  41. if (index == 2) return eye2;
  42. if (index == 3) return eye3;
  43. if (index == 4) return eye4;
  44. NSLog(@"Render:eyeHelper:%ld unknown index!", (long)index);
  45. return eye0;
  46. }
  47. - (CGImageRef)dayHelper:(NSInteger)index {
  48. if (index == -1) return fontSunday;
  49. if (index == 0) return fontMonday;
  50. if (index == 1) return fontTuesday;
  51. if (index == 2) return fontWednesday;
  52. if (index == 3) return fontThursday;
  53. if (index == 4) return fontFriday;
  54. if (index == 5) return fontSaturday;
  55. if (index == 6) return fontSunday;
  56. NSLog(@"Render:dayHelper:%ld unknown index!", (long)index);
  57. return fontMonday;
  58. }
  59. - (id)initWithParent:(MainView *)par {
  60. self = [super init];
  61. if (self == nil) return nil;
  62. parent = par;
  63. NSImage *otaconImage = [NSImage imageNamed:@"otacon"];
  64. NSImage *bubbleImage = [NSImage imageNamed:@"bubble"];
  65. NSImage *eye0Image = [NSImage imageNamed:@"eyes_0"];
  66. NSImage *eye1Image = [NSImage imageNamed:@"eyes_1"];
  67. NSImage *eye2Image = [NSImage imageNamed:@"eyes_2"];
  68. NSImage *eye3Image = [NSImage imageNamed:@"eyes_3"];
  69. NSImage *eye4Image = [NSImage imageNamed:@"eyes_4"];
  70. NSImage *fontDaysImage = [NSImage imageNamed:@"font_days"];
  71. NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithAttributes:nil];
  72. otaconGraphic = [otaconImage CGImageForProposedRect:nil context:context hints:nil];
  73. bubbleGraphic = [bubbleImage CGImageForProposedRect:nil context:context hints:nil];
  74. eye0 = [eye0Image CGImageForProposedRect:nil context:context hints:nil];
  75. eye1 = [eye1Image CGImageForProposedRect:nil context:context hints:nil];
  76. eye2 = [eye2Image CGImageForProposedRect:nil context:context hints:nil];
  77. eye3 = [eye3Image CGImageForProposedRect:nil context:context hints:nil];
  78. eye4 = [eye4Image CGImageForProposedRect:nil context:context hints:nil];
  79. CGImageRef fontDays = [fontDaysImage CGImageForProposedRect:nil context:context hints:nil];
  80. NSRect rectDay;
  81. rectDay.size.width = FONT_DAYS_WIDTH;
  82. rectDay.size.height = FONT_DAYS_HEIGHT;
  83. rectDay.origin.x = 0;
  84. rectDay.origin.y = 0;
  85. fontMonday = CGImageCreateWithImageInRect(fontDays, rectDay);
  86. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  87. fontTuesday = CGImageCreateWithImageInRect(fontDays, rectDay);
  88. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  89. fontWednesday = CGImageCreateWithImageInRect(fontDays, rectDay);
  90. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  91. fontThursday = CGImageCreateWithImageInRect(fontDays, rectDay);
  92. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  93. fontFriday = CGImageCreateWithImageInRect(fontDays, rectDay);
  94. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  95. fontSaturday = CGImageCreateWithImageInRect(fontDays, rectDay);
  96. rectDay.origin.y += FONT_DAYS_HEIGHT + FONT_DAYS_PADDING;
  97. fontSunday = CGImageCreateWithImageInRect(fontDays, rectDay);
  98. fullSize.width = FULL_IMAGE_WIDTH;
  99. fullSize.height = FULL_IMAGE_HEIGHT;
  100. drawContext = CGBitmapContextCreate(NULL, fullSize.width, fullSize.height, 8, 0, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
  101. eyeToDraw = 0;
  102. dayOfWeek = 0;
  103. return self;
  104. }
  105. - (NSSize)baseSize {
  106. return fullSize;
  107. }
  108. - (void)drawWithDate:(NSDate *)date {
  109. NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date];
  110. dayOfWeek = [components weekday] - 2;
  111. // Check if something changed, if so, set needsDisplay
  112. // if (bla) {
  113. // parent.needsDisplay = YES;
  114. // }
  115. }
  116. - (void)drawWithEye:(NSInteger)eyeIndex {
  117. eyeToDraw = eyeIndex;
  118. }
  119. - (void)drawInto:(NSView *)view {
  120. // Clear entire canvas
  121. CGRect size;
  122. size.size = fullSize;
  123. size.origin = NSZeroPoint;
  124. CGContextClearRect(drawContext, size);
  125. // Draw Speech Bubble
  126. size.size.width = CGImageGetWidth(bubbleGraphic);
  127. size.size.height = CGImageGetHeight(bubbleGraphic);
  128. size.origin.y = BUBBLE_Y_OFFSET;
  129. CGContextDrawImage(drawContext, size, bubbleGraphic);
  130. // Draw Date
  131. // Draw Day of Week
  132. CGImageRef day = [self dayHelper:dayOfWeek];
  133. size.size.width = FONT_DAYS_WIDTH;
  134. size.size.height = FONT_DAYS_HEIGHT;
  135. size.origin.x = FONT_DAYS_X_OFFSET;
  136. size.origin.y = FONT_DAYS_Y_OFFSET;
  137. CGContextDrawImage(drawContext, size, day);
  138. // Draw Time
  139. // Draw Otacon
  140. size.size.width = CGImageGetWidth(otaconGraphic);
  141. size.size.height = CGImageGetHeight(otaconGraphic);
  142. size.origin = NSZeroPoint;
  143. size.origin.x = CGImageGetWidth(bubbleGraphic) + OTACON_X_OFFSET;
  144. CGContextDrawImage(drawContext, size, otaconGraphic);
  145. // Draw eyes
  146. size.size.width = CGImageGetWidth([self eyeHelper:eyeToDraw]);
  147. size.size.height = CGImageGetHeight([self eyeHelper:eyeToDraw]);
  148. size.origin.x = EYE_X_OFFSET;
  149. size.origin.y = EYE_Y_OFFSET;
  150. CGContextDrawImage(drawContext, size, [self eyeHelper:eyeToDraw]);
  151. // Render resulting canvas into bitmap and scale this into our window
  152. CGImageRef drawnImage = CGBitmapContextCreateImage(drawContext);
  153. NSImage *result = [[NSImage alloc] initWithCGImage:drawnImage size:fullSize];
  154. [result drawInRect:[view bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:NO hints:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:NSImageInterpolationNone] forKey:NSImageHintInterpolation]];
  155. }
  156. @end