Issue 479
[zxing.git] / iphone / ZXingWidget / Classes / OverlayView.m
index 27ebd44..2093bff 100755 (executable)
 
 static const CGFloat kPadding = 10;
 
+@interface OverlayView()
+@property (nonatomic,assign) UIButton *cancelButton;
+@end
+
+
 @implementation OverlayView
 
 @synthesize delegate, oneDMode;
 @synthesize points = _points;
-//@synthesize image;
-
+@synthesize cancelButton;
+@synthesize cropRect;
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
-- (id)initWithCancelEnabled:(BOOL)cancelEnabled frame:(CGRect)frame {
-       if( self = [super initWithFrame:frame] ) {
-               self.backgroundColor = [UIColor clearColor];
-       }
-       if (cancelEnabled) {
-               cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
-               [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
-               if (oneDMode) {
-                       [cancelButton setTransform:CGAffineTransformMakeRotation(M_PI/2)];
-                       [cancelButton setFrame:CGRectMake(20, 175, 45, 130)];
-               }
-               else {
-                       [cancelButton setFrame:CGRectMake(95, 420, 130, 45)];                   
-               }
-
-               [cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
-               [self addSubview:cancelButton];
-       }
-       return self;
+- (id) initWithFrame:(CGRect)theFrame cancelEnabled:(BOOL)isCancelEnabled oneDMode:(BOOL)isOneDModeEnabled {
+  if( self = [super initWithFrame:theFrame] ) {
+
+    CGFloat rectSize = self.frame.size.width - kPadding * 2;
+    if (!oneDMode) {
+      cropRect = CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
+    } else {
+      CGFloat rectSize2 = self.frame.size.height - kPadding * 2;
+      cropRect = CGRectMake(kPadding, kPadding, rectSize, rectSize2);          
+    }
+
+    self.backgroundColor = [UIColor clearColor];
+    self.oneDMode = isOneDModeEnabled;
+    if (isCancelEnabled) {
+      UIButton *butt = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
+      self.cancelButton = butt;
+      [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
+      if (oneDMode) {
+        [cancelButton setTransform:CGAffineTransformMakeRotation(M_PI/2)];
+        [cancelButton setFrame:CGRectMake(20, 175, 45, 130)];
+      }
+      else {
+        [cancelButton setFrame:CGRectMake(95, 420, 130, 45)];                  
+      }
+      
+      [cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
+      [self addSubview:cancelButton];
+      [self addSubview:imageView];
+    }
+  }
+  return self;
 }
 
 - (void)cancel:(id)sender {
@@ -57,10 +74,7 @@ static const CGFloat kPadding = 10;
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 - (void) dealloc {
        [imageView release];
-       imageView = nil;
        [_points release];
-       _points = nil;
-       [cancelButton release];
        [super dealloc];
 }
 
@@ -75,16 +89,43 @@ static const CGFloat kPadding = 10;
        CGContextStrokePath(context);
 }
 
+- (CGPoint)map:(CGPoint)point {
+    CGPoint center;
+    center.x = cropRect.size.width/2;
+    center.y = cropRect.size.height/2;
+    float x = point.x - center.x;
+    float y = point.y - center.y;
+    int rotation = 90;
+    switch(rotation) {
+    case 0:
+        point.x = x;
+        point.y = y;
+        break;
+    case 90:
+        point.x = -y;
+        point.y = x;
+        break;
+    case 180:
+        point.x = -x;
+        point.y = -y;
+        break;
+    case 270:
+        point.x = y;
+        point.y = -x;
+        break;
+    }
+    point.x = point.x + center.x;
+    point.y = point.y + center.y;
+    return point;
+}
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 - (void)drawRect:(CGRect)rect {
        [super drawRect:rect];
        CGContextRef c = UIGraphicsGetCurrentContext();
-
-       CGRect cropRect = [self cropRect];
-       
+  
        if (nil != _points) {
-//             [imageView.image drawAtPoint:cropRect.origin];
+    //         [imageView.image drawAtPoint:cropRect.origin];
        }
        
        CGFloat white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
@@ -92,7 +133,7 @@ static const CGFloat kPadding = 10;
        CGContextSetFillColor(c, white);
        [self drawRect:cropRect inContext:c];
        
-//     CGContextSetStrokeColor(c, white);
+  //   CGContextSetStrokeColor(c, white);
        //      CGContextSetStrokeColor(c, white);
        CGContextSaveGState(c);
        if (oneDMode) {
@@ -129,8 +170,8 @@ static const CGFloat kPadding = 10;
                CGContextSetStrokeColor(c, blue);
                CGContextSetFillColor(c, blue);
                if (oneDMode) {
-                       CGPoint val1 = [[_points objectAtIndex:0] CGPointValue];
-                       CGPoint val2 = [[_points objectAtIndex:1] CGPointValue];
+                       CGPoint val1 = [self map:[[_points objectAtIndex:0] CGPointValue]];
+                       CGPoint val2 = [self map:[[_points objectAtIndex:1] CGPointValue]];
                        CGContextMoveToPoint(c, offset, val1.x);
                        CGContextAddLineToPoint(c, offset, val2.x);
                        CGContextStrokePath(c);
@@ -138,10 +179,10 @@ static const CGFloat kPadding = 10;
                else {
                        CGRect smallSquare = CGRectMake(0, 0, 10, 10);
                        for( NSValue* value in _points ) {
-                               CGPoint point = [value CGPointValue];
+                               CGPoint point = [self map:[value CGPointValue]];
                                smallSquare.origin = CGPointMake(
-                                                               cropRect.origin.x + point.x - smallSquare.size.width / 2,
-                                                               cropRect.origin.y + point.y - smallSquare.size.height / 2);
+                                         cropRect.origin.x + point.x - smallSquare.size.width / 2,
+                                         cropRect.origin.y + point.y - smallSquare.size.height / 2);
                                [self drawRect:smallSquare inContext:c];
                        }
                }
@@ -151,26 +192,26 @@ static const CGFloat kPadding = 10;
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 /*
-- (void) setImage:(UIImage*)image {
      if( nil == _imageView ) {
-               _imageView = [[UIImageView alloc] initWithImage:image];
-               _imageView.alpha = 0.5;
-       } else {
              _imageView.image = image;
      }
-       
      CGRect frame = _imageView.frame;
      frame.origin.x = self.cropRect.origin.x;
      frame.origin.y = self.cropRect.origin.y;
      _imageView.frame = frame;
-       
      [_points release];
      _points = nil;
      self.backgroundColor = [UIColor clearColor];
-       
      [self setNeedsDisplay];
-}
-*/
+ - (void) setImage:(UIImage*)image {
//if( nil == imageView ) {
+// imageView = [[UIImageView alloc] initWithImage:image];
+// imageView.alpha = 0.5;
+// } else {
+ imageView.image = image;
//}
//CGRect frame = imageView.frame;
//frame.origin.x = self.cropRect.origin.x;
//frame.origin.y = self.cropRect.origin.y;
//imageView.frame = CGRectMake(0,0, 30, 50);
//[_points release];
//_points = nil;
//self.backgroundColor = [UIColor clearColor];
//[self setNeedsDisplay];
+ }
+ */
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 - (UIImage*) image {
@@ -179,28 +220,26 @@ static const CGFloat kPadding = 10;
 
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
-- (CGRect) cropRect {
-       CGFloat rectSize = self.frame.size.width - kPadding * 2;
-       if (!oneDMode) {
-               return CGRectMake(kPadding, (self.frame.size.height - rectSize) / 2, rectSize, rectSize);
-       }
-       else {
-               CGFloat rectSize2 = self.frame.size.height - kPadding * 2;
-               return CGRectMake(kPadding, kPadding, rectSize, rectSize2);             
-       }
+- (void) setPoints:(NSMutableArray*)pnts {
+    [pnts retain];
+    [_points release];
+    _points = pnts;
+       
+    if (pnts != nil) {
+        self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
+    }
+    [self setNeedsDisplay];
 }
 
-
-////////////////////////////////////////////////////////////////////////////////////////////////////
-- (void) setPoints:(NSArray*)pnts {
-       [pnts retain];
-       [_points release];
-       _points = pnts;
-       
-       if (pnts != nil) {
-               self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.25];
-       }
-       [self setNeedsDisplay];
+- (void) setPoint:(CGPoint)point {
+    if (!_points) {
+        _points = [[NSMutableArray alloc] init];
+    }
+    if (_points.count > 3) {
+        [_points removeObjectAtIndex:0];
+    }
+    [_points addObject:[NSValue valueWithCGPoint:point]];
+    [self setNeedsDisplay];
 }