From 559e545e3636873634e6cc7994cc9336aec36452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Ku=C5=9Bnierz?= Date: Tue, 2 Jun 2015 22:46:33 +0200 Subject: [PATCH] Few more minor fixes to selection in game console --- modules/game_console/console.lua | 56 +++++++++++++++----------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/modules/game_console/console.lua b/modules/game_console/console.lua index 85360a03..c9cb62e9 100644 --- a/modules/game_console/console.lua +++ b/modules/game_console/console.lua @@ -148,12 +148,12 @@ function init() end function getSelection(widget) - if not widget.selectionChildFirst or not widget.selectionChildLast then + if not widget.selection then return widget:getSelection() end local text = {} - for selectionChild = widget.selectionChildFirst, widget.selectionChildLast do + for selectionChild = widget.selection.first, widget.selection.last do local label = widget:getParent():getChildByIndex(selectionChild) table.insert(text, label:getSelection()) end @@ -161,6 +161,19 @@ function getSelection(widget) return table.concat(text, '\r\n') end +function invalidateSelection(widget) + local parent = widget:getParent() + if widget.selection then + for selectionChild = widget.selection.first, widget.selection.last do + local label = parent:getChildByIndex(selectionChild) + if label ~= widget then + label:clearSelection() + end + end + widget.selection = nil + end +end + function toggleChat() if consoleToggleChat:isChecked() then disableChat() @@ -590,21 +603,6 @@ function addTabText(text, speaktype, tab, creatureName) end end - -- remove selection - local removeSelectedText = function(self) - local parent = self:getParent() - if self.selectionChildFirst and self.selectionChildLast then - for selectionChild = self.selectionChildFirst, self.selectionChildLast do - local label = parent:getChildByIndex(selectionChild) - if label ~= self then - label:clearSelection() - end - end - self.selectionChildFirst = nil - self.selectionChildLast = nil - end - end - label.name = creatureName label.onMouseRelease = function(self, mousePos, mouseButton) -- TODO: regain lost selection @@ -612,10 +610,10 @@ function addTabText(text, speaktype, tab, creatureName) end label.onFocusChange = function(self, focused, reason) -- TODO: we are losing focus on context menu and therefore the selection - if not focused then removeSelectedText(self) end + if not focused then invalidateSelection(self) end end label.onMousePress = function(self, mousePos, button) - if button == MouseLeftButton then removeSelectedText(self) end + if button == MouseLeftButton then invalidateSelection(self) end end label.onMouseMove = function(self, mousePos, mouseMoved) if self:isPressed() then @@ -625,16 +623,13 @@ function addTabText(text, speaktype, tab, creatureName) local childIndex = parent:getChildIndex(child) -- remove old selection - removeSelectedText(self) + invalidateSelection(self) -- choose new selection if child and child ~= self then - self.selectionChildFirst = math.min(selfIndex, childIndex) - self.selectionChildLast = math.max(selfIndex, childIndex) - - for selectionChild = self.selectionChildFirst + 1, self.selectionChildLast - 1 do - local label = parent:getChildByIndex(selectionChild) - label:selectAll() + self.selection = {first = math.min(selfIndex, childIndex), last = math.max(selfIndex, childIndex)} + for selectionChild = self.selection.first + 1, self.selection.last - 1 do + parent:getChildByIndex(selectionChild):selectAll() end local textPos = child:getTextPos(mousePos) @@ -650,7 +645,9 @@ function addTabText(text, speaktype, tab, creatureName) end if consoleBuffer:getChildCount() > MAX_LINES then - consoleBuffer:getFirstChild():destroy() + local child = consoleBuffer:getFirstChild() + if child.selection then invalidateSelection(child) end + child:destroy() end end @@ -712,8 +709,9 @@ function processMessageMenu(mousePos, mouseButton, creatureName, text, label, ta menu:addOption(tr('Copy name'), function () g_window.setClipboardText(creatureName) end) end - if label:hasSelection() then - menu:addOption(tr('Copy'), function() g_window.setClipboardText(getSelection(label)) end, '(Ctrl+C)') + local selectedText = getSelection(label) + if #selectedText > 0 then + menu:addOption(tr('Copy'), function() g_window.setClipboardText(selectedText) end, '(Ctrl+C)') end menu:addOption(tr('Copy message'), function() g_window.setClipboardText(text) end) menu:addOption(tr('Select all'), function() label:selectAll() end)