HRBrightnessCursor.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*-
  2. * Copyright (c) 2011 Ryota Hayashi
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * $FreeBSD$
  26. */
  27. #import "HRBrightnessCursor.h"
  28. #import "HRCgUtil.h"
  29. @implementation HRBrightnessCursor
  30. - (id)initWithPoint:(CGPoint)point
  31. {
  32. CGSize size = CGSizeMake(18.0f, 40.0f);
  33. CGRect frame = CGRectMake(point.x - size.width/2.0f, point.y - size.height/2.0f, size.width, size.height);
  34. self = [super initWithFrame:frame];
  35. if (self) {
  36. [self setBackgroundColor:[UIColor clearColor]];
  37. [self setUserInteractionEnabled:FALSE];
  38. }
  39. return self;
  40. }
  41. - (void)drawRect:(CGRect)rect
  42. {
  43. CGContextRef context = UIGraphicsGetCurrentContext();
  44. CGContextSaveGState(context);
  45. HRSetRoundedRectanglePath(context, CGRectMake(2.0f, 13.0f, 14.0f, 14.0f), 2.0f);
  46. [[UIColor colorWithWhite:0.98f alpha:1.0f] set];
  47. CGContextSetLineWidth(context, 2.0f);
  48. CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 1.0f), 2.0f, [UIColor colorWithWhite:0.0f alpha:0.3f].CGColor);
  49. CGContextDrawPath(context, kCGPathFillStroke);
  50. CGContextRestoreGState(context);
  51. CGContextSaveGState(context);
  52. HRSetRoundedRectanglePath(context, CGRectMake(2.0f, 13.0f, 14.0f, 14.0f), 2.0f);
  53. CGContextClip(context);
  54. float top_color = 0.9f;
  55. float bottom_color = 0.98f;
  56. float alpha = 1.0f;
  57. CGFloat gradient_color[] = {
  58. top_color ,top_color ,top_color ,alpha,
  59. bottom_color ,bottom_color ,bottom_color ,alpha
  60. };
  61. CGGradientRef gradient;
  62. CGColorSpaceRef colorSpace;
  63. size_t num_locations = 2;
  64. CGFloat locations[2] = { 0.0, 1.0 };
  65. colorSpace = CGColorSpaceCreateDeviceRGB();
  66. gradient = CGGradientCreateWithColorComponents(colorSpace, gradient_color,
  67. locations, num_locations);
  68. CGPoint startPoint = CGPointMake(self.frame.size.width/2, 0.0);
  69. CGPoint endPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height);
  70. CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
  71. // GradientとColorSpaceを開放する
  72. CGColorSpaceRelease(colorSpace);
  73. CGGradientRelease(gradient);
  74. CGContextRestoreGState(context);
  75. CGContextSaveGState(context);
  76. [[UIColor colorWithWhite:1.0f alpha:1.0f] setStroke];
  77. CGContextMoveToPoint(context, 6.0f, 17.0f);
  78. CGContextAddLineToPoint(context, 6.0f, 24.0f);
  79. CGContextMoveToPoint(context, 9.0f, 17.0f);
  80. CGContextAddLineToPoint(context, 9.0f, 24.0f);
  81. CGContextMoveToPoint(context, 12.0f, 17.0f);
  82. CGContextAddLineToPoint(context, 12.0f, 24.0f);
  83. CGContextDrawPath(context, kCGPathStroke);
  84. CGContextRestoreGState(context);
  85. }
  86. @end