[dasher: 14/21] Move iPhone-only params out of Parameters.{h, cpp} and into iPhone-only classes



commit a5098732e731261b7e6913c89ed669c36ab43800
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Tue Jul 12 13:23:33 2011 +0100

    Move iPhone-only params out of Parameters.{h,cpp} and into iPhone-only classes
    
    (accessing NSUserDefaults directly)

 Src/DasherCore/Parameters.cpp               |    8 ----
 Src/DasherCore/Parameters.h                 |    6 ---
 Src/iPhone/Classes/CalibrationController.mm |   34 ++++++++++++------
 Src/iPhone/Classes/DasherAppDelegate.mm     |    1 -
 Src/iPhone/Classes/IPhoneFilters.h          |   12 +------
 Src/iPhone/Classes/IPhoneFilters.mm         |   45 +----------------------
 Src/iPhone/Classes/IPhoneInputs.h           |   16 ++++++++-
 Src/iPhone/Classes/IPhoneInputs.mm          |   52 ++++++++++++++++++++++++++-
 Src/iPhone/Classes/InputMethodSelector.mm   |    4 +-
 9 files changed, 93 insertions(+), 85 deletions(-)
---
diff --git a/Src/DasherCore/Parameters.cpp b/Src/DasherCore/Parameters.cpp
index a461cea..2970223 100644
--- a/Src/DasherCore/Parameters.cpp
+++ b/Src/DasherCore/Parameters.cpp
@@ -59,10 +59,6 @@ const bp_table boolparamtable[] = {
 #endif
   {BP_CONTROL_MODE_HAS_COPY, "ControlHasCopy", PERS, true, "Provide copy-to-clipboard actions in Control Mode (if platforms supports)"},
   {BP_CONTROL_MODE_HAS_SPEECH, "ControlHasSpeech", PERS, true, "Provide speech actions in Control Mode (if platform supports)"},
-#ifdef TARGET_OS_IPHONE
-  {BP_CUSTOM_TILT, "CustomTilt", PERS, false, "Use custom tilt axes"},
-  {BP_DOUBLE_X, "DoubleXCoords", PERS, false, "Double X-coordinate of touch"},
-#endif
 };
 
 const lp_table longparamtable[] = {
@@ -182,10 +178,6 @@ const sp_table stringparamtable[] = {
   {SP_BUTTON_4, "Button4", PERS, "", "Assignment to button 4"},
   {SP_BUTTON_10, "Button10", PERS, "", "Assignment to button 10"},
   {SP_JOYSTICK_DEVICE, "JoystickDevice", PERS, "/dev/input/js0", "Joystick device"},
-#ifdef TARGET_OS_IPHONE
-  {SP_CUSTOM_TILT, "CustomTiltParams", PERS, "(0.0,0.0,0.0) - (0.0,-1.0,0.0) / (-1.0,0.0,0.0)", "Custom tilt axes"},
-  {SP_VERTICAL_TILT, "VerticalTiltParams", PERS, "-0.1 - -0.9 / -0.4 - 0.4", "Limits of vertical tilting"}, 
-#endif
 };
 
 ParameterType GetParameterType(int iParameter) {
diff --git a/Src/DasherCore/Parameters.h b/Src/DasherCore/Parameters.h
index f98d867..72bce81 100644
--- a/Src/DasherCore/Parameters.h
+++ b/Src/DasherCore/Parameters.h
@@ -42,9 +42,6 @@ enum {
   BP_TWOBUTTON_REVERSE, BP_2B_INVERT_DOUBLE, BP_SLOW_START,
   BP_COPY_ALL_ON_STOP, BP_SPEAK_ALL_ON_STOP, BP_SPEAK_WORDS,
   BP_CONTROL_MODE_HAS_HALT, BP_CONTROL_MODE_HAS_EDIT, BP_CONTROL_MODE_HAS_COPY, BP_CONTROL_MODE_HAS_SPEECH,
-#ifdef TARGET_OS_IPHONE
-  BP_CUSTOM_TILT, BP_DOUBLE_X,
-#endif
   END_OF_BPS
 };
 
@@ -74,9 +71,6 @@ enum {
   SP_COLOUR_ID, SP_DEFAULT_COLOUR_ID, SP_DASHER_FONT, SP_SYSTEM_LOC, SP_USER_LOC, SP_GAME_TEXT_FILE,
   SP_SOCKET_INPUT_X_LABEL, SP_SOCKET_INPUT_Y_LABEL, SP_INPUT_FILTER, SP_INPUT_DEVICE,
   SP_BUTTON_0, SP_BUTTON_1, SP_BUTTON_2, SP_BUTTON_3, SP_BUTTON_4, SP_BUTTON_10, SP_JOYSTICK_DEVICE,
-#ifdef TARGET_OS_IPHONE
-  SP_CUSTOM_TILT, SP_VERTICAL_TILT,
-#endif
   END_OF_SPS
 };
 
diff --git a/Src/iPhone/Classes/CalibrationController.mm b/Src/iPhone/Classes/CalibrationController.mm
index 8f737f8..5c836a7 100644
--- a/Src/iPhone/Classes/CalibrationController.mm
+++ b/Src/iPhone/Classes/CalibrationController.mm
@@ -12,6 +12,10 @@
 #import "DasherUtil.h"
 #include <sstream>
 
+static NSString *CUSTOM_TILT_STRING = @"CustomTiltParams";
+static NSString *VERTICAL_TILT_STRING = @"VerticalTiltParams";
+static NSString *USE_CUSTOM_TILT = @"CustomTilt";
+
 using namespace std;
 
 //private method...
@@ -38,12 +42,16 @@ void saveVerticalState(float minY, float maxY, float minX, float maxX, BOOL inve
 	ostringstream os;
 	if (invert) os << maxY << " - " << minY; else os << minY << " - " << maxY;
 	os << " / " << minX << " - " << maxX;
-	[DasherAppDelegate theApp].dasherInterface->SetStringParameter(SP_VERTICAL_TILT, os.str());
+  [[NSUserDefaults standardUserDefaults] setObject:NSStringFromStdString(os.str()) forKey:VERTICAL_TILT_STRING];
 }
 
 void getVerticalState(float &minY, float &maxY, float &minX, float &maxX, BOOL &invert)
 {
-	istringstream is([DasherAppDelegate theApp].dasherInterface->GetStringParameter(SP_VERTICAL_TILT));
+  NSString *val = ([[NSUserDefaults standardUserDefaults] objectForKey:VERTICAL_TILT_STRING])
+  ? [[NSUserDefaults standardUserDefaults] stringForKey:VERTICAL_TILT_STRING]
+  : @"-0.1 - -0.9 / -0.4 - 0.4";
+
+	istringstream is(StdStringFromNSString(val));
 	string hyphen1, stroke, hyphen2;
 	is >> minY >> hyphen1 >> maxY >> stroke >> minX >> hyphen2 >> maxX;
 	DASHER_ASSERT(hyphen1 == "-" && stroke == "/" && hyphen2 == "-");
@@ -61,12 +69,16 @@ void saveCustomState(const Vec3 &min, const Vec3 &max, const Vec3 &slow)
 {
 	ostringstream os;
 	os << min << " - " << max << " / " << slow;
-	[DasherAppDelegate theApp].dasherInterface->SetStringParameter(SP_CUSTOM_TILT, os.str());
+  [[NSUserDefaults standardUserDefaults] setObject:NSStringFromStdString(os.str()) forKey:CUSTOM_TILT_STRING];
 }
 
 void getCustomState(Vec3 &min, Vec3 &max, Vec3 &slow)
 {
-	istringstream is([DasherAppDelegate theApp].dasherInterface->GetStringParameter(SP_CUSTOM_TILT));
+  NSString *val = ([[NSUserDefaults standardUserDefaults] objectForKey:CUSTOM_TILT_STRING] == nil)
+     ? @"(0.0,0.0,0.0) - (0.0,-1.0,0.0) / (-1.0,0.0,0.0)" //default
+  : [[NSUserDefaults standardUserDefaults] stringForKey:CUSTOM_TILT_STRING];
+    
+	istringstream is(StdStringFromNSString(val));
 	string hyphen, stroke;
 	is >> min >> hyphen >> max >> stroke >> slow;
 	DASHER_ASSERT(hyphen == "-" && stroke == "/");
@@ -91,7 +103,7 @@ void setVerticalTiltAxes(float minY, float maxY, float minX, float maxX, BOOL in
 
 @implementation CalibrationController
 +(void)doSetup {
-	if ([DasherAppDelegate theApp].dasherInterface->GetBoolParameter(BP_CUSTOM_TILT))
+	if ([[NSUserDefaults standardUserDefaults] boolForKey:USE_CUSTOM_TILT])
 	{
 		Vec3 min, max, slow;
 		getCustomState(min, max, slow);
@@ -283,7 +295,7 @@ void setVerticalTiltAxes(float minY, float maxY, float minX, float maxX, BOOL in
 		{
 			saveVerticalState(minY, maxY, minX, maxX, invert.on);
 			//labels up-to-date, were changed by didAccelerate
-			if (![DasherAppDelegate theApp].dasherInterface->GetBoolParameter(BP_CUSTOM_TILT)) setVerticalTiltAxes(minY, maxY, minX, maxX, invert.on);
+			if (![[NSUserDefaults standardUserDefaults] boolForKey:USE_CUSTOM_TILT]) setVerticalTiltAxes(minY, maxY, minX, maxX, invert.on);
 		}
 		else
 		{
@@ -310,13 +322,13 @@ void setVerticalTiltAxes(float minY, float maxY, float minX, float maxX, BOOL in
 					break;
 			}
 			saveCustomState(min, max, x);
-			if ([DasherAppDelegate theApp].dasherInterface->GetBoolParameter(BP_CUSTOM_TILT)) setCustomTiltAxes(min, max, x);
+			if ([[NSUserDefaults standardUserDefaults] boolForKey:USE_CUSTOM_TILT]) setCustomTiltAxes(min, max, x);
 
 		}
 	}
 	else if (settingParam == SETTING_VERT)
 	{
-		//no change - reload captions from stored SP_VERTICAL_TILT
+		//no change - reload captions from stored VERTICAL_TILT_STRING
 		[self loadVerticalLabels];
 	}
 	//else, was setting custom; ignore.
@@ -350,7 +362,7 @@ void setVerticalTiltAxes(float minY, float maxY, float minX, float maxX, BOOL in
   getVerticalState(y1, y2, x1, x2, oldInvert);
   //chances are, that oldInvert = !invert.on...but not relying on that.
   saveVerticalState(y1, y2, x1, x2, invert.on);
-  if (![DasherAppDelegate theApp].dasherInterface->GetBoolParameter(BP_CUSTOM_TILT)) setVerticalTiltAxes(y1, y2, x1, x2, invert.on);
+  if (![[NSUserDefaults standardUserDefaults] boolForKey:USE_CUSTOM_TILT]) setVerticalTiltAxes(y1, y2, x1, x2, invert.on);
 }
 	
 -(void) loadVerticalLabels {
@@ -389,7 +401,7 @@ void setVerticalTiltAxes(float minY, float maxY, float minX, float maxX, BOOL in
 		[header addSubview:sel];
 		sel.frame = CGRectMake(210.0, 7.0, 100.0, 25.0);
 		[sel addTarget:self action:@selector(selectClicked:) forControlEvents:UIControlEventTouchUpInside];
-		NSInteger currentlySelected = [DasherAppDelegate theApp].dasherInterface->GetBoolParameter(BP_CUSTOM_TILT) ? 1 : 0; 
+		NSInteger currentlySelected = [[NSUserDefaults standardUserDefaults] boolForKey:USE_CUSTOM_TILT] ? 1 : 0; 
 		sel.enabled = (section != currentlySelected);
 	}
 	return headers[section];
@@ -403,7 +415,7 @@ void setVerticalTiltAxes(float minY, float maxY, float minX, float maxX, BOOL in
 		{
 			button.enabled = NO;
 			selButtons[1-i].enabled = YES;
-			[DasherAppDelegate theApp].dasherInterface->SetBoolParameter(BP_CUSTOM_TILT, i==1);
+      [[NSUserDefaults standardUserDefaults] setBool:(i==1) forKey:USE_CUSTOM_TILT];
 			[CalibrationController doSetup];
 			break;
 		}
diff --git a/Src/iPhone/Classes/DasherAppDelegate.mm b/Src/iPhone/Classes/DasherAppDelegate.mm
index 983189e..416703e 100644
--- a/Src/iPhone/Classes/DasherAppDelegate.mm
+++ b/Src/iPhone/Classes/DasherAppDelegate.mm
@@ -74,7 +74,6 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
   {LP_OUTLINE_WIDTH, T_LONG, -5, 5, 1, -1, ""},
   {BP_AUTO_SPEEDCONTROL, T_BOOL, -1, -1, -1, -1, ""},
   {LP_NONLINEAR_X, T_LONG, 0, 10, 1, -1, ""},
-  {BP_DOUBLE_X, T_BOOL, -1, -1, -1, -1, ""},
 };
 
 using namespace Dasher;
diff --git a/Src/iPhone/Classes/IPhoneFilters.h b/Src/iPhone/Classes/IPhoneFilters.h
index 7b9453b..1273a3f 100644
--- a/Src/iPhone/Classes/IPhoneFilters.h
+++ b/Src/iPhone/Classes/IPhoneFilters.h
@@ -11,22 +11,12 @@
 #include "StylusFilter.h"
 #include "IPhoneInputs.h"
 
- class NSUserDefaultsObserver;
-
-class IPhonePrefsObserver {
-public:
-  virtual void iPhonePrefsChanged(NSString *key)=0;
-protected:
-  void ObserveKeys(NSString *key,...);
-  ~IPhonePrefsObserver();
-private:
-  NSUserDefaultsObserver *obsvr;
-};
 #ifndef __IPHONE_FILTERS_MM__
 extern NSString *HOLD_TO_GO;
 extern NSString *TILT_1D;
 extern NSString *TILT_USE_TOUCH_X;
 extern NSString *TOUCH_USE_TILT_X;
+extern NSString *DOUBLE_TOUCH_X;
 #endif
 
 #define TILT_FILTER "IPhone Tilt Filter"
diff --git a/Src/iPhone/Classes/IPhoneFilters.mm b/Src/iPhone/Classes/IPhoneFilters.mm
index 63d9d30..bfafd71 100644
--- a/Src/iPhone/Classes/IPhoneFilters.mm
+++ b/Src/iPhone/Classes/IPhoneFilters.mm
@@ -19,50 +19,7 @@ NSString *HOLD_TO_GO=@"HoldToGo";
 NSString *TILT_1D=@"Tilt1D";
 NSString * TILT_USE_TOUCH_X=@"UseTouchX";
 NSString * TOUCH_USE_TILT_X=@"UseTiltX";
-
- interface NSUserDefaultsObserver : NSObject {
-  IPhonePrefsObserver *po;
-}
--(id)initForPrefsObserver:(IPhonePrefsObserver *)po;
--(void)stopObserving;
--(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
- end
-
- implementation NSUserDefaultsObserver
-
--(id)initForPrefsObserver:(IPhonePrefsObserver *)_po {
-  if (self = [super init]) {
-    self->po = _po;
-  }
-  return self;
-}
-
--(void)stopObserving {po=nil; [self autorelease];}
-
--(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
-  NSAssert(object == [NSUserDefaults standardUserDefaults],@"Only observing user defaults?");
-  if (po) po->iPhonePrefsChanged(keyPath);
-  else [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:keyPath];
-}
- end
-
-void IPhonePrefsObserver::ObserveKeys(NSString *key,...) {
-  va_list args;
-  va_start(args, key);
-  
-  obsvr = [[NSUserDefaultsObserver alloc] initForPrefsObserver:this];
-  while (key) {
-    [[NSUserDefaults standardUserDefaults] addObserver:obsvr forKeyPath:key options:0 context:nil];
-    iPhonePrefsChanged(key);
-    key=va_arg(args, NSString *);
-  }
-  va_end(args);
-}
-
-IPhonePrefsObserver::~IPhonePrefsObserver() {
-  [obsvr stopObserving];
-  [obsvr release];
-}
+NSString *DOUBLE_TOUCH_X=@"DoubleXCoords";
 
 using namespace Dasher;
 
diff --git a/Src/iPhone/Classes/IPhoneInputs.h b/Src/iPhone/Classes/IPhoneInputs.h
index d1aacfd..254b766 100644
--- a/Src/iPhone/Classes/IPhoneInputs.h
+++ b/Src/iPhone/Classes/IPhoneInputs.h
@@ -20,6 +20,17 @@
 #define TWO_FINGER_INPUT "Two-finger (multitouch) input"
 
 @class EAGLView;
+ class NSUserDefaultsObserver;
+
+class IPhonePrefsObserver {
+public:
+  virtual void iPhonePrefsChanged(NSString *key)=0;
+protected:
+  void ObserveKeys(NSString *key,...);
+  ~IPhonePrefsObserver();
+private:
+  NSUserDefaultsObserver *obsvr;
+};
 namespace Dasher {
 
 class CIPhoneTiltInput : public CScreenCoordInput {
@@ -60,11 +71,14 @@ protected:
   EAGLView * const m_pView;
 };
 
-class CIPhoneMouseInput : public UndoubledTouch, protected CSettingsUser {
+class CIPhoneMouseInput : public UndoubledTouch, protected CSettingsUser, private IPhonePrefsObserver {
 public:
 	CIPhoneMouseInput(CSettingsUser *pCreator, EAGLView *pView);
   
   bool GetScreenCoords(screenint &iX, screenint &iY, CDasherView *pView);
+private:
+  void iPhonePrefsChanged(NSString *str);
+  bool m_bDoubleX;
 };
   
   class CIPhoneTwoFingerInput : public CDasherCoordInput {
diff --git a/Src/iPhone/Classes/IPhoneInputs.mm b/Src/iPhone/Classes/IPhoneInputs.mm
index a97166f..c03a910 100644
--- a/Src/iPhone/Classes/IPhoneInputs.mm
+++ b/Src/iPhone/Classes/IPhoneInputs.mm
@@ -16,6 +16,50 @@
 using namespace std;
 using namespace Dasher;
 
+ interface NSUserDefaultsObserver : NSObject {
+  IPhonePrefsObserver *po;
+}
+-(id)initForPrefsObserver:(IPhonePrefsObserver *)po;
+-(void)stopObserving;
+-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
+ end
+
+ implementation NSUserDefaultsObserver
+
+-(id)initForPrefsObserver:(IPhonePrefsObserver *)_po {
+  if (self = [super init]) {
+    self->po = _po;
+  }
+  return self;
+}
+
+-(void)stopObserving {po=nil; [self autorelease];}
+
+-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
+  NSAssert(object == [NSUserDefaults standardUserDefaults],@"Only observing user defaults?");
+  if (po) po->iPhonePrefsChanged(keyPath);
+  else [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:keyPath];
+}
+ end
+
+void IPhonePrefsObserver::ObserveKeys(NSString *key,...) {
+  va_list args;
+  va_start(args, key);
+  
+  obsvr = [[NSUserDefaultsObserver alloc] initForPrefsObserver:this];
+  while (key) {
+    [[NSUserDefaults standardUserDefaults] addObserver:obsvr forKeyPath:key options:0 context:nil];
+    iPhonePrefsChanged(key);
+    key=va_arg(args, NSString *);
+  }
+  va_end(args);
+}
+
+IPhonePrefsObserver::~IPhonePrefsObserver() {
+  [obsvr stopObserving];
+  [obsvr release];
+}
+
 @interface Accel : NSObject<UIAccelerometerDelegate> {
   CIPhoneTiltInput *tilt;
 }
@@ -116,13 +160,14 @@ bool UndoubledTouch::GetScreenCoords(screenint &iX, screenint &iY, CDasherView *
 
 CIPhoneMouseInput::CIPhoneMouseInput(CSettingsUser *pCreator, EAGLView *pView) 
 	: UndoubledTouch(9, TOUCH_INPUT, pView), CSettingsUser(pCreator) {
+    ObserveKeys(DOUBLE_TOUCH_X, nil);
 };
 
 bool CIPhoneMouseInput::GetScreenCoords(screenint &iX, screenint &iY, CDasherView *pView) {
   if (!UndoubledTouch::GetScreenCoords(iX,iY, pView)) return false;
   CDasherScreen *scr(pView->Screen());
   //double x/y
-  if (GetBoolParameter(BP_DOUBLE_X)) {
+  if ([[NSUserDefaults standardUserDefaults] boolForKey:DOUBLE_TOUCH_X]) {
     switch (GetLongParameter(LP_REAL_ORIENTATION)) {
       case Opts::LeftToRight:
         iX=min(iX*2, scr->GetWidth());
@@ -141,6 +186,11 @@ bool CIPhoneMouseInput::GetScreenCoords(screenint &iX, screenint &iY, CDasherVie
   return true;
 }
 
+void CIPhoneMouseInput::iPhonePrefsChanged(NSString *str) {
+  if ([str isEqualToString:DOUBLE_TOUCH_X])
+    m_bDoubleX = [[NSUserDefaults standardUserDefaults] boolForKey:DOUBLE_TOUCH_X];
+}
+
 CIPhoneTwoFingerInput::CIPhoneTwoFingerInput(EAGLView *pView) : CDasherCoordInput(10, TWO_FINGER_INPUT), m_pGlView(pView) {
 }
 
diff --git a/Src/iPhone/Classes/InputMethodSelector.mm b/Src/iPhone/Classes/InputMethodSelector.mm
index 13c6314..059ed41 100644
--- a/Src/iPhone/Classes/InputMethodSelector.mm
+++ b/Src/iPhone/Classes/InputMethodSelector.mm
@@ -24,8 +24,8 @@ typedef struct __FILTER_DESC__ {
   NSString **iPhoneOpts;
 } SFilterDesc;
 
-NSString *touchSettings[] = {TOUCH_USE_TILT_X, NULL};
-NSString *tiltSettings[] = {HOLD_TO_GO, TILT_USE_TOUCH_X, TILT_1D, @"CircleStart", NULL};
+NSString *touchSettings[] = {TOUCH_USE_TILT_X, DOUBLE_TOUCH_X, NULL};
+NSString *tiltSettings[] = {HOLD_TO_GO, TILT_USE_TOUCH_X, DOUBLE_TOUCH_X, TILT_1D, @"CircleStart", NULL};
 
 NSString *calibBtn=@"Calibrate...";//pointer equality used to mark cell for special-casing in cellForRowAtIndexPath: below
 



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