finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
authorkoda
Sun, 26 Sep 2010 03:41:08 +0200
changeset 3904 22e4d74240e5
parent 3903 db01c37494af
child 3905 7768ac820d5a
finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
hedgewars/PascalExports.pas
hedgewars/SDLh.pas
hedgewars/hwengine.pas
hedgewars/uGame.pas
project_files/HedgewarsMobile/Classes/EditableCellView.h
project_files/HedgewarsMobile/Classes/EditableCellView.m
project_files/HedgewarsMobile/Classes/GameSetup.m
project_files/HedgewarsMobile/Classes/OverlayViewController.h
project_files/HedgewarsMobile/Classes/OverlayViewController.m
project_files/HedgewarsMobile/Classes/PascalImports.h
project_files/HedgewarsMobile/Classes/SavedGamesViewController.m
project_files/HedgewarsMobile/Classes/SavedGamesViewController.xib
project_files/HedgewarsMobile/Resources/MainMenuViewController-iPad.xib
--- a/hedgewars/PascalExports.pas	Sat Sep 25 18:38:05 2010 +0200
+++ b/hedgewars/PascalExports.pas	Sun Sep 26 03:41:08 2010 +0200
@@ -165,11 +165,6 @@
     if closeFrontend then alsoShutdownFrontend:= true;
 end;
 
-procedure HW_dismissReady; cdecl; export;
-begin
-    ReadyTimeLeft:= 0;
-end;
-
 procedure HW_setLandscape(landscape: boolean); cdecl; export;
 begin
     if landscape then
--- a/hedgewars/SDLh.pas	Sat Sep 25 18:38:05 2010 +0200
+++ b/hedgewars/SDLh.pas	Sun Sep 26 03:41:08 2010 +0200
@@ -848,6 +848,8 @@
 procedure startSpinning; cdecl; external;
 procedure stopSpinning; cdecl; external;
 function  isPhone: Boolean; cdecl; external;
+procedure replayBegan; cdecl; external;
+procedure replayFinished; cdecl; external;
 {$ENDIF}
 implementation
 
--- a/hedgewars/hwengine.pas	Sat Sep 25 18:38:05 2010 +0200
+++ b/hedgewars/hwengine.pas	Sun Sep 26 03:41:08 2010 +0200
@@ -266,10 +266,6 @@
     ControllerInit(); // has to happen before InitKbdKeyTable to map keys
     InitKbdKeyTable();
 
-    if recordFileName = '' then
-        InitIPC;
-    WriteLnToConsole(msgGettingConfig);
-
     LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
     if cLocaleFName <> 'en.txt' then
     begin
@@ -279,10 +275,19 @@
         LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName);
     end;
 
+    WriteLnToConsole(msgGettingConfig);
     if recordFileName = '' then
-        SendIPCAndWaitReply('C')        // ask for game config
+    begin
+        InitIPC;
+        SendIPCAndWaitReply('C');        // ask for game config
+    end
     else
+    begin
         LoadRecordFromFile(recordFileName);
+{$IFDEF IPHONEOS}
+        replayBegan();
+{$ENDIF}
+    end;
 
     ScriptOnGameInit;
 
--- a/hedgewars/uGame.pas	Sat Sep 25 18:38:05 2010 +0200
+++ b/hedgewars/uGame.pas	Sun Sep 26 03:41:08 2010 +0200
@@ -26,7 +26,7 @@
 ////////////////////
    implementation
 ////////////////////
-uses uMisc, uConsts, uKeys, uTeams, uIO, uAI, uGears, uScript, uSound;
+uses uMisc, uConsts, uKeys, uTeams, uIO, uAI, uGears, uScript, uSound, SDLh;
 
 procedure DoGameTick(Lag: LongInt);
 var i: LongInt;
@@ -68,6 +68,9 @@
                         if isSoundEnabled then playMusic;
                         GameType:= gmtLocal;
                         InitIPC;
+{$IFDEF IPHONEOS}
+                        replayFinished();
+{$ENDIF}
                         end;
                end
           else ProcessGears
--- a/project_files/HedgewarsMobile/Classes/EditableCellView.h	Sat Sep 25 18:38:05 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/EditableCellView.h	Sun Sep 26 03:41:08 2010 +0200
@@ -33,6 +33,7 @@
     UILabel *titleLabel;
     NSInteger minimumCharacters;
     NSInteger maximumCharacters;
+    BOOL respectEditing;
 
 @private
     NSString *oldValue;
@@ -43,6 +44,7 @@
 @property (nonatomic,retain,readonly) UILabel *titleLabel;
 @property (nonatomic,assign) NSInteger minimumCharacters;
 @property (nonatomic,assign) NSInteger maximumCharacters;
+@property (nonatomic,assign) BOOL respectEditing;
 @property (nonatomic,retain) NSString *oldValue;
 
 -(void) replyKeyboard;
--- a/project_files/HedgewarsMobile/Classes/EditableCellView.m	Sat Sep 25 18:38:05 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/EditableCellView.m	Sun Sep 26 03:41:08 2010 +0200
@@ -23,7 +23,7 @@
 #import "CommodityFunctions.h"
 
 @implementation EditableCellView
-@synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, oldValue;
+@synthesize delegate, textField, titleLabel, minimumCharacters, maximumCharacters, respectEditing, oldValue;
 
 -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
     if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
@@ -37,6 +37,7 @@
         textField.clearsOnBeginEditing = NO;
         textField.returnKeyType = UIReturnKeyDone;
         textField.adjustsFontSizeToFitWidth = YES;
+        textField.userInteractionEnabled = YES;
         [textField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
 
         [self.contentView addSubview:textField];
@@ -51,6 +52,7 @@
 
         minimumCharacters = 1;
         maximumCharacters = 64;
+        respectEditing = NO;
         oldValue = nil;
     }
     return self;
@@ -98,9 +100,11 @@
     return !([aTextField.text length] > self.maximumCharacters && [string length] > range.length);
 }
 
-// allow editing only if delegate is set and conformant to protocol
+// allow editing only if delegate is set and conformant to protocol, and if editableOnlyWhileEditing
 -(BOOL) textFieldShouldBeginEditing:(UITextField *)aTextField {
-    return (delegate != nil) && [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)];
+    return (delegate != nil) &&
+           [delegate respondsToSelector:@selector(saveTextFieldValue:withTag:)] &&
+           (respectEditing) ? ((UITableView*)[self superview]).editing : YES;
 }
 
 // the textfield is being modified, update the navigation controller
@@ -177,4 +181,12 @@
     self.oldValue = nil;
 }
 
+// when field is editable only when the tableview is editable, resign responder when exiting editing mode
+-(void) willTransitionToState:(UITableViewCellStateMask)state {
+    if (respectEditing && state == UITableViewCellStateDefaultMask)
+        [self save:nil];
+
+    [super willTransitionToState:state];
+}
+
 @end
--- a/project_files/HedgewarsMobile/Classes/GameSetup.m	Sat Sep 25 18:38:05 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/GameSetup.m	Sun Sep 26 03:41:08 2010 +0200
@@ -25,6 +25,7 @@
 #import "PascalImports.h"
 #import "CommodityFunctions.h"
 #import "NSStringExtra.h"
+#import "OverlayViewController.h"
 
 #define BUFFER_SIZE 255     // like in original frontend
 
@@ -375,7 +376,6 @@
                     DLog(@"ERROR - wrong protocol number: [%s] - expecting %d", &buffer[1], eProto);
                     clientQuit = YES;
                 }
-
                 break;
             case 'i':
                 switch (buffer[1]) {
@@ -393,6 +393,10 @@
             case 'q':
                 // game ended, can remove the savefile
                 [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
+                // so update the relative viewcontroler
+                [[NSNotificationCenter defaultCenter] postNotificationName:@"removedSave" object:nil];
+                // and disable the overlay
+                setGameRunning(NO);
                 break;
             default:
                 // is it performant to reopen the stream every time? 
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Sat Sep 25 18:38:05 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Sun Sep 26 03:41:08 2010 +0200
@@ -66,7 +66,10 @@
 
 @end
 
-// understands when the loading screen is done
+// actual game started (controls should be enabled)
 BOOL isGameRunning;
+void setGameRunning(BOOL value);
+// black screen present
+BOOL isReplay;
 // cache the grenade time
 NSInteger cachedGrenadeTime;
\ No newline at end of file
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Sat Sep 25 18:38:05 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Sun Sep 26 03:41:08 2010 +0200
@@ -36,6 +36,7 @@
 
 #define CONFIRMATION_TAG 5959
 #define GRENADE_TAG 9595
+#define BLACKVIEW_TAG 9955
 #define ANIMATION_DURATION 0.25
 #define removeConfirmationInput()   [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview];
 
@@ -97,6 +98,7 @@
 #pragma mark View Management
 -(void) viewDidLoad {
     isGameRunning = NO;
+    isReplay = NO;
     cachedGrenadeTime = 2;
     isAttacking = NO;
     
@@ -212,7 +214,7 @@
 
 // dim the overlay when there's no more input for a certain amount of time
 -(IBAction) buttonReleased:(id) sender {
-    if (!isGameRunning)
+    if (isGameRunning == NO)
         return;
 
     UIButton *theButton = (UIButton *)sender;
@@ -244,15 +246,12 @@
 // issue certain action based on the tag of the button
 -(IBAction) buttonPressed:(id) sender {
     [self activateOverlay];
-    if (isPopoverVisible) {
+    
+    if (isGameRunning == NO)
+        return;
+    
+    if (isPopoverVisible)
         [self dismissPopover];
-    }
-
-    if (!isGameRunning)
-        return;
-
-    if (HW_isWaiting())
-        HW_dismissReady();
     
     UIButton *theButton = (UIButton *)sender;
     switch (theButton.tag) {
@@ -402,12 +401,11 @@
     NSSet *allTouches = [event allTouches];
     CGPoint currentPosition = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view];
 
+    if (isGameRunning == NO)
+        return;
+    
     switch ([allTouches count]) {
         case 1:
-            // this dismisses the "get ready"
-            if (HW_isWaiting())
-                HW_dismissReady();
-
             // if we're in the menu we just click in the point
             if (HW_isAmmoOpen()) {
                 HW_setCursor(HWXZ(currentPosition.x), HWYZ(currentPosition.y));
@@ -507,9 +505,11 @@
     CGRect screen = [[UIScreen mainScreen] bounds];
     NSSet *allTouches = [event allTouches];
     int x, y, dx, dy;
-
     UITouch *touch, *first, *second;
 
+    if (isGameRunning == NO)
+        return;
+    
     switch ([allTouches count]) {
         case 1:
             touch = [[allTouches allObjects] objectAtIndex:0];
@@ -562,9 +562,13 @@
 
 #pragma mark -
 #pragma mark Functions called by pascal
-// called from AddProgress and FinishProgress (respectively)
+void setGameRunning(BOOL value) {
+    isGameRunning = value;
+}
+
+// called by uStore from AddProgress
 void startSpinning() {
-    isGameRunning = NO;
+    setGameRunning(NO);
     CGRect screen = [[UIScreen mainScreen] bounds];
     UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
     indicator.tag = 987654;
@@ -575,13 +579,16 @@
     [indicator release];
 }
 
+// called by uStore from FinishProgress
 void stopSpinning() {
     UIActivityIndicatorView *indicator = (UIActivityIndicatorView *)[[[[UIApplication sharedApplication] keyWindow] viewWithTag:SDL_VIEW_TAG] viewWithTag:987654];
     [indicator stopAnimating];
-    isGameRunning = YES;
     HW_zoomSet(1.7);
+    if (isReplay == NO)
+        setGameRunning(YES);
 }
 
+// called by CCHandlers from chNextTurn
 void clearView() {
     UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
     UIButton *theButton = (UIButton *)[theWindow viewWithTag:CONFIRMATION_TAG];
@@ -599,4 +606,34 @@
     cachedGrenadeTime = 2;
 }
 
+// called by hwengine
+void replayBegan() {
+    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
+    UIView *blackView = [[UIView alloc] initWithFrame:theWindow.frame];
+    blackView.backgroundColor = [UIColor blackColor];
+    blackView.alpha = 0.6;
+    blackView.tag = BLACKVIEW_TAG;
+    blackView.exclusiveTouch = NO;
+    blackView.multipleTouchEnabled = NO;
+    blackView.userInteractionEnabled = NO;
+    [theWindow addSubview:blackView];
+    [blackView release];
+    isReplay = YES;
+}
+
+// called by uGame
+void replayFinished() {
+    UIWindow *theWindow = [[UIApplication sharedApplication] keyWindow];
+    UIView *blackView = (UIView *)[theWindow viewWithTag:BLACKVIEW_TAG];
+    
+    [UIView beginAnimations:@"removing black" context:NULL];
+    [UIView setAnimationDuration:1];
+    blackView.alpha = 0;
+    [UIView commitAnimations];
+    [theWindow performSelector:@selector(removeFromSuperview) withObject:blackView afterDelay:1];
+    
+    setGameRunning(YES);
+    isReplay = NO;
+}
+
 @end
--- a/project_files/HedgewarsMobile/Classes/PascalImports.h	Sat Sep 25 18:38:05 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/PascalImports.h	Sun Sep 26 03:41:08 2010 +0200
@@ -66,7 +66,6 @@
     void HW_pause(void);
 
     void HW_terminate(BOOL andCloseFrontend);
-    void HW_dismissReady(void);
 
     void HW_setLandscape(BOOL rotate);
     void HW_setCursor(int x, int y);
@@ -74,7 +73,6 @@
 
     BOOL HW_isAmmoOpen(void);
     BOOL HW_isPaused(void);
-    BOOL HW_isWaiting(void);
     BOOL HW_isWeaponRequiringClick(void);
     BOOL HW_isWeaponTimerable(void);
     BOOL HW_isWeaponSwitch(void);
--- a/project_files/HedgewarsMobile/Classes/SavedGamesViewController.m	Sat Sep 25 18:38:05 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/SavedGamesViewController.m	Sun Sep 26 03:41:08 2010 +0200
@@ -32,7 +32,10 @@
 
 -(void) viewDidLoad {
     self.tableView.backgroundView = nil;
-
+    [[NSNotificationCenter defaultCenter] addObserver:self
+                                             selector:@selector(viewWillAppear:)
+                                                 name:@"removedSave"
+                                               object:nil];
     [super viewDidLoad];
 }
 
@@ -53,16 +56,9 @@
     [[self parentViewController] dismissModalViewControllerAnimated:YES];
 }
 
-// modifies the navigation bar to add the "Add" and "Done" buttons
 -(IBAction) toggleEdit:(id) sender {
     BOOL isEditing = self.tableView.editing;
     [self.tableView setEditing:!isEditing animated:YES];
-
-    UIBarButtonItem *barButton = (UIBarButtonItem *)sender;
-    if (isEditing)
-        [barButton setTitle:NSLocalizedString(@"Edit",@"")];
-    else
-        [barButton setTitle:NSLocalizedString(@"Commit",@"")];
 }
 
 #pragma mark -
@@ -84,7 +80,7 @@
         editableCell.delegate = self;
     }
     editableCell.tag = [indexPath row];
-
+    editableCell.respectEditing = YES;
     editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
     editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     
@@ -97,13 +93,24 @@
 
     return (UITableViewCell *)editableCell;
 }
-/*
+
 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section {
-    UITableViewCellEditingStyleInsert
-}*//*
--(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
-    return UITableViewCellEditingStyleInsert;
-}*/
+    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
+    footer.backgroundColor = [UIColor clearColor];
+    
+    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*80/100, 100)];
+    label.center = CGPointMake(self.tableView.frame.size.width/2,70);
+    label.textAlignment = UITextAlignmentCenter;
+    label.font = [UIFont systemFontOfSize:16];
+    label.textColor = [UIColor lightGrayColor];
+    label.numberOfLines = 5;
+    label.text = NSLocalizedString(@"Games are automatically saved and can be resumed by selecting an entry above.\nYou can modify this list by pressing the 'Edit' button.\nNotice that completed games are deleted, so make backups.",@"");
+
+    label.backgroundColor = [UIColor clearColor];
+    [footer addSubview:label];
+    [label release];
+    return [footer autorelease];
+}
 
 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
     NSUInteger row = [indexPath row];
--- a/project_files/HedgewarsMobile/Classes/SavedGamesViewController.xib	Sat Sep 25 18:38:05 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/SavedGamesViewController.xib	Sun Sep 26 03:41:08 2010 +0200
@@ -12,7 +12,7 @@
 		</object>
 		<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
 			<bool key="EncodedWithXMLCoder">YES</bool>
-			<integer value="2"/>
+			<integer value="4"/>
 		</object>
 		<object class="NSArray" key="IBDocument.PluginDependencies">
 			<bool key="EncodedWithXMLCoder">YES</bool>
@@ -75,10 +75,9 @@
 								<int key="IBUISystemItemIdentifier">5</int>
 							</object>
 							<object class="IBUIBarButtonItem" id="793091239">
-								<string key="IBUITitle">Edit</string>
 								<string key="targetRuntimeIdentifier">IBIPadFramework</string>
-								<int key="IBUIStyle">1</int>
 								<reference key="IBUIToolbar" ref="832454237"/>
+								<int key="IBUISystemItemIdentifier">2</int>
 							</object>
 						</object>
 					</object>
--- a/project_files/HedgewarsMobile/Resources/MainMenuViewController-iPad.xib	Sat Sep 25 18:38:05 2010 +0200
+++ b/project_files/HedgewarsMobile/Resources/MainMenuViewController-iPad.xib	Sun Sep 26 03:41:08 2010 +0200
@@ -12,7 +12,6 @@
 		</object>
 		<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
 			<bool key="EncodedWithXMLCoder">YES</bool>
-			<integer value="1"/>
 		</object>
 		<object class="NSArray" key="IBDocument.PluginDependencies">
 			<bool key="EncodedWithXMLCoder">YES</bool>
@@ -47,6 +46,7 @@
 						<int key="NSvFlags">274</int>
 						<string key="NSFrameSize">{1024, 768}</string>
 						<reference key="NSSuperview" ref="191373211"/>
+						<reference key="NSWindow"/>
 						<bool key="IBUIUserInteractionEnabled">NO</bool>
 						<string key="targetRuntimeIdentifier">IBIPadFramework</string>
 						<object class="NSCustomResource" key="IBUIImage">
@@ -59,6 +59,7 @@
 						<int key="NSvFlags">292</int>
 						<string key="NSFrame">{{383, 427}, {263, 244}}</string>
 						<reference key="NSSuperview" ref="191373211"/>
+						<reference key="NSWindow"/>
 						<bool key="IBUIOpaque">NO</bool>
 						<string key="targetRuntimeIdentifier">IBIPadFramework</string>
 						<int key="IBUIContentHorizontalAlignment">0</int>
@@ -95,6 +96,7 @@
 						<int key="NSvFlags">-2147483356</int>
 						<string key="NSFrame">{{611, 417}, {263, 244}}</string>
 						<reference key="NSSuperview" ref="191373211"/>
+						<reference key="NSWindow"/>
 						<bool key="IBUIOpaque">NO</bool>
 						<int key="IBUITag">1</int>
 						<string key="targetRuntimeIdentifier">IBIPadFramework</string>
@@ -120,9 +122,11 @@
 					<object class="IBUIButton" id="95106947">
 						<reference key="NSNextResponder" ref="191373211"/>
 						<int key="NSvFlags">292</int>
-						<string key="NSFrame">{{20, 19}, {18, 19}}</string>
+						<string key="NSFrame">{{788, 300}, {18, 19}}</string>
 						<reference key="NSSuperview" ref="191373211"/>
+						<reference key="NSWindow"/>
 						<bool key="IBUIOpaque">NO</bool>
+						<float key="IBUIAlpha">0.31690141558647156</float>
 						<int key="IBUITag">3</int>
 						<string key="targetRuntimeIdentifier">IBIPadFramework</string>
 						<int key="IBUIContentHorizontalAlignment">0</int>
@@ -142,6 +146,7 @@
 						<int key="NSvFlags">292</int>
 						<string key="NSFrame">{{376, 345}, {271, 21}}</string>
 						<reference key="NSSuperview" ref="191373211"/>
+						<reference key="NSWindow"/>
 						<bool key="IBUIOpaque">NO</bool>
 						<bool key="IBUIClipsSubviews">YES</bool>
 						<int key="IBUIContentMode">7</int>
@@ -170,6 +175,7 @@
 						<int key="NSvFlags">292</int>
 						<string key="NSFrame">{{932, 686}, {72, 62}}</string>
 						<reference key="NSSuperview" ref="191373211"/>
+						<reference key="NSWindow"/>
 						<bool key="IBUIOpaque">NO</bool>
 						<int key="IBUITag">2</int>
 						<string key="targetRuntimeIdentifier">IBIPadFramework</string>
@@ -192,6 +198,7 @@
 						<int key="NSvFlags">292</int>
 						<string key="NSFrame">{{20, 686}, {72, 62}}</string>
 						<reference key="NSSuperview" ref="191373211"/>
+						<reference key="NSWindow"/>
 						<bool key="IBUIOpaque">NO</bool>
 						<int key="IBUITag">4</int>
 						<string key="targetRuntimeIdentifier">IBIPadFramework</string>
@@ -209,6 +216,7 @@
 				</object>
 				<string key="NSFrameSize">{1024, 768}</string>
 				<reference key="NSSuperview"/>
+				<reference key="NSWindow"/>
 				<object class="NSColor" key="IBUIBackgroundColor">
 					<int key="NSColorSpace">1</int>
 					<bytes key="NSRGB">MCAwIDAAA</bytes>
@@ -303,8 +311,8 @@
 							<reference ref="607338789"/>
 							<reference ref="898948205"/>
 							<reference ref="867308721"/>
+							<reference ref="894101036"/>
 							<reference ref="95106947"/>
-							<reference ref="894101036"/>
 						</object>
 						<reference key="parent" ref="0"/>
 					</object>