Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic gesture detection to delete commands #6470

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion engine/User.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
---@field targetId? EntityId
---@field blueprintId? UnitId
---@field commandType number
---@field commandId number

---@alias SubmergeStatus
---| -1 # submerged
Expand Down Expand Up @@ -480,7 +481,7 @@ end

---
---@param key string
---@return string[]
---@return string
function GetOptions(key)
end

Expand Down
30 changes: 30 additions & 0 deletions lua/options/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,36 @@ options = {
end
end,
},

{
title = 'Gesture control',
type = 'header',

-- these are expected everywhere
default = '',
key = '',
},

{
title = "<LOC OPTIONS_0315>Gesture to delete commands",
key = 'gesture_delete_commands',
type = 'toggle',
default = 'off',
custom = {
states = {
{ text = "<LOC _Off>", key = 'off' },
{ text = "<LOC gesture_build>Build commands only", key = 'build' },
{ text = "<LOC gesture_build>Engineer related commands only", key = 'engineering' },
{ text = "<LOC gesture_build>All commands", key = 'all' },
},
},

set = function(control, value, startup)
if SessionIsActive() then
import("/lua/ui/game/cursor/deletebuildcommand.lua").SetGestureDetectionCommandType(value)
end
end,
},
},
},
video = {
Expand Down
150 changes: 150 additions & 0 deletions lua/ui/game/cursor/DeleteBuildCommand.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
--******************************************************************************************************
--** Copyright (c) 2024 FAForever
--**
--** Permission is hereby granted, free of charge, to any person obtaining a copy
--** of this software and associated documentation files (the "Software"), to deal
--** in the Software without restriction, including without limitation the rights
--** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
--** copies of the Software, and to permit persons to whom the Software is
--** furnished to do so, subject to the following conditions:
--**
--** The above copyright notice and this permission notice shall be included in all
--** copies or substantial portions of the Software.
--**
--** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
--** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
--** SOFTWARE.
--******************************************************************************************************

---@alias UIGestureDetectionCommandType 'off' | 'build' | 'engineering' | 'all'

---@type TrashBag
local ModuleTrash = TrashBag()

--- See also: https://github.com/FAForever/FA-Binary-Patches/pull/22
local BuildCommandTypes = {
[8] = true, -- BuildMobile
[9] = true, -- BuildAssist
}

--- See also: https://github.com/FAForever/FA-Binary-Patches/pull/22
local EngineeringCommandTypes = {
[8] = true, -- BuildMobile
[9] = true, -- BuildAssist
[15] = true, -- Guard
[19] = true, -- Reclaim
[20] = true, -- Repair
[21] = true, -- Capture
[32] = true, -- Sacrifice
}

---@type UIGestureDetectionCommandType
local GestureDetectionCommandType = import("/lua/user/prefs.lua").GetOption('gesture_delete_commands')

---@type number
local GestureDetectionSuccessionThreshold = 3

--- Starts a basic gesture detection thread to delete a (build) command
local GestureDetectionThread = function()

-- local scope for performance
local WaitFrames = WaitFrames
local GetHighlightCommand = GetHighlightCommand
local GetGameTimeSeconds = GetGameTimeSeconds
local DeleteCommand = DeleteCommand

-- internal state
local gestureStart = 0
local gestureTargetId = nil
local gestureSuccessive = 0
local oldCommand = nil

while true do
WaitFrames(1)

-- check for early exit
if GestureDetectionCommandType == 'off' then
return
end

-- retrieve a command
local command = GetHighlightCommand()

-- only register the event when we just started hovering over a command
if command and not oldCommand then
-- check for early exit
if GestureDetectionCommandType == 'build' and not BuildCommandTypes[command.commandType] then
gestureTargetId = nil
continue
elseif GestureDetectionCommandType == 'engineering' and not EngineeringCommandTypes[command.commandType] then
gestureTargetId = nil
continue
end

-- keep track of the gesture
local gameTimeSeconds = GetGameTimeSeconds()
if command.commandId == gestureTargetId and gameTimeSeconds - gestureStart < 0.5 then
gestureSuccessive = gestureSuccessive + 1
gestureStart = gameTimeSeconds
else
gestureStart = gameTimeSeconds
gestureTargetId = command.commandId
gestureSuccessive = 0
end

if gestureSuccessive == GestureDetectionSuccessionThreshold then
DeleteCommand(gestureTargetId)
gestureTargetId = 0
end
end

oldCommand = command
end
end

--- Starts the gesture detection thread. Function is idempotent.
StartGestureDetectionThread = function()
ModuleTrash:Destroy()

-- logic requires an active session
if SessionIsActive() then
return
end

ModuleTrash:Add(ForkThread(GestureDetectionThread))
end

--- Sets the gesture detection command type and attempts to restart the gesture detection thread.
---@param commandType UIGestureDetectionCommandType
SetGestureDetectionCommandType = function(commandType)
GestureDetectionCommandType = commandType
StartGestureDetectionThread()
end

-------------------------------------------------------------------------------
--#region Debugging

--- Called by the module manager when this module is reloaded
---@param newModule any
function __moduleinfo.OnReload(newModule)
newModule.StartGestureDetectionThread()
end

--- Called by the module manager when this module becomes dirty
function __moduleinfo.OnDirty()
ModuleTrash:Destroy()

-- trigger a reload
ForkThread(
function()
WaitSeconds(1.0)
import(__moduleinfo.name)
end
)
end

--#endregion
1 change: 1 addition & 0 deletions lua/ui/game/gamemain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ function CreateUI(isReplay)
import("/lua/system/performance.lua")
import("/lua/ui/game/cursor/depth.lua")
import("/lua/ui/game/cursor/hover.lua")
import("/lua/ui/game/cursor/DeleteBuildCommand.lua").StartGestureDetectionThread()

-- casting tools

Expand Down