• R/O
  • SSH
  • HTTPS

mani2: Commit


Commit MetaInfo

Revision64 (tree)
Time2009-06-21 21:16:39
Authorhikarin

Log Message

[mani2/touch] * implemented ManiTouchViewController

Change Summary

Incremental Difference

--- trunk/touch/Classes/ManiTouchViewController.h (revision 63)
+++ trunk/touch/Classes/ManiTouchViewController.h (revision 64)
@@ -1,3 +1,22 @@
1+/*
2+ * Copyright (C) 2008-2009 hkrn
3+ *
4+ * This library is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Library General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2 of the License, or (at your option) any later version.
8+ *
9+ * This library is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * Library General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Library General Public
15+ * License along with this library; if not, write to the
16+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17+ * Boston, MA 02110-1301, USA.
18+ */
19+
120 //
221 // ManiTouchViewController.h
322 // ManiTouch
@@ -5,23 +24,41 @@
524 // Created by hkrn on 08/12/28.
625 // Copyright hkrn 2008. All rights reserved.
726 //
8-// $Id$
27+// $Id$
928 //
1029
1130 #import "MMPlayer.h"
1231 #import "MMUIView.h"
1332
14-#define kFrameRate (1.0 / 10.0)
33+#define kFrameRate 0.5
1534 #define kSpeed 100
16-#define kInputtedValue @"3879"
1735
1836 @interface ManiTouchViewController : UIViewController {
1937 IBOutlet MMUIView *canvas;
38+ IBOutlet UISearchBar *searchBar;
39+ IBOutlet UIProgressView *progressView;
40+ IBOutlet UILabel *label;
2041 MMPlayer *player;
21- NSTimer *timer;
42+ NSUInteger speed;
43+ NSUInteger frameCount;
44+ NSUInteger totalFrameCount;
45+ NSString *durationString;
46+ NSString *beforeInput;
47+ BOOL isActive;
2248 }
2349
24-- (void)start;
50+- (void)playFromSource:(NSString *)value;
2551
52+- (IBAction)play:(id)sender;
53+- (IBAction)pause:(id)sender;
54+- (IBAction)rewind:(id)sender;
55+
56+@property BOOL isActive;
57+@property NSUInteger speed;
58+@property NSUInteger frameCount;
59+@property NSUInteger totalFrameCount;
60+@property(retain) NSString *durationString;
61+@property(readonly) UISearchBar *searchBar;
62+
2663 @end
2764
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
--- trunk/touch/Classes/ManiTouchViewController.m (revision 63)
+++ trunk/touch/Classes/ManiTouchViewController.m (revision 64)
@@ -1,3 +1,22 @@
1+/*
2+ * Copyright (C) 2008-2009 hkrn
3+ *
4+ * This library is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Library General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2 of the License, or (at your option) any later version.
8+ *
9+ * This library is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+ * Library General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Library General Public
15+ * License along with this library; if not, write to the
16+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17+ * Boston, MA 02110-1301, USA.
18+ */
19+
120 //
221 // ManiTouchViewController.m
322 // ManiTouch
@@ -5,7 +24,7 @@
524 // Created by hkrn on 08/12/28.
625 // Copyright hkrn 2008. All rights reserved.
726 //
8-// $Id$
27+// $Id$
928 //
1029
1130 #import "ManiTouchViewController.h"
@@ -12,92 +31,179 @@
1231
1332 @implementation ManiTouchViewController
1433
15-/*
34+@synthesize isActive;
35+@synthesize speed;
36+@synthesize frameCount;
37+@synthesize totalFrameCount;
38+@synthesize durationString;
39+@synthesize searchBar;
40+
1641 // The designated initializer. Override to perform setup that is required before the view is loaded.
17-- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
18- if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
19- // Custom initialization
42+- (id)initWithNibName:(NSString *)nibNameOrNil
43+ bundle:(NSBundle *)nibBundleOrNil
44+{
45+ if (self = [super initWithNibName:nibNameOrNil
46+ bundle:nibBundleOrNil]) {
47+ beforeInput = @"";
48+ durationString = @"";
2049 }
2150 return self;
2251 }
23-*/
2452
25-- (void)dealloc {
53+- (void)dealloc
54+{
2655 [player release];
27- [canvas release];
56+ [beforeInput release];
57+ [durationString release];
2858 [super dealloc];
2959 }
3060
3161 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
32-- (void)viewDidLoad {
33- NSError *error = nil;
34- NSString *inputtedValue = kInputtedValue;
35- if ([MMPlayer createPlayerFromSource:inputtedValue player:&player error:&error]) {
36- canvas.size = player.size;
37- [canvas scale];
38- [self start];
39- }
62+- (void)viewDidLoad
63+{
64+ label.text = NSLocalizedString(@"Ready", nil);
65+ player = nil;
66+ self.speed = 10;
4067 [super viewDidLoad];
4168 }
4269
43-/*
4470 // Override to allow orientations other than the default portrait orientation.
45- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
46- // Return YES for supported orientations
47- return (interfaceOrientation == UIInterfaceOrientationPortrait);
48- }
49- */
71+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
72+{
73+ // Return YES for supported orientations
74+ return (interfaceOrientation == UIInterfaceOrientationPortrait);
75+}
5076
51-- (void)didReceiveMemoryWarning {
77+- (void)didReceiveMemoryWarning
78+{
5279 [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
5380 // Release anything that's not essential, such as cached data
5481 }
5582
56-- (void)start {
57- SEL selector = @selector(render);
58- NSMethodSignature *signature = [self methodSignatureForSelector:selector];
59- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
60- [invocation setTarget:self];
61- [invocation setSelector:selector];
62- timer = [[NSTimer scheduledTimerWithTimeInterval:kFrameRate invocation:invocation repeats:YES] retain];
83+- (void)start
84+{
85+ self.isActive = YES;
86+ label.text = NSLocalizedString(@"Started", nil);
87+ [NSThread detachNewThreadSelector:@selector(render)
88+ toTarget:self
89+ withObject:nil];
6390 }
6491
65-- (void)stop {
66- if (timer != nil) {
67- [timer invalidate];
68- [timer release];
69- timer = nil;
70- }
92+- (void)stop
93+{
94+ self.isActive = NO;
95+ if (self.frameCount == self.totalFrameCount) {
96+ label.text = NSLocalizedString(@"Finished", nil);
97+ }
98+ else
99+ label.text = NSLocalizedString(@"Stopped", nil);
71100 }
72101
73-- (BOOL)prepareRenderingOrError:(NSError **)err {
74- @try {
75- NSUInteger speedCount = kSpeed;
76- while (speedCount--) {
77- if (![player next])
78- return NO;
79- }
80- return YES;
81- }
82- @catch (NSException *exception) {
83- *err = [MMAux errorFromException:exception domain:kMMApplicationExceptionErrorDomain];
84- }
85- return NO;
102+- (void)update
103+{
104+ [canvas setNeedsDisplay];
105+ progressView.progress = self.frameCount / (float)self.totalFrameCount;
86106 }
87107
88-- (void)render {
89- NSError *err = nil;
90- if ([self prepareRenderingOrError:&err]) {
91- CGContextRef bitmap = player.bitmap;
92- canvas.bitmap = bitmap;
93- [canvas setNeedsDisplay];
94- }
95- else if (err != nil) {
96- [self stop];
97- }
98- else {
99- [self stop];
100- }
108+- (void)render
109+{
110+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
111+ while (self.isActive) {
112+ NSUInteger speedCount = self.speed;
113+ while (speedCount--) {
114+ if (![player nextFrame])
115+ [self stop];
116+ else {
117+ self.frameCount = self.frameCount + 1;
118+ self.durationString = [[NSString alloc] initWithFormat:@"%d / %d",
119+ self.frameCount, self.totalFrameCount];
120+ }
121+ }
122+ canvas.bitmap = player.bitmap;
123+ [self performSelectorOnMainThread:@selector(update)
124+ withObject:nil
125+ waitUntilDone:YES];
126+ NSDate *dateToWait = [[NSDate alloc] initWithTimeIntervalSinceNow:kFrameRate];
127+ [NSThread sleepUntilDate:dateToWait];
128+ [dateToWait release];
129+ }
130+ if (self.frameCount == self.totalFrameCount)
131+ self.frameCount = 0;
132+ [pool release];
101133 }
102134
135+- (void)playFromSource:(NSString *)value
136+{
137+ // 前回のキャンバスを破棄し、最初から読み直す
138+ if (player != nil)
139+ [player release];
140+ NSError *error = nil;
141+ player = [MMPlayer playerWithSource:value
142+ error:&error];
143+ self.durationString = @"0 / 0";
144+ self.frameCount = 0;
145+ if (player != nil) {
146+ self.totalFrameCount = player.totalFrameCount;
147+ canvas.size = player.size;
148+ [canvas scale];
149+ [self start];
150+ }
151+ else {
152+ NSString *desc = [error localizedDescription];
153+ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
154+ message:desc
155+ delegate:self
156+ cancelButtonTitle:@"OK"
157+ otherButtonTitles:nil];
158+ [alert show];
159+ [alert release];
160+ label.text = desc;
161+ }
162+}
163+
164+- (IBAction)play:(id)sender
165+{
166+ if (self.isActive)
167+ // 再生中
168+ return;
169+ else if (self.frameCount > 0)
170+ // 再生途中
171+ [self start];
172+ else {
173+ NSString *inputtedValue = searchBar.text;
174+ // 前回入力したキャンバスのIDと異なる
175+ if (![inputtedValue isEqualToString:beforeInput]) {
176+ self.isActive = NO;
177+ [beforeInput release];
178+ beforeInput = [inputtedValue retain];
179+ [self playFromSource:inputtedValue];
180+ }
181+ }
182+}
183+
184+- (IBAction)pause:(id)sender
185+{
186+ [self stop];
187+}
188+
189+- (IBAction)rewind:(id)sender
190+{
191+ [self stop];
192+ [self playFromSource:beforeInput];
193+}
194+
195+#pragma mark -
196+#pragma mark UISearchBarDelegate
197+
198+- (void)searchBarSearchButtonClicked:(UISearchBar *)sender
199+{
200+ [sender resignFirstResponder];
201+ [self play:sender];
202+}
203+
204+- (void)searchBarCancelButtonClicked:(UISearchBar *)sender
205+{
206+ [sender resignFirstResponder];
207+}
208+
103209 @end
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Show on old repository browser