[Avida-SVN] r1756 - in development: Avida.xcodeproj source/targets/avida-viewer source/targets/viewer-text source/tools source/viewer-core

ofria at myxo.css.msu.edu ofria at myxo.css.msu.edu
Fri Jul 6 10:52:40 PDT 2007


Author: ofria
Date: 2007-07-06 13:52:40 -0400 (Fri, 06 Jul 2007)
New Revision: 1756

Added:
   development/source/targets/viewer-text/ncurses-defs.h
   development/source/tools/cCoords.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/targets/avida-viewer/cBaseTextWindow.h
   development/source/targets/avida-viewer/cTextWindow.h
   development/source/targets/viewer-text/cDriver_TextViewer.cc
   development/source/targets/viewer-text/cDriver_TextViewer.h
   development/source/targets/viewer-text/viewer-text.cc
   development/source/viewer-core/cCoreView_Info.cc
   development/source/viewer-core/cCoreView_Info.h
Log:
Checpoint in the development of the new core viewer.

* Pulled cCoords out into its own class to handle coordinates.  Right now its still being used
only in the interface, but eventually it can be tied in to the topology manager.

* Created an ncurses-defs.h file to deal with all of the special defs to get ncurses to work properly.

* Setup cDriver_TextViewer to correctly drive the run (this will need to get merged with the other
driver changes however).  It can manage input and output, deal with paused runs, and its hooked into
cCoreView_Info, where I've put most generic functionaity.



Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2007-07-06 14:29:33 UTC (rev 1755)
+++ development/Avida.xcodeproj/project.pbxproj	2007-07-06 17:52:40 UTC (rev 1756)
@@ -24,7 +24,6 @@
 
 /* Begin PBXBuildFile section */
 		56F555E50C3B402A00E2E929 /* cDriver_TextViewer.cc in Sources */ = {isa = PBXBuildFile; fileRef = 56F555DF0C3B402A00E2E929 /* cDriver_TextViewer.cc */; };
-		56F555E60C3B402A00E2E929 /* cTextViewerManager.cc in Sources */ = {isa = PBXBuildFile; fileRef = 56F555E10C3B402A00E2E929 /* cTextViewerManager.cc */; };
 		56F555E70C3B402A00E2E929 /* viewer-text.cc in Sources */ = {isa = PBXBuildFile; fileRef = 56F555E40C3B402A00E2E929 /* viewer-text.cc */; };
 		700E2996085A1F6000CF158A /* avida in CopyFiles */ = {isa = PBXBuildFile; fileRef = DCC3164D07626CF3008F7A48 /* avida */; };
 		700E2B87085DE54400CF158A /* avida-viewer in CopyFiles */ = {isa = PBXBuildFile; fileRef = 700E2B83085DE50C00CF158A /* avida-viewer */; };
@@ -1777,7 +1776,6 @@
 			buildActionMask = 2147483647;
 			files = (
 				56F555E50C3B402A00E2E929 /* cDriver_TextViewer.cc in Sources */,
-				56F555E60C3B402A00E2E929 /* cTextViewerManager.cc in Sources */,
 				56F555E70C3B402A00E2E929 /* viewer-text.cc in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

Modified: development/source/targets/avida-viewer/cBaseTextWindow.h
===================================================================
--- development/source/targets/avida-viewer/cBaseTextWindow.h	2007-07-06 14:29:33 UTC (rev 1755)
+++ development/source/targets/avida-viewer/cBaseTextWindow.h	2007-07-06 17:52:40 UTC (rev 1756)
@@ -14,30 +14,12 @@
 #include "cTools.h"
 #endif
 
+#ifndef cCoords_h
+#include "cCoords.h"
+#endif
+
 #define MAX_SUB_WINDOWS 4
 
-class cCoords {
-private:
-  int x;
-  int y;
-public:
-  cCoords(int _x, int _y) { x = _x, y = _y; }
-  cCoords(const cCoords & _coords) { x = _coords.x, y = _coords.y; }
-  ~cCoords() { ; }
-
-  void Translate(int _x, int _y) { x += _x, y += _y; }
-
-  void operator=(const cCoords & _coords) { x = _coords.x, y = _coords.y; }
-  int operator==(const cCoords & _c) { return (x == _c.x && y == _c.y); }
-  int operator!=(const cCoords & _c) { return !operator==(_c); }
-
-  int GetX() { return x; }
-  int GetY() { return y; }
-
-  void Set(int _x, int _y) { x = _x; y = _y; }
-};
-
-
 #ifdef ENABLE_UNIT_TESTS
 namespace nBaseTextWindow {
   /**

Modified: development/source/targets/avida-viewer/cTextWindow.h
===================================================================
--- development/source/targets/avida-viewer/cTextWindow.h	2007-07-06 14:29:33 UTC (rev 1755)
+++ development/source/targets/avida-viewer/cTextWindow.h	2007-07-06 17:52:40 UTC (rev 1756)
@@ -26,7 +26,7 @@
 #include <csignal>
 #include <cstdarg>
 
-// All colors are defines except black on black:
+// All colors are defined except black on black:
 #define COLOR_OFF    8
 
 // Define special characters

Modified: development/source/targets/viewer-text/cDriver_TextViewer.cc
===================================================================
--- development/source/targets/viewer-text/cDriver_TextViewer.cc	2007-07-06 14:29:33 UTC (rev 1755)
+++ development/source/targets/viewer-text/cDriver_TextViewer.cc	2007-07-06 17:52:40 UTC (rev 1756)
@@ -48,10 +48,32 @@
   : m_world(world), m_info(m_world->GetPopulation(), 12), m_done(false)
 {
   // Setup the initial view mode (loaded from avida.cfg)
-//  SetViewMode(world->GetConfig().VIEW_MODE.Get());
+  m_info.SetViewMode(world->GetConfig().VIEW_MODE.Get());
     
   cDriverManager::Register(static_cast<cAvidaDriver*>(this));
   world->SetDriver(this);
+
+  // Setup NCURSES...
+  initscr();                // Set up the terminal for curses.
+  //  cbreak();                 // Don't buffer input.
+  raw();                    // Don't even buffer escape characters!
+  noecho();                 // Don't echo keypresses to the screen.
+  nonl();                   // No new line with CR (when echo is on)
+
+  keypad(stdscr, 1);        // Allows the keypad to be used.
+  nodelay(stdscr, 1);       // Don't wait for input if no key is pressed.
+
+  // Setup colors
+
+  if (has_colors()) start_color();
+  init_pair(COLOR_WHITE,   COLOR_WHITE,   COLOR_BLACK);
+  init_pair(COLOR_GREEN,   COLOR_GREEN,   COLOR_BLACK);
+  init_pair(COLOR_RED,     COLOR_RED,     COLOR_BLACK);
+  init_pair(COLOR_BLUE,    COLOR_BLUE,    COLOR_BLACK);
+  init_pair(COLOR_CYAN,    COLOR_CYAN,    COLOR_BLACK);
+  init_pair(COLOR_YELLOW,  COLOR_YELLOW,  COLOR_BLACK);
+  init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
+  init_pair(COLOR_OFF,     COLOR_BLACK,   COLOR_BLACK);
 }
 
 cDriver_TextViewer::~cDriver_TextViewer()
@@ -65,6 +87,8 @@
 
 void cDriver_TextViewer::Run()
 {
+  clog << "Ping!" << endl;
+
   cClassificationManager& classmgr = m_world->GetClassificationManager();
   cPopulation& population = m_world->GetPopulation();
   cStats& stats = m_world->GetStats();
@@ -80,7 +104,7 @@
     }
     
     m_world->GetEvents(ctx);
-    if (m_done == true) break;
+    if (m_done == true) break;  // Stop here if told to do so by an event.
     
     // Increment the Update.
     stats.IncCurrentUpdate();
@@ -187,8 +211,118 @@
 
 bool cDriver_TextViewer::ProcessKeypress(int keypress)
 {
-//  return m_view.ProcessKeypress(keypress);
-  return false;
+  bool unknown = false;
+
+  switch (keypress) {
+//   case 'a':
+//   case 'A':
+//     ChangeCurScreen(analyze_screen);
+//     break;
+  case 'b':
+  case 'B':
+//    ChangeCurScreen(NULL);
+    break;
+//   case 'C':
+//   case 'c':
+//     NavigateMapWindow();
+//     // Now we need to restore the proper window mode (already cleared)
+//     ChangeCurScreen(cur_screen);
+//     break;
+//   case 'e':
+//   case 'E':
+//     ChangeCurScreen(environment_screen);
+//     break;
+//   case 'h':
+//   case 'H':
+//     ChangeCurScreen(hist_screen);
+//     break;
+//   case 'm':
+//   case 'M':
+//     ChangeCurScreen(map_screen);
+//     break;
+//   case 'n':
+//   case 'N':
+//     if (info.GetPauseLevel() == PAUSE_ON) {
+//       info.SetPauseLevel(PAUSE_ADVANCE_UPDATE);
+//       // parasite_zoom = false; // if executing, show code that is running
+//       info.GetActiveCell()->GetOrganism()->GetPhenotype().SetFault("");
+//       nodelay(stdscr, true); // Don't delay for input; get to processing.
+//     }
+//     if (cur_screen) cur_screen->AdvanceUpdate();
+//     break;
+//   case 'o':
+//   case 'O':
+//     ChangeCurScreen(options_screen);
+//     break;
+  case 'p':
+  case 'P':
+    if (m_info.TogglePause() == true) {
+      nodelay(stdscr, false); // Wait for input if pause is on...
+    } else {
+      nodelay(stdscr, true); // Don't delay if pause is off.
+    }
+
+    // Redraw the screen to account for the toggled pause.
+//    if (cur_screen) cur_screen->Draw();
+
+    break;
+  case 'q':
+//    if (!Confirm("Are you sure you want to quit?")) break;
+  case 'Q':      // Note: Capital 'Q' quits w/o confirming.
+    // clear the windows before we go.  Do bar window last to end at top.
+//     base_window->Redraw();
+//     bar_screen->Clear();
+//     bar_screen->Refresh();
+    ExitTextViewer(0);  // This implementation calls exit(), blowing us clean away
+    break;
+//   case 's':
+//   case 'S':
+//     ChangeCurScreen(stats_screen);
+//     break;
+//   case 'W':
+//   case 'w':
+//     CloneSoup();
+//     break;
+//   case 'X':
+//   case 'x':
+//     ExtractCreature();
+//     break;
+//   case 'z':
+//   case 'Z':
+//     ChangeCurScreen(zoom_screen);
+//     break;
+  case 3: // CTRL-C...
+    exit(0);
+    break;
+  case 12: // CTRL-L...
+//    Refresh();
+    break;
+  case 26: // CTRL-Z
+    kill(getpid(), SIGTSTP);
+    break;
+//   case '*':   // Test Key!!!
+//     if (true) {
+//       Confirm("Starting Tests.");
+//       cMenuWindow menu(50);
+//       char message[40];
+//       for (int j = 0; j < 50; j++) {
+// 	sprintf(message, "Line %d", j);
+// 	menu.AddOption(j, message);
+//       }
+//       menu.SetActive(3);
+//       menu.Activate(base_window);
+//       Redraw();
+//     }
+//     break;
+  case ERR:
+    break;
+  default:
+    clog << "Unknown Key!" << endl;
+    unknown = true;
+    break;
+  }
+
+  return !unknown;
 }
 
 
@@ -205,10 +339,40 @@
 
 void cDriver_TextViewer::NotifyUpdate()
 {
-  // @CAO What should happen on an update?
-  // Update bar at top of screen
-  // Update current view
-  // Check for Inputs...
+  // @CAO What else should happen on an update?
+  // - Update bar at top of screen
+  // - Update current view
+  // - Check for Inputs...
+
+  const int update = m_world->GetStats().GetUpdate();
+  if (update % 10 == 0) clog << update << endl;
+
+  const int pause_level = m_info.GetPauseLevel();
+
+  // If we are stepping in some way, we've come to a stop, so revert to a normal pause.
+  if (pause_level == cCoreView_Info::PAUSE_ADVANCE_INST) {
+    m_info.ExitStepMode();
+  }
+
+  if (pause_level == cCoreView_Info::PAUSE_ADVANCE_UPDATE ||
+      pause_level == cCoreView_Info::PAUSE_ADVANCE_DIVIDE) {
+    m_info.SetPauseLevel(cCoreView_Info::PAUSE_ON);
+  }
+
+  // If we are paused at all, delay doing anything else until we recieve user input.
+  if (pause_level != cCoreView_Info::PAUSE_OFF) nodelay(stdscr, false);
+
+  // If there is any input in the buffer, process all of it.
+  int cur_char = ERR;
+  while ((cur_char = GetKeypress()) != ERR || m_info.GetPauseLevel() == cCoreView_Info::PAUSE_ON) {
+    bool found_keypress = ProcessKeypress(cur_char);
+
+    // If we couldn't manage the keypress here, check the current screen.
+//    if (found_keypress == false && cur_screen) cur_screen->DoInput(cur_char);
+  }
+
+  nodelay(stdscr, true);
+
 }
 
 void cDriver_TextViewer::NotifyComment(const cString& in_string)

Modified: development/source/targets/viewer-text/cDriver_TextViewer.h
===================================================================
--- development/source/targets/viewer-text/cDriver_TextViewer.h	2007-07-06 14:29:33 UTC (rev 1755)
+++ development/source/targets/viewer-text/cDriver_TextViewer.h	2007-07-06 17:52:40 UTC (rev 1756)
@@ -38,6 +38,10 @@
 #include "cWorldDriver.h"
 #endif
 
+#ifndef ncurses_defs_h
+#include "ncurses-defs.h"
+#endif
+
 #include <sstream>
 #include <iostream>
 
@@ -62,11 +66,17 @@
   void SignalBreakpoint();
   void SetDone() { m_done = true; }
 
+  // IO
   void Flush();
+  int GetKeypress() { return getch(); }
   bool ProcessKeypress(int keypress);
   
   void RaiseException(const cString& in_string);
   void RaiseFatalException(int exit_code, const cString& in_string);
+
+  // Screen manipulations...
+  void Refresh();      // Update any changes to the screen...
+  void Redraw();       // Clear the screen and redraw its contents.
   
   // Notifications
   void NotifyUpdate();
@@ -76,6 +86,8 @@
   void NotifyOutput(const cString& in_string);
   void Notify(const cString& in_string);
 
+  int Confirm(const cString& in_string) { ; } // @CAO Do this...
+
   // Tests
   bool IsInteractive() { return true; }
 };

Added: development/source/targets/viewer-text/ncurses-defs.h
===================================================================
--- development/source/targets/viewer-text/ncurses-defs.h	                        (rev 0)
+++ development/source/targets/viewer-text/ncurses-defs.h	2007-07-06 17:52:40 UTC (rev 1756)
@@ -0,0 +1,31 @@
+#ifndef ncurses_defs_h
+#define ncurses_defs_h
+
+#ifdef HAVE_NCURSES_H
+#include <ncurses.h>
+#undef OK
+#else
+#include <curses.h>
+#undef OK
+#endif
+
+// All colors are defined except black on black:
+#define COLOR_OFF    8
+
+// Define special characters
+
+#define CHAR_TTEE     ACS_TTEE
+#define CHAR_BTEE     ACS_BTEE
+#define CHAR_LTEE     ACS_LTEE
+#define CHAR_RTEE     ACS_RTEE
+#define CHAR_PLUS     ACS_PLUS
+#define CHAR_HLINE    ACS_HLINE
+#define CHAR_VLINE    ACS_VLINE
+#define CHAR_ULCORNER ACS_ULCORNER
+#define CHAR_URCORNER ACS_URCORNER
+#define CHAR_LLCORNER ACS_LLCORNER
+#define CHAR_LRCORNER ACS_LRCORNER
+#define CHAR_BULLET   '#'   // ACS_BULLET
+
+
+#endif

Modified: development/source/targets/viewer-text/viewer-text.cc
===================================================================
--- development/source/targets/viewer-text/viewer-text.cc	2007-07-06 14:29:33 UTC (rev 1755)
+++ development/source/targets/viewer-text/viewer-text.cc	2007-07-06 17:52:40 UTC (rev 1756)
@@ -49,13 +49,13 @@
   // Test to see if we should be in analyze mode only...
   if (world->GetConfig().ANALYZE_MODE.Get() > 0); // @CAO Do something here...
 
+  cout << "We Made IT!  Now we stop." << endl;
+
   driver = new cDriver_TextViewer(world);
 
-  cout << "We Made IT!  Now we stop." << endl;
-  exit(0);
+  cout << "And a bit more for the console to swallow..." << endl;
 
-  cout << endl;
-  
+
   driver->Run();
   
   // Exit Nicely

Added: development/source/tools/cCoords.h
===================================================================
--- development/source/tools/cCoords.h	                        (rev 0)
+++ development/source/tools/cCoords.h	2007-07-06 17:52:40 UTC (rev 1756)
@@ -0,0 +1,50 @@
+/*
+ *  cCoords.cc
+ *  Avida
+ *
+ *  Created by Charles on 7/6/07
+ *  Copyright 1999-2007 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef cCoords_h
+#define cCoords_h
+
+class cCoords {
+private:
+  int m_x;
+  int m_y;
+public:
+  cCoords(int _x, int _y) : m_x(_x), m_y(_y) { ; }
+  cCoords(const cCoords & _coords) : m_x(_coords.m_x), m_y(_coords.m_y) { ; }
+  ~cCoords() { ; }
+
+  void Translate(int _x, int _y) { m_x += _x, m_y += _y; }
+
+  void operator=(const cCoords & _coords) { m_x = _coords.m_x, m_y = _coords.m_y; }
+  int operator==(const cCoords & _c) { return (m_x == _c.m_x && m_y == _c.m_y); }
+  int operator!=(const cCoords & _c) { return !operator==(_c); }
+
+  int GetX() { return m_x; }
+  int GetY() { return m_y; }
+
+  void Set(int _x, int _y) { m_x = _x; m_y = _y; }
+};
+
+
+#endif

Modified: development/source/viewer-core/cCoreView_Info.cc
===================================================================
--- development/source/viewer-core/cCoreView_Info.cc	2007-07-06 14:29:33 UTC (rev 1755)
+++ development/source/viewer-core/cCoreView_Info.cc	2007-07-06 17:52:40 UTC (rev 1756)
@@ -5,11 +5,12 @@
   , m_total_colors(in_total_colors)
   , m_threshold_colors(in_total_colors * 5 / 6)
   , m_pause_level(PAUSE_OFF)
-  , m_step_organism(-1)
+  , m_step_organism_id(-1)
+  , m_step_organism_thread(-1)
 {
   // Redirect standard output...
-  std::cout.rdbuf(out_stream.rdbuf());
-  std::cerr.rdbuf(err_stream.rdbuf());
+  std::cout.rdbuf(m_cout_stream.rdbuf());
+  std::cerr.rdbuf(m_cerr_stream.rdbuf());
 }
 
 cCoreView_Info::~cCoreView_Info()
@@ -21,12 +22,48 @@
 
 void cCoreView_Info::FlushOut()
 {
-  cout_list.Load(out_stream.str().c_str(), '\n');  // Load the stored stream...
-  out_stream.str("");  // Clear the streams.
+  m_cout_list.Load(m_cout_stream.str().c_str(), '\n');  // Load the stored stream...
+  m_cout_stream.str("");  // Clear the streams.
 }
 
 void cCoreView_Info::FlushErr()
 {
-  cerr_list.Load(err_stream.str().c_str(), '\n');  // Load the stored stream...
-  err_stream.str("");  // Clear the streams.
+  m_cerr_list.Load(m_cerr_stream.str().c_str(), '\n');  // Load the stored stream...
+  m_cerr_stream.str("");  // Clear the streams.
 }
+
+
+/////////////////////////
+//  Other functions...
+
+void cCoreView_Info::EnterStepMode(int org_id)
+{
+  SetPauseLevel(PAUSE_ADVANCE_INST);
+  SetStepOrganism(org_id);
+}
+
+void cCoreView_Info::ExitStepMode()
+{
+  SetPauseLevel(PAUSE_ON);
+  SetStepOrganism(-1);
+}
+
+bool cCoreView_Info::TogglePause()
+{
+  // If pause is off, turn it on.
+  if (m_pause_level == PAUSE_OFF) {
+    SetPauseLevel(PAUSE_ON);
+    return true;
+  }
+
+  // Otherwise pause is on; carefully turn it off.
+  if (m_pause_level == PAUSE_ADVANCE_INST) ExitStepMode();
+
+  // Clean up any faults we may have been tracking in step mode.
+//   if (info.GetActiveCell()->IsOccupied()) {
+//     info.GetActiveCell()->GetOrganism()->GetPhenotype().SetFault("");
+//   }
+  SetPauseLevel(PAUSE_OFF);
+
+  return false;
+}

Modified: development/source/viewer-core/cCoreView_Info.h
===================================================================
--- development/source/viewer-core/cCoreView_Info.h	2007-07-06 14:29:33 UTC (rev 1755)
+++ development/source/viewer-core/cCoreView_Info.h	2007-07-06 17:52:40 UTC (rev 1756)
@@ -19,12 +19,12 @@
   cPopulation & m_population;
 
   // Setup streams to capture stdin and stdout so we can control them as needed.
-  std::stringstream out_stream;
-  std::stringstream err_stream;
+  std::stringstream m_cout_stream;
+  std::stringstream m_cerr_stream;
   
   // And string lists to convert outputs to for easier management.
-  cStringList cout_list;
-  cStringList cerr_list;
+  cStringList m_cout_list;
+  cStringList m_cerr_list;
 
   // Constant Inforation setup by specific viewer.
   const int m_total_colors;
@@ -32,11 +32,13 @@
 
   // Variable information, changing modes based on user input.
   int m_pause_level;
-  int m_step_organism;
+  int m_step_organism_id;
+  int m_step_organism_thread;
+  int m_view_mode;
 
   // Helper Methods
-  void FlushOut();  // Move stored cout to string list 'out_list'
-  void FlushErr();  // Move stored cerr to string list 'err_list'
+  void FlushOut();  // Move stored cout to string list 'cout_list'
+  void FlushErr();  // Move stored cerr to string list 'cerr_list'
   
 public:
   // Constant Information across all viewers.
@@ -51,14 +53,23 @@
 
   // Accessors for variable information
   int GetPauseLevel() const { return m_pause_level; }
-  int GetStepOrganism() const { return m_step_organism; }
+  int GetStepOrganism() const { return m_step_organism_id; }
+  int GetStepThread() const { return m_step_organism_thread; }
+  int GetViewMode() const { return m_view_mode; }
 
   void SetPauseLevel(int in_level) { m_pause_level = in_level; }
-  void SetStepOrganism(int in_id) { m_step_organism = in_id; }
+  void SetStepOrganism(int in_id) { m_step_organism_id = in_id; }
+  void SetStepThread(int in_thread) { m_step_organism_thread = in_thread; }
+  void SetViewMode(int in_mode) { m_view_mode = in_mode; }
 
   // Special accessors...
-  cStringList & GetOutList() { FlushOut(); return cout_list; }
-  cStringList & GetErrList() { FlushErr(); return cerr_list; }
+  cStringList & GetOutList() { FlushOut(); return m_cout_list; }
+  cStringList & GetErrList() { FlushErr(); return m_cerr_list; }
+
+  // Other functions...
+  void EnterStepMode(int org_id);
+  void ExitStepMode();
+  bool TogglePause();
 };
 
 #endif




More information about the Avida-cvs mailing list