forked from atomton/ATMHud
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ATMHud.m
336 lines (283 loc) · 7.67 KB
/
ATMHud.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* ATMHud.m
* ATMHud
*
* Created by Marcel Müller on 2011-03-01.
* Copyright (c) 2010-2011, Marcel Müller (atomcraft)
* All rights reserved.
*
* https://github.com/atomton/ATMHud
*/
#import "ATMHud.h"
#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioServices.h>
#import "ATMHudView.h"
#import "ATMProgressLayer.h"
#import "ATMHudDelegate.h"
#import "ATMSoundFX.h"
#import "ATMHudQueueItem.h"
@interface ATMHud (Private)
- (void)construct;
@end
@implementation ATMHud
@synthesize margin, padding, alpha, appearScaleFactor, disappearScaleFactor, progressBorderRadius, progressBorderWidth, progressBarRadius, progressBarInset;
@synthesize gray;
@synthesize delegate, accessoryPosition;
@synthesize center;
@synthesize shadowEnabled, blockTouches, allowSuperviewInteraction;
@synthesize showSound, updateSound, hideSound;
@synthesize __view, sound, displayQueue, queuePosition;
@synthesize autocenter;
@synthesize autoBringToFront;
- (id)init {
if ((self = [super init])) {
[self construct];
}
return self;
}
- (id)initWithDelegate:(id)hudDelegate {
if ((self = [super init])) {
delegate = hudDelegate;
[self construct];
}
return self;
}
- (void)loadView {
UIView *base = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
base.backgroundColor = [UIColor clearColor];
base.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
base.userInteractionEnabled = NO;
[base addSubview:__view];
self.view = base;
[base release];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[sound release];
[__view release];
[displayQueue release];
[showSound release];
[updateSound release];
[hideSound release];
[super dealloc];
}
+ (NSString *)buildInfo {
return @"atomHUD 1.2 • 2011-03-01";
}
#pragma mark -
#pragma mark Overrides
- (void)setAppearScaleFactor:(CGFloat)value {
if (value == 0) {
value = 0.01;
}
appearScaleFactor = value;
}
- (void)setDisappearScaleFactor:(CGFloat)value {
if (value == 0) {
value = 0.01;
}
disappearScaleFactor = value;
}
- (void)setAlpha:(CGFloat)value {
alpha = value;
[CATransaction begin];
[CATransaction setDisableActions:YES];
__view.backgroundLayer.backgroundColor = [UIColor colorWithWhite:gray alpha:value].CGColor;
[CATransaction commit];
}
- (void)setGray:(CGFloat)value {
gray = value;
[CATransaction begin];
[CATransaction setDisableActions:YES];
__view.backgroundLayer.backgroundColor = [UIColor colorWithWhite:gray alpha:alpha].CGColor;
[CATransaction commit];
}
- (void)setShadowEnabled:(BOOL)value {
shadowEnabled = value;
if (shadowEnabled) {
__view.layer.shadowOpacity = 0.4;
} else {
__view.layer.shadowOpacity = 0.0;
}
}
#pragma mark -
#pragma mark Property forwards
- (void)setCaption:(NSString *)caption {
__view.caption = caption;
}
- (void)setImage:(UIImage *)image {
__view.image = image;
}
- (void)setActivity:(BOOL)activity {
__view.showActivity = activity;
if (activity) {
[__view.activity startAnimating];
} else {
[__view.activity stopAnimating];
}
}
- (void)setActivityStyle:(UIActivityIndicatorViewStyle)activityStyle {
__view.activityStyle = activityStyle;
if (activityStyle == UIActivityIndicatorViewStyleWhiteLarge) {
__view.activitySize = CGSizeMake(37, 37);
} else {
__view.activitySize = CGSizeMake(20, 20);
}
}
- (void)setFixedSize:(CGSize)fixedSize {
__view.fixedSize = fixedSize;
}
- (void)setProgress:(CGFloat)progress {
__view.progress = progress;
[__view.progressLayer setTheProgress:progress];
[__view.progressLayer setNeedsDisplay];
}
- (void)setCenter:(CGPoint)p {
center = p;
__view.center = center;
}
#pragma mark -
#pragma mark Queue
- (void)addQueueItem:(ATMHudQueueItem *)item {
[displayQueue addObject:item];
}
- (void)addQueueItems:(NSArray *)items {
[displayQueue addObjectsFromArray:items];
}
- (void)clearQueue {
[displayQueue removeAllObjects];
}
- (void)startQueue {
queuePosition = 0;
if (!CGSizeEqualToSize(__view.fixedSize, CGSizeZero)) {
CGSize newSize = __view.fixedSize;
CGSize targetSize;
ATMHudQueueItem *queueItem;
for (int i = 0; i < [displayQueue count]; i++) {
queueItem = [displayQueue objectAtIndex:i];
targetSize = [__view calculateSizeForQueueItem:queueItem];
if (targetSize.width > newSize.width) {
newSize.width = targetSize.width;
}
if (targetSize.height > newSize.height) {
newSize.height = targetSize.height;
}
}
[self setFixedSize:newSize];
}
[self showQueueAtIndex:queuePosition];
}
- (void)showNextInQueue {
queuePosition++;
[self showQueueAtIndex:queuePosition];
}
- (void)showQueueAtIndex:(NSInteger)index {
if ([displayQueue count] > 0) {
queuePosition = index;
if (queuePosition == [displayQueue count]) {
[self hide];
return;
}
ATMHudQueueItem *item = [displayQueue objectAtIndex:queuePosition];
__view.caption = item.caption;
__view.image = item.image;
BOOL flag = item.showActivity;
__view.showActivity = flag;
if (flag) {
[__view.activity startAnimating];
} else {
[__view.activity stopAnimating];
}
self.accessoryPosition = item.accessoryPosition;
[self setActivityStyle:item.activityStyle];
if (queuePosition == 0) {
[__view show];
} else {
[__view update];
}
}
}
#pragma mark -
#pragma mark Controlling
- (void)show {
if (autocenter) {
CGRect svb = self.view.superView.bounds;
self.view.center = CGPointMake(svb.origin.x + svb.size.width / 2,
svb.origin.y + svb.size.height / 2);
}
if (autoBringToFront)
[self.superView bringSubviewToFront:self.view];
[__view show];
}
- (void)update {
[__view update];
}
- (void)hide {
[__view hide];
}
- (void)hideAfter:(NSTimeInterval)delay {
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(hide)
object:nil];
[self performSelector:@selector(hide) withObject:nil afterDelay:delay];
}
- (void)showWithCaption:(NSString *)caption andHideAfter:(NSTimeInterval)delay {
[self setCaption:caption];
[self show];
[self hideAfter:delay];
}
#pragma mark -
#pragma mark Internal methods
- (void)construct {
margin = padding = 10.0;
alpha = 0.7;
gray = 0.0;
progressBorderRadius = 8.0;
progressBorderWidth = 2.0;
progressBarRadius = 5.0;
progressBarInset = 3.0;
accessoryPosition = ATMHudAccessoryPositionBottom;
appearScaleFactor = disappearScaleFactor = 1.4;
__view = [[ATMHudView alloc] initWithFrame:CGRectZero andController:self];
__view.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin);
displayQueue = [[NSMutableArray alloc] init];
queuePosition = 0;
center = CGPointZero;
blockTouches = NO;
allowSuperviewInteraction = NO;
autocenter = YES;
autoBringToFront = YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (!blockTouches) {
UITouch *aTouch = [touches anyObject];
if (aTouch.tapCount == 1) {
CGPoint p = [aTouch locationInView:self.view];
if (CGRectContainsPoint(__view.frame, p)) {
if ([(id)self.delegate respondsToSelector:@selector(userDidTapHud:)]) {
[self.delegate userDidTapHud:self];
}
}
}
}
}
- (void)playSound:(NSString *)soundPath {
sound = [[ATMSoundFX alloc] initWithContentsOfFile:soundPath];
[sound play];
}
@end