[dasher: 35/43] iPhone game display with UIWebView formatted text



commit f082669a0cd92360a5383f4f191c18eddac728c5
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Mon May 16 22:47:16 2011 +0100

    iPhone game display with UIWebView formatted text

 Src/iPhone/Classes/CDasherInterfaceBridge.h  |    1 +
 Src/iPhone/Classes/CDasherInterfaceBridge.mm |   45 +++++++++++++++++++++++++-
 Src/iPhone/Classes/DasherAppDelegate.h       |    4 ++-
 Src/iPhone/Classes/DasherAppDelegate.mm      |   19 ++++++++++-
 4 files changed, 66 insertions(+), 3 deletions(-)
---
diff --git a/Src/iPhone/Classes/CDasherInterfaceBridge.h b/Src/iPhone/Classes/CDasherInterfaceBridge.h
index db1d47e..9a89f21 100644
--- a/Src/iPhone/Classes/CDasherInterfaceBridge.h
+++ b/Src/iPhone/Classes/CDasherInterfaceBridge.h
@@ -65,6 +65,7 @@ public:
   void editProtect(CDasherNode *pNode);
   ///Override for asynchronous messages only...TODO?
   void Message(const string &strText, bool bInterrupt);
+  CGameModule *CreateGameModule(CDasherView *pView,CDasherModel *pModel);
 private:
   virtual void ScanAlphabetFiles(std::vector<std::string> &vFileList);
   virtual void ScanColourFiles(std::vector<std::string> &vFileList);
diff --git a/Src/iPhone/Classes/CDasherInterfaceBridge.mm b/Src/iPhone/Classes/CDasherInterfaceBridge.mm
index 2a8726d..e758f49 100644
--- a/Src/iPhone/Classes/CDasherInterfaceBridge.mm
+++ b/Src/iPhone/Classes/CDasherInterfaceBridge.mm
@@ -19,13 +19,52 @@
 #import "TwoButtonDynamicFilter.h"
 #import "TwoPushDynamicFilter.h"
 #import <iostream>
-
 #import <fcntl.h>
 
 #import <sys/stat.h>
 
 using namespace std;
 
+class IPhoneGameModule : public CGameModule {
+public:
+  IPhoneGameModule(Dasher::CSettingsUser *pCreateFrom, CDasherInterfaceBridge *pIntf, CDasherView *pView, CDasherModel *pModel, UIWebView *pWebView)
+  : CGameModule(pCreateFrom,pIntf,pView,pModel), m_pWebView(pWebView), cachedStr(nil) {
+  };
+  ~IPhoneGameModule() {
+  }
+protected:
+  virtual void HandleEvent(const CEditEvent *pEvt) {
+    CGameModule::HandleEvent(pEvt);
+    if (cachedStr) {
+      [cachedStr release];
+      cachedStr=nil;
+    }
+  }
+  virtual void DrawText(CDasherView *pView) {
+    if (cachedStr) return; //unchanged since last time!
+    const vector<symbol> &target(targetSyms());
+    string html;
+    
+    if (lastCorrectSym()>-1) {
+      html="<span style=\"color:#0f0\">";
+      for (int i=0; i<=lastCorrectSym(); i++)
+        html+=m_pAlph->GetText(target[i]);
+      html+="</span>";
+    }
+    //any wrongly entered text in red with strikethrough
+    html+="<span style=\"color:#f00; text-decoration:line-through;\">"+m_strWrong+"</span><span id=\"here\">";
+    //target still in black...with div id "here" which will be scrolled to the center
+    for (int i=lastCorrectSym()+1; i<target.size(); i++)
+      html+=m_pAlph->GetText(target[i]);
+    html+="</span>";
+    cachedStr = [NSStringFromStdString(html) retain];
+    [m_pWebView loadHTMLString:cachedStr baseURL:[NSURL URLWithString:@"http://localhost/";]];
+  }
+private:
+  NSString *cachedStr;
+  UIWebView *m_pWebView;
+};
+
 CDasherInterfaceBridge::CDasherInterfaceBridge(DasherAppDelegate *aDasherApp) : CDashIntfScreenMsgs(new COSXSettingsStore()), dasherApp(aDasherApp) {
 }
 
@@ -242,3 +281,7 @@ void CDasherInterfaceBridge::WriteTrainFile(const std::string &filename,const st
   write(fd,strNewText.c_str(),strNewText.length());
   close(fd);
 }
+
+CGameModule *CDasherInterfaceBridge::CreateGameModule(CDasherView *pView, CDasherModel *pModel) {
+  return new IPhoneGameModule(this, this, pView, pModel, [dasherApp getWebView]);  
+}
diff --git a/Src/iPhone/Classes/DasherAppDelegate.h b/Src/iPhone/Classes/DasherAppDelegate.h
index 22af105..a9c66d6 100644
--- a/Src/iPhone/Classes/DasherAppDelegate.h
+++ b/Src/iPhone/Classes/DasherAppDelegate.h
@@ -10,15 +10,17 @@
 #import "CDasherInterfaceBridge.h"
 #import "TextView.h"
 #import "Actions.h"
+#import "GameModule.h"
 
 @class EAGLView;
 @class FliteTTS;
 
- interface DasherAppDelegate : UIViewController <UIApplicationDelegate, UIActionSheetDelegate, UITextViewDelegate> {
+ interface DasherAppDelegate : UIViewController <UIApplicationDelegate, UIActionSheetDelegate, UITextViewDelegate, UIWebViewDelegate> {
     UIWindow *window;
     EAGLView *glView;
   FliteTTS *fliteEng;
 	TextView *textView;
+  UIWebView *webView;
 	NSRange selectedText;
 	CDasherInterfaceBridge* _dasherInterface;
 	UIButton *speedBtn;
diff --git a/Src/iPhone/Classes/DasherAppDelegate.mm b/Src/iPhone/Classes/DasherAppDelegate.mm
index f5e1a07..63cbd66 100644
--- a/Src/iPhone/Classes/DasherAppDelegate.mm
+++ b/Src/iPhone/Classes/DasherAppDelegate.mm
@@ -161,6 +161,7 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
       NSAssert(false, @"Unexpected interface orientation");
   }
   textView.frame = textRect;
+  webView.frame = textRect;
   messageLabel.frame = CGRectMake(0.0, textRect.origin.y + textRect.size.height, mainSize.width, 0.0);
   [NSObject cancelPreviousPerformRequestsWithTarget:messageLabel];
   if (glView) glView.frame=dashRect;
@@ -186,6 +187,7 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
   
   //create GUI components...
 	textView = [[[TextView alloc] init] autorelease];
+  webView = [[[UIWebView alloc] init] autorelease];
   messageLabel = [[[UITextView alloc] init] autorelease];
   tools = [[UIToolbar alloc] init]; //retain a reference (until dealloc) because of rotation
 	glView = [[[EAGLView alloc] initWithFrame:[self doLayout:UIInterfaceOrientationPortrait] Delegate:self] autorelease];
@@ -201,6 +203,9 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
 	selectedText.location = selectedText.length = 0;
   textView.selectedRange=selectedText;
 
+  webView.dataDetectorTypes = UIDataDetectorTypeNone;
+  webView.delegate = self;
+  
   messageLabel.editable = NO;
   messageLabel.backgroundColor = [UIColor grayColor];
   messageLabel.textColor = [UIColor whiteColor];
@@ -222,7 +227,7 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
   [self refreshToolbar];
 	
 	[self.view addSubview:glView];
-	[self.view addSubview:textView];
+	[self.view addSubview:textView]; [self.view addSubview:webView];
   //relying here on things added later being on top of those added earlier.
   //Seems to work ok but not sure whether this is guaranteed?!
   [self.view addSubview:speedSlider];
@@ -251,9 +256,17 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
     [toolbarItems replaceObjectAtIndex:4 withObject:game];
     [toolbarItems replaceObjectAtIndex:6 withObject:action];
   }
+  textView.hidden = _dasherInterface->GetBoolParameter(BP_GAME_MODE);
+  webView.hidden = !_dasherInterface->GetBoolParameter(BP_GAME_MODE);
   [tools setItems:toolbarItems];
 }
 
+-(void)webViewDidFinishLoad:(UIWebView *)sender {
+  DASHER_ASSERT(sender==webView);
+  //try for approx twice as much beneath as above
+  [sender stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollBy(0,Math.max(0,document.getElementById(\"here\").offsetTop-%d));",static_cast<int>(sender.frame.size.height/3)]];
+}
+
 - (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);
@@ -317,6 +330,10 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
   _dasherInterface->SetBoolParameter(BP_GAME_MODE, !_dasherInterface->GetBoolParameter(BP_GAME_MODE));
 }
 
+-(UIWebView *)getWebView {
+  return webView;
+}
+
 - (void)settings {
   //avoid awful muddle if we change out of tap-to-start mode whilst running.... 
   _dasherInterface->Stop();



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