Issue 479
[zxing.git] / iphone / ZXingWidget / Classes / OverlayView.m
index 7c55d49..2093bff 100755 (executable)
@@ -28,11 +28,21 @@ static const CGFloat kPadding = 10;
 @synthesize delegate, oneDMode;
 @synthesize points = _points;
 @synthesize cancelButton;
+@synthesize cropRect;
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 - (id) initWithFrame:(CGRect)theFrame cancelEnabled:(BOOL)isCancelEnabled oneDMode:(BOOL)isOneDModeEnabled {
-       if( self = [super initWithFrame:theFrame] ) {
-               self.backgroundColor = [UIColor clearColor];
+  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]; 
@@ -48,9 +58,10 @@ static const CGFloat kPadding = 10;
       
       [cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
       [self addSubview:cancelButton];
+      [self addSubview:imageView];
     }
   }
-       return self;
+  return self;
 }
 
 - (void)cancel:(id)sender {
@@ -63,9 +74,7 @@ static const CGFloat kPadding = 10;
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 - (void) dealloc {
        [imageView release];
-       imageView = nil;
        [_points release];
-       _points = nil;
        [super dealloc];
 }
 
@@ -80,14 +89,41 @@ 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];
        }
@@ -134,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);
@@ -143,7 +179,7 @@ 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);
@@ -157,23 +193,23 @@ 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;
- }
//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;
//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];
//[_points release];
//_points = nil;
//self.backgroundColor = [UIColor clearColor];
  
- [self setNeedsDisplay];
//[self setNeedsDisplay];
  }
  */
 
@@ -184,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];
 }