[dasher: 18/28] Refactor: model just expands on a node it is given (rather than creating root)



commit 6b5f7bd0f937e8c1b3f2d0e342e7dd0075c00b1f
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Mon Sep 26 16:03:24 2011 +0100

    Refactor: model just expands on a node it is given (rather than creating root)
    
    => model can be started on a non-Alphabet node; root changes preserve NF_GAME.

 Src/DasherCore/DasherInterfaceBase.cpp |   13 ++++++++-----
 Src/DasherCore/DasherInterfaceBase.h   |    9 +++++++--
 Src/DasherCore/DasherModel.cpp         |   14 +++++---------
 Src/DasherCore/DasherModel.h           |   12 +++---------
 4 files changed, 23 insertions(+), 25 deletions(-)
---
diff --git a/Src/DasherCore/DasherInterfaceBase.cpp b/Src/DasherCore/DasherInterfaceBase.cpp
index bb39fb9..eed28a8 100644
--- a/Src/DasherCore/DasherInterfaceBase.cpp
+++ b/Src/DasherCore/DasherInterfaceBase.cpp
@@ -410,8 +410,7 @@ void CDasherInterfaceBase::CreateNCManager() {
     //and start a new tree of nodes from it (retaining old offset -
     // this will be a sensible default of 0 if no nodes previously existed).
     // This deletes the old tree of nodes...
-    m_pDasherModel->SetOffset(m_pDasherModel->GetOffset(), m_pNCManager->GetAlphabetManager(), m_pDasherView, true);
-    ScheduleRedraw();
+    SetOffset(m_pDasherModel->GetOffset(), true);
   } //else, if there is no screen, the model should not contain any nodes from the old NCManager. (Assert, somehow?)
 
   //...so now we can delete the old manager
@@ -650,8 +649,7 @@ void CDasherInterfaceBase::ChangeScreen(CDasherScreen *NewScreen) {
   if (m_pNCManager) {
     m_pNCManager->ChangeScreen(m_DasherScreen);
     if (m_pDasherModel)
-      m_pDasherModel->SetOffset(m_pDasherModel->GetOffset(), m_pNCManager->GetAlphabetManager(), m_pDasherView, true);
-      //Redraw already scheduled by ChangeView / ScreenResized
+      SetOffset(m_pDasherModel->GetOffset(), true);
   }
 }
 
@@ -841,7 +839,12 @@ bool CDasherInterfaceBase::GetModuleSettings(const std::string &strName, SModule
 }
 
 void CDasherInterfaceBase::SetOffset(int iOffset, bool bForce) {
-  m_pDasherModel->SetOffset(iOffset, m_pNCManager->GetAlphabetManager(), m_pDasherView, bForce);
+  if (iOffset == m_pDasherModel->GetOffset() && !bForce) return;
+
+  CDasherNode *pNode = m_pNCManager->GetAlphabetManager()->GetRoot(NULL, iOffset!=0, iOffset);
+  if (GetBoolParameter(BP_GAME_MODE)) pNode->SetFlag(NF_GAME, true);
+  m_pDasherModel->SetNode(pNode);
+  
   //ACL TODO note that CTL_MOVE, etc., do not come here (that would probably
   // rebuild the model / violently repaint the screen every time!). But we
   // still want to notifyOffset all text actions, so the "New" suboption sees
diff --git a/Src/DasherCore/DasherInterfaceBase.h b/Src/DasherCore/DasherInterfaceBase.h
index 671e201..20303a0 100644
--- a/Src/DasherCore/DasherInterfaceBase.h
+++ b/Src/DasherCore/DasherInterfaceBase.h
@@ -258,8 +258,13 @@ public:
   ///Equivalent to SetOffset(iOffset, true)
   void SetBuffer(int iOffset) {SetOffset(iOffset, true);}
 
-  /// Tells the model to rebuild itself with the
-  /// cursor at the specified offset (position within textbox/buffer).
+  /// Rebuilds the model at the specified location, potentially reusing nodes if !bForce
+  /// @param iOffset Cursor position in attached buffer from which to obtain context
+  /// @param bForce if true, model should be completely rebuilt (even for
+  /// same offset) - characters at old offsets may have changed, or we have
+  /// a new AlphabetManager. If false, assume buffer and alphabet unchanged,
+  /// so no need to rebuild the model if an existing node covers this point.
+
   /// @param bForce true meaning the entire context may have changed,
   /// false if we've just moved around within it.
   void SetOffset(int iOffset, bool bForce=false);
diff --git a/Src/DasherCore/DasherModel.cpp b/Src/DasherCore/DasherModel.cpp
index 4282f56..505ef59 100644
--- a/Src/DasherCore/DasherModel.cpp
+++ b/Src/DasherCore/DasherModel.cpp
@@ -198,18 +198,14 @@ void CDasherModel::ClearRootQueue() {
   }
 }
 
-void CDasherModel::SetOffset(int iOffset, CAlphabetManager *pMgr, CDasherView *pView, bool bForce) {
-  //if we don't have a root, always "re"build the tree!
-  // (if we have a root, only rebuild to move location or if bForce says to)
-  if (m_Root && iOffset == GetOffset() && !bForce) return;
+void CDasherModel::SetNode(CDasherNode *pNewRoot) {
 
   if (m_pLastOutput) m_pLastOutput->Leave();
   AbortOffset();
   ClearRootQueue();
   delete m_Root;
 
-  m_Root = pMgr->GetRoot(NULL, iOffset!=0, iOffset);
-  if (GetBoolParameter(BP_GAME_MODE)) m_Root->SetFlag(NF_GAME, true);
+  m_Root = pNewRoot;
 
   // Create children of the root...
   ExpandNode(m_Root);
@@ -259,7 +255,7 @@ bool CDasherModel::NextScheduledStep()
       DASHER_ASSERT(m_Rootmin + ((pChild->Lbnd() * iWidth) / NORMALIZATION) <= ORIGIN_Y);
       if (m_Rootmin + ((pChild->Hbnd() * iWidth) / NORMALIZATION) > ORIGIN_Y) {
         //found child to make root. proceed only if new root is on the game path....
-        if (GetBoolParameter(BP_GAME_MODE) && !pChild->GetFlag(NF_GAME)) {
+        if (m_Root->GetFlag(NF_GAME) && !pChild->GetFlag(NF_GAME)) {
           //If the user's strayed that far off the game path,
           // having Dasher stop seems reasonable!
           return false;
@@ -484,8 +480,8 @@ void CDasherModel::RenderToView(CDasherView *pView, CExpansionPolicy &policy) {
     }
 #endif
     if (pNewRoot->GetFlag(NF_SUPER) &&
-        ////GAME MODE TEMP - only change the root if it is on the game path/////////
-        (!GetBoolParameter(BP_GAME_MODE) || m_Root->onlyChildRendered->GetFlag(NF_GAME))) {
+        // Stay on the game path, if there is one (!)
+        (!m_Root->GetFlag(NF_GAME) || pNewRoot->GetFlag(NF_GAME))) {
       Make_root(pNewRoot);
     } else
       break;
diff --git a/Src/DasherCore/DasherModel.h b/Src/DasherCore/DasherModel.h
index ada4a03..89ec322 100644
--- a/Src/DasherCore/DasherModel.h
+++ b/Src/DasherCore/DasherModel.h
@@ -71,7 +71,7 @@ class Dasher::CDasherModel: private CSettingsUser, public Observable<CDasherNode
   static const myint ORIGIN_X=2048, ORIGIN_Y=2048, MAX_Y=4096;
 
   /// Constructs a new CDasherModel. Note, must be followed by a call to
-  /// SetOffset() before the model can be used.
+  /// SetNode() before the model can be used.
   CDasherModel(CSettingsUser *pCreateFrom);
   ~CDasherModel();
 
@@ -177,16 +177,10 @@ class Dasher::CDasherModel: private CSettingsUser, public Observable<CDasherNode
   void LimitRoot(int iMaxWidth);
 
   ///
-  /// Rebuild the tree of nodes (may reuse the existing ones if !bForce).
-  /// @param iLocation offset (cursor position) in attached buffer from which to obtain context
-  /// @param pMgr Manager to use to create nodes
-  /// @param bForce if true, model should be completely rebuilt (even for
-  /// same offset) - characters at old offsets may have changed, or we have
-  /// a new AlphabetManager. If false, assume buffer and alphabet unchanged,
-  /// so no need to rebuild the model if an existing node covers this point.
+  /// Rebuild the tree of nodes from a given root
   ///
 
-  void SetOffset(int iLocation, CAlphabetManager *pMgr, CDasherView *pView, bool bForce);
+  void SetNode(CDasherNode *pNewRoot);
 
   ///
   /// The current offset of the cursor/insertion point in the text buffer



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