[Scummvm-cvs-logs] CVS: scummvm/gui console.cpp,1.13,1.14 console.h,1.10,1.11

Max Horn fingolfin at users.sourceforge.net
Sat Dec 14 18:25:03 CET 2002


Update of /cvsroot/scummvm/scummvm/gui
In directory sc8-pr-cvs1:/tmp/cvs-serv21167

Modified Files:
	console.cpp console.h 
Log Message:
added a history (based on code by olki)

Index: console.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/console.cpp,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- console.cpp	15 Dec 2002 00:36:34 -0000	1.13
+++ console.cpp	15 Dec 2002 02:24:32 -0000	1.14
@@ -72,8 +72,16 @@
 	
 	_promptStartPos = _promptEndPos = -1;
 	
+	// Init callback
 	_callbackProc = 0;
 	_callbackRefCon = 0;
+ 
+ 	// Init History
+ 	_historyIndex = 0;
+ 	_historyLine = 0;
+ 	_historySize = 0;
+ 	for (int i = 0; i < kHistorySize; i++)
+ 		_history[i][0] = '\0';
 }
 
 void ConsoleDialog::open()
@@ -148,8 +156,10 @@
 			char str[len + 1];
 			for (i = 0; i < len; i++)
 				str[i] = _buffer[(_promptStartPos + i) % kBufferSize];
-			str[len] = 0;
+			str[len] = '\0';
 			
+ 			addToHistory(str);
+
 			bool keepRunning = true;
 			if (_callbackProc)
 				keepRunning = (*_callbackProc)(this, str, _callbackRefCon);
@@ -203,6 +213,12 @@
 			updateScrollBar();
 			draw();
 			break;
+		case 273:	// cursor up
+			historyScroll(+1);
+			break;
+		case 274:	// cursor down
+			historyScroll(-1);
+			break;
 		case 275:	// cursor right
 			if (_currentPos < _promptEndPos)
 				_currentPos++;
@@ -301,6 +317,54 @@
 	_buffer[_promptEndPos % kBufferSize] = ' ';
 	_promptEndPos -= cnt + 1;
 }
+
+void ConsoleDialog::addToHistory(const char *str)
+{
+	strcpy(_history[_historyIndex], str);
+	_historyIndex = (_historyIndex + 1) % kHistorySize;
+	_historyLine = 0;
+	if (_historySize < kHistorySize)
+		_historySize++;
+}
+
+void ConsoleDialog::historyScroll(int direction)
+{
+	if (_historySize == 0)
+		return;
+	
+	// Advance to the next line in the history
+	int line = _historyLine + direction;
+	if ((direction < 0 && line < 0) || (direction > 0 && line > _historySize))
+		return;
+	_historyLine = line;
+
+	// Hide caret if visible
+	if (_caretVisible)
+		drawCaret(true);
+
+	// Remove the current user text
+	_currentPos = _promptStartPos;
+	killLine();
+
+	// ... and ensure the prompt is visible
+	scrollToCurrent();
+
+	// Print the text from the history
+	if (_historyLine > 0) {
+		int idx = (_historyIndex - _historyLine + _historySize) % _historySize;
+		for (int i = 0; i < kLineBufferSize && _history[idx][i] != '\0'; i++)
+			putcharIntern(_history[idx][i]);
+		_promptEndPos = _currentPos;
+	
+		// Ensure once more the caret is visible (in case of very long history entries)
+		scrollToCurrent();
+	} else {
+		// TODO print the text which the user had typed before using the history
+	}
+	
+	draw();
+}
+
 
 void ConsoleDialog::nextLine()
 {

Index: console.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/gui/console.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- console.h	15 Dec 2002 00:36:34 -0000	1.10
+++ console.h	15 Dec 2002 02:24:32 -0000	1.11
@@ -29,7 +29,9 @@
 enum {
 	kBufferSize	= 32768,
 	kLineBufferSize = 256,
-	kCharWidth = 8
+	kCharWidth = 8,
+
+	kHistorySize = 20,
 };
 
 class ScrollBarWidget;
@@ -61,6 +63,11 @@
 	InputCallbackProc _callbackProc;
 	void	*_callbackRefCon;
 
+	char _history[kHistorySize][kLineBufferSize];
+	int _historySize;
+	int _historyIndex;
+	int _historyLine;
+
 public:
 	ConsoleDialog(NewGui *gui);
 
@@ -97,6 +104,10 @@
 	void killChar();
 	void killLine();
 	void killLastWord();
+
+	// History
+	void addToHistory(const char *str);
+	void historyScroll(int direction);
 };
 
 #endif





More information about the Scummvm-git-logs mailing list