Replace input.js doubleclick hack and ease doubleclicking on moving units.

Differential Revision: https://code.wildfiregames.com/D326
Patch By: causative
Refs #4414

This was SVN commit r19444.
This commit is contained in:
elexis 2017-04-22 01:24:03 +00:00
parent 175cded64a
commit 8f36ca47a1
2 changed files with 20 additions and 34 deletions

View file

@ -42,12 +42,8 @@ var mouseIsOverObject = false;
// Number of pixels the mouse can move before the action is considered a drag
var maxDragDelta = 4;
// Time in milliseconds in which a double click is recognized
const doubleClickTime = 500;
var doubleClickTimer = 0;
var doubleClicked = false;
// Store the previously clicked entity - ensure a double/triple click happens on the same entity
var prevClickedEntity = 0;
// Store the clicked entity on mousedown or mouseup
var clickedEntity = 0;
// Same double-click behaviour for hotkey presses
const doublePressTime = 500;
@ -862,6 +858,10 @@ function handleInputAfterGui(ev)
{
dragStart = [ ev.x, ev.y ];
inputState = INPUT_SELECTING;
// If a single click occured, reset the clickedEntity.
// Also set it if we're double/triple clicking and missed the unit earlier.
if (ev.clicks == 1 || clickedEntity == INVALID_ENTITY)
clickedEntity = Engine.PickEntityAtPoint(ev.x, ev.y);
return true;
}
else if (ev.button == SDL_BUTTON_RIGHT)
@ -966,9 +966,9 @@ function handleInputAfterGui(ev)
case "mousebuttonup":
if (ev.button == SDL_BUTTON_LEFT)
{
var ents = [];
var selectedEntity = Engine.PickEntityAtPoint(ev.x, ev.y);
if (selectedEntity == INVALID_ENTITY)
if (clickedEntity == INVALID_ENTITY)
clickedEntity = Engine.PickEntityAtPoint(ev.x, ev.y);
if (clickedEntity == INVALID_ENTITY)
{
if (!Engine.HotkeyIsPressed("selection.add") && !Engine.HotkeyIsPressed("selection.remove"))
{
@ -979,15 +979,14 @@ function handleInputAfterGui(ev)
return true;
}
var now = new Date();
// If camera following and we select different unit, stop
if (Engine.GetFollowedEntity() != selectedEntity)
{
if (Engine.GetFollowedEntity() != clickedEntity)
Engine.CameraFollow(0);
}
if (now.getTime() - doubleClickTimer < doubleClickTime && selectedEntity == prevClickedEntity)
var ents = [];
if (ev.clicks == 1)
ents = [clickedEntity];
else
{
// Double click or triple click has occurred
var showOffscreen = Engine.HotkeyIsPressed("selection.offscreen");
@ -995,39 +994,25 @@ function handleInputAfterGui(ev)
var templateToMatch;
// Check for double click or triple click
if (!doubleClicked)
if (ev.clicks == 2)
{
// If double click hasn't already occurred, this is a double click.
// Select similar units regardless of rank
templateToMatch = GetEntityState(selectedEntity).identity.selectionGroupName;
templateToMatch = GetEntityState(clickedEntity).identity.selectionGroupName;
if (templateToMatch)
matchRank = false;
else
// No selection group name defined, so fall back to exact match
templateToMatch = GetEntityState(selectedEntity).template;
templateToMatch = GetEntityState(clickedEntity).template;
doubleClicked = true;
// Reset the timer so the user has an extra period 'doubleClickTimer' to do a triple-click
doubleClickTimer = now.getTime();
}
else
// Double click has already occurred, so this is a triple click.
// Triple click
// Select units matching exact template name (same rank)
templateToMatch = GetEntityState(selectedEntity).template;
templateToMatch = GetEntityState(clickedEntity).template;
// TODO: Should we handle "control all units" here as well?
ents = Engine.PickSimilarPlayerEntities(templateToMatch, showOffscreen, matchRank, false);
}
else
{
// It's single click right now but it may become double or triple click
doubleClicked = false;
doubleClickTimer = now.getTime();
prevClickedEntity = selectedEntity;
// We only want to include the first picked unit in the selection
ents = [selectedEntity];
}
// Update the list of selected units
if (Engine.HotkeyIsPressed("selection.add"))

View file

@ -102,6 +102,7 @@ template<> void ScriptInterface::ToJSVal<SDL_Event_>(JSContext* cx, JS::MutableH
SET(obj, "state", (int)val.ev.button.state);
SET(obj, "x", (int)val.ev.button.x);
SET(obj, "y", (int)val.ev.button.y);
SET(obj, "clicks", (int)val.ev.button.clicks);
break;
}
case SDL_HOTKEYDOWN: