From b20e95a7884b0270417263b56dfcc09c178774e7 Mon Sep 17 00:00:00 2001
From: DarkGod <darkgod@net-core.org>
Date: Wed, 8 Jul 2020 17:15:17 +0200
Subject: [PATCH] WASD keys no longer affect negatively arrow keys WASD keys
 now have a repeat function if if keep them pressed, just like normal keys do
 (the timing may bit a bit different from other keys as it can't be handled by
 the OS itself)

---
 game/engines/default/data/keybinds/move.lua |  8 ++--
 game/modules/tome/class/Game.lua            | 49 ++++++++++++++-------
 2 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/game/engines/default/data/keybinds/move.lua b/game/engines/default/data/keybinds/move.lua
index b8a9406c6c..5112df28c2 100644
--- a/game/engines/default/data/keybinds/move.lua
+++ b/game/engines/default/data/keybinds/move.lua
@@ -138,25 +138,25 @@ defineAction{
 
 -- Character movements with classic WASD
 defineAction{
-	default = { "sym:_a:false:false:false:false", "sym:_LEFT:false:false:false:false" },
+	default = { "sym:_a:false:false:false:false" },
 	type = "MOVE_WASD_LEFT", updown=true,
 	group = _t"movement",
 	name = _t"Move left (WASD directions)",
 }
 defineAction{
-	default = { "sym:_d:false:false:false:false", "sym:_RIGHT:false:false:false:false" },
+	default = { "sym:_d:false:false:false:false" },
 	type = "MOVE_WASD_RIGHT", updown=true,
 	group = _t"movement",
 	name = _t"Move right (WASD directions)",
 }
 defineAction{
-	default = { "sym:_w:false:false:false:false", "sym:_UP:false:false:false:false" },
+	default = { "sym:_w:false:false:false:false" },
 	type = "MOVE_WASD_UP", updown=true,
 	group = _t"movement",
 	name = _t"Move up (WASD directions)",
 }
 defineAction{
-	default = { "sym:_s:false:false:false:false", "sym:_DOWN:false:false:false:false" },
+	default = { "sym:_s:false:false:false:false" },
 	type = "MOVE_WASD_DOWN", updown=true,
 	group = _t"movement",
 	name = _t"Move down (WASD directions)",
diff --git a/game/modules/tome/class/Game.lua b/game/modules/tome/class/Game.lua
index cc1e22bbd8..d619645f71 100644
--- a/game/modules/tome/class/Game.lua
+++ b/game/modules/tome/class/Game.lua
@@ -1937,6 +1937,13 @@ function _M:display(nb_keyframes)
 		self.full_fbo:toScreen(0, 0, self.w, self.h, self.full_fbo_shader.shad)
 	end
 
+	if self.wasd_state and self.wasd_state.cnt > 0 then
+		self.wasd_state.cd = self.wasd_state.cd - nb_keyframes
+		if self.wasd_state.cd <= 0 then			
+			self.wasd_state.cd = self.wasd_state.base_cd
+			self:onTickEnd(function() self:executeWASD() end)
+		end
+	end
 end
 
 --- Called when a dialog is registered to appear on screen
@@ -2492,27 +2499,39 @@ do return end
 	self.key:setCurrent()
 end
 
+function _M:executeWASD()
+	local ws = self.wasd_state
+	if     ws.left  and ws.up   then self.key:triggerVirtual("MOVE_LEFT_UP")
+	elseif ws.right and ws.up   then self.key:triggerVirtual("MOVE_RIGHT_UP")
+	elseif ws.left  and ws.down then self.key:triggerVirtual("MOVE_LEFT_DOWN")
+	elseif ws.right and ws.down then self.key:triggerVirtual("MOVE_RIGHT_DOWN")
+	elseif ws.right             then self.key:triggerVirtual("MOVE_RIGHT")
+	elseif ws.left              then self.key:triggerVirtual("MOVE_LEFT")
+	elseif ws.up                then self.key:triggerVirtual("MOVE_UP")
+	elseif ws.down              then self.key:triggerVirtual("MOVE_DOWN")
+	end
+	ws.has_executed_once = true
+end
+
 function _M:setupWASD()
-	self.wasd_state = {}
-	local function handle_wasd()
+	self.wasd_state = {cnt=0, cd=12, base_cd=3}
+	local function update_wasd()
 		local ws = self.wasd_state
-		if     ws.left  and ws.up   then self.key:triggerVirtual("MOVE_LEFT_UP")
-		elseif ws.right and ws.up   then self.key:triggerVirtual("MOVE_RIGHT_UP")
-		elseif ws.left  and ws.down then self.key:triggerVirtual("MOVE_LEFT_DOWN")
-		elseif ws.right and ws.down then self.key:triggerVirtual("MOVE_RIGHT_DOWN")
-		elseif ws.right             then self.key:triggerVirtual("MOVE_RIGHT")
-		elseif ws.left              then self.key:triggerVirtual("MOVE_LEFT")
-		elseif ws.up                then self.key:triggerVirtual("MOVE_UP")
-		elseif ws.down              then self.key:triggerVirtual("MOVE_DOWN")
-		end
+		local old_cnt = ws.cnt
+		ws.cnt = 0
+		if ws.left then ws.cnt = ws.cnt + 1 end
+		if ws.right then ws.cnt = ws.cnt + 1 end
+		if ws.up then ws.cnt = ws.cnt + 1 end
+		if ws.down then ws.cnt = ws.cnt + 1 end
+		if ws.cnt == 0 then ws.has_executed_once = false ws.cd = ws.cd + 3 * ws.base_cd end
 	end
 
 	if config.settings.tome.use_wasd then
 		self.key:addBinds{
-			MOVE_WASD_UP    = function(sym, ctrl, shift, alt, meta, unicode, isup, key) if isup then handle_wasd() end self.wasd_state.up = not isup and true or false end,
-			MOVE_WASD_DOWN  = function(sym, ctrl, shift, alt, meta, unicode, isup, key) if isup then handle_wasd() end self.wasd_state.down = not isup and true or false end,
-			MOVE_WASD_LEFT  = function(sym, ctrl, shift, alt, meta, unicode, isup, key) if isup then handle_wasd() end self.wasd_state.left = not isup and true or false end,
-			MOVE_WASD_RIGHT = function(sym, ctrl, shift, alt, meta, unicode, isup, key) if isup then handle_wasd() end self.wasd_state.right = not isup and true or false end,
+			MOVE_WASD_UP    = function(sym, ctrl, shift, alt, meta, unicode, isup, key) if isup and not self.wasd_state.has_executed_once then self:executeWASD() end self.wasd_state.up = not isup and true or false update_wasd() end,
+			MOVE_WASD_DOWN  = function(sym, ctrl, shift, alt, meta, unicode, isup, key) if isup and not self.wasd_state.has_executed_once then self:executeWASD() end self.wasd_state.down = not isup and true or false update_wasd() end,
+			MOVE_WASD_LEFT  = function(sym, ctrl, shift, alt, meta, unicode, isup, key) if isup and not self.wasd_state.has_executed_once then self:executeWASD() end self.wasd_state.left = not isup and true or false update_wasd() end,
+			MOVE_WASD_RIGHT = function(sym, ctrl, shift, alt, meta, unicode, isup, key) if isup and not self.wasd_state.has_executed_once then self:executeWASD() end self.wasd_state.right = not isup and true or false update_wasd() end,
 		}
 	else
 		self.key:removeBind("MOVE_WASD_UP")
-- 
GitLab