[dasher: 34/43] iPhone: Include discard/trashcan icon as action; add game mode (play/stop) btn



commit a73181ae37b4382a6bb01887f82b1b04916abd87
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Thu May 12 16:37:56 2011 +0100

    iPhone: Include discard/trashcan icon as action; add game mode (play/stop) btn
    
    Add [DasherAppDelegate refreshToolbar] by storing items => mutate and (re)set
    Fix decl/defn int numActions (now const)

 Src/iPhone/Classes/ActionConfigurator.h      |    3 +-
 Src/iPhone/Classes/ActionConfigurator.mm     |    9 +----
 Src/iPhone/Classes/Actions.h                 |   10 ++---
 Src/iPhone/Classes/Actions.mm                |   44 +++++++++++++++----------
 Src/iPhone/Classes/CDasherInterfaceBridge.mm |    2 +
 Src/iPhone/Classes/DasherAppDelegate.h       |    3 +-
 Src/iPhone/Classes/DasherAppDelegate.mm      |   45 ++++++++++++++++---------
 7 files changed, 66 insertions(+), 50 deletions(-)
---
diff --git a/Src/iPhone/Classes/ActionConfigurator.h b/Src/iPhone/Classes/ActionConfigurator.h
index 855c62c..bf21545 100644
--- a/Src/iPhone/Classes/ActionConfigurator.h
+++ b/Src/iPhone/Classes/ActionConfigurator.h
@@ -12,6 +12,5 @@
   ActionButton *button;
   UIView *headers[3];
 }
--(id)initWithButton:(ActionButton *)_button;
-+(ActionConfigurator *)instanceForButton:(ActionButton *)button;
+-(id)init;
 @end
diff --git a/Src/iPhone/Classes/ActionConfigurator.mm b/Src/iPhone/Classes/ActionConfigurator.mm
index 5475ef4..6444436 100644
--- a/Src/iPhone/Classes/ActionConfigurator.mm
+++ b/Src/iPhone/Classes/ActionConfigurator.mm
@@ -18,9 +18,8 @@ using Dasher::Settings::GetParameterName;
 
 @implementation ActionConfigurator
 
-- (id)initWithButton:(ActionButton *)_button {
+- (id)init {
   if (self = [super initWithStyle:UITableViewStyleGrouped]) {
-    button = _button;
     self.tabBarItem.title=@"Actions";
     self.tabBarItem.image=[UIImage imageNamed:@"spanner_lg.png"];
     self.navigationItem.title=@"Configure Actions";
@@ -29,10 +28,6 @@ using Dasher::Settings::GetParameterName;
   return self;
 }
 
-+(ActionConfigurator *)instanceForButton:(ActionButton *)button {
-  return [[[self alloc] initWithButton:button] autorelease];
-}
-
 #pragma mark -
 #pragma mark View lifecycle
 
@@ -182,7 +177,7 @@ using Dasher::Settings::GetParameterName;
   UISwitch *sw = (UISwitch *)sender;
   SAction *act = &actions[ sw.tag -1 ];
   [[NSUserDefaults standardUserDefaults] setBool:sw.on forKey:act->settingName];
-  [button refresh];
+  [[DasherAppDelegate theApp] refreshToolbar];
 }
 
 -(void)paramSlid:(id)sender {
diff --git a/Src/iPhone/Classes/Actions.h b/Src/iPhone/Classes/Actions.h
index 47ff3e7..47fe63d 100644
--- a/Src/iPhone/Classes/Actions.h
+++ b/Src/iPhone/Classes/Actions.h
@@ -14,15 +14,13 @@ typedef struct {
   NSString *toolbarIconFile;
 } SAction;
 
-#ifndef __ACTIONS_MM__
+//#ifndef __ACTIONS_MM__
 extern SAction actions[];
-extern int numActions;
-#endif
+extern const int numActions;
+//#endif
 
 @interface ActionButton : UIBarButtonItem <UIActionSheetDelegate> {
   UIToolbar *toolbar;
-  int numActionsOn, *actionsOn;
 }
-- (id)initForToolbar:(UIToolbar *)toolbar;
-- (void)refresh;
++(ActionButton *)buttonForToolbar:(UIToolbar *)toolbar;
 @end
diff --git a/Src/iPhone/Classes/Actions.mm b/Src/iPhone/Classes/Actions.mm
index c8b46f8..05e03b6 100644
--- a/Src/iPhone/Classes/Actions.mm
+++ b/Src/iPhone/Classes/Actions.mm
@@ -18,34 +18,25 @@ SAction actions[] = {
   {@"Copy to Clipboard",@"iphone_act_copy", @"copy.png"},
   {@"Cut to Clipboard", @"iphone_act_cut", @"scissors.png"},
   {@"Paste from Clipboard", @"iphone_act_paste", @"paste.png"},
+  {@"Discard",@"iphone_act_discard",@"trash.png"},
 };
 
-int numActions = sizeof(actions) / sizeof(actions[0]);
+const int numActions = sizeof(actions) / sizeof(actions[0]);
 
 @interface ActionButton ()
 - (void)performAction:(int)which checkClear:(BOOL)bCheck;
 @end;
 
+int numActionsOn;
+int actionsOn[numActions];
 
 static NSString *actionIconFile = @"spanner.png";
 
- implementation ActionButton
+ActionButton *sysTrash=nil, *general=nil;
 
--(id)initForToolbar:(UIToolbar *)_toolbar {
-  if (self = [super initWithImage:[UIImage imageNamed:actionIconFile] style:UIBarButtonItemStylePlain target:self action:@selector(clicked)]) {
-    toolbar = _toolbar;
-    actionsOn = new int[numActions];
-    [self refresh];
-  }
-  return self;
-}
-
--(void)dealloc {
-  delete[] actionsOn;
-  [super dealloc];
-}
+ implementation ActionButton
 
--(void)refresh {
++(ActionButton *)buttonForToolbar:(UIToolbar *)_toolbar {
   numActionsOn=0;
   NSUserDefaults *settings=[NSUserDefaults standardUserDefaults];
   NSString *iconFile=nil;
@@ -59,14 +50,28 @@ static NSString *actionIconFile = @"spanner.png";
   for (int i=numActionsOn; i<numActions; i++) actionsOn[i]=-1;
   
   //multiple items, _or_ none (=>configure), display generic actions/tools icon
+  if (numActionsOn==1 && actionsOn[0]==6) {
+    if (!sysTrash) {
+      sysTrash = [[ActionButton alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:@selector(clicked)];
+      sysTrash.target = sysTrash;
+    }
+    sysTrash->toolbar = _toolbar;
+    return sysTrash;
+  }
   if (numActionsOn!=1) iconFile = actionIconFile;
-  [self setImage:[UIImage imageNamed:iconFile]];
+  if (!general) {
+    general = [[ActionButton alloc] initWithImage:[UIImage imageNamed:actionIconFile] style:UIBarButtonItemStylePlain target:nil action:@selector(clicked)];
+    general.target = general;
+  } else
+    [general setImage:[UIImage imageNamed:iconFile]];
+  general->toolbar = _toolbar;
+  return general;
 }
 
 - (void)clicked {
   if (numActionsOn==0) {
     //no actions enabled! display configurator...
-    [[DasherAppDelegate theApp] presentModalViewController:[[[UINavigationController alloc] initWithRootViewController:[ActionConfigurator instanceForButton:self]] autorelease] animated:YES];
+    [[DasherAppDelegate theApp] presentModalViewController:[[[UINavigationController alloc] initWithRootViewController:[[[ActionConfigurator alloc] init] autorelease]] autorelease] animated:YES];
   } else if (numActionsOn==1) {
     //a single action is enabled...
     [self performAction:actionsOn[0] checkClear:YES];
@@ -95,6 +100,9 @@ static NSString *actionIconFile = @"spanner.png";
       case 6:
         choice = [[[UIActionSheet alloc] initWithTitle:@"Actions" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:actions[actionsOn[0]].dispName,actions[actionsOn[1]].dispName,actions[actionsOn[2]].dispName,actions[actionsOn[3]].dispName,actions[actionsOn[4]].dispName,actions[actionsOn[5]].dispName,nil] autorelease];
         break;
+      case 7:
+        choice = [[[UIActionSheet alloc] initWithTitle:@"Actions" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:actions[actionsOn[0]].dispName,actions[actionsOn[1]].dispName,actions[actionsOn[2]].dispName,actions[actionsOn[3]].dispName,actions[actionsOn[4]].dispName,actions[actionsOn[5]].dispName,actions[actionsOn[6]].dispName,nil] autorelease];
+        break;
       default:
         //ok, some other number! But implementing for future-proofing...
         // We don't use this method normally because the cancel button will appear in the wrong place (at the top!),
diff --git a/Src/iPhone/Classes/CDasherInterfaceBridge.mm b/Src/iPhone/Classes/CDasherInterfaceBridge.mm
index af82e9f..2a8726d 100644
--- a/Src/iPhone/Classes/CDasherInterfaceBridge.mm
+++ b/Src/iPhone/Classes/CDasherInterfaceBridge.mm
@@ -143,6 +143,8 @@ void CDasherInterfaceBridge::HandleEvent(int iParameter) {
     [dasherApp notifySpeedChange];
   else if (iParameter == SP_ALPHABET_ID)
     [dasherApp setAlphabet:GetActiveAlphabet()];
+  else if (iParameter == BP_GAME_MODE)
+    [dasherApp refreshToolbar];
   CDasherInterfaceBase::HandleEvent(iParameter);
 }
 
diff --git a/Src/iPhone/Classes/DasherAppDelegate.h b/Src/iPhone/Classes/DasherAppDelegate.h
index 1745310..22af105 100644
--- a/Src/iPhone/Classes/DasherAppDelegate.h
+++ b/Src/iPhone/Classes/DasherAppDelegate.h
@@ -29,7 +29,7 @@
   BOOL m_bLandscapeSupported;
   /// Should really be part of UIViewController (lockable), below...but then, how to find?
   UILabel *screenLockLabel;
-  ActionButton *actions;
+  NSMutableArray *toolbarItems;
   NSString *m_wordBoundary, *m_sentenceBoundary, *m_lineBoundary;
 }
 
@@ -49,6 +49,7 @@
 - (void)clearText;
 - (NSString *)allText;
 - (void)notifySpeedChange;
+- (void)refreshToolbar;
 - (NSString *)textAtOffset:(unsigned int)offset Length:(unsigned int)length;
 - (void)setLockText:(NSString *)s;
 - (void)displayMessage:(NSString *)msg;
diff --git a/Src/iPhone/Classes/DasherAppDelegate.mm b/Src/iPhone/Classes/DasherAppDelegate.mm
index 739a45f..f5e1a07 100644
--- a/Src/iPhone/Classes/DasherAppDelegate.mm
+++ b/Src/iPhone/Classes/DasherAppDelegate.mm
@@ -215,23 +215,11 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
   [speedSlider addTarget:self action:@selector(fadeSlider) forControlEvents:UIControlEventAllTouchEvents];
   [speedSlider addTarget:self action:@selector(speedSlid:) forControlEvents:UIControlEventValueChanged];
   //...and lay them out
-	UIBarButtonItem *settings = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"cog.png"] style:UIBarButtonItemStylePlain target:self action:@selector(settings)] autorelease];
 	speedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 	[speedBtn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 2.0, 0.0, 2.0)];
 	[speedBtn addTarget:self action:@selector(fadeSlider) forControlEvents:UIControlEventAllTouchEvents];
   
-  actions = [[[ActionButton alloc] initForToolbar:tools] autorelease];
-  
-	UIBarButtonItem *clear = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(clearBtn)] autorelease];
-	[tools setItems:[NSArray arrayWithObjects:
-				settings,
-				[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
-				[[[UIBarButtonItem alloc] initWithCustomView:speedBtn] autorelease],
-				[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
-				clear,
-				[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
-				actions,
-				nil]];
+  [self refreshToolbar];
 	
 	[self.view addSubview:glView];
 	[self.view addSubview:textView];
@@ -245,6 +233,27 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
   // to execute finishStartup, and finally unlock the display, when it's done with training etc.
 }
 
+-(void)refreshToolbar {
+  UIBarButtonSystemItem icon = _dasherInterface->GetBoolParameter(BP_GAME_MODE) ? UIBarButtonSystemItemStop : UIBarButtonSystemItemPlay;
+  UIBarButtonItem *game = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:icon target:self action:@selector(toggleGameMode)] autorelease];
+  UIBarButtonItem *action = [ActionButton buttonForToolbar:tools];
+  if (!toolbarItems) {
+    toolbarItems = [[NSMutableArray arrayWithObjects:
+                   [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"cog.png"] style:UIBarButtonItemStylePlain target:self action:@selector(settings)] autorelease],
+                   [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
+                   [[[UIBarButtonItem alloc] initWithCustomView:speedBtn] autorelease],
+                   [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
+                   game,
+                   [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
+                   action,
+                   nil] retain];
+  } else {
+    [toolbarItems replaceObjectAtIndex:4 withObject:game];
+    [toolbarItems replaceObjectAtIndex:6 withObject:action];
+  }
+  [tools setItems:toolbarItems];
+}
+
 - (void)initDasherInterface {
   //training takes too long to perform in applicationDidFinishLaunching;
   // so we do it here instead (having let the main thread display a "training" message);
@@ -297,13 +306,17 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
 - (void)clearText {
   textView.text=@"";
   selectedText.location = selectedText.length = 0;
-  _dasherInterface->SetOffset(0);
+  _dasherInterface->SetBuffer(0);
 }
 	
 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
 	if (buttonIndex == actionSheet.destructiveButtonIndex) [self clearText];
 }
-	
+
+- (void)toggleGameMode {
+  _dasherInterface->SetBoolParameter(BP_GAME_MODE, !_dasherInterface->GetBoolParameter(BP_GAME_MODE));
+}
+
 - (void)settings {
   //avoid awful muddle if we change out of tap-to-start mode whilst running.... 
   _dasherInterface->Stop();
@@ -321,7 +334,7 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
 						    [[[LanguagesController alloc] init] autorelease],
                 [[[StringParamController alloc] initWithTitle:@"Colour" image:[UIImage imageNamed:@"palette.png"] settingParam:SP_COLOUR_ID] autorelease],
 						    misc,
-                [ActionConfigurator instanceForButton:actions],
+                [[[ActionConfigurator alloc] init] autorelease],
 						    nil];
   [self presentModalViewController:settings animated:YES];
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]