[dasher: 38/43] MacOSX Game Mode display using the NSTextView (for coloured text)



commit 7f58c9103c8443c402d30d16c85f3c28c01cc945
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Sat Jun 18 17:16:16 2011 +0100

    MacOSX Game Mode display using the NSTextView (for coloured text)

 Src/MacOSX/COSXDasherControl.h  |    1 +
 Src/MacOSX/COSXDasherControl.mm |   52 ++++++++++++++++++++++++++++++++++++++-
 Src/MacOSX/DasherApp.mm         |    5 +++
 Src/MacOSX/DasherTextView.h     |    4 +++
 Src/MacOSX/DasherTextView.mm    |    4 +++
 5 files changed, 65 insertions(+), 1 deletions(-)
---
diff --git a/Src/MacOSX/COSXDasherControl.h b/Src/MacOSX/COSXDasherControl.h
index 7c6313b..c2746de 100644
--- a/Src/MacOSX/COSXDasherControl.h
+++ b/Src/MacOSX/COSXDasherControl.h
@@ -56,6 +56,7 @@ public:
   virtual int GetFileSize(const std::string &strFileName);
   void HandleEvent(int iParameter);
   void SetEdit(id<DasherEdit> pEdit);
+  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/MacOSX/COSXDasherControl.mm b/Src/MacOSX/COSXDasherControl.mm
index a2483b3..c8e010a 100644
--- a/Src/MacOSX/COSXDasherControl.mm
+++ b/Src/MacOSX/COSXDasherControl.mm
@@ -17,6 +17,7 @@
 #import "DasherEdit.h"
 #import "Event.h"
 #import "../Common/Common.h"
+#import "GameModule.h"
 
 #import <iostream>
 
@@ -27,6 +28,53 @@
 using namespace std;
 using namespace Dasher::Settings;
 
+class COSXGameModule : public CGameModule {
+public:
+  COSXGameModule(CSettingsUser *pCreator, CDasherInterfaceBase *pIntf, CDasherView *pView, CDasherModel *pModel, NSTextView *_textView)
+  : CGameModule(pCreator, pIntf, pView, pModel), textView(_textView) {
+    enteredAtts = [[NSDictionary dictionaryWithObject:[NSColor greenColor] forKey:NSForegroundColorAttributeName] retain];
+    wrongAtts = [[NSDictionary dictionaryWithObjectsAndKeys:[NSColor redColor],NSForegroundColorAttributeName,[NSNumber numberWithInt:(NSUnderlineStyleThick | NSUnderlinePatternSolid)],NSStrikethroughStyleAttributeName,nil] retain];
+    targetAtts = [[NSDictionary dictionary] retain]; //empty, black is default
+  }
+  void ChunkGenerated() {
+    string sText;
+    for (vector<symbol>::const_iterator it=targetSyms().begin(); it!=targetSyms().end(); it++)
+      sText += m_pAlph->GetText(*it);
+    [[textView textStorage] setAttributedString:[[[NSAttributedString alloc] initWithString:NSStringFromStdString(sText) attributes:targetAtts] autorelease]];
+    textView.selectedRange=NSMakeRange(0, 0);
+    numWrong=0;
+  }
+  void HandleEvent(const CEditEvent *pEvt) {
+    const int iPrev(lastCorrectSym());
+    CGameModule::HandleEvent(pEvt);
+    NSTextStorage *storage([textView textStorage]);
+    if (iPrev==lastCorrectSym()) {
+      NSRange r = NSMakeRange(iPrev+1, numWrong);
+      NSString *newWrong = NSStringFromStdString(m_strWrong);
+      numWrong = [newWrong length];
+      NSAttributedString *nwa = [[[NSAttributedString alloc] initWithString:newWrong attributes:wrongAtts] autorelease];
+      [storage replaceCharactersInRange:r withAttributedString:nwa];
+      [textView scrollRangeToVisible:NSMakeRange(r.location+r.length,0)];
+    } else {
+      DASHER_ASSERT(m_strWrong=="" && numWrong==0);
+      if (iPrev<lastCorrectSym()) {
+        //added more
+        [storage setAttributes:enteredAtts range:NSMakeRange(iPrev+1,lastCorrectSym()-iPrev)];
+      } else {
+        [storage setAttributes:targetAtts range:NSMakeRange(lastCorrectSym()+1,iPrev-lastCorrectSym())];
+      }
+      [textView scrollRangeToVisible:NSMakeRange(lastCorrectSym()+1, 0)];
+    }
+    [textView setSelectedRange:NSMakeRange(lastCorrectSym()+1+numWrong, 0)];
+  }
+  void DrawText(CDasherView *pView) {
+  }
+private:
+  NSDictionary *enteredAtts, *wrongAtts, *targetAtts;
+  NSInteger numWrong;
+  NSTextView *textView;
+};
+
 COSXDasherControl::COSXDasherControl(DasherApp *aDasherApp)
 : CDashIntfScreenMsgs(new COSXSettingsStore()), dasherApp(aDasherApp), dasherEdit(nil) {
 }
@@ -319,4 +367,6 @@ void COSXDasherControl::ClearAllContext() {
   SetBuffer(0);
 }
 
-
+CGameModule *COSXDasherControl::CreateGameModule(CDasherView *pView, CDasherModel *pModel) {
+  return new COSXGameModule(this, this, pView, pModel, [dasherApp textView]);
+}
diff --git a/Src/MacOSX/DasherApp.mm b/Src/MacOSX/DasherApp.mm
index 26754e4..f98082c 100644
--- a/Src/MacOSX/DasherApp.mm
+++ b/Src/MacOSX/DasherApp.mm
@@ -417,4 +417,9 @@ static NSString *FilenameToUntitledName = @"NilToUntitled";
   } //else, cancel - do nothing
 }
 
+//"private" method, used by COSXGameModule
+-(NSTextView *)textView {
+  return textView;
+}
+
 @end
diff --git a/Src/MacOSX/DasherTextView.h b/Src/MacOSX/DasherTextView.h
index 201d149..a1a0154 100644
--- a/Src/MacOSX/DasherTextView.h
+++ b/Src/MacOSX/DasherTextView.h
@@ -10,6 +10,10 @@
 #import "DasherApp.h"
 #import "DasherEdit.h"
 
+///Implements DasherEdit protocol by making necessary changes to itself
+/// (as it _is_ the textbox). However NOTE, bit of a hack, it checks for
+/// game mode and disables most functions while game mode is on, as the
+/// COSXGameModule will be mutating the text storage instead.
 @interface DasherTextView : NSTextView<DasherEdit> {
   IBOutlet DasherApp *dasherApp;
   BOOL suppressCursorEvents;
diff --git a/Src/MacOSX/DasherTextView.mm b/Src/MacOSX/DasherTextView.mm
index 329a185..d90bb44 100644
--- a/Src/MacOSX/DasherTextView.mm
+++ b/Src/MacOSX/DasherTextView.mm
@@ -39,6 +39,7 @@
 #pragma mark DasherEdit methods
 
 - (void)outputCallback:(NSString *)aString {
+  if (dasherApp.gameModeOn) return;
   //offsets are wrong after any control mode node has been executed; since _any_
   // adding of text (at least by following method) generally causes textViewDidChangeSelection
   // callbacks, we have to suppress these during text adding in all cases...
@@ -50,6 +51,7 @@
 }
 
 - (void)deleteCallback:(NSString *)s {
+  if (dasherApp.gameModeOn) return;
   suppressCursorEvents=YES; //similarly to outputCallback
   int len = [s length];
   NSRange curSelection = [self selectedRange];
@@ -81,6 +83,7 @@
 }
 
 -(unsigned int)currentCursorPos {
+  if (dasherApp.gameModeOn) return 0;
   return [self selectedRange].location;
 }
 
@@ -169,6 +172,7 @@
 }
 
 -(void)textViewDidChangeSelection:(NSNotification *)notification {
+  if (dasherApp.gameModeOn) return;
   //NSLog(@"DidChangeSelection %i+%i",[textView selectedRange].location,[textView selectedRange].length);
   if (!suppressCursorEvents)
     [dasherApp aquaDasherControl]->SetOffset([self selectedRange].location, false);



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