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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "Render.h"
  9. #define FULL_IMAGE_WIDTH 86
  10. #define FULL_IMAGE_HEIGHT 80
  11. #define BUBBLE_Y_OFFSET 45
  12. #define OTACON_X_OFFSET 1
  13. #define EYE_X_OFFSET 60
  14. #define EYE_Y_OFFSET 45
  15. @interface Render ()
  16. @property (assign) CGImageRef otaconGraphic;
  17. @property (assign) CGImageRef bubbleGraphic;
  18. @property (assign) CGImageRef eye0, eye1, eye2, eye3, eye4;
  19. @property (assign) NSSize fullSize;
  20. @property (assign) CGContextRef drawContext;
  21. @end
  22. @implementation Render
  23. @synthesize otaconGraphic;
  24. @synthesize bubbleGraphic;
  25. @synthesize eye0, eye1, eye2, eye3, eye4;
  26. @synthesize fullSize;
  27. @synthesize drawContext;
  28. - (CGImageRef)eyeHelper:(NSInteger)index {
  29. if (index == 0) return eye0;
  30. if (index == 1) return eye1;
  31. if (index == 2) return eye2;
  32. if (index == 3) return eye3;
  33. if (index == 4) return eye4;
  34. NSLog(@"Render:eyeHelper:%ld unknown index!", (long)index);
  35. return eye0;
  36. }
  37. - (id)init {
  38. self = [super init];
  39. if (self == nil) return nil;
  40. NSImage *otaconImage = [NSImage imageNamed:@"otacon"];
  41. NSImage *bubbleImage = [NSImage imageNamed:@"bubble"];
  42. NSImage *eye0Image = [NSImage imageNamed:@"eyes_0"];
  43. NSImage *eye1Image = [NSImage imageNamed:@"eyes_1"];
  44. NSImage *eye2Image = [NSImage imageNamed:@"eyes_2"];
  45. NSImage *eye3Image = [NSImage imageNamed:@"eyes_3"];
  46. NSImage *eye4Image = [NSImage imageNamed:@"eyes_4"];
  47. NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithAttributes:nil];
  48. otaconGraphic = [otaconImage CGImageForProposedRect:nil context:context hints:nil];
  49. bubbleGraphic = [bubbleImage CGImageForProposedRect:nil context:context hints:nil];
  50. eye0 = [eye0Image CGImageForProposedRect:nil context:context hints:nil];
  51. eye1 = [eye1Image CGImageForProposedRect:nil context:context hints:nil];
  52. eye2 = [eye2Image CGImageForProposedRect:nil context:context hints:nil];
  53. eye3 = [eye3Image CGImageForProposedRect:nil context:context hints:nil];
  54. eye4 = [eye4Image CGImageForProposedRect:nil context:context hints:nil];
  55. fullSize.width = FULL_IMAGE_WIDTH;
  56. fullSize.height = FULL_IMAGE_HEIGHT;
  57. drawContext = CGBitmapContextCreate(NULL, fullSize.width, fullSize.height, 8, 0, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
  58. return self;
  59. }
  60. - (NSSize)baseSize {
  61. return fullSize;
  62. }
  63. - (void)drawWith:(NSInteger)eyeIndex {
  64. CGRect size;
  65. // Clear entire canvas
  66. size.size = fullSize;
  67. size.origin = NSZeroPoint;
  68. CGContextClearRect(drawContext, size);
  69. // Draw Speech Bubble
  70. size.size.width = CGImageGetWidth(bubbleGraphic);
  71. size.size.height = CGImageGetHeight(bubbleGraphic);
  72. size.origin.y = BUBBLE_Y_OFFSET;
  73. CGContextDrawImage(drawContext, size, bubbleGraphic);
  74. // Draw Otacon
  75. size.size.width = CGImageGetWidth(otaconGraphic);
  76. size.size.height = CGImageGetHeight(otaconGraphic);
  77. size.origin = NSZeroPoint;
  78. size.origin.x = CGImageGetWidth(bubbleGraphic) + OTACON_X_OFFSET;
  79. CGContextDrawImage(drawContext, size, otaconGraphic);
  80. // Draw eyes
  81. size.size.width = CGImageGetWidth([self eyeHelper:eyeIndex]);
  82. size.size.height = CGImageGetHeight([self eyeHelper:eyeIndex]);
  83. size.origin.x = EYE_X_OFFSET;
  84. size.origin.y = EYE_Y_OFFSET;
  85. CGContextDrawImage(drawContext, size, [self eyeHelper:eyeIndex]);
  86. }
  87. - (void)drawInto:(NSView *)view {
  88. CGImageRef drawnImage = CGBitmapContextCreateImage(drawContext);
  89. NSImage *result = [[NSImage alloc] initWithCGImage:drawnImage size:fullSize];
  90. [result drawInRect:[view bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:NO hints:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:NSImageInterpolationNone] forKey:NSImageHintInterpolation]];
  91. }
  92. @end