[dasher: 39/43] Simplify DasherInterfaceBase creation/destruction, inc. call srand()



commit 3053340e5048556bf93cf15f5c8aa9041867654b
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Sun Jun 19 11:30:58 2011 +0100

    Simplify DasherInterfaceBase creation/destruction, inc. call srand()
    
    RM ChangeState, m_iCurrentState; overblown and does little. DashIntf methods now
      perform their function anytime the necessary components have been created.
    
    Rm OnUIRealised,StartShutdown - they only wrapped platform-specific Timer meths.
     (+ StartShutdown called WriteTrainFileFull, subclasses should do that instead)
    
    Rm pure virtual Start/ShutdownTimer, subclasses to manage themselves.

 Src/DasherCore/DasherInterfaceBase.cpp       |   54 +++-----------------------
 Src/DasherCore/DasherInterfaceBase.h         |   51 +-----------------------
 Src/Gtk2/DasherControl.cpp                   |   13 +------
 Src/Gtk2/DasherControl.h                     |    2 -
 Src/Gtk2/GtkDasherControl.cpp                |    8 +++-
 Src/MacOSX/COSXDasherControl.h               |    2 -
 Src/MacOSX/COSXDasherControl.mm              |   12 +-----
 Src/MacOSX/DasherApp.mm                      |    3 +-
 Src/Win32/Dasher.cpp                         |   16 ++-----
 Src/Win32/Dasher.h                           |    2 -
 Src/Win32/DasherWindow.cpp                   |    3 +-
 Src/iPhone/Classes/CDasherInterfaceBridge.h  |    9 ++--
 Src/iPhone/Classes/CDasherInterfaceBridge.mm |   14 +------
 Src/iPhone/Classes/DasherAppDelegate.h       |    2 -
 Src/iPhone/Classes/DasherAppDelegate.mm      |   12 +----
 15 files changed, 38 insertions(+), 165 deletions(-)
---
diff --git a/Src/DasherCore/DasherInterfaceBase.cpp b/Src/DasherCore/DasherInterfaceBase.cpp
index 3b7ac81..c6a2d3d 100644
--- a/Src/DasherCore/DasherInterfaceBase.cpp
+++ b/Src/DasherCore/DasherInterfaceBase.cpp
@@ -107,8 +107,6 @@ CDasherInterfaceBase::CDasherInterfaceBase(CSettingsStore *pSettingsStore) : CSe
   // Various state variables
   m_bRedrawScheduled = false;
 
-  m_iCurrentState = ST_START;
-
   //  m_bGlobalLock = false;
 
 #ifndef _WIN32_WCE
@@ -120,12 +118,14 @@ CDasherInterfaceBase::CDasherInterfaceBase(CSettingsStore *pSettingsStore) : CSe
 
 }
 
-void CDasherInterfaceBase::Realize() {
+void CDasherInterfaceBase::Realize(unsigned long ulTime) {
 
   //if ChangeScreen has been called, we'll have created a view;
   // otherwise, we still can't create a view, until we have a screen!
   DASHER_ASSERT(m_DasherScreen ? m_pDasherView!=NULL : m_pDasherView==NULL);
 
+  srand(ulTime);
+  
   //create the model... (no nodes just yet)
   m_pDasherModel = new CDasherModel(this, this);
 
@@ -181,14 +181,10 @@ void CDasherInterfaceBase::Realize() {
   if (m_pUserLog != NULL)
     m_pUserLog->InitIsDone();
 #endif
-
-  // TODO: Make things work when model is created latet
-  ChangeState(TR_MODEL_INIT);
 }
 
 CDasherInterfaceBase::~CDasherInterfaceBase() {
-  DASHER_ASSERT(m_iCurrentState == ST_SHUTDOWN);
-
+  //WriteTrainFileFull();???
   delete m_pDasherModel;        // The order of some of these deletions matters
   delete m_pDasherView;
   delete m_ColourIO;
@@ -522,10 +518,6 @@ void CDasherInterfaceBase::NewFrame(unsigned long iTime, bool bForceRedraw) {
   }
   bReentered=true;
 
-  // Fail if Dasher is locked
-  // if(m_iCurrentState != ST_NORMAL)
-  //  return;
-
   if(m_DasherScreen) {
     //ok, can draw _something_. Try and see what we can :).
 
@@ -746,7 +738,7 @@ CUserLogBase* CDasherInterfaceBase::GetUserLogPtr() {
 }
 
 void CDasherInterfaceBase::KeyDown(int iTime, int iId, bool bPos, int iX, int iY) {
-  if(m_iCurrentState != ST_NORMAL || isLocked())
+  if(isLocked())
     return;
 
   if(m_pInputFilter) {
@@ -759,7 +751,7 @@ void CDasherInterfaceBase::KeyDown(int iTime, int iId, bool bPos, int iX, int iY
 }
 
 void CDasherInterfaceBase::KeyUp(int iTime, int iId, bool bPos, int iX, int iY) {
-  if(m_iCurrentState != ST_NORMAL || isLocked())
+  if(isLocked())
     return;
 
   if(m_pInputFilter) {
@@ -853,10 +845,6 @@ void CDasherInterfaceBase::GetPermittedValues(int iParameter, std::vector<std::s
   }
 }
 
-void CDasherInterfaceBase::StartShutdown() {
-  ChangeState(TR_SHUTDOWN);
-}
-
 bool CDasherInterfaceBase::GetModuleSettings(const std::string &strName, SModuleSettings **pSettings, int *iCount) {
   return GetModuleByName(strName)->GetSettings(pSettings, iCount);
 }
@@ -950,36 +938,6 @@ void CDasherInterfaceBase::AddActionButton(const std::string &strName) {
   m_vRightButtons.push_back(new CActionButton(this, strName, false));
 }
 
-
-void CDasherInterfaceBase::OnUIRealised() {
-  StartTimer();
-  ChangeState(TR_UI_INIT);
-}
-
-
-void CDasherInterfaceBase::ChangeState(ETransition iTransition) {
-  static EState iTransitionTable[ST_NUM][TR_NUM] = {
-    {ST_MODEL, ST_UI, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN},//ST_START
-    {ST_FORBIDDEN, ST_NORMAL, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN},//ST_MODEL
-    {ST_NORMAL, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN},//ST_UI
-    {ST_FORBIDDEN, ST_FORBIDDEN, ST_LOCKED, ST_FORBIDDEN, ST_SHUTDOWN},//ST_NORMAL
-    {ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_NORMAL, ST_FORBIDDEN},//ST_LOCKED
-    {ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN, ST_FORBIDDEN}//ST_SHUTDOWN
-  //TR_MODEL_INIT, TR_UI_INIT,   TR_LOCK,      TR_UNLOCK,    TR_SHUTDOWN
-  };
-
-  EState iNewState(iTransitionTable[m_iCurrentState][iTransition]);
-
-  if(iNewState != ST_FORBIDDEN) {
-    if (iNewState == ST_SHUTDOWN) {
-      ShutdownTimer();
-      WriteTrainFileFull();
-    }
-
-    m_iCurrentState = iNewState;
-  }
-}
-
 void CDasherInterfaceBase::SetOffset(int iOffset, bool bForce) {
   m_pDasherModel->SetOffset(iOffset, m_pNCManager->GetAlphabetManager(), m_pDasherView, bForce);
   //ACL TODO note that CTL_MOVE, etc., do not come here (that would probably
diff --git a/Src/DasherCore/DasherInterfaceBase.h b/Src/DasherCore/DasherInterfaceBase.h
index 3052679..8c89da0 100644
--- a/Src/DasherCore/DasherInterfaceBase.h
+++ b/Src/DasherCore/DasherInterfaceBase.h
@@ -382,14 +382,10 @@ protected:
   /// Both Realize and ChangeScreen must be called after construction before other functions
   /// will work, but they can be called in either order (as the SettingsStore is passed into
   /// the c'tor).
+  /// \param ulTime timestamp, much as per NewFrame, used for initializing the RNG (i.e. srand).
+  /// (Is that too hacky?)
   ///
-  void Realize();
-
-  ///
-  /// Notify the core that the UI has been realised. At this point drawing etc. is expected to work
-  ///
-
-  void OnUIRealised();
+  void Realize(unsigned long ulTime);
 
   ///
   /// Creates a default set of modules. Override in subclasses to create any
@@ -423,33 +419,6 @@ protected:
   /// (i.e. Display() called) - the default just returns false.
   virtual bool FinishRender(unsigned long ulTime) {return false;}
 
-  enum ETransition {
-    TR_MODEL_INIT = 0,
-    TR_UI_INIT,
-    TR_LOCK,
-    TR_UNLOCK,
-    TR_SHUTDOWN,
-    TR_NUM
-  };
-
-  enum EState {
-    ST_START = 0,
-    ST_MODEL,
-    ST_UI,
-    ST_NORMAL,
-    ST_LOCKED,
-    ST_SHUTDOWN,
-    ST_NUM,
-    ST_FORBIDDEN,
-    ST_DELAY
-  };
-
-  /// @name State machine functions
-  /// ...
-  /// @{
-
-  void ChangeState(ETransition iTransition);
-
   /// @}
 
   CDasherScreen *m_DasherScreen;
@@ -509,19 +478,6 @@ protected:
 
   virtual void SetupUI() = 0;
 
-  ///
-  /// Start the callback timer
-  ///
-
-  virtual void StartTimer() = 0;
-
-  ///
-  /// Shutdown the callback timer (permenantly - this is called once
-  /// Dasher is committed to closing).
-  ///
-
-  virtual void ShutdownTimer() = 0;
-
   /// @}
 
 
@@ -584,7 +540,6 @@ protected:
   /// Represent the current overall state of the core
   /// @{
   bool m_bRedrawScheduled;
-  EState m_iCurrentState;
   bool m_bOldVisible;
 
   /// @}
diff --git a/Src/Gtk2/DasherControl.cpp b/Src/Gtk2/DasherControl.cpp
index 726fa42..bbba009 100644
--- a/Src/Gtk2/DasherControl.cpp
+++ b/Src/Gtk2/DasherControl.cpp
@@ -81,7 +81,7 @@ CDasherControl::CDasherControl(GtkVBox *pVBox, GtkDasherControl *pDasherControl)
   m_pScreen = new CCanvas(m_pCanvas);
   ChangeScreen(m_pScreen);
 
-  Realize();
+  Realize(get_time());
  
   //  m_pKeyboardHelper = new CKeyboardHelper(this);
   //  m_pKeyboardHelper->Grab(GetBoolParameter(BP_GLOBAL_KEYBOARD));
@@ -289,10 +289,6 @@ void CDasherControl::RealizeCanvas(GtkWidget *pWidget) {
 #ifdef DEBUG
   std::cout << "RealizeCanvas()" << std::endl;
 #endif
-  OnUIRealised();
-}
-
-void CDasherControl::StartTimer() {
   // Start the timer loops as everything is set up.
   // Aim for 40 frames per second, computers are getting faster.
 
@@ -303,13 +299,6 @@ void CDasherControl::StartTimer() {
   }
 }
 
-void CDasherControl::ShutdownTimer() {
-  // TODO: Figure out how to implement this - at the moment it's done
-  // through a return value from the timer callback, but it would be
-  // nicer to prevent any further calls as soon as the shutdown signal
-  // has been receieved.
-}
-
 int CDasherControl::CanvasConfigureEvent() {
   GtkAllocation a;
 
diff --git a/Src/Gtk2/DasherControl.h b/Src/Gtk2/DasherControl.h
index 419cd33..320630e 100644
--- a/Src/Gtk2/DasherControl.h
+++ b/Src/Gtk2/DasherControl.h
@@ -171,8 +171,6 @@ private:
   virtual void SetupPaths();
   virtual void CreateModules();
   virtual void SetupUI();
-  virtual void StartTimer();
-  virtual void ShutdownTimer();
   
   /// Override to emit Gtk2 signals (previously in response to CCommandEvent)
   void ExecuteCommand(const std::string &strName);
diff --git a/Src/Gtk2/GtkDasherControl.cpp b/Src/Gtk2/GtkDasherControl.cpp
index feafcf4..dda6d10 100644
--- a/Src/Gtk2/GtkDasherControl.cpp
+++ b/Src/Gtk2/GtkDasherControl.cpp
@@ -135,7 +135,13 @@ gtk_dasher_control_finalize(GObject *pObject) {
   /* TODO: Check that this actually gets called correctly */
 
   /* TODO: Should just call constructor - this should just be a lightweight wrapper class */
-  pPrivate->pControl->StartShutdown();
+  //ACL not sure what that's about...?
+  //The following comment taken from the old ShutdownTimer, which we need to do now:
+    // TODO: Figure out how to implement this - at the moment it's done
+    // through a return value from the timer callback, but it would be
+    // nicer to prevent any further calls as soon as the shutdown signal
+    // has been receieved.
+  pPrivate->pControl->WriteTrainFileFull();
 
   delete pPrivate->pControl;
   //  g_free(pDasherControl->private_data);
diff --git a/Src/MacOSX/COSXDasherControl.h b/Src/MacOSX/COSXDasherControl.h
index c2746de..35fc331 100644
--- a/Src/MacOSX/COSXDasherControl.h
+++ b/Src/MacOSX/COSXDasherControl.h
@@ -63,8 +63,6 @@ private:
   virtual void SetupPaths();
   virtual void CreateModules();
   virtual void SetupUI();
-  virtual void StartTimer();
-  virtual void ShutdownTimer();
   virtual bool SupportsSpeech();
   virtual void Speak(const std::string &strText, bool bInterrupt);
   virtual bool SupportsClipboard() {return true;}
diff --git a/Src/MacOSX/COSXDasherControl.mm b/Src/MacOSX/COSXDasherControl.mm
index c8e010a..326f736 100644
--- a/Src/MacOSX/COSXDasherControl.mm
+++ b/Src/MacOSX/COSXDasherControl.mm
@@ -105,8 +105,8 @@ void COSXDasherControl::SetupUI() {
 // TODO: hack to get around things being used before they were created.  is there a better way?
 // TODO: maybe this should go in setupui?
 void COSXDasherControl::Realize2() {
-  CDasherInterfaceBase::Realize();
-  OnUIRealised();
+  CDasherInterfaceBase::Realize(get_time());
+  [dasherApp startTimer];
 }
 
 void COSXDasherControl::SetupPaths() {
@@ -168,14 +168,6 @@ void COSXDasherControl::goddamn(unsigned long iTime, bool bForceRedraw) {
   NewFrame(iTime, bForceRedraw);
 }
 
-void COSXDasherControl::StartTimer() {
-  [dasherApp startTimer];
-}
-
-void COSXDasherControl::ShutdownTimer() {
-  [dasherApp shutdownTimer];
-}
-
 void COSXDasherControl::TimerFired(NSPoint p) {
   m_pMouseInput->SetCoordinates(p.x, p.y);
   m_p1DMouseInput->SetCoordinates(p.x, p.y);
diff --git a/Src/MacOSX/DasherApp.mm b/Src/MacOSX/DasherApp.mm
index f98082c..7bb2a9e 100644
--- a/Src/MacOSX/DasherApp.mm
+++ b/Src/MacOSX/DasherApp.mm
@@ -254,7 +254,8 @@ static NSString *FilenameToUntitledName = @"NilToUntitled";
 }
 
 - (void)applicationWillTerminate:(NSNotification *)aNotification {
-  aquaDasherControl->StartShutdown();
+  [self shutdownTimer];
+  aquaDasherControl->WriteTrainFileFull();
   delete aquaDasherControl;
   aquaDasherControl=NULL;
 }
diff --git a/Src/Win32/Dasher.cpp b/Src/Win32/Dasher.cpp
index 7d373ef..eb9a756 100644
--- a/Src/Win32/Dasher.cpp
+++ b/Src/Win32/Dasher.cpp
@@ -44,7 +44,8 @@ CDasher::CDasher(HWND Parent, CDasherWindow *pWindow, CEdit *pEdit)
   CoInitialize(NULL);
 #endif
 
-  Realize();
+  DWORD dwTicks = GetTickCount();
+  Realize(dwTicks);
 }
 
 CDasher::~CDasher(void) {
@@ -275,7 +276,9 @@ void CDasher::SetupUI() {
   m_pCanvas = new CCanvas(this);
   m_pCanvas->Create(m_hParent); // TODO - check return 
 
-  OnUIRealised();
+  // TODO: See MessageLoop, Main in CDasherWindow - should be brought into this class
+  // Framerate settings: currently 40fps.
+  SetTimer(m_pCanvas->getwindow(), 1, 25, NULL);
 }
 
 int CDasher::GetFileSize(const std::string &strFileName) {
@@ -289,15 +292,6 @@ int CDasher::GetFileSize(const std::string &strFileName) {
 #endif
 }
 
-void CDasher::StartTimer() {
-  // TODO: See MessageLoop, Main in CDasherWindow - should be brought into this class
-  // Framerate settings: currently 40fps.
-  SetTimer(m_pCanvas->getwindow(), 1, 25, NULL);
-}
-
-void CDasher::ShutdownTimer() {
-}
-
 // TODO: Check that syntax here is sensible
 void CDasher::Move(int iX, int iY, int iWidth, int iHeight) {
   if(m_pCanvas)
diff --git a/Src/Win32/Dasher.h b/Src/Win32/Dasher.h
index 56abdca..e5966a8 100644
--- a/Src/Win32/Dasher.h
+++ b/Src/Win32/Dasher.h
@@ -70,8 +70,6 @@ private:
   virtual void SetupPaths();
   virtual void SetupUI();
   virtual void CreateModules();
-  virtual void StartTimer();
-  virtual void ShutdownTimer();
 
   void ScanDirectory(const Tstring &strMask, std::vector<std::string> &vFileList);
   bool                    GetWindowSize(int* pTop, int* pLeft, int* pBottom, int* pRight);
diff --git a/Src/Win32/DasherWindow.cpp b/Src/Win32/DasherWindow.cpp
index d94396a..e180404 100644
--- a/Src/Win32/DasherWindow.cpp
+++ b/Src/Win32/DasherWindow.cpp
@@ -177,7 +177,8 @@ int CDasherWindow::MessageLoop() {
 		}
 	}
 
-  m_pDasher->StartShutdown();
+  m_pDasher->WriteTrainFileFull();
+  //ACL also stop the timer...but ShutdownTimer() was a no-op, so assume we don't need to?
 
   return msg.wParam;
 }
diff --git a/Src/iPhone/Classes/CDasherInterfaceBridge.h b/Src/iPhone/Classes/CDasherInterfaceBridge.h
index 9a89f21..852fda5 100644
--- a/Src/iPhone/Classes/CDasherInterfaceBridge.h
+++ b/Src/iPhone/Classes/CDasherInterfaceBridge.h
@@ -42,8 +42,11 @@ public:
   CDasherInterfaceBridge(DasherAppDelegate *aDasherApp);
   ~CDasherInterfaceBridge();
   
-  //redefinitions to make public....
-  void Realize();//also calls OnUIRealised
+  ///Calls superclass Realize with system time, starts timer, etc.
+  /// - i.e. everything ve need to make ourselves operational (except ChangeScreen)
+  void Realize();
+  
+  //redefine to make public....
   void NewFrame(unsigned long iTime, bool bForceRedraw);
 
   void SetTiltAxes(Vec3 main, float off, Vec3 slow, float off2);
@@ -72,8 +75,6 @@ private:
   virtual void SetupPaths();
   virtual void CreateModules();
   virtual void SetupUI();
-  virtual void StartTimer();
-  virtual void ShutdownTimer();
   
   ///
   /// Pass events coming from the core to the appropriate handler.
diff --git a/Src/iPhone/Classes/CDasherInterfaceBridge.mm b/Src/iPhone/Classes/CDasherInterfaceBridge.mm
index e758f49..dbb07f1 100644
--- a/Src/iPhone/Classes/CDasherInterfaceBridge.mm
+++ b/Src/iPhone/Classes/CDasherInterfaceBridge.mm
@@ -106,10 +106,9 @@ void CDasherInterfaceBridge::SetupUI() {
   NSLog(@"CDasherInterfaceBridge::SetupUI");
 }
 
-void CDasherInterfaceBridge::Realize() {  
-  CDasherInterfaceBase::Realize();
+void CDasherInterfaceBridge::Realize() {
+  CDasherInterfaceBase::Realize(get_time());
   HandleEvent(SP_ALPHABET_ID); //calls dasherApp::SetAlphabet
-  CDasherInterfaceBase::OnUIRealised();
 }
 
 void CDasherInterfaceBridge::SetupPaths() {
@@ -159,19 +158,10 @@ void CDasherInterfaceBridge::ScanColourFiles(std::vector<std::string> &vFileList
   }  
 }
 
-
 void CDasherInterfaceBridge::NewFrame(unsigned long iTime, bool bForceRedraw) {
   CDasherInterfaceBase::NewFrame(iTime, bForceRedraw);
 }
 
-void CDasherInterfaceBridge::StartTimer() {
-  [dasherApp startTimer];
-}
-
-void CDasherInterfaceBridge::ShutdownTimer() {
-  [dasherApp shutdownTimer];
-}
-
 void CDasherInterfaceBridge::HandleEvent(int iParameter) {
   
   // don't need to do anything because the PreferencesController is observing changes to the 
diff --git a/Src/iPhone/Classes/DasherAppDelegate.h b/Src/iPhone/Classes/DasherAppDelegate.h
index a9c66d6..766784a 100644
--- a/Src/iPhone/Classes/DasherAppDelegate.h
+++ b/Src/iPhone/Classes/DasherAppDelegate.h
@@ -35,8 +35,6 @@
   NSString *m_wordBoundary, *m_sentenceBoundary, *m_lineBoundary;
 }
 
-- (void)startTimer;
-- (void)shutdownTimer;
 - (void)setAlphabet:(const CAlphInfo *)pAlph;
 - (void)outputCallback:(NSString *)s;
 - (void)deleteCallback:(NSString *)s;
diff --git a/Src/iPhone/Classes/DasherAppDelegate.mm b/Src/iPhone/Classes/DasherAppDelegate.mm
index 63cbd66..ad42cc6 100644
--- a/Src/iPhone/Classes/DasherAppDelegate.mm
+++ b/Src/iPhone/Classes/DasherAppDelegate.mm
@@ -271,6 +271,7 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
   //training takes too long to perform in applicationDidFinishLaunching;
   // so we do it here instead (having let the main thread display a "training" message);
   _dasherInterface->Realize();
+  [glView startAnimation];
   //the rest has to be done on the main thread to avoid problems with OpenGL contexts
   // (which are local to one thread); however, we'll have the background thread wait...
   [self performSelectorOnMainThread:@selector(finishStartup) withObject:nil waitUntilDone:YES];
@@ -366,14 +367,6 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
 	[glView startAnimation];
 }
 
-- (void)startTimer {
-	[glView startAnimation];
-}
-
-- (void)shutdownTimer {//Dasher closing...
-	[glView stopAnimation];
-}
-
 - (void)outputCallback:(NSString *)s {
 	textView.text=[textView.text stringByReplacingCharactersInRange:selectedText withString:s];
 	selectedText.location+=[s length]; 
@@ -430,7 +423,8 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
 }
 
 -(void)applicationWillTerminate:(UIApplication *)application {
-  self.dasherInterface->StartShutdown();
+  [glView stopAnimation];
+  self.dasherInterface->WriteTrainFileFull();
 }
 
 - (void)newFrameAt:(unsigned long)time ForceRedraw:(BOOL)bForce {



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