RSS

(root)/iphone/common : 61 : Jigs/source/ImageWell.m

« back to all changes in this revision

Viewing changes to Jigs/source/ImageWell.m

Dömötör Gulyás
2010-01-18 09:03:51
Revision ID: dognotdog@gmail.com-20100118080351-ib2knxvk4w8ssw3h
made common a nested tree

Show diffs side-by-side

added added

removed removed

1
 
//
2
 
//  ImageWell.m
3
 
//  Jigs
4
 
//
5
 
//  Created by döme on 11.08.2009.
6
 
//  Copyright 2009 __MyCompanyName__. All rights reserved.
7
 
//
8
 
 
9
 
#import "ImageWell.h"
10
 
#import "ElAnimation.h"
11
 
 
12
 
#import <QuartzCore/QuartzCore.h>
13
 
 
14
 
void createPathInRoundedRect(CGContextRef c, CGRect rect, float radius)
15
 
{
16
 
        CGContextBeginPath(c);
17
 
        CGContextMoveToPoint(c, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect) );
18
 
        CGContextAddLineToPoint(c, CGRectGetMaxX(rect) - radius, CGRectGetMinY(rect));
19
 
        CGContextAddArcToPoint(c, CGRectGetMaxX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMinY(rect) + radius, radius);
20
 
        CGContextAddLineToPoint(c, CGRectGetMaxX(rect), CGRectGetMaxY(rect) - radius);
21
 
        CGContextAddArcToPoint(c, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMaxX(rect) - radius, CGRectGetMaxY(rect), radius);
22
 
        CGContextAddLineToPoint(c, CGRectGetMinX(rect) + radius, CGRectGetMaxY(rect));
23
 
        CGContextAddArcToPoint(c, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMaxY(rect) - radius, radius);
24
 
        CGContextAddLineToPoint(c, CGRectGetMinX(rect), CGRectGetMinY(rect) + radius );
25
 
        CGContextAddArcToPoint(c, CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetMinX(rect) + radius, CGRectGetMinY(rect), radius);
26
 
        CGContextClosePath(c);
27
 
        
28
 
 
29
 
}
30
 
 
31
 
 
32
 
 
33
 
@implementation ElImageView
34
 
 
35
 
- (id) initWithFrame: (CGRect) frame
36
 
{
37
 
    if (!(self = [super initWithFrame: frame]))
38
 
                return nil;
39
 
 
40
 
        borderColor = [[UIColor grayColor] retain];
41
 
        cornerRadius = 5.0f;
42
 
        borderWidth = 1.0f;
43
 
        imageAlpha = 1.0f;
44
 
 
45
 
        [self setBackgroundColor: [UIColor redColor]];
46
 
 
47
 
        return self;
48
 
}
49
 
 
50
 
 
51
 
- (id) initWithCoder: (NSCoder*) coder
52
 
{
53
 
    if (!(self = [super initWithCoder: coder]))
54
 
                return nil;
55
 
        
56
 
        borderColor = [[UIColor grayColor] retain];
57
 
        cornerRadius = 5.0f;
58
 
        borderWidth = 1.0f;
59
 
        imageAlpha = 1.0f;
60
 
        
61
 
        fillView = NO;
62
 
        
63
 
        [self setBackgroundColor: [UIColor clearColor]];
64
 
 
65
 
        return self;
66
 
}
67
 
 
68
 
- (void) dealloc
69
 
{
70
 
        [image release];
71
 
        [borderColor release];
72
 
    [super dealloc];
73
 
}
74
 
 
75
 
 
76
 
- (void) drawRect: (CGRect) rect
77
 
{
78
 
        CGContextRef c = UIGraphicsGetCurrentContext();
79
 
        //[[UIColor blueColor] set];
80
 
        //CGContextFillRect(c, rect);
81
 
 
82
 
        CGContextSaveGState(c);
83
 
 
84
 
        UIImage* img = nil;
85
 
        
86
 
        @synchronized(self)
87
 
        {
88
 
                img = image;
89
 
        }
90
 
        if (!img)
91
 
                img = [UIImage imageNamed: @"image-not-found-icon.png"];
92
 
        
93
 
        CGRect bounds = CGRectInset([self bounds], 0.5*borderWidth,0.5*borderWidth);
94
 
        CGSize imgSize = [img size];
95
 
        
96
 
        
97
 
        float drawHeight = imgSize.height;
98
 
        float drawWidth = imgSize.width;
99
 
        
100
 
        float heightR = imgSize.height/bounds.size.height;
101
 
        float widthR = imgSize.width/bounds.size.width;
102
 
        
103
 
        if (heightR > 1.0f)
104
 
        {
105
 
                drawHeight = bounds.size.height;
106
 
                drawWidth *= 1.0f/heightR;
107
 
                widthR = drawWidth/bounds.size.width;
108
 
        }
109
 
 
110
 
        if (widthR > 1.0f)
111
 
        {
112
 
                drawWidth = bounds.size.width;
113
 
                drawHeight *= 1.0f/widthR;
114
 
        }
115
 
        
116
 
        CGRect drawRect = CGRectMake(bounds.origin.x + 0.5f*(bounds.size.width - drawWidth), bounds.origin.y + 0.5f*(bounds.size.height - drawHeight), drawWidth, drawHeight);
117
 
 
118
 
        if (fillView)
119
 
                drawRect = bounds;
120
 
        
121
 
        createPathInRoundedRect(c, drawRect, cornerRadius);
122
 
 
123
 
        CGContextClip(c);
124
 
 
125
 
        [img drawInRect: drawRect blendMode: kCGBlendModeNormal alpha: imageAlpha];
126
 
 
127
 
        CGContextRestoreGState(c);
128
 
        if (borderWidth)
129
 
        {
130
 
                [[borderColor colorWithAlphaComponent: imageAlpha] set];
131
 
                createPathInRoundedRect(c, drawRect, cornerRadius);
132
 
                CGContextSetLineWidth(c, borderWidth);
133
 
                CGContextStrokePath(c);
134
 
        }
135
 
}
136
 
 
137
 
- (void) setImageAlpha: (float) a
138
 
{
139
 
        imageAlpha = a;
140
 
        [self setNeedsDisplay];
141
 
}
142
 
 
143
 
- (void) setImage: (UIImage*) img
144
 
{
145
 
        @synchronized(self)
146
 
        {
147
 
                [img retain];
148
 
                [image release];
149
 
                image = img;
150
 
        }
151
 
        [self performSelectorOnMainThread: @selector(setNeedsDisplay) withObject: nil waitUntilDone: NO];
152
 
        //[self setNeedsDisplay];
153
 
}
154
 
 
155
 
@synthesize image, cornerRadius, borderColor, borderWidth, fillView, imageAlpha;
156
 
 
157
 
@end
158
 
 
159
 
 
160
 
 
161
 
@implementation ImageWell
162
 
 
163
 
 
164
 
- (id) initWithFrame: (CGRect) frame
165
 
{
166
 
    if (!(self = [super initWithFrame: frame]))
167
 
                return nil;
168
 
 
169
 
        return self;
170
 
}
171
 
 
172
 
- (id) initWithCoder: (NSCoder*) coder
173
 
{
174
 
    if (!(self = [super initWithCoder: coder]))
175
 
                return nil;
176
 
        
177
 
        instructionsView = [[UITextView alloc] initWithFrame: CGRectInset([self bounds], 0.0f, 0.0f)];
178
 
        [instructionsView setText: @"\n\n\nTap and hold to paste image,\n\nor\n\nDouble-tap to select image from gallery."];
179
 
        //[instructionsView setBackgroundColor: [UIColor yellowColor]];
180
 
        [instructionsView setTextColor: [UIColor whiteColor]];
181
 
        [instructionsView setBackgroundColor: [UIColor clearColor]];
182
 
        [instructionsView setFont: [UIFont systemFontOfSize: [UIFont labelFontSize]]];
183
 
        [instructionsView setEditable: NO];
184
 
        [instructionsView setUserInteractionEnabled: NO];
185
 
        [instructionsView setTextAlignment: UITextAlignmentCenter];
186
 
        [self addSubview: instructionsView];
187
 
 
188
 
        [instructionsView layer].opacity = 0.0f;
189
 
 
190
 
 
191
 
        return self;
192
 
}
193
 
 
194
 
- (void) setFrame: (CGRect) frame
195
 
{
196
 
        [super setFrame: frame];
197
 
        [instructionsView setFrame: CGRectInset([self bounds], 0.0f, 0.0f)];
198
 
}
199
 
 
200
 
 
201
 
 
202
 
- (void) dealloc
203
 
{
204
 
    [super dealloc];
205
 
}
206
 
 
207
 
- (void) pasteAction: (id) info
208
 
{
209
 
        NSArray* images = [[UIPasteboard generalPasteboard] images];
210
 
        if ([images count])
211
 
        {
212
 
                UIImage* img = [images objectAtIndex: 0];
213
 
                
214
 
                [self setImage: img];
215
 
                [delegate imageWasSelectedInWell: self];
216
 
        }
217
 
        pasteTimer = nil;
218
 
}
219
 
 
220
 
- (void) showInstructions: (float) time
221
 
{
222
 
                time = MAX(1.0f, time);
223
 
                
224
 
                ElFloatAnimation* anim = nil;
225
 
 
226
 
                anim = [[[ElFloatAnimation alloc] init] autorelease];
227
 
                
228
 
                [anim setStartValue: [[instructionsView layer] opacity]];
229
 
                [anim setEndValue: 0.667f];
230
 
                [anim setDuration: 0.5f];
231
 
                [anim setTarget: [instructionsView layer]];
232
 
                [anim setProperty: @"opacity"];
233
 
                [anim queueForKey: @"text-opacity" atBeginning: YES cancelPending: YES];
234
 
                
235
 
                
236
 
                anim = [[[ElFloatAnimation alloc] init] autorelease];
237
 
                
238
 
                [anim setStartValue: imageAlpha];
239
 
                [anim setEndValue: 0.333f];
240
 
                [anim setDuration: 0.5f];
241
 
                [anim setTarget: self];
242
 
                [anim setProperty: @"imageAlpha"];
243
 
                [anim queueForKey: @"image-opacity" atBeginning: YES cancelPending: YES];
244
 
                
245
 
                [[ElAnimation delayedAnimationWithDelay: time - 0.5f] queueForKey: @"text-opacity" atBeginning: NO cancelPending: NO];
246
 
                [[ElAnimation delayedAnimationWithDelay: time - 0.5f] queueForKey: @"image-opacity" atBeginning: NO cancelPending: NO];
247
 
 
248
 
                anim = [[[ElFloatAnimation alloc] init] autorelease];
249
 
                
250
 
                [anim setStartValue: 0.667f];
251
 
                [anim setEndValue: 0.0f];
252
 
                [anim setDuration: 0.5f];
253
 
                [anim setTarget: [instructionsView layer]];
254
 
                [anim setProperty: @"opacity"];
255
 
                [anim queueForKey: @"text-opacity" atBeginning: NO cancelPending: NO];
256
 
 
257
 
 
258
 
                anim = [[[ElFloatAnimation alloc] init] autorelease];
259
 
                
260
 
                [anim setStartValue: 0.333f];
261
 
                [anim setEndValue: 1.0f];
262
 
                [anim setDuration: 0.5f];
263
 
                [anim setTarget: self];
264
 
                [anim setProperty: @"imageAlpha"];
265
 
                [anim queueForKey: @"image-opacity" atBeginning: NO cancelPending: NO];
266
 
}
267
 
 
268
 
- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
269
 
{
270
 
        UITouch* touch = [touches anyObject];
271
 
        
272
 
        if ([touch tapCount] == 1)
273
 
        {
274
 
                [self showInstructions: 3.0f];
275
 
        }
276
 
 
277
 
        
278
 
        touchBeganTime = [event timestamp];
279
 
        
280
 
        pasteTimer = [NSTimer scheduledTimerWithTimeInterval: 3.0 target: self selector:@selector(pasteAction:) userInfo: nil repeats: NO];
281
 
        
282
 
        if ([touch tapCount] == 2)
283
 
        {
284
 
                [pasteTimer invalidate];
285
 
                pasteTimer = nil;
286
 
 
287
 
                if (!imagePicker)
288
 
                {
289
 
                        imagePicker = [[UIImagePickerController alloc] init];
290
 
 
291
 
                        //[imagePicker setSourceType: UIImagePickerControllerSourceTypeCamera];
292
 
                        [imagePicker setDelegate: self];
293
 
                        //[imagePicker setAllowsImageEditing: YES];
294
 
                        //[imagePicker setShowsCameraControls: NO];
295
 
                }
296
 
                
297
 
                assert(delegate);
298
 
                [delegate presentModalViewController: imagePicker animated: YES];
299
 
        }
300
 
}
301
 
 
302
 
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
303
 
{
304
 
//      UITouch* touch = [touches anyObject];
305
 
        
306
 
        [pasteTimer invalidate];
307
 
        pasteTimer = nil;
308
 
        
309
 
}
310
 
 
311
 
- (void) touchesCancelled: (NSSet*) touches withEvent: (UIEvent*) event
312
 
{
313
 
//      UITouch* touch = [touches anyObject];
314
 
        
315
 
        [pasteTimer invalidate];
316
 
        pasteTimer = nil;
317
 
        
318
 
}
319
 
 
320
 
- (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
321
 
{
322
 
        
323
 
        UIImage* img = [info objectForKey: UIImagePickerControllerEditedImage];
324
 
        
325
 
        if (!img)
326
 
                img = [info objectForKey: UIImagePickerControllerOriginalImage];
327
 
 
328
 
        if (img)
329
 
        {
330
 
                [self setImage: img];
331
 
                [delegate imageWasSelectedInWell: self];
332
 
        }
333
 
 
334
 
        [delegate dismissModalViewControllerAnimated: YES];
335
 
 
336
 
}
337
 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
338
 
{
339
 
        [delegate dismissModalViewControllerAnimated: YES];
340
 
}
341
 
 
342
 
 
343
 
@synthesize delegate;
344
 
 
345
 
@end

Loggerhead 1.17 is a web-based interface for Bazaar branches