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 answer to whether a press and a release were a click

Olcay Seygan40800b7b7a78
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ClickTolerance.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ClickTolerance.h
new file mode 100644
index 000000000..0f2c2204a
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ClickTolerance.h
@@ -0,0 +1,56 @@
+/*
+** 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/>.
+*/
+
+// ClickTolerance.h ///////////////////////////////////////////////////////////////////////////////
+// Whether a press and a release were close enough to count as a click rather than a drag.
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#ifndef _CLICK_TOLERANCE_H_
+#define _CLICK_TOLERANCE_H_
+
+#include "Lib/BaseType.h"
+
+/**
+ * A click is a press and a release that are close together in three senses: in time, in where
+ * the pointer sat on the screen, and in where the camera sat in the world. The third one is
+ * what a fast scroll breaks - the pointer can hold still while the world slides under it, and
+ * the two ends of the click then mean different places on the map.
+ *
+ * Distances are compared as radii. Testing each screen axis against the tolerance separately
+ * let a diagonal drag of 1.41 times the tolerance still read as a click.
+ */
+inline Bool ClickTolerance_isClick( Real screenDeltaX, Real screenDeltaY, Real cameraDelta,
+ UnsignedInt elapsedMilliseconds,
+ Real screenTolerance, Real cameraTolerance,
+ UnsignedInt millisecondTolerance )
+{
+ if( elapsedMilliseconds > millisecondTolerance )
+ return FALSE;
+
+ if( screenDeltaX * screenDeltaX + screenDeltaY * screenDeltaY > screenTolerance * screenTolerance )
+ return FALSE;
+
+ if( cameraDelta > cameraTolerance )
+ return FALSE;
+
+ return TRUE;
+}
+
+#endif // _CLICK_TOLERANCE_H_
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/CommandXlat.h b/GeneralsMD/Code/GameEngine/Include/GameClient/CommandXlat.h
index 411c5dd12..99c112b7d 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/CommandXlat.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/CommandXlat.h
@@ -58,6 +58,8 @@ private:
ICoord2D m_mouseRightDragLift; // the location of a possible mouse drag end
UnsignedInt m_mouseRightDown; // when the mouse down happened
UnsignedInt m_mouseRightUp; // when the mouse up happened
+ Coord3D m_mouseRightDragAnchorCamera; // where the camera was at the press
+ Coord3D m_mouseRightDragLiftCamera; // where the camera was at the release
// latched the moment the right button goes down: whether this drag is drawing a formation line.
// Latched rather than asked again, so a selection that changes mid-drag cannot abandon a line
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/Mouse.h b/GeneralsMD/Code/GameEngine/Include/GameClient/Mouse.h
index cdb33363b..1ff85c34c 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/Mouse.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/Mouse.h
@@ -294,7 +294,11 @@ public:
void mouseNotifyResolutionChange(void);
- Bool isClick(const ICoord2D *anchor, const ICoord2D *dest, UnsignedInt previousMouseClick, UnsignedInt currentMouseClick);
+ /// True when press and release are close enough in time, on screen and in the world to be a click.
+ /// Pass NULL for both camera positions where the camera cannot have moved between the two.
+ Bool isClick(const ICoord2D *anchor, const ICoord2D *dest,
+ const Coord3D *cameraAtAnchor, const Coord3D *cameraAtDest,
+ UnsignedInt previousMouseClick, UnsignedInt currentMouseClick);
AsciiString m_tooltipFontName; ///< tooltip font
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
index 15a1d51b4..a23298f67 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
@@ -682,12 +682,13 @@ void GameClient::update( void )
if( TheMouse )
TheMouse->setVisibility( TRUE );
- // redraw all views, update the GUI
+ // Update before drawing, the way every other pass does it. The other order drew the
+ // frame the update was about to change, so the intro was always one frame behind.
{
- TheDisplay->DRAW();
+ TheDisplay->UPDATE();
}
{
- TheDisplay->UPDATE();
+ TheDisplay->DRAW();
}
return;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/Input/Mouse.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/Input/Mouse.cpp
index 7bc877ec8..3044b0c10 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/Input/Mouse.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/Input/Mouse.cpp
@@ -35,6 +35,7 @@
#include "Common/GlobalData.h"
#include "Common/INI.h"
+#include "GameClient/ClickTolerance.h"
#include "GameClient/Display.h"
#include "GameClient/DisplayStringManager.h"
#include "GameClient/GameClient.h"
@@ -393,22 +394,26 @@ void Mouse::checkForDrag( void )
//-------------------------------------------------------------------------------------------------
/** Check for mouse click, using allowed drag forgiveness */
//-------------------------------------------------------------------------------------------------
-Bool Mouse::isClick(const ICoord2D *anchor, const ICoord2D *dest, UnsignedInt previousMouseClick, UnsignedInt currentMouseClick)
+Bool Mouse::isClick(const ICoord2D *anchor, const ICoord2D *dest,
+ const Coord3D *cameraAtAnchor, const Coord3D *cameraAtDest,
+ UnsignedInt previousMouseClick, UnsignedInt currentMouseClick)
{
- ICoord2D delta;
- delta.x = anchor->x - dest->x;
- delta.y = anchor->y - dest->y;
-
-
- // if the mouse hasn't moved further than the tolerance distance
- // or the click took less than the tolerance duration
- if ( abs(delta.x) > m_dragTolerance
- || abs(delta.y) > m_dragTolerance
- || currentMouseClick - previousMouseClick > m_dragToleranceMS)
+ Real cameraDelta = 0.0f;
+ if (cameraAtAnchor && cameraAtDest)
{
- return FALSE;
+ Coord3D delta = *cameraAtDest;
+ delta.sub(cameraAtAnchor);
+ cameraDelta = delta.length();
}
- return TRUE;
+
+ return ClickTolerance_isClick(
+ (Real)(anchor->x - dest->x),
+ (Real)(anchor->y - dest->y),
+ cameraDelta,
+ currentMouseClick - previousMouseClick,
+ (Real)m_dragTolerance,
+ (Real)m_dragTolerance3D,
+ m_dragToleranceMS );
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp
index 506072636..4a5829c4c 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp
@@ -1399,6 +1399,8 @@ CommandTranslator::CommandTranslator() :
m_mouseRightDragAnchor.y = 0;
m_mouseRightDragLift.x = 0;
m_mouseRightDragLift.y = 0;
+ m_mouseRightDragAnchorCamera.zero();
+ m_mouseRightDragLiftCamera.zero();
}
//====================================================================================
@@ -3917,6 +3919,7 @@ GameMessageDisposition CommandTranslator::translateGameMessage(const GameMessage
// 2) Time has exceeded the time which we allow for this to be a click.
m_mouseRightDragAnchor = msg->getArgument( 0 )->pixel;
m_mouseRightDown = (UnsignedInt) msg->getArgument( 2 )->integer;
+ TheTacticalView->getPosition( &m_mouseRightDragAnchorCamera );
m_formationDragArmed = isFormationDragArmed();
@@ -3953,6 +3956,7 @@ GameMessageDisposition CommandTranslator::translateGameMessage(const GameMessage
// register this event for determining if the click was fast or short enough not to be a drag
m_mouseRightDragLift = msg->getArgument( 0 )->pixel;
m_mouseRightUp = (UnsignedInt) msg->getArgument( 2 )->integer;
+ TheTacticalView->getPosition( &m_mouseRightDragLiftCamera );
if( m_formationDragArmed )
{
@@ -4005,7 +4009,9 @@ GameMessageDisposition CommandTranslator::translateGameMessage(const GameMessage
//a bug where you couldn't cancel the sneak attack mode via right click. This only happened when you
//didn't have anything selected which is possible via the shortcut bar. Normally, it would get deselected
//via the deselect drawable code.
- if( TheMouse->isClick(&m_mouseRightDragAnchor, &m_mouseRightDragLift, m_mouseRightDown, m_mouseRightUp) )
+ if( TheMouse->isClick(&m_mouseRightDragAnchor, &m_mouseRightDragLift,
+ &m_mouseRightDragAnchorCamera, &m_mouseRightDragLiftCamera,
+ m_mouseRightDown, m_mouseRightUp) )
{
TheInGameUI->placeBuildAvailable( NULL, NULL );
}
@@ -4038,7 +4044,9 @@ GameMessageDisposition CommandTranslator::translateGameMessage(const GameMessage
// The right button is the order button, always. It used to depend on UseAlternateMouse,
// which is gone: a click here commands, a drag draws a formation line, and neither of
// them scrolls.
- if (TheMouse->isClick(&m_mouseRightDragAnchor, &m_mouseRightDragLift, m_mouseRightDown, m_mouseRightUp))
+ if (TheMouse->isClick(&m_mouseRightDragAnchor, &m_mouseRightDragLift,
+ &m_mouseRightDragAnchorCamera, &m_mouseRightDragLiftCamera,
+ m_mouseRightDown, m_mouseRightUp))
{
Bool isPoint = (msg->getArgument(0)->pixelRegion.height() == 0 && msg->getArgument(0)->pixelRegion.width() == 0);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp
index f86d7eff0..f751d9877 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp
@@ -1105,38 +1105,17 @@ GameMessageDisposition SelectionTranslator::translateGameMessage(const GameMessa
//-----------------------------------------------------------------------------
case GameMessage::MSG_RAW_MOUSE_RIGHT_BUTTON_UP:
{
- ICoord2D delta, pixel;
- UnsignedInt currentTime;
Coord3D cameraPos;
-
TheTacticalView->getPosition(&cameraPos);
- cameraPos.sub(&m_deselectDownCameraPosition);
-
- pixel = msg->getArgument( 0 )->pixel;
- currentTime = (UnsignedInt) msg->getArgument( 2 )->integer;
- delta.x = m_deselectFeedbackAnchor.x - pixel.x;
- delta.y = m_deselectFeedbackAnchor.y - pixel.y;
-
- Bool isClick = TRUE;
- if (isClick &&
- abs(delta.x) > TheMouse->m_dragTolerance ||
- abs(delta.y) > TheMouse->m_dragTolerance)
- {
- isClick = FALSE;
- }
+ const ICoord2D pixel = msg->getArgument( 0 )->pixel;
+ const UnsignedInt currentTime = (UnsignedInt) msg->getArgument( 2 )->integer;
- if (isClick &&
- currentTime - m_lastClick > TheMouse->m_dragToleranceMS)
- {
- isClick = FALSE;
- }
-
- if (isClick &&
- cameraPos.length() > TheMouse->m_dragTolerance3D)
- {
- isClick = FALSE;
- }
+ // This test used to be written out here, on its own, next to a second copy of it in
+ // the command translator that had no camera term at all. One function answers it now.
+ const Bool isClick = TheMouse->isClick( &m_deselectFeedbackAnchor, &pixel,
+ &m_deselectDownCameraPosition, &cameraPos,
+ m_lastClick, currentTime );
// right click behavior (not right drag)
if (isClick)
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 04f03ff0b..92c0b2f67 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -42,6 +42,7 @@
#include "GameNetwork/LinkSimulation.h"
#include "Common/Energy.h"
#include "Common/RandomValue.h"
+#include "GameClient/ClickTolerance.h"
#include "GameLogic/ScenarioDrill.h"
#include "Common/ControlServer.h"
#include "GameLogic/LogicRandomValue.h"
@@ -10365,3 +10366,37 @@ TEST(control_server_answers_the_rfc_handshake_example)
char tooSmall[ 8 ];
CHECK( !ControlServer_computeAcceptKey( "dGhlIHNhbXBsZSBub25jZQ==", tooSmall, sizeof( tooSmall ) ) );
}
+
+/* Telling a click from a drag.
+ *
+ * Three terms, and each of them has been wrong at some point. The screen distance was compared one
+ * axis at a time, which let a diagonal drag of 1.41 times the tolerance pass as a click. The camera
+ * term existed in the selection translator and was simply missing from the command translator, so
+ * a right click that scrolled the map still counted as a click there.
+ */
+TEST(click_tolerance_is_a_radius_in_time_screen_and_world)
+{
+ const Real screenTolerance = 10.0f;
+ const Real cameraTolerance = 20.0f;
+ const UnsignedInt msTolerance = 250;
+
+ // dead centre, no time, no camera movement
+ CHECK( ClickTolerance_isClick( 0.0f, 0.0f, 0.0f, 0, screenTolerance, cameraTolerance, msTolerance ) );
+
+ // held too long
+ CHECK( !ClickTolerance_isClick( 0.0f, 0.0f, 0.0f, 251, screenTolerance, cameraTolerance, msTolerance ) );
+ CHECK( ClickTolerance_isClick( 0.0f, 0.0f, 0.0f, 250, screenTolerance, cameraTolerance, msTolerance ) );
+
+ // on the axes the tolerance is exactly the tolerance
+ CHECK( ClickTolerance_isClick( 10.0f, 0.0f, 0.0f, 0, screenTolerance, cameraTolerance, msTolerance ) );
+ CHECK( !ClickTolerance_isClick( 11.0f, 0.0f, 0.0f, 0, screenTolerance, cameraTolerance, msTolerance ) );
+ CHECK( ClickTolerance_isClick( 0.0f, -10.0f, 0.0f, 0, screenTolerance, cameraTolerance, msTolerance ) );
+
+ // the diagonal is what the old per-axis test got wrong: 8,8 is 11.3 away, outside a radius of 10
+ CHECK( !ClickTolerance_isClick( 8.0f, 8.0f, 0.0f, 0, screenTolerance, cameraTolerance, msTolerance ) );
+ CHECK( ClickTolerance_isClick( 7.0f, 7.0f, 0.0f, 0, screenTolerance, cameraTolerance, msTolerance ) );
+
+ // the world moved under a pointer that did not
+ CHECK( ClickTolerance_isClick( 0.0f, 0.0f, 20.0f, 0, screenTolerance, cameraTolerance, msTolerance ) );
+ CHECK( !ClickTolerance_isClick( 0.0f, 0.0f, 20.5f, 0, screenTolerance, cameraTolerance, msTolerance ) );
+}