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.

fix(gameclient): one keystroke does one thing, badges show held keys

Olcay Seyganfb6c3bc9cb21
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fde8aec2a..8613be6cc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -368,7 +368,7 @@ found and fixed, EA's own, not port damage.**
- Export and Import on the Hotkeys page take your keys to a file and back, for another PC or a fresh install. Before an import the page tells you how many keys it changes and how many would clash; a file with a line it cannot read changes nothing and quotes that line. Keys for a mod you have not installed come along and wait for it.
- The arrow keys scroll the camera in the German game, as they do in English. German Zero Hour put next and previous unit and worker on the bare arrows, so the arrows picked units and only Ctrl and an arrow moved the camera; those four now sit on Ctrl and an arrow, as the English game ships them. Any language that puts a command on a bare arrow gets the same, and an arrow you gave a command yourself on the Hotkeys page stays where you put it.
- Grid keys are back, keyed by the cell a button sits in. The first grid moved buttons around to give them a place's letter and still left two places without one; this one moves nothing. With the grid on, the top row of the bar is Q W E R T Y U and the bottom row A S D F G H J, column by column, so the first cell is Q on a Barracks, a War Factory and a tank alike, and the cell under it A. A mod's nine-column bar adds I O over K L. Upgrades take X C V B N in the order they stand on the bar, so the first upgrade of any building is X. The fixed keys step aside on the bar: attack move and stop take their cells' letters like every other button, while Ctrl still forces an attack. A letter you give a button still beats its cell, and a letter can now go to one cell alone, which is what a transport needs: its passengers' exit buttons are all one button, and with a key per cell the third passenger out can be M while the rest keep the grid. Accept on the in-game Hotkeys tab keeps the grid and every per-cell key, and the tab's list shows the grid's letters.
-- W no longer starts a War Factory. The launcher had moved China's War Factory onto W, which Zero Hour already gives to Select all aircraft. With a tank selected and no aircraft on the map, W dropped the tank, the idle worker's bar took its place, and the same press let go on that bar's War Factory. The launcher now keeps the buttons it moves off every letter a keyboard command holds on its own, W, Q and the idle worker's I among them, and China's War Factory sits on D. Start the launcher once and it rewrites its part of the file. A letter you picked yourself stays where you put it.
+- W no longer starts a War Factory. The launcher had moved China's War Factory onto W, which Zero Hour already gives to Select all aircraft. With a tank selected and no aircraft on the map, W dropped the tank, the idle worker's bar took its place, and the same press let go on that bar's War Factory. The launcher now keeps the buttons it moves off every letter a keyboard command holds on its own, W, Q and the idle worker's I among them, and China's War Factory sits on D. Start the launcher once and it rewrites its part of the file. A letter you picked yourself stays where you put it. The game now settles it for any letter, yours and the grid's included: a key press does one thing. If a button on the bar wears the letter when the key goes down, the button gets it and the keyboard command on that letter waits; if none does, the command fires and the bar that comes up after it is left alone. A button that loses its letter to another, on the bar or the strip of general's powers, shows no letter instead of one that does nothing.
## The command bar moves to the middle
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/HotKey.h b/GeneralsMD/Code/GameEngine/Include/GameClient/HotKey.h
index 667126acf..0c21b877b 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/HotKey.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/HotKey.h
@@ -100,8 +100,10 @@ public:
virtual void reset( void ); ///< Reset
//-----------------------------------------------------------------------------------------------
- /// barSlot: win's command bar window slot (0-based), -1 off the bar; see playerButtonKey
- void addHotKey( GameWindow *win, const AsciiString& key, Int barSlot = -1 );
+ /** TRUE when win holds key afterwards, FALSE when a stronger or earlier claimant keeps it.
+ * barSlot: win's command bar window slot (0-based), -1 off the bar; see playerButtonKey.
+ * *displaced, when given, gets the window that held key until now, or NULL. */
+ Bool addHotKey( GameWindow *win, const AsciiString& key, Int barSlot = -1, GameWindow **displaced = NULL );
Bool executeHotKey( const AsciiString& key); // called fromt eh HotKeyTranslator
AsciiString searchHotKey( const AsciiString& label);
@@ -138,6 +140,18 @@ public:
/** Each of `count` command bar slots' upgradeRank: GUI_COMMAND_PLAYER_UPGRADE and
* GUI_COMMAND_OBJECT_UPGRADE count 0, 1, 2 in slot order, any other type is -1. */
static void commandBarUpgradeRanks( const Int *types, Int count, Int *ranks );
+ /** One keystroke does one thing (issue 128), by the key's scan code. On a press, strokeDown
+ * records whether a showing hotkey button holds its letter (barHoldsKey); while one does
+ * (strokeToBar) the command map lets the key alone, press and release. A press the command map
+ * did take is marked with strokeToCommand, and strokePressesBar on the release then says FALSE,
+ * so the button the press's command put on the bar is not pressed too. strokePressesBar ends
+ * the stroke. */
+ static void strokeDown( Int key, Bool barHolds );
+ static Bool strokeToBar( Int key );
+ static void strokeToCommand( Int key );
+ static Bool strokePressesBar( Int key );
+ /// a showing button in the hotkey map holds the letter this key types, pressed bare or with shift
+ static Bool barHoldsKey( Int key, Int modState );
/// CommandBarGrid in Hotkeys.ini: every command bar button keyed by its window slot
static Bool commandBarGrid( void );
static void setCommandBarGrid( Bool grid );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp
index 415afc824..e78733a14 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp
@@ -4589,12 +4589,21 @@ void ControlBar::setControlCommand( GameWindow *button, const CommandButton *com
const WideChar letter = standInOrder ? 0 : HotKeyManager::playerButtonKey( commandButton->getName(),
TheGameText ? TheGameText->fetchEnglishHotKey( commandButton->getTextLabel() ) : 0,
commandButton->getCommandType(), barSlot, barSlot >= 0 ? m_commandUpgradeRank[ barSlot ] : -1 );
- if( letter != 0 )
+ // the badge shows a letter only while this window holds the key: one that lost it to a
+ // stronger or earlier button shows none, and one that takes it wipes the badge it took it from
+ GameWindow *displaced = NULL;
+ const AsciiString key = letter != 0 ? HotKeyManager::nameOf( letter ) : AsciiString::TheEmptyString;
+ if( letter != 0 && TheHotKeyManager->addHotKey( button, key, barSlot, &displaced ) )
{
- TheHotKeyManager->addHotKey( button, HotKeyManager::nameOf( letter ), barSlot );
const WideChar text[ 2 ] = { letter, 0 };
GadgetButtonSetText( button, UnicodeString( text ) );
button->winSetStatus( WIN_STATUS_SHORTCUT_BUTTON );
+ const UnicodeString shown = displaced ? displaced->winGetText() : UnicodeString::TheEmptyString;
+ if( shown.getLength() == 1 && HotKeyManager::nameOf( shown.getCharAt( 0 ) ) == key )
+ {
+ GadgetButtonSetText( displaced, UnicodeString( u"" ) );
+ displaced->winClearStatus( WIN_STATUS_SHORTCUT_BUTTON );
+ }
}
else
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenuHotkeys.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenuHotkeys.cpp
index a5e204d66..56494f7d9 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenuHotkeys.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenuHotkeys.cpp
@@ -297,16 +297,28 @@ void findBars( void )
}
const Int forceSlot = ControlBar_forceAttackSlot( types, MAX_COMMANDS_PER_SET, ControlBar_forceFires( thing ) );
if( forceSlot >= 0 && force )
+ {
slots[ forceSlot ] = force;
+ types[ forceSlot ] = force->getCommandType();
+ }
+ // the windows as the bar fills them (ControlBar::populateCommand), so a slot here is the
+ // slot a Name@slot key and the grid's letters mean: a set's button past the bar's
+ // windows moves into an empty one
+ Bool windowed[ MAX_COMMANDS_PER_SET ];
+ Int shown[ MAX_COMMANDS_PER_SET ];
+ for( Int slot = 0; slot < MAX_COMMANDS_PER_SET; slot++ )
+ windowed[ slot ] = slot < BAR_SLOTS;
+ ControlBar_fitCommandSlots( types, windowed, MAX_COMMANDS_PER_SET, shown );
BarObject object;
object.building = thing->isKindOf( KINDOF_STRUCTURE );
Bool any = FALSE;
for( Int slot = 0; slot < BAR_SLOTS; slot++ )
{
- // a button with no picture is a leftover no player sees, and what only it builds is not reached
- object.slots[ slot ] = ( slots[ slot ] && slots[ slot ]->getButtonImage() ) ? slots[ slot ] : NULL;
+ object.slots[ slot ] = shown[ slot ] >= 0 ? slots[ shown[ slot ] ] : NULL;
+ // a button with no picture is a blank cell on the bar that still holds its key and its
+ // place among the upgrades; it is not listed, and what only it builds is not reached
const CommandButton *button = object.slots[ slot ];
- if( button == NULL )
+ if( button == NULL || button->getButtonImage() == NULL )
continue;
any = TRUE;
const GUICommandType type = button->getCommandType();
@@ -458,7 +470,7 @@ void fillButtons( void )
for( Int slot = 0; slot < BAR_SLOTS; slot++ )
{
const CommandButton *button = object->slots[ slot ];
- if( button == NULL )
+ if( button == NULL || button->getButtonImage() == NULL )
continue;
const UnicodeString key = buttonKeyText( object, slot, keys );
const Bool waiting = s_capture == CAPTURE_BUTTON && button == s_button;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
index 143aba29e..8a442c9ba 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
@@ -70,6 +70,7 @@
#include "GameClient/Drawable.h"
#include "GameClient/GadgetPushButton.h"
#include "GameClient/InGameUI.h"
+#include "GameClient/Shell.h"
//-----------------------------------------------------------------------------
// DEFINES ////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
@@ -78,6 +79,79 @@
// PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
+//-----------------------------------------------------------------------------
+/** The letter the player's key with this scan code types, the one a label's '&' and the bar's keys
+ * mean. The game's own key tables know four layouts and name every key by its US position, so on
+ * a Turkish keyboard the key marked i read as an apostrophe; on Windows the player's layout says. */
+static WideChar typedLetter( UnsignedByte scanCode )
+{
+ WideChar typed = TheKeyboard->getPrintableKey(scanCode, 0);
+#if defined(_WIN32)
+ HKL layout = GetKeyboardLayout(0);
+ const UINT virtualKey = MapVirtualKeyExW(scanCode, MAPVK_VSC_TO_VK, layout);
+ BYTE keys[256] = { 0 };
+ WCHAR out[4] = { 0 };
+ // a Cyrillic or other non-Latin letter never names a hotkey, so that player keeps the US position
+ if( virtualKey != 0 && ToUnicodeEx(virtualKey, scanCode, keys, out, 4, 0x4, layout) == 1 && out[0] < 0x0250 )
+ typed = (WideChar)out[0];
+#endif
+ return typed;
+}
+
+//-----------------------------------------------------------------------------
+/** One keystroke does one thing (issue 128). MetaEventTranslator fires a command map key on its
+ * press and this translator a command bar button on its release, so a letter both held used to do
+ * both: W selected every aircraft, the empty selection put a worker's bar up, and the release
+ * started his War Factory. The press decides now (HotKeyManager::strokeDown): a showing button on
+ * the letter takes the stroke and the command stands aside; otherwise the command keeps it as
+ * before and the release presses nothing. By scan code, the key both translators are handed. */
+enum { STROKE_KEYS = 256 };
+static Bool s_strokeToBar[ STROKE_KEYS ];
+static Bool s_strokeToCommand[ STROKE_KEYS ];
+
+void HotKeyManager::strokeDown( Int key, Bool barHolds )
+{
+ if( key < 0 || key >= STROKE_KEYS )
+ return;
+ s_strokeToBar[ key ] = barHolds;
+ s_strokeToCommand[ key ] = FALSE;
+}
+
+Bool HotKeyManager::strokeToBar( Int key )
+{
+ return key >= 0 && key < STROKE_KEYS && s_strokeToBar[ key ];
+}
+
+void HotKeyManager::strokeToCommand( Int key )
+{
+ if( key >= 0 && key < STROKE_KEYS )
+ s_strokeToCommand[ key ] = TRUE;
+}
+
+Bool HotKeyManager::strokePressesBar( Int key )
+{
+ if( key < 0 || key >= STROKE_KEYS )
+ return TRUE;
+ const Bool command = s_strokeToCommand[ key ];
+ s_strokeToBar[ key ] = s_strokeToCommand[ key ] = FALSE;
+ return !command;
+}
+
+Bool HotKeyManager::barHoldsKey( Int key, Int modState )
+{
+ // the release presses a button bare or with shift (a batch); a chord with Ctrl or Alt is the command's
+ if( TheHotKeyManager == NULL || TheKeyboard == NULL || key < 0 || key >= STROKE_KEYS || ( modState & ~SHIFT ) != 0 )
+ return FALSE;
+ if( TheShell && TheShell->isShellActive() )
+ return FALSE; // the menus have no command bar, whatever the map still names
+ const WideChar letter = typedLetter( (UnsignedByte)key );
+ if( letter == 0 )
+ return FALSE;
+ Bool pressable = FALSE;
+ GameWindow *win = TheHotKeyManager->findHotKey( nameOf( letter ), &pressable );
+ return win != NULL && !BitTest( win->winGetStatus(), WIN_STATUS_HIDDEN );
+}
+
//-----------------------------------------------------------------------------
GameMessageDisposition HotKeyTranslator::translateGameMessage(const GameMessage *msg)
{
@@ -86,7 +160,10 @@ GameMessageDisposition HotKeyTranslator::translateGameMessage(const GameMessage
if ( t == GameMessage::MSG_RAW_KEY_UP)
{
-
+ // a press the command map took presses no button on its release, whatever the bar shows now
+ if( !HotKeyManager::strokePressesBar( msg->getArgument(0)->integer ) )
+ return disp;
+
//char key = msg->getArgument(0)->integer;
Int keyState = msg->getArgument(1)->integer;
@@ -118,21 +195,7 @@ GameMessageDisposition HotKeyTranslator::translateGameMessage(const GameMessage
if( !repeatChord && ( keyState & KEY_STATE_PRESSED_WITH_CTRL_ALT ) )
return disp;
const UnsignedByte scanCode = (UnsignedByte)msg->getArgument(0)->integer;
- WideChar typed = TheKeyboard->getPrintableKey(scanCode, 0);
-#if defined(_WIN32)
- // The game's own key tables know four layouts and name every key by its US position, so on a
- // Turkish keyboard the key marked i read as an apostrophe. The letter the player's layout
- // puts on that key is the one a label's '&' means.
- {
- HKL layout = GetKeyboardLayout(0);
- const UINT virtualKey = MapVirtualKeyExW(scanCode, MAPVK_VSC_TO_VK, layout);
- BYTE keys[256] = { 0 };
- WCHAR out[4] = { 0 };
- // a Cyrillic or other non-Latin letter never names a hotkey, so that player keeps the US position
- if( virtualKey != 0 && ToUnicodeEx(virtualKey, scanCode, keys, out, 4, 0x4, layout) == 1 && out[0] < 0x0250 )
- typed = (WideChar)out[0];
- }
-#endif
+ const WideChar typed = typedLetter( scanCode );
if( repeatChord )
{
// straight to the bar rather than as a click: a repeated unit the bank cannot pay for
@@ -195,7 +258,7 @@ void HotKeyManager::reset( void )
static Int keyClaim( GameWindow *win, const AsciiString& key, Int barSlot );
//-----------------------------------------------------------------------------
-void HotKeyManager::addHotKey( GameWindow *win, const AsciiString& keyIn, Int barSlot )
+Bool HotKeyManager::addHotKey( GameWindow *win, const AsciiString& keyIn, Int barSlot, GameWindow **displaced )
{
AsciiString key = keyIn;
key.toLower();
@@ -206,13 +269,16 @@ void HotKeyManager::addHotKey( GameWindow *win, const AsciiString& keyIn, Int ba
if( it != m_hotKeyMap.end() && it->second.m_win != win && claim <= it->second.m_claim )
{
DEBUG_LOG(( "HOTKEYS: %s already on %s, not on %s\n", key.str(), it->second.m_win->winGetInstanceData()->m_decoratedNameString.str(), win->winGetInstanceData()->m_decoratedNameString.str() ));
- return;
+ return FALSE;
}
+ if( displaced )
+ *displaced = ( it != m_hotKeyMap.end() && it->second.m_win != win ) ? it->second.m_win : NULL;
HotKey newHK;
newHK.m_key.set(key);
newHK.m_win = win;
newHK.m_claim = claim;
m_hotKeyMap[key] = newHK;
+ return TRUE;
}
//-----------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp
index 801060fac..03d9152cd 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp
@@ -50,6 +50,7 @@
#include "GameClient/GUICallbacks.h"
#include "GameClient/DebugDisplay.h" // for AudioDebugDisplay
#include "GameClient/GameText.h"
+#include "GameClient/HotKey.h"
#include "GameClient/MetaEvent.h"
#include "GameLogic/GameLogic.h" // for TheGameLogic->getFrame()
@@ -515,6 +516,16 @@ GameMessageDisposition MetaEventTranslator::translateGameMessage(const GameMessa
key == KEY_LSHIFT || key == KEY_RSHIFT ||
key == KEY_LALT || key == KEY_RALT );
+ // one keystroke does one thing (HotKeyManager::strokeDown): a letter a showing button holds,
+ // as the press found the bar, is the button's on its release, and no command fires on it
+ Bool barKey = FALSE;
+ if( !isModifierKey )
+ {
+ if( t == GameMessage::MSG_RAW_KEY_DOWN && ( keyState & KEY_STATE_DOWN ) && !( keyState & KEY_STATE_AUTOREPEAT ) )
+ HotKeyManager::strokeDown( key, HotKeyManager::barHoldsKey( key, newModState ) );
+ barKey = HotKeyManager::strokeToBar( key );
+ }
+
if( isModifierKey && ( keyState & KEY_STATE_UP ) )
{
for( Int keyIndex = 0; keyIndex < NUM_MAPPABLE_KEYS; ++keyIndex )
@@ -607,7 +618,8 @@ GameMessageDisposition MetaEventTranslator::translateGameMessage(const GameMessa
// ok, now check for "normal" key transitions.
Int cmpModState = metaIgnoresShift(map) ? (newModState & ~SHIFT) : newModState;
if (
- map->m_key == key &&
+ !barKey &&
+ map->m_key == key &&
map->m_modState == cmpModState &&
(
(map->m_transition == UP && (keyState & KEY_STATE_UP)) ||
@@ -649,6 +661,9 @@ GameMessageDisposition MetaEventTranslator::translateGameMessage(const GameMessa
/*GameMessage *metaMsg =*/ TheMessageStream->appendMessage(map->m_meta);
//DEBUG_LOG(("Frame %d: MetaEventTranslator::translateGameMessage() normal: %s\n", TheGameLogic->getFrame(), findGameMessageNameByType(map->m_meta)));
}
+ // the press is the command's: its release presses no button
+ if( keyState & KEY_STATE_DOWN )
+ HotKeyManager::strokeToCommand( key );
disp = DESTROY_MESSAGE;
break;
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index ffb82315d..6a91eace5 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -5466,6 +5466,45 @@ TEST(hotkeys_command_bar_grid_keys_by_window_slot)
CHECK( Hotkeys_format( file, rows ).find( "CommandBarGrid" ) == std::string::npos );
}
+/* Issue 128: one keystroke does one thing, decided on the press. W selected every aircraft on its
+ press, the empty selection put a worker's bar up, and the release started his War Factory from W */
+TEST(hotkeys_one_keystroke_does_one_thing)
+{
+ enum { W = 0x11, Q = 0x10, E = 0x12 };
+
+ // a showing button holds W as the key goes down: the command map stands aside, the release presses it
+ HotKeyManager::strokeDown( W, TRUE );
+ CHECK( HotKeyManager::strokeToBar( W ) );
+ CHECK( HotKeyManager::strokePressesBar( W ) );
+ CHECK( !HotKeyManager::strokeToBar( W ) ); // the release ends the stroke
+
+ // nothing holds W on the press, so the command fires; the bar its selection brought up presses nothing
+ HotKeyManager::strokeDown( W, FALSE );
+ CHECK( !HotKeyManager::strokeToBar( W ) );
+ HotKeyManager::strokeToCommand( W );
+ CHECK( !HotKeyManager::strokePressesBar( W ) );
+ // and the next stroke starts clean
+ HotKeyManager::strokeDown( W, FALSE );
+ CHECK( HotKeyManager::strokePressesBar( W ) );
+
+ // a key with no command and no button on its press still presses what the bar shows on release
+ HotKeyManager::strokeDown( E, FALSE );
+ CHECK( HotKeyManager::strokePressesBar( E ) );
+
+ // strokes are per key: Q held for the bar while W went to a command
+ HotKeyManager::strokeDown( Q, TRUE );
+ HotKeyManager::strokeDown( W, FALSE );
+ HotKeyManager::strokeToCommand( W );
+ CHECK( HotKeyManager::strokeToBar( Q ) );
+ CHECK( HotKeyManager::strokePressesBar( Q ) );
+ CHECK( !HotKeyManager::strokePressesBar( W ) );
+
+ // no hotkey manager, no bar: a key out of range or with Ctrl never belongs to it
+ CHECK( !HotKeyManager::barHoldsKey( W, 0 ) );
+ CHECK( !HotKeyManager::strokeToBar( -1 ) );
+ CHECK( HotKeyManager::strokePressesBar( 4096 ) );
+}
+
TEST(metamap_format_chord_names_the_hold)
{
CHECK( MetaMap_formatChord( MK_NONE, CTRL, COMMANDUSABLE_GAME ) == UnicodeString( u"Ctrl" ) );