(svn r13300) [NoAI] -Add: a scrollbar in AIDebug window. noai
authorglx
Tue, 27 May 2008 21:04:10 +0000
branchnoai
changeset 10750 e2c339d613d1
parent 10740 325e94c419ac
child 10774 2c882f0468f2
(svn r13300) [NoAI] -Add: a scrollbar in AIDebug window.
src/ai/ai_gui.cpp
src/ai/api/ai_log.cpp
src/ai/api/ai_log.hpp
--- a/src/ai/ai_gui.cpp	Tue May 27 15:24:23 2008 +0000
+++ b/src/ai/ai_gui.cpp	Tue May 27 21:04:10 2008 +0000
@@ -40,6 +40,7 @@
 		AID_WIDGET_NAME_TEXT,
 		AID_WIDGET_RELOAD_TOGGLE,
 		AID_WIDGET_LOG_PANEL,
+		AID_WIDGET_SCROLLBAR,
 		AID_WIDGET_UNUSED_1,
 		AID_WIDGET_UNUSED_2,
 		AID_WIDGET_UNUSED_3,
@@ -63,6 +64,10 @@
 		}
 		this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
 
+		this->vscroll.cap = 14;
+		this->vscroll.pos = 0;
+		this->resize.step_height = 12;
+
 		if (ai_debug_player != INVALID_PLAYER) this->LowerWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
 
 		this->FindWindowPlacementAndResize(desc);
@@ -134,20 +139,21 @@
 
 		PlayerID old_cp = _current_player;
 		_current_player = ai_debug_player;
+		AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
+		_current_player = old_cp;
 
-		if (AIObject::GetLogPointer() == NULL) {
-			_current_player = old_cp;
-			return;
+		uint16 new_count = (log == NULL) ? 0 : log->used;
+		if (new_count != this->vscroll.count) {
+			SetVScrollCount(this, new_count);
+			this->SetDirty();
 		}
-		AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
+		if (log == NULL) return;
 
-		for (int i = 0; i < log->count; i++) {
+		int y = 6;
+		for (int i = this->vscroll.pos; i < (this->vscroll.cap + this->vscroll.pos); i++) {
 			uint pos = (log->count + log->pos - i) % log->count;
 			if (log->lines[pos] == NULL) break;
 
-			int y = 12 * i;
-			if (y >= this->widget[AID_WIDGET_LOG_PANEL].bottom - this->widget[AID_WIDGET_LOG_PANEL].top - 12) break;
-
 			uint colour;
 			switch (log->type[pos]) {
 				case AILog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
@@ -158,10 +164,9 @@
 				default:                  colour = TC_BLACK;  break;
 			}
 
-			DoDrawStringTruncated(log->lines[pos], 7, this->widget[AID_WIDGET_LOG_PANEL].top + 6 + y, colour, this->widget[AID_WIDGET_LOG_PANEL].right - this->widget[AID_WIDGET_LOG_PANEL].left - 14);
+			DoDrawStringTruncated(log->lines[pos], 7, this->widget[AID_WIDGET_LOG_PANEL].top + y, colour, this->widget[AID_WIDGET_LOG_PANEL].right - this->widget[AID_WIDGET_LOG_PANEL].left - 14);
+			y += 12;
 		}
-
-		_current_player = old_cp;
 	}
 
 	virtual void OnClick(Point pt, int widget)
@@ -197,6 +202,11 @@
 	{
 		if (data == -1 || ai_debug_player == data) this->SetDirty();
 	}
+
+	virtual void OnResize(Point new_size, Point delta)
+	{
+		this->vscroll.cap += delta.y / (int)this->resize.step_height;
+	}
 };
 
 PlayerID AIDebugWindow::ai_debug_player = INVALID_PLAYER;
@@ -208,7 +218,8 @@
 
 {      WWT_PANEL,  RESIZE_RIGHT,    14,     0,   149,    28,    47, 0x0,                        STR_AI_DEBUG_NAME_TIP},                 // AID_WIDGET_NAME_TEXT
 { WWT_PUSHTXTBTN,     RESIZE_LR,    14,   150,   298,    28,    47, STR_AI_DEBUG_RELOAD,        STR_AI_DEBUG_RELOAD_TIP},               // AID_WIDGET_RELOAD_TOGGLE
-{      WWT_PANEL,     RESIZE_RB,    14,     0,   298,    48,   227, 0x0,                        STR_NULL},                              // AID_WIDGET_LOG_PANEL
+{      WWT_PANEL,     RESIZE_RB,    14,     0,   286,    48,   227, 0x0,                        STR_NULL},                              // AID_WIDGET_LOG_PANEL
+{  WWT_SCROLLBAR,    RESIZE_LRB,    14,   287,   298,    48,   215, STR_NULL,                   STR_0190_SCROLL_BAR_SCROLLS_LIST},      // AID_WIDGET_SCROLLBAR
 /* As this is WIP, leave the next few so we can work a bit with the GUI */
 {      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,    88,   107, 0x0,                        STR_NULL},                              // AID_WIDGET_UNUSED_1
 {      WWT_EMPTY,   RESIZE_NONE,    14,     0,   298,   108,   127, 0x0,                        STR_NULL},                              // AID_WIDGET_UNUSED_2
--- a/src/ai/api/ai_log.cpp	Tue May 27 15:24:23 2008 +0000
+++ b/src/ai/api/ai_log.cpp	Tue May 27 21:04:10 2008 +0000
@@ -33,12 +33,15 @@
 		log->type = CallocT<AILog::AILogType>(80);
 		log->count = 80;
 		log->pos = log->count;
+		log->used = 0;
 	}
 	LogData *log = (LogData *)AIObject::GetLogPointer();
 
 	/* Go to the next log-line */
 	log->pos = (log->pos + 1) % log->count;
 
+	if (log->used != log->count) log->used++;
+
 	/* Free last message, and write new message */
 	free(log->lines[log->pos]);
 	log->lines[log->pos] = strdup(message);
--- a/src/ai/api/ai_log.hpp	Tue May 27 15:24:23 2008 +0000
+++ b/src/ai/api/ai_log.hpp	Tue May 27 21:04:10 2008 +0000
@@ -39,6 +39,7 @@
 		AILog::AILogType *type; //!< Per line, which type of log it was.
 		int count;              //!< Total amount of log-lines possible.
 		int pos;                //!< Current position in lines.
+		int used;               //!< Total amount of used log-lines.
 	};
 
 	/**