(svn r13057) [NoAI] -Codechange: port the AIDebugWindow to the new Window framework. noai
authorrubidium
Sun, 11 May 2008 20:23:26 +0000
branchnoai
changeset 10514 8d9dbe82af3c
parent 10513 33cb70ff2f5d
child 10613 c1570be72d6c
(svn r13057) [NoAI] -Codechange: port the AIDebugWindow to the new Window framework.
src/ai/ai_gui.cpp
--- a/src/ai/ai_gui.cpp	Sun May 11 20:09:34 2008 +0000
+++ b/src/ai/ai_gui.cpp	Sun May 11 20:23:26 2008 +0000
@@ -29,192 +29,187 @@
 
 extern AIFactoryBase *AI_GetPlayerFactory(PlayerID player);
 
-enum AIDebugWindowWidgets {
-	AID_WIDGET_CLOSEBOX = 0,
-	AID_WIDGET_CAPTION,
-	AID_WIDGET_VIEW,
-	AID_WIDGET_NAME_TEXT,
-	AID_WIDGET_RELOAD_TOGGLE,
-	AID_WIDGET_LOG_PANEL,
-	AID_WIDGET_UNUSED_1,
-	AID_WIDGET_UNUSED_2,
-	AID_WIDGET_UNUSED_3,
-	AID_WIDGET_UNUSED_4,
-	AID_WIDGET_UNUSED_5,
-	AID_WIDGET_UNUSED_6,
-	AID_WIDGET_UNUSED_7,
-
-	AID_WIDGET_COMPANY_BUTTON_START,
-	AID_WIDGET_COMPANY_BUTTON_END = AID_WIDGET_COMPANY_BUTTON_START + 8,
-};
-
-static void AIDebugWndProc(Window *w, WindowEvent *e)
-{
-	static PlayerID _ai_debug_player = INVALID_PLAYER;
-
-	switch (e->event) {
-		case WE_PAINT: {
-			byte x;
-
-			/* Draw standard stuff */
-			DrawWindowWidgets(w);
-
-			/* Check if the currently selected player is still active. */
-			if (_ai_debug_player == INVALID_PLAYER || !GetPlayer(_ai_debug_player)->is_active) {
-				if (_ai_debug_player != INVALID_PLAYER) {
-					/* Raise and disable the widget for the previous selection. */
-					w->RaiseWidget(_ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
-					w->DisableWidget(_ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
-					SetWindowDirty(w);
-
-					_ai_debug_player = INVALID_PLAYER;
-				}
+struct AIDebugWindow : public Window {
+	enum AIDebugWindowWidgets {
+		AID_WIDGET_CLOSEBOX = 0,
+		AID_WIDGET_CAPTION,
+		AID_WIDGET_VIEW,
+		AID_WIDGET_NAME_TEXT,
+		AID_WIDGET_RELOAD_TOGGLE,
+		AID_WIDGET_LOG_PANEL,
+		AID_WIDGET_UNUSED_1,
+		AID_WIDGET_UNUSED_2,
+		AID_WIDGET_UNUSED_3,
+		AID_WIDGET_UNUSED_4,
+		AID_WIDGET_UNUSED_5,
+		AID_WIDGET_UNUSED_6,
+		AID_WIDGET_UNUSED_7,
 
-				for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
-					if (GetPlayer(i)->is_active && GetPlayer(i)->is_ai) {
-						/* Lower the widget corresponding to this player. */
-						w->LowerWidget(i + AID_WIDGET_COMPANY_BUTTON_START);
-						w->EnableWidget(AID_WIDGET_RELOAD_TOGGLE);
-						SetWindowDirty(w);
-
-						_ai_debug_player = i;
-						break;
-					}
-				}
-			}
-
-			/* If there are no active players, don't display anything else. */
-			if (_ai_debug_player == INVALID_PLAYER) break;
-
-			/* Paint the player icons */
-			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
-				if (!GetPlayer(i)->is_active || !GetPlayer(i)->is_ai) {
-					/* Check if we have the player as an active player */
-					if (!w->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) {
-						/* Bah, player gone :( */
-						w->DisableWidget(i + AID_WIDGET_COMPANY_BUTTON_START);
-						w->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
+		AID_WIDGET_COMPANY_BUTTON_START,
+		AID_WIDGET_COMPANY_BUTTON_END = AID_WIDGET_COMPANY_BUTTON_START + 8,
+	};
 
-						/* We need a repaint */
-						SetWindowDirty(w);
-					}
-					continue;
-				}
+	static PlayerID ai_debug_player;
+	int redraw_timer;
 
-				/* Check if we have the player marked as inactive */
-				if (w->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) {
-					/* New AI! Yippie :p */
-					w->EnableWidget(i + AID_WIDGET_COMPANY_BUTTON_START);
-					w->EnableWidget(AID_WIDGET_RELOAD_TOGGLE);
-					/* We need a repaint */
-					SetWindowDirty(w);
-				}
+	AIDebugWindow(const WindowDesc *desc, void *data, WindowNumber number) : Window(desc, data, number)
+	{
+		/* Disable the players who are not active or not an AI */
+		for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
+			this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !GetPlayer(i)->is_active || !GetPlayer(i)->is_ai);
+		}
+		this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
 
-				x = (i == _ai_debug_player) ? 1 : 0;
-				DrawPlayerIcon(i, i * 37 + 13 + x, 16 + x);
-			}
+		this->redraw_timer = 5 * DAY_TICKS;
 
-			AIFactoryBase *factory = AI_GetPlayerFactory(_ai_debug_player);
-			if (factory == NULL) {
-				DoDrawString("(unknown)", 7, 34, TC_BLACK);
-				w->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
-			} else {
-				DoDrawString(factory->GetAIName(), 7, 34, TC_BLACK);
+		if (ai_debug_player != INVALID_PLAYER) this->LowerWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
+		this->SetDirty();
+
+		this->FindWindowPlacementAndResize(desc);
+	}
+
+	virtual void OnPaint()
+	{
+		/* Draw standard stuff */
+		DrawWindowWidgets(this);
+
+		/* Check if the currently selected player is still active. */
+		if (ai_debug_player == INVALID_PLAYER || !GetPlayer(ai_debug_player)->is_active) {
+			if (ai_debug_player != INVALID_PLAYER) {
+				/* Raise and disable the widget for the previous selection. */
+				this->RaiseWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
+				this->DisableWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
+				this->SetDirty();
+
+				ai_debug_player = INVALID_PLAYER;
 			}
 
-			PlayerID old_cp = _current_player;
-			_current_player = _ai_debug_player;
-
-			if (AIObject::GetLogPointer() == NULL) {
-				_current_player = old_cp;
-				break;
-			}
-			AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
-
-			for (int i = 0; i < log->count; i++) {
-				uint pos = (log->count + log->pos - i) % log->count;
-				if (log->type[pos] == 0) break;
-
-				int y = 12 * i;
-				if (y >= w->widget[AID_WIDGET_LOG_PANEL].bottom - w->widget[AID_WIDGET_LOG_PANEL].top - 12) break;
+			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
+				if (GetPlayer(i)->is_active && GetPlayer(i)->is_ai) {
+					/* Lower the widget corresponding to this player. */
+					this->LowerWidget(i + AID_WIDGET_COMPANY_BUTTON_START);
+					this->EnableWidget(AID_WIDGET_RELOAD_TOGGLE);
+					this->SetDirty();
 
-				uint colour;
-				switch (log->type[pos]) {
-					case AILog::LOG_SQ_INFO:  colour = TC_BLACK;  break;
-					case AILog::LOG_SQ_ERROR: colour = TC_RED;    break;
-					case AILog::LOG_INFO:     colour = TC_BLACK;  break;
-					case AILog::LOG_WARNING:  colour = TC_YELLOW; break;
-					case AILog::LOG_ERROR:    colour = TC_RED;    break;
-					default:                  colour = TC_BLACK;  break;
-				}
-
-				DoDrawStringTruncated(log->lines[pos], 7, w->widget[AID_WIDGET_LOG_PANEL].top + 6 + y, colour, w->widget[AID_WIDGET_LOG_PANEL].right - w->widget[AID_WIDGET_LOG_PANEL].left - 14);
-			}
-
-			_current_player = old_cp;
-
-			break;
-		}
-
-		case WE_CLICK:
-			/* Check which button is clicked */
-			if (IsInsideMM(e->we.click.widget, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END)) {
-				/* Is it no on disable? */
-				if (!w->IsWidgetDisabled(e->we.click.widget)) {
-					w->RaiseWidget(_ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
-					_ai_debug_player = (PlayerID)(e->we.click.widget - AID_WIDGET_COMPANY_BUTTON_START);
-					w->LowerWidget(_ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
-					SetWindowDirty(w);
+					ai_debug_player = i;
+					break;
 				}
 			}
-			if (e->we.click.widget == AID_WIDGET_RELOAD_TOGGLE && !w->IsWidgetDisabled(e->we.click.widget)) {
-				/* Set the current AI as forced next AI */
-				AIFactoryBase *factory = AI_GetPlayerFactory(_ai_debug_player);
-				AI_ForceAI(factory->GetAIName());
-
-				/* First kill the company of the AI, then start a new one. This should start the current AI again */
-				DoCommandP(0, 2, _ai_debug_player, NULL, CMD_PLAYER_CTRL);
-				DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
-			}
-			break;
-
-		case WE_CREATE: {
-			/* Disable the players who are not active or not an AI */
-			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
-				w->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !GetPlayer(i)->is_active || !GetPlayer(i)->is_ai);
-			}
-			w->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
-
-			w->custom[0] = DAY_TICKS;
-			w->custom[1] = 5;
-
-			if (_ai_debug_player != INVALID_PLAYER) w->LowerWidget(_ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
-			SetWindowDirty(w);
-
-			break;
 		}
 
-		case WE_TICK:
-			if (_pause_game != 0) break;
+		/* If there are no active players, don't display anything else. */
+		if (ai_debug_player == INVALID_PLAYER) return;
 
-			/* Update the AI logs every 5 days */
-			if (--w->custom[0] == 0) {
-				w->custom[0] = DAY_TICKS;
-				if (--w->custom[1] == 0) {
-					w->custom[1] = 5;
+		/* Paint the player icons */
+		for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
+			if (!GetPlayer(i)->is_active || !GetPlayer(i)->is_ai) {
+				/* Check if we have the player as an active player */
+				if (!this->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) {
+					/* Bah, player gone :( */
+					this->DisableWidget(i + AID_WIDGET_COMPANY_BUTTON_START);
+					this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
 
-					SetWindowDirty(w);
+					/* We need a repaint */
+					this->SetDirty();
 				}
+				continue;
 			}
 
-			break;
+			/* Check if we have the player marked as inactive */
+			if (this->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) {
+				/* New AI! Yippie :p */
+				this->EnableWidget(i + AID_WIDGET_COMPANY_BUTTON_START);
+				this->EnableWidget(AID_WIDGET_RELOAD_TOGGLE);
+				/* We need a repaint */
+				this->SetDirty();
+			}
 
-		case WE_TIMEOUT:
-			w->RaiseWidget(AID_WIDGET_RELOAD_TOGGLE);
-			SetWindowDirty(w);
-			break;
+			byte x = (i == ai_debug_player) ? 1 : 0;
+			DrawPlayerIcon(i, i * 37 + 13 + x, 16 + x);
+		}
+
+		AIFactoryBase *factory = AI_GetPlayerFactory(ai_debug_player);
+		if (factory == NULL) {
+			DoDrawString("(unknown)", 7, 34, TC_BLACK);
+			this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE);
+		} else {
+			DoDrawString(factory->GetAIName(), 7, 34, TC_BLACK);
+		}
+
+		PlayerID old_cp = _current_player;
+		_current_player = ai_debug_player;
+
+		if (AIObject::GetLogPointer() == NULL) {
+			_current_player = old_cp;
+			return;
+		}
+		AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer();
+
+		for (int i = 0; i < log->count; i++) {
+			uint pos = (log->count + log->pos - i) % log->count;
+			if (log->type[pos] == 0) 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;
+				case AILog::LOG_SQ_ERROR: colour = TC_RED;    break;
+				case AILog::LOG_INFO:     colour = TC_BLACK;  break;
+				case AILog::LOG_WARNING:  colour = TC_YELLOW; break;
+				case AILog::LOG_ERROR:    colour = TC_RED;    break;
+				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);
+		}
+
+		_current_player = old_cp;
 	}
-}
+
+	virtual void OnClick(Point pt, int widget)
+	{
+		/* Check which button is clicked */
+		if (IsInsideMM(widget, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END)) {
+			/* Is it no on disable? */
+			if (!this->IsWidgetDisabled(widget)) {
+				this->RaiseWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
+				ai_debug_player = (PlayerID)(widget - AID_WIDGET_COMPANY_BUTTON_START);
+				this->LowerWidget(ai_debug_player + AID_WIDGET_COMPANY_BUTTON_START);
+				this->SetDirty();
+			}
+		}
+		if (widget == AID_WIDGET_RELOAD_TOGGLE && !this->IsWidgetDisabled(widget)) {
+			/* Set the current AI as forced next AI */
+			AIFactoryBase *factory = AI_GetPlayerFactory(ai_debug_player);
+			AI_ForceAI(factory->GetAIName());
+
+			/* First kill the company of the AI, then start a new one. This should start the current AI again */
+			DoCommandP(0, 2, ai_debug_player, NULL, CMD_PLAYER_CTRL);
+			DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
+		}
+	}
+
+	virtual void OnTick()
+	{
+		if (_pause_game != 0) return;
+
+		/* Update the AI logs every 5 days */
+		if (--this->redraw_timer == 0) {
+			this->redraw_timer = 5 * DAY_TICKS;
+			this->SetDirty();
+		}
+	}
+
+	virtual void OnTimeout()
+	{
+		this->RaiseWidget(AID_WIDGET_RELOAD_TOGGLE);
+		this->SetDirty();
+	}
+};
+
+PlayerID AIDebugWindow::ai_debug_player = INVALID_PLAYER;
 
 static const Widget _ai_debug_widgets[] = {
 {   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,                   STR_018B_CLOSE_WINDOW},                 // AID_WIDGET_CLOSEBOX
@@ -249,10 +244,10 @@
 	WC_PERFORMANCE_DETAIL, WC_NONE,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
 	_ai_debug_widgets,
-	AIDebugWndProc
+	NULL
 };
 
 void ShowAIDebugWindow()
 {
-	AllocateWindowDescFront<Window>(&_ai_debug_desc, 0);
+	AllocateWindowDescFront<AIDebugWindow>(&_ai_debug_desc, 0);
 }