Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

feat(gameengine): build the spectator drop-down from an HTML page

Olcay Seyganf43ad73cac3f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8912d5f25..ff7f6820c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -476,7 +476,7 @@ found and fixed — EA's own, not port damage.**
- Watching, clicking one player's unit is clicking his name in the player list, and clicking empty ground puts the whole match back. The command bar brings up his readouts and his money and wears his side's metal; the production rows on the left close down to his, and get longer since he has the strip to himself; the promotions on the right are his alone. The tilde key, or the general's stars on the right of the bar, then opens his promotion screen, on his side's artwork, with his skill points and his rank - greyed out, because they are not yours to spend. With nothing selected the key does nothing and the stars are dark; there is no one general to show.
- A general who has taken Artillery Barrage to level 3 shows one Artillery Barrage on the promotions strip, not three copies of the same picture in a row.
- The strips close up again: across a row the trays slide an eighth of a tray under each other, so a row reads as one run of metal. The cameos never overlap each other, because every tray goes down before any picture does.
-- Watching, the strips can be switched off. A small "Strips" drop-down in the top left corner opens into three boxes - the production queue, the generals' promotions and the superweapon timers - and each box switches its strip the moment it is clicked and is remembered for the next match you watch. The message list moves down to start under it. Playing, there is no drop-down and every strip is up, whatever you chose in the stands.
+- Watching, the strips can be switched off. A small "Strips" drop-down in the top left corner opens into three boxes - the production queue, the generals' promotions and the superweapon timers - and each box switches its strip the moment it is clicked and is remembered for the next match you watch. The message list moves down to start under it. Playing, there is no drop-down and every strip is up, whatever you chose in the stands. The drop-down is written as an HTML page under the game's Window folder: open it in a browser and the same boxes are there, and a box added to it for another on/off setting shows up in the next match you watch.
- A minimised command bar stays minimised. Changing seats, or clicking another player's unit while watching, put the whole bar back up over the battlefield you had just cleared. A new match still opens with the bar up.
## Placing buildings
diff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txt
index 37c37f2d9..1c7ff860a 100644
--- a/GeneralsMD/Code/CMakeLists.txt
+++ b/GeneralsMD/Code/CMakeLists.txt
@@ -770,10 +770,12 @@ set_target_properties(test_gameengine PROPERTIES CXX_STANDARD 17)
# fxlist_reforged_ini_parses_and_keeps_its_light runs the fork's own FXList file through the real
# INI parser, so it needs the tracked master rather than whatever sits in Run/.
# the_options_menu_buttons_all_wear_one_font reads the tracked layout master, not Run/'s copy.
+# the_hud_toggles_page_binds_only_on_off_options reads the tracked page master the same way.
target_compile_definitions(test_gameengine PRIVATE
GAMEENGINE_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/GameEngine"
FXLIST_REFORGED_INI="${CMAKE_CURRENT_SOURCE_DIR}/Data/INI/FXListReforged.ini"
OPTIONS_MENU_WND="${CMAKE_CURRENT_SOURCE_DIR}/Data/Window/Menus/OptionsMenu.wnd"
+ HUD_TOGGLES_HTML="${CMAKE_CURRENT_SOURCE_DIR}/Data/Window/Html/HudToggles.html"
PLATE_TEXTURE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/Data/Art/Textures")
# The three hand-written self-checks predate the harness; run them from ctest
diff --git a/GeneralsMD/Code/Data/Window/Html/HudToggles.html b/GeneralsMD/Code/Data/Window/Html/HudToggles.html
new file mode 100644
index 000000000..ac5c7083f
--- /dev/null
+++ b/GeneralsMD/Code/Data/Window/Html/HudToggles.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<!--
+ The spectator's drop-down in the top left corner of a match. The game reads a small part of
+ HTML (see GameClient/HtmlPanel.h): <details> and <summary>, <label>, <input type="checkbox">
+ and loose text, one row each. name= is an Options.ini key from OptionsCatalog.cpp that holds
+ yes or no; data-text= is a string table label the game shows instead of the words between the
+ tags. <head> is skipped, so the style below is only for opening this file in a browser.
+ Edit the copy under Run/Window/Html and the next match reads it.
+-->
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>HUD toggles</title>
+ <style>
+ body { background: #333; color: #eee; font: 12px sans-serif; }
+ label { display: block; }
+ </style>
+</head>
+<body>
+ <details>
+ <summary data-text="GUI:HudToggles">Strips</summary>
+ <label data-text="GUI:HudProductionStrip"><input type="checkbox" name="ShowProductionStrip"> Production queue</label>
+ <label data-text="GUI:HudSkillStrip"><input type="checkbox" name="ShowSkillStrip"> Generals' promotions</label>
+ <label data-text="GUI:HudSuperweaponStrip"><input type="checkbox" name="ShowSuperweaponStrip"> Superweapon timers</label>
+ </details>
+</body>
+</html>
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/HtmlPanel.h b/GeneralsMD/Code/GameEngine/Include/GameClient/HtmlPanel.h
new file mode 100644
index 000000000..6e6a6840a
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/HtmlPanel.h
@@ -0,0 +1,69 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** 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, either version 3 of the License, or
+** (at your option) any later version.
+**
+** 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, see <http://www.gnu.org/licenses/>.
+*/
+
+// HtmlPanel.h ////////////////////////////////////////////////////////////////////////////////////
+// A panel over the battlefield described by an .html file under Window/Html.
+//
+// The game reads a small part of HTML and turns it into a column of rows: a <summary> is a row
+// that opens and closes its <details>, a <label> or a bare <input type="checkbox"> is a check box
+// row, and loose text is a row of words. <head>, <style>, <script> and <title> are skipped, so a
+// page can carry the style a browser needs to show the same panel. There is no layout beyond the
+// column and no CSS: the look is the game's.
+//
+// An attribute carries what the words between the tags cannot: name= on a check box is the
+// Options.ini key it is bound to, data-text= is a string table label the game shows instead of
+// the literal words, so the page reads in English in a browser and in the player's language in
+// the game.
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#ifndef _HTML_PANEL_H_
+#define _HTML_PANEL_H_
+
+#include "Lib/BaseType.h"
+
+#include <string>
+#include <vector>
+
+enum HtmlRowKind
+{
+ HTML_ROW_SUMMARY, ///< a <summary>: clicking it opens or closes the rows of its <details>
+ HTML_ROW_CHECKBOX, ///< a check box, bound to the Options.ini key in `name`
+ HTML_ROW_TEXT, ///< words and nothing to click
+};
+
+struct HtmlRow
+{
+ HtmlRowKind kind;
+ std::string text; ///< the words between the tags, whitespace collapsed, UTF-8
+ std::string textKey; ///< data-text: a string table label, empty when `text` is the label
+ std::string name; ///< a check box's name=
+ Int owner; ///< the summary row of the <details> this row sits in, -1 at the top level
+ Bool open; ///< a summary: whether its <details> is open
+};
+
+/** The rows an HTML page describes, in document order. Anything outside the part of HTML the
+ * panel reads is passed over rather than refused, the way a browser passes over a tag it does
+ * not know. */
+extern void HtmlPanel_buildRows( const std::string &html, std::vector< HtmlRow > &rows );
+
+/** Is this row on screen: every <details> round it open? */
+extern Bool HtmlPanel_isRowShown( const std::vector< HtmlRow > &rows, Int row );
+
+#endif // _HTML_PANEL_H_
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index e65a73373..62a3032e0 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -43,6 +43,7 @@
#include "Common/SubsystemInterface.h"
#include "Common/UnicodeString.h"
#include "GameClient/DisplayString.h"
+#include "GameClient/HtmlPanel.h"
#include "GameClient/Mouse.h"
#include "GameClient/RadiusDecal.h"
#include "GameClient/View.h"
@@ -1192,17 +1193,18 @@ protected:
DisplayString * m_scoreboardStrings[ SCOREBOARD_STRING_COUNT ];
//
- // The drop-down in the top left corner that switches the strips on and off. Row 0 is its header,
- // the rest are one check box each.
+ // The drop-down in the top left corner that switches the strips on and off, read from
+ // Window/Html/HudToggles.html: one row per summary, check box or line of words in it.
//
- enum { HUD_TOGGLE_ROWS = 4 };
+ void loadHudToggles( void );
+ void freeHudToggleStrings( void );
void drawHudToggles( void );
Bool handleHudTogglesClick( const ICoord2D *mouse, Bool act ); ///< TRUE when the click landed on it
- Bool m_hudTogglesOpen;
- Int m_hudToggleRowsShown; ///< rows drawn this frame, header included
+ Bool m_hudTogglesLoaded; ///< read once a match, so an edited page shows in the next one
Int m_hudTogglesBottom; ///< its bottom edge, so the message list starts under it
- IRegion2D m_hudToggleRects[ HUD_TOGGLE_ROWS ];
- DisplayString * m_hudToggleStrings[ HUD_TOGGLE_ROWS ];
+ std::vector< HtmlRow > m_hudToggleRows;
+ std::vector< IRegion2D > m_hudToggleRects; ///< each row's place this frame, empty while it is hidden
+ std::vector< DisplayString * > m_hudToggleStrings;
Bool m_placementRangeRingUp; ///< the structure on the cursor is armed, so its reach is drawn
Real m_placementRingRadius; ///< how far from its centre it hits
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlPanel.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlPanel.cpp
new file mode 100644
index 000000000..d4869aafb
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlPanel.cpp
@@ -0,0 +1,376 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** 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, either version 3 of the License, or
+** (at your option) any later version.
+**
+** 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, see <http://www.gnu.org/licenses/>.
+*/
+
+// HtmlPanel.cpp //////////////////////////////////////////////////////////////////////////////////
+// The rows an HTML page describes. See HtmlPanel.h for the part of HTML that is read.
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
+
+#include "GameClient/HtmlPanel.h"
+
+#include <ctype.h>
+
+namespace
+{
+
+struct DetailsFrame
+{
+ Bool open; ///< the open attribute
+ Int summaryRow; ///< its <summary>'s row, -1 until that has been read
+};
+
+struct HtmlTag
+{
+ std::string name; ///< lower case
+ Bool closing;
+ std::vector< std::pair< std::string, std::string > > attributes; ///< names lower case, values decoded
+
+ const std::string *attribute( const char *wanted ) const
+ {
+ for( size_t index = 0; index < attributes.size(); index++ )
+ if( attributes[ index ].first == wanted )
+ return &attributes[ index ].second;
+ return NULL;
+ }
+};
+
+struct HtmlParse
+{
+ std::vector< HtmlRow > *rows;
+ std::vector< DetailsFrame > details;
+ Bool rowOpen; ///< a <label> or <summary> is collecting its words into `row`
+ HtmlRow row;
+ std::string looseText; ///< words outside any label, waiting for the tag that ends them
+};
+
+struct HtmlEntity
+{
+ const char *name;
+ const char *text;
+};
+
+// &nbsp; becomes a plain space: the panel collapses whitespace and has no line to keep together
+const HtmlEntity TheHtmlEntities[] =
+{
+ { "amp", "&" }, { "lt", "<" }, { "gt", ">" }, { "quot", "\"" }, { "apos", "'" }, { "#39", "'" }, { "nbsp", " " }
+};
+
+// elements whose content is never a row: a browser's business, or not text at all
+const char *const TheSkippedElements[] = { "head", "style", "script", "title" };
+
+// elements that sit inside a line of words, so they do not end the loose text before them
+const char *const TheInlineElements[] = { "a", "b", "i", "em", "strong", "span", "small", "code" };
+
+template< size_t COUNT >
+Bool isListed( const std::string &name, const char *const (&list)[ COUNT ] )
+{
+ for( size_t index = 0; index < COUNT; index++ )
+ if( name == list[ index ] )
+ return TRUE;
+ return FALSE;
+}
+
+std::string decodedEntities( const std::string &raw )
+{
+ std::string decoded;
+ size_t at = 0;
+ while( at < raw.size() )
+ {
+ Bool replaced = FALSE;
+ const size_t end = raw[ at ] == '&' ? raw.find( ';', at ) : std::string::npos;
+ if( end != std::string::npos )
+ {
+ const std::string name = raw.substr( at + 1, end - at - 1 );
+ for( size_t entity = 0; entity < ARRAY_SIZE( TheHtmlEntities ) && !replaced; entity++ )
+ {
+ if( name != TheHtmlEntities[ entity ].name )
+ continue;
+ decoded += TheHtmlEntities[ entity ].text;
+ at = end + 1;
+ replaced = TRUE;
+ }
+ }
+ if( !replaced )
+ decoded += raw[ at++ ];
+ }
+ return decoded;
+}
+
+std::string collapsedWhitespace( const std::string &text )
+{
+ std::string collapsed;
+ Bool gap = FALSE;
+ for( size_t index = 0; index < text.size(); index++ )
+ {
+ const char letter = text[ index ];
+ if( isspace( (unsigned char)letter ) )
+ {
+ gap = !collapsed.empty();
+ continue;
+ }
+ if( gap )
+ collapsed += ' ';
+ gap = FALSE;
+ collapsed += letter;
+ }
+ return collapsed;
+}
+
+std::string lowered( const std::string &text )
+{
+ std::string lower = text;
+ for( size_t index = 0; index < lower.size(); index++ )
+ lower[ index ] = (char)tolower( (unsigned char)lower[ index ] );
+ return lower;
+}
+
+/** The summary row of the nearest <details> round this point, -1 at the top level. A <summary>
+ * skips its own <details>: it is the row that opens that one, so it belongs to the one outside. */
+Int enclosingSummary( const std::vector< DetailsFrame > &details, Bool skipInnermost )
+{
+ for( Int frame = (Int)details.size() - ( skipInnermost ? 2 : 1 ); frame >= 0; frame-- )
+ if( details[ frame ].summaryRow >= 0 )
+ return details[ frame ].summaryRow;
+ return -1;
+}
+
+/** Read the tag whose '<' is at `start`, and return the index just past its '>'. */
+size_t readTag( const std::string &html, size_t start, HtmlTag &tag )
+{
+ const size_t length = html.size();
+ size_t at = start + 1;
+
+ tag.closing = at < length && html[ at ] == '/';
+ if( tag.closing )
+ at++;
+ while( at < length && ( isalnum( (unsigned char)html[ at ] ) || html[ at ] == '-' ) )
+ tag.name += (char)tolower( (unsigned char)html[ at++ ] );
+
+ while( at < length && html[ at ] != '>' )
+ {
+ if( isspace( (unsigned char)html[ at ] ) || html[ at ] == '/' )
+ {
+ at++;
+ continue;
+ }
+
+ std::string name;
+ while( at < length && !isspace( (unsigned char)html[ at ] ) && strchr( "=>/", html[ at ] ) == NULL )
+ name += (char)tolower( (unsigned char)html[ at++ ] );
+ while( at < length && isspace( (unsigned char)html[ at ] ) )
+ at++;
+
+ std::string value;
+ if( at < length && html[ at ] == '=' )
+ {
+ at++;
+ while( at < length && isspace( (unsigned char)html[ at ] ) )
+ at++;
+ if( at < length && ( html[ at ] == '"' || html[ at ] == '\'' ) )
+ {
+ const size_t close = html.find( html[ at ], at + 1 );
+ const size_t end = close == std::string::npos ? length : close;
+ value = html.substr( at + 1, end - at - 1 );
+ at = end == length ? length : end + 1;
+ }
+ else
+ {
+ while( at < length && !isspace( (unsigned char)html[ at ] ) && html[ at ] != '>' )
+ value += html[ at++ ];
+ }
+ }
+ tag.attributes.push_back( std::make_pair( name, decodedEntities( value ) ) );
+ }
+
+ return at < length ? at + 1 : length;
+}
+
+void pushRow( HtmlParse &parse, const HtmlRow &row )
+{
+ parse.rows->push_back( row );
+}
+
+void flushLooseText( HtmlParse &parse )
+{
+ HtmlRow row;
+ row.kind = HTML_ROW_TEXT;
+ row.text = collapsedWhitespace( decodedEntities( parse.looseText ) );
+ row.owner = enclosingSummary( parse.details, FALSE );
+ row.open = FALSE;
+ parse.looseText.clear();
+
+ if( !row.text.empty() )
+ pushRow( parse, row );
+}
+
+void closeRow( HtmlParse &parse )
+{
+ if( !parse.rowOpen )
+ return;
+ parse.rowOpen = FALSE;
+
+ HtmlRow &row = parse.row;
+ row.text = collapsedWhitespace( decodedEntities( row.text ) );
+ if( row.kind == HTML_ROW_TEXT && row.text.empty() && row.textKey.empty() )
+ return;
+
+ if( row.kind == HTML_ROW_SUMMARY && !parse.details.empty() && parse.details.back().summaryRow < 0 )
+ {
+ parse.details.back().summaryRow = (Int)parse.rows->size();
+ row.open = parse.details.back().open;
+ }
+ pushRow( parse, row );
+}
+
+void openRow( HtmlParse &parse, const HtmlTag &tag )
+{
+ closeRow( parse );
+
+ const Bool summary = tag.name == "summary";
+ const std::string *textKey = tag.attribute( "data-text" );
+
+ parse.rowOpen = TRUE;
+ parse.row.kind = summary ? HTML_ROW_SUMMARY : HTML_ROW_TEXT;
+ parse.row.text.clear();
+ parse.row.textKey = textKey ? *textKey : std::string();
+ parse.row.name.clear();
+ parse.row.owner = enclosingSummary( parse.details, summary );
+ parse.row.open = FALSE;
+}
+
+void readCheckbox( HtmlParse &parse, const HtmlTag &tag )
+{
+ const std::string *type = tag.attribute( "type" );
+ if( type == NULL || lowered( *type ) != "checkbox" )
+ return;
+
+ const std::string *name = tag.attribute( "name" );
+ if( parse.rowOpen && parse.row.kind == HTML_ROW_TEXT )
+ {
+ parse.row.kind = HTML_ROW_CHECKBOX;
+ parse.row.name = name ? *name : std::string();
+ return;
+ }
+ if( parse.rowOpen )
+ return; // a check box inside a <summary> would have two things to do on one click
+
+ HtmlRow row;
+ row.kind = HTML_ROW_CHECKBOX;
+ row.name = name ? *name : std::string();
+ row.owner = enclosingSummary( parse.details, FALSE );
+ row.open = FALSE;
+ pushRow( parse, row );
+}
+
+void readElement( HtmlParse &parse, const HtmlTag &tag )
+{
+ const Bool rowElement = tag.name == "summary" || tag.name == "label";
+
+ if( tag.closing )
+ {
+ if( rowElement )
+ closeRow( parse );
+ else if( tag.name == "details" && !parse.details.empty() )
+ parse.details.pop_back();
+ return;
+ }
+
+ if( tag.name == "details" )
+ {
+ DetailsFrame frame;
+ frame.open = tag.attribute( "open" ) != NULL;
+ frame.summaryRow = -1;
+ parse.details.push_back( frame );
+ }
+ else if( rowElement )
+ openRow( parse, tag );
+ else if( tag.name == "input" )
+ readCheckbox( parse, tag );
+}
+
+} // namespace
+
+//-------------------------------------------------------------------------------------------------
+void HtmlPanel_buildRows( const std::string &html, std::vector< HtmlRow > &rows )
+{
+ rows.clear();
+
+ HtmlParse parse;
+ parse.rows = &rows;
+ parse.rowOpen = FALSE;
+
+ const std::string lowerHtml = lowered( html );
+ const size_t length = html.size();
+ size_t at = 0;
+ while( at < length )
+ {
+ // a '<' that does not start a tag is a letter, the way a browser reads "a < b"
+ const Bool tagStart = html[ at ] == '<' && at + 1 < length
+ && ( isalpha( (unsigned char)html[ at + 1 ] ) || strchr( "/!?", html[ at + 1 ] ) != NULL );
+ if( !tagStart )
+ {
+ const size_t next = html.find( '<', at + 1 );
+ const size_t end = next == std::string::npos ? length : next;
+ ( parse.rowOpen ? parse.row.text : parse.looseText ) += html.substr( at, end - at );
+ at = end;
+ continue;
+ }
+
+ if( html.compare( at, 4, "<!--" ) == 0 )
+ {
+ const size_t end = html.find( "-->", at );
+ at = end == std::string::npos ? length : end + 3;
+ continue;
+ }
+ if( html[ at + 1 ] == '!' || html[ at + 1 ] == '?' )
+ {
+ const size_t end = html.find( '>', at );
+ at = end == std::string::npos ? length : end + 1;
+ continue;
+ }
+
+ HtmlTag tag;
+ at = readTag( html, at, tag );
+
+ if( !parse.rowOpen && !isListed( tag.name, TheInlineElements ) )
+ flushLooseText( parse );
+
+ if( !tag.closing && isListed( tag.name, TheSkippedElements ) )
+ {
+ const size_t close = lowerHtml.find( "</" + tag.name, at );
+ const size_t end = close == std::string::npos ? std::string::npos : html.find( '>', close );
+ at = end == std::string::npos ? length : end + 1;
+ continue;
+ }
+
+ readElement( parse, tag );
+ }
+
+ closeRow( parse );
+ flushLooseText( parse );
+}
+
+//-------------------------------------------------------------------------------------------------
+Bool HtmlPanel_isRowShown( const std::vector< HtmlRow > &rows, Int row )
+{
+ for( Int owner = rows[ row ].owner; owner >= 0; owner = rows[ owner ].owner )
+ if( !rows[ owner ].open )
+ return FALSE;
+ return TRUE;
+}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 64473e645..a94159719 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -60,6 +60,10 @@
#include "GameClient/Eva.h"
#include "GameClient/GameText.h"
#include "Common/UserPreferences.h"
+#include "Common/OptionsCatalog.h"
+#include "Common/FileSystem.h"
+#include "Common/file.h"
+#include "GameNetwork/GameSpy/ThreadUtils.h"
#include "GameClient/GameWindowManager.h"
#include "GameClient/Drawable.h"
#include "GameClient/GadgetPushButton.h"
@@ -1184,11 +1188,8 @@ InGameUI::InGameUI()
m_productionStripTraySource = NULL;
for( Int stripString = 0; stripString < STRIP_OVERFLOW_STRINGS; stripString++ )
m_productionStripOverflow[ stripString ] = NULL;
- m_hudTogglesOpen = FALSE;
- m_hudToggleRowsShown = 0;
+ m_hudTogglesLoaded = FALSE;
m_hudTogglesBottom = 0;
- for( Int toggleRow = 0; toggleRow < HUD_TOGGLE_ROWS; toggleRow++ )
- m_hudToggleStrings[ toggleRow ] = NULL;
m_scoreboardOpen = FALSE;
m_scoreboardStringsUsed = 0;
for( Int scoreboardString = 0; scoreboardString < SCOREBOARD_STRING_COUNT; scoreboardString++ )
@@ -1307,6 +1308,8 @@ InGameUI::~InGameUI()
m_productionStripTray = NULL;
m_productionStripTraySource = NULL;
}
+
+ freeHudToggleStrings();
}
//-------------------------------------------------------------------------------------------------
@@ -1729,26 +1732,16 @@ static Real elevatedReach( Real reach, Real range, const Coord3D &center, Real a
}
//-------------------------------------------------------------------------------------------------
-// The top left drop-down. Row 0 is its header; each row after it is one strip, its words, the
-// GlobalData switch it flips and the Options.ini key that switch is saved under.
+// The top left drop-down is a page: its rows come from this file, and each check box in it names
+// the Options.ini key it flips. A loose copy under Run/ beats the archive, so it can be edited
+// between two matches without a build.
//-------------------------------------------------------------------------------------------------
-static const char *const TheHudToggleLabels[] =
-{
- "GUI:HudToggles", "GUI:HudProductionStrip", "GUI:HudSkillStrip", "GUI:HudSuperweaponStrip"
-};
-static Bool GlobalData::* const TheHudToggleFlags[] =
-{
- NULL, &GlobalData::m_showProductionStrip, &GlobalData::m_showSkillStrip, &GlobalData::m_showSuperweaponStrip
-};
-static const char *const TheHudToggleKeys[] =
-{
- NULL, "ShowProductionStrip", "ShowSkillStrip", "ShowSuperweaponStrip"
-};
+static const char *const HUD_TOGGLES_PAGE = "Window\\Html\\HudToggles.html";
enum
{
HUD_TOGGLES_INSET = 6, ///< from the top and left edges of the screen, 800x600
- HUD_TOGGLES_WIDTH = 150, ///< the whole drop-down's width, 800x600
+ HUD_TOGGLES_WIDTH = 150, ///< the drop-down's narrowest width, 800x600; longer words widen it
HUD_TOGGLES_PAD = 3 ///< round the words and the boxes inside a row, 800x600
};
@@ -1770,57 +1763,118 @@ static Bool stripSwitchedOff( Bool GlobalData::* flag )
return localPlayerWatching() && !( TheGlobalData->*flag );
}
+//-------------------------------------------------------------------------------------------------
+/** Read the drop-down's page and make a string for each of its rows. A check box whose name is not
+ * an on/off option has nothing to flip, so it is shown as its words alone. */
+//-------------------------------------------------------------------------------------------------
+void InGameUI::loadHudToggles( void )
+{
+ m_hudTogglesLoaded = TRUE;
+ freeHudToggleStrings();
+ m_hudToggleRows.clear();
+ m_hudToggleRects.clear();
+
+ File *file = TheFileSystem->openFile( HUD_TOGGLES_PAGE, File::READ | File::BINARY );
+ if( file == NULL )
+ {
+ DEBUG_LOG(( "HUD toggles: %s is missing, so the drop-down is not drawn\n", HUD_TOGGLES_PAGE ));
+ return;
+ }
+ const Int size = file->size();
+ char *page = file->readEntireAndClose();
+ HtmlPanel_buildRows( std::string( page, size ), m_hudToggleRows );
+ delete [] page;
+
+ GameFont *font = TheFontLibrary->getFont( m_superweaponNormalFont,
+ TheGlobalLanguageData->adjustFontSize( HUD_OVERLAY_POINT_SIZE ), TRUE );
+ for( size_t row = 0; row < m_hudToggleRows.size(); row++ )
+ {
+ HtmlRow &html = m_hudToggleRows[ row ];
+ if( html.kind == HTML_ROW_CHECKBOX )
+ {
+ const OptionDef *option = findOptionDef( html.name.c_str() );
+ if( option == NULL || option->kind != OPTION_BOOL )
+ {
+ DEBUG_LOG(( "HUD toggles: check box name=\"%s\" in %s is not an on/off option\n", html.name.c_str(), HUD_TOGGLES_PAGE ));
+ html.kind = HTML_ROW_TEXT;
+ }
+ }
+
+ DisplayString *words = TheDisplayStringManager->newDisplayString();
+ words->setFont( font );
+ if( html.textKey.empty() )
+ words->setText( UnicodeString( MultiByteToWideCharSingleLine( html.text.c_str() ).c_str() ) );
+ else
+ words->setText( TheGameText->fetch( html.textKey.c_str() ) );
+ m_hudToggleStrings.push_back( words );
+ }
+ m_hudToggleRects.resize( m_hudToggleRows.size() );
+}
+
+//-------------------------------------------------------------------------------------------------
+void InGameUI::freeHudToggleStrings( void )
+{
+ for( size_t row = 0; row < m_hudToggleStrings.size(); row++ )
+ TheDisplayStringManager->freeDisplayString( m_hudToggleStrings[ row ] );
+ m_hudToggleStrings.clear();
+}
+
//-------------------------------------------------------------------------------------------------
/** The strips over the battlefield, switched on and off from a drop-down in the top left corner.
*
* Watching a match three of them fight over the same picture - the production rows, the
* promotions, the superweapon countdowns - and which of them a spectator wants depends on what he
- * is watching for. Closed it is one line with a plus in it. Each box flips its strip the moment
+ * is watching for. Closed it is one line with a plus in it. Each box flips its option the moment
* it is clicked and saves the choice with the rest of the options. Only while watching: playing,
* the top left corner belongs to the messages. */
//-------------------------------------------------------------------------------------------------
void InGameUI::drawHudToggles( void )
{
- m_hudToggleRowsShown = 0;
m_hudTogglesBottom = 0;
+ for( size_t row = 0; row < m_hudToggleRects.size(); row++ )
+ m_hudToggleRects[ row ].lo = m_hudToggleRects[ row ].hi = ICoord2D();
if( TheGameLogic == NULL || !TheGameLogic->isInGame() || TheGameLogic->isInShellGame() )
return;
if( !localPlayerWatching() )
return;
- const Int rows = m_hudTogglesOpen ? HUD_TOGGLE_ROWS : 1;
- for( Int row = 0; row < rows; row++ )
- {
- if( m_hudToggleStrings[ row ] )
- continue;
-
- m_hudToggleStrings[ row ] = TheDisplayStringManager->newDisplayString();
- m_hudToggleStrings[ row ]->setFont( TheFontLibrary->getFont( m_superweaponNormalFont,
- TheGlobalLanguageData->adjustFontSize( HUD_OVERLAY_POINT_SIZE ), TRUE ) );
- m_hudToggleStrings[ row ]->setText( TheGameText->fetch( TheHudToggleLabels[ row ] ) );
- }
-
- Int textW = 0, textH = 0;
- m_hudToggleStrings[ 0 ]->getSize( &textW, &textH );
+ if( !m_hudTogglesLoaded )
+ loadHudToggles();
+ if( m_hudToggleRows.empty() )
+ return;
const Int pad = stripPixels( HUD_TOGGLES_PAD );
const Int left = stripPixels( HUD_TOGGLES_INSET );
const Int top = stripPixels( HUD_TOGGLES_INSET );
- const Int width = stripPixels( HUD_TOGGLES_WIDTH );
- const Int rowH = textH + 2 * pad;
- const Int box = textH;
+ const Int box = m_hudToggleStrings[ 0 ]->getFont()->height;
+ const Int rowH = box + 2 * pad;
const Color plate = GameMakeColor( 0, 0, 0, 150 );
const Color edge = GameMakeColor( 200, 200, 200, 255 );
const Color on = GameMakeColor( 90, 200, 90, 255 );
const Color words = GameMakeColor( 235, 235, 235, 255 );
const Color shade = GameMakeColor( 0, 0, 0, 255 );
+ // one width for every row, so the plates stack into one panel
+ Int width = stripPixels( HUD_TOGGLES_WIDTH );
+ for( Int row = 0; row < (Int)m_hudToggleRows.size(); row++ )
+ {
+ if( !HtmlPanel_isRowShown( m_hudToggleRows, row ) )
+ continue;
+ Int textW = 0, textH = 0;
+ m_hudToggleStrings[ row ]->getSize( &textW, &textH );
+ width = max( width, box + textW + 3 * pad );
+ }
+
TheDisplay->beginBatch2D();
- for( Int row = 0; row < rows; row++ )
+ Int y = top;
+ for( Int row = 0; row < (Int)m_hudToggleRows.size(); row++ )
{
- const Int y = top + row * rowH;
+ if( !HtmlPanel_isRowShown( m_hudToggleRows, row ) )
+ continue;
+
+ const HtmlRow &html = m_hudToggleRows[ row ];
IRegion2D *rect = &m_hudToggleRects[ row ];
rect->lo.x = left;
rect->lo.y = y;
@@ -1831,29 +1885,34 @@ void InGameUI::drawHudToggles( void )
const Int boxX = left + pad;
const Int boxY = y + pad;
- TheDisplay->drawOpenRect( boxX, boxY, box, box, 1.0f, edge );
+ Int wordsX = boxX;
+ if( html.kind != HTML_ROW_TEXT )
+ {
+ TheDisplay->drawOpenRect( boxX, boxY, box, box, 1.0f, edge );
+ wordsX = boxX + box + 2 * pad;
+ }
- if( row == 0 )
+ if( html.kind == HTML_ROW_SUMMARY )
{
// a minus to close it, a plus to open it
const Int middleY = boxY + box / 2;
const Int middleX = boxX + box / 2;
TheDisplay->drawLine( boxX + pad, middleY, boxX + box - pad, middleY, 2.0f, edge );
- if( !m_hudTogglesOpen )
+ if( !html.open )
TheDisplay->drawLine( middleX, boxY + pad, middleX, boxY + box - pad, 2.0f, edge );
}
- else if( TheGlobalData->*TheHudToggleFlags[ row ] )
+ else if( html.kind == HTML_ROW_CHECKBOX && findOptionDef( html.name.c_str() )->get() )
{
TheDisplay->drawFillRect( boxX + pad, boxY + pad, box - 2 * pad, box - 2 * pad, on );
}
- m_hudToggleStrings[ row ]->draw( boxX + box + 2 * pad, y + pad, words, shade );
+ m_hudToggleStrings[ row ]->draw( wordsX, y + pad, words, shade );
+ y += rowH;
}
TheDisplay->endBatch2D();
- m_hudToggleRowsShown = rows;
- m_hudTogglesBottom = top + rows * rowH;
+ m_hudTogglesBottom = y;
}
//-------------------------------------------------------------------------------------------------
@@ -1862,7 +1921,7 @@ void InGameUI::drawHudToggles( void )
//-------------------------------------------------------------------------------------------------
Bool InGameUI::handleHudTogglesClick( const ICoord2D *mouse, Bool act )
{
- for( Int row = 0; row < m_hudToggleRowsShown; row++ )
+ for( size_t row = 0; row < m_hudToggleRects.size(); row++ )
{
const IRegion2D &rect = m_hudToggleRects[ row ];
if( mouse->x < rect.lo.x || mouse->x >= rect.hi.x || mouse->y < rect.lo.y || mouse->y >= rect.hi.y )
@@ -1871,18 +1930,20 @@ Bool InGameUI::handleHudTogglesClick( const ICoord2D *mouse, Bool act )
if( !act )
return TRUE;
- if( row == 0 )
- {
- m_hudTogglesOpen = !m_hudTogglesOpen;
- return TRUE;
- }
+ HtmlRow &html = m_hudToggleRows[ row ];
+ if( html.kind == HTML_ROW_SUMMARY )
+ html.open = !html.open;
- const Bool now = !( TheGlobalData->*TheHudToggleFlags[ row ] );
- TheWritableGlobalData->*TheHudToggleFlags[ row ] = now;
+ if( html.kind == HTML_ROW_CHECKBOX )
+ {
+ const OptionDef *option = findOptionDef( html.name.c_str() );
+ const Bool now = !option->get();
+ option->set( now );
- OptionPreferences pref;
- pref[ AsciiString( TheHudToggleKeys[ row ] ) ] = AsciiString( now ? "yes" : "no" );
- pref.write();
+ OptionPreferences pref;
+ pref[ AsciiString( option->iniKey ) ] = AsciiString( now ? "yes" : "no" );
+ pref.write();
+ }
return TRUE;
}
@@ -3313,6 +3374,7 @@ void InGameUI::reset( void )
{
m_isQuitMenuVisible = FALSE;
m_scoreboardOpen = FALSE;
+ m_hudTogglesLoaded = FALSE;
m_inputEnabled = true;
// reset the command bar
TheControlBar->reset();
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 244fe5400..548d5f8e5 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -46,6 +46,7 @@
#include "GameClient/ChromaKeyboard.h"
#include "GameClient/MetaEvent.h"
#include "GameClient/ClickTolerance.h"
+#include "GameClient/HtmlPanel.h"
#include "GameClient/KeyDownInfo.h"
#include "GameClient/GameWindowTransitions.h"
#include "GameLogic/ScenarioDrill.h"
@@ -13526,6 +13527,98 @@ TEST(chroma_money_bar_is_a_thousand_credits_a_lamp)
CHECK_EQ(chromaMoneySegments(400000, 15), 15);
}
+// A page reads as the column a browser would show: the summary first, the rows of its details
+// under it and hidden while it is closed, and the head never a row even though it holds words.
+TEST(html_panel_reads_details_labels_and_loose_text)
+{
+ const std::string page =
+ "<!DOCTYPE html><html><head><title>Not a row</title><style>label { display: block; }</style></head>"
+ "<body><!-- nor this -->"
+ "<details><SUMMARY data-text=\"GUI:Top\">Top</SUMMARY>"
+ "<label><input type=checkbox name='First'> First &amp;\n one </label>"
+ "<details open><summary>Inner</summary><input type=\"checkbox\" name=\"Second\"></details>"
+ "Words a < b</details>"
+ "<p>Out<b>side</b></p></body></html>";
+
+ std::vector< HtmlRow > rows;
+ HtmlPanel_buildRows( page, rows );
+
+ CHECK_EQ( (Int)rows.size(), 6 );
+ if( rows.size() != 6 )
+ return;
+
+ CHECK_EQ( (Int)rows[ 0 ].kind, (Int)HTML_ROW_SUMMARY );
+ CHECK( rows[ 0 ].textKey == "GUI:Top" );
+ CHECK_EQ( rows[ 0 ].owner, -1 );
+ CHECK( !rows[ 0 ].open );
+
+ CHECK_EQ( (Int)rows[ 1 ].kind, (Int)HTML_ROW_CHECKBOX );
+ CHECK( rows[ 1 ].name == "First" );
+ CHECK( rows[ 1 ].text == "First & one" );
+ CHECK_EQ( rows[ 1 ].owner, 0 );
+
+ CHECK_EQ( (Int)rows[ 2 ].kind, (Int)HTML_ROW_SUMMARY );
+ CHECK( rows[ 2 ].text == "Inner" );
+ CHECK_EQ( rows[ 2 ].owner, 0 );
+ CHECK( rows[ 2 ].open );
+
+ CHECK_EQ( (Int)rows[ 3 ].kind, (Int)HTML_ROW_CHECKBOX );
+ CHECK( rows[ 3 ].name == "Second" );
+ CHECK_EQ( rows[ 3 ].owner, 2 );
+
+ CHECK_EQ( (Int)rows[ 4 ].kind, (Int)HTML_ROW_TEXT );
+ CHECK( rows[ 4 ].text == "Words a < b" );
+ CHECK_EQ( rows[ 4 ].owner, 0 );
+
+ CHECK( rows[ 5 ].text == "Outside" );
+ CHECK_EQ( rows[ 5 ].owner, -1 );
+
+ // the outer details is closed, so only its summary and what lies outside it are shown
+ CHECK( HtmlPanel_isRowShown( rows, 0 ) );
+ CHECK( !HtmlPanel_isRowShown( rows, 1 ) );
+ CHECK( !HtmlPanel_isRowShown( rows, 3 ) );
+ CHECK( HtmlPanel_isRowShown( rows, 5 ) );
+
+ rows[ 0 ].open = TRUE;
+ CHECK( HtmlPanel_isRowShown( rows, 1 ) );
+ CHECK( HtmlPanel_isRowShown( rows, 3 ) );
+ rows[ 2 ].open = FALSE;
+ CHECK( !HtmlPanel_isRowShown( rows, 3 ) );
+}
+
+// The spectator's drop-down is the shipped page. A check box whose name is not an on/off option
+// would draw as words with nothing to flip, and only a match would show it.
+TEST(the_hud_toggles_page_binds_only_on_off_options)
+{
+ FILE *fp = fopen( HUD_TOGGLES_HTML, "rb" );
+ CHECK( fp != NULL );
+ if( fp == NULL )
+ return;
+
+ std::string page;
+ char chunk[ 1024 ];
+ size_t got = 0;
+ while( ( got = fread( chunk, 1, sizeof( chunk ), fp ) ) > 0 )
+ page.append( chunk, got );
+ fclose( fp );
+
+ std::vector< HtmlRow > rows;
+ HtmlPanel_buildRows( page, rows );
+
+ Int checkboxes = 0;
+ for( size_t row = 0; row < rows.size(); row++ )
+ {
+ if( rows[ row ].kind != HTML_ROW_CHECKBOX )
+ continue;
+ checkboxes++;
+ const OptionDef *option = findOptionDef( rows[ row ].name.c_str() );
+ CHECK( option != NULL );
+ CHECK( option != NULL && option->kind == OPTION_BOOL );
+ }
+ CHECK_EQ( checkboxes, 3 );
+ CHECK( !rows.empty() && rows[ 0 ].kind == HTML_ROW_SUMMARY );
+}
+
#include "test_camera_behavior.inc"
#include "test_production_input.inc"
#include "test_minimap_input.inc"