From 5d0dab3093089d196f8697c34b9a05f8ce3fe701 Mon Sep 17 00:00:00 2001 From: Helwor Date: Sun, 16 Jun 2024 13:20:12 +0200 Subject: [PATCH 1/3] Implement up/down/escape behaviour as ingame Browse through message history with up/down, escape clear the input field. Similar behaviour as ingame --- LuaMenu/widgets/chobby/components/console.lua | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/LuaMenu/widgets/chobby/components/console.lua b/LuaMenu/widgets/chobby/components/console.lua index 03fb2364c..412bb0253 100644 --- a/LuaMenu/widgets/chobby/components/console.lua +++ b/LuaMenu/widgets/chobby/components/console.lua @@ -5,6 +5,12 @@ function Console:init(channelName, sendMessageListener, noHistoryLoad, onResizeF self.showDate = true self.dateFormat = "%H:%M" + self.sentMsgHistory = {} + self.sentMsgHistoryCount = 0 + self.sentMsgHistoryMax = 500 + self.sentMsgHistoryCursor = 1 + self.currentMsgBuffer = "" + self.channelName = channelName local onResize @@ -89,24 +95,73 @@ function Console:init(channelName, sendMessageListener, noHistoryLoad, onResizeF end Configuration:AddListener("OnConfigurationChange", onConfigurationChange) + local keyUP, keyDOWN, keyENTER, keyKP_ENTER, keyESCAPE, keyTAB = + Spring.GetKeyCode("up"), + Spring.GetKeyCode("down"), + Spring.GetKeyCode("enter"), + Spring.GetKeyCode("numpad_enter"), + Spring.GetKeyCode("escape"), + Spring.GetKeyCode("tab") + self.ebInputText.KeyPress = function(something, key, ...) - if key == Spring.GetKeyCode("tab") then + if key == keyTAB then self:Autocomplete(self.ebInputText.text) return false else self.subword = nil + + local up, down = key == keyUP, key == keyDOWN + local newtext, block = false, false + if up or down then + local cursor = self.sentMsgHistoryCursor + local count = self.sentMsgHistoryCount + cursor = cursor + (up and -1 or 1) + if cursor == count + 2 then + -- skip, the msg history has not yet be browsed up, nothing to do + elseif cursor == 0 then + -- skip, we've already gone to the first msg in history + else + if cursor == count + 1 then -- we gone back to the current text + newtext = self.currentMsgBuffer + self.currentMsgBuffer = "" + else + if up and cursor == count then -- we're starting going into history, saving the current text + self.currentMsgBuffer = self.ebInputText.text + end + newtext = self.sentMsgHistory[cursor] + end + self.sentMsgHistoryCursor = cursor + end + elseif key == keyESCAPE then + if self.ebInputText.text ~= "" then + -- clearing field and resetting cursor pos + self.sentMsgHistoryCursor = self.sentMsgHistoryCount + 1 + newtext = "" + block = true -- block the action of escape that would leave the current lobby tab + end + end + if newtext then + self.ebInputText:SetText(newtext) + self.ebInputText.cursor = #self.ebInputText.text + 1 + self.ebInputText:Invalidate() + return block + end + return Chili.EditBox.KeyPress(something, key, ...) end end + + self.ebInputText.OnKeyPress = { function(obj, key, mods, ...) - if key == Spring.GetKeyCode("enter") or - key == Spring.GetKeyCode("numpad_enter") then + if key == keyENTER or + key == keyKP_ENTER then self:SendMessage() return true end end } + self.fakeImage = Image:New { x = 0, y = 0, bottom = 0, right = 0, @@ -186,6 +241,17 @@ end function Console:SendMessage() if self.ebInputText.text ~= "" then message = self.ebInputText.text + + local count = self.sentMsgHistoryCount + if count == self.sentMsgHistoryMax then + table.remove(self.sentMsgHistory, 1) + else + count = count + 1 + self.sentMsgHistoryCount = count + end + self.sentMsgHistory[count] = message + self.sentMsgHistoryCursor = count + 1 + if self.listener then self.listener(message) end From 9e3a3fb65c35cc219792413a4721f7fe7775ab6c Mon Sep 17 00:00:00 2001 From: Helwor Date: Wed, 19 Jun 2024 12:47:34 +0200 Subject: [PATCH 2/3] don't add to history message coming from history --- LuaMenu/widgets/chobby/components/console.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/LuaMenu/widgets/chobby/components/console.lua b/LuaMenu/widgets/chobby/components/console.lua index 412bb0253..9a0b66ce0 100644 --- a/LuaMenu/widgets/chobby/components/console.lua +++ b/LuaMenu/widgets/chobby/components/console.lua @@ -242,15 +242,20 @@ function Console:SendMessage() if self.ebInputText.text ~= "" then message = self.ebInputText.text + local cursor = self.sentMsgHistoryCursor local count = self.sentMsgHistoryCount - if count == self.sentMsgHistoryMax then - table.remove(self.sentMsgHistory, 1) + if cursor < count + 1 and self.sentMsgHistory[cursor] == self.ebInputText.text then + -- skip, don't add to history message coming from history / keep cursor at current else - count = count + 1 - self.sentMsgHistoryCount = count + if count == self.sentMsgHistoryMax then + table.remove(self.sentMsgHistory, 1) + else + count = count + 1 + self.sentMsgHistoryCount = count + end + self.sentMsgHistory[count] = message + self.sentMsgHistoryCursor = count + 1 end - self.sentMsgHistory[count] = message - self.sentMsgHistoryCursor = count + 1 if self.listener then self.listener(message) From db4513eba735210268748e29673211ddd8a48b6b Mon Sep 17 00:00:00 2001 From: Helwor Date: Wed, 19 Jun 2024 15:52:19 +0200 Subject: [PATCH 3/3] now exactly as ingame The last update wasn't good, to stay at cursor on send it would need more coding around. Now it works exactly as ingame: not remembering the most recent message repeated. --- LuaMenu/widgets/chobby/components/console.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/LuaMenu/widgets/chobby/components/console.lua b/LuaMenu/widgets/chobby/components/console.lua index 9a0b66ce0..b9b34d7b7 100644 --- a/LuaMenu/widgets/chobby/components/console.lua +++ b/LuaMenu/widgets/chobby/components/console.lua @@ -244,8 +244,9 @@ function Console:SendMessage() local cursor = self.sentMsgHistoryCursor local count = self.sentMsgHistoryCount - if cursor < count + 1 and self.sentMsgHistory[cursor] == self.ebInputText.text then - -- skip, don't add to history message coming from history / keep cursor at current + if cursor == count and self.sentMsgHistory[cursor] == self.ebInputText.text then + -- skip, don't add to history the last message coming from history + self.sentMsgHistoryCursor = count + 1 else if count == self.sentMsgHistoryMax then table.remove(self.sentMsgHistory, 1)