Browse Source

Few more minor fixes to selection in game console

Konrad Kuśnierz 5 years ago
parent
commit
559e545e36
1 changed files with 27 additions and 29 deletions
  1. 27
    29
      modules/game_console/console.lua

+ 27
- 29
modules/game_console/console.lua View File

@@ -148,12 +148,12 @@ function init()
148 148
 end
149 149
 
150 150
 function getSelection(widget)
151
-  if not widget.selectionChildFirst or not widget.selectionChildLast then
151
+  if not widget.selection then
152 152
     return widget:getSelection()
153 153
   end
154 154
 
155 155
   local text = {}
156
-  for selectionChild = widget.selectionChildFirst, widget.selectionChildLast do
156
+  for selectionChild = widget.selection.first, widget.selection.last do
157 157
     local label = widget:getParent():getChildByIndex(selectionChild)
158 158
     table.insert(text, label:getSelection())
159 159
   end
@@ -161,6 +161,19 @@ function getSelection(widget)
161 161
   return table.concat(text, '\r\n')
162 162
 end
163 163
 
164
+function invalidateSelection(widget)
165
+  local parent = widget:getParent()
166
+  if widget.selection then
167
+    for selectionChild = widget.selection.first, widget.selection.last do
168
+      local label = parent:getChildByIndex(selectionChild)
169
+      if label ~= widget then
170
+        label:clearSelection()
171
+      end
172
+    end
173
+    widget.selection = nil
174
+  end
175
+end
176
+
164 177
 function toggleChat()
165 178
   if consoleToggleChat:isChecked() then
166 179
     disableChat()
@@ -590,21 +603,6 @@ function addTabText(text, speaktype, tab, creatureName)
590 603
     end
591 604
   end
592 605
 
593
-  -- remove selection
594
-  local removeSelectedText = function(self)
595
-    local parent = self:getParent()
596
-    if self.selectionChildFirst and self.selectionChildLast then
597
-        for selectionChild = self.selectionChildFirst, self.selectionChildLast do
598
-          local label = parent:getChildByIndex(selectionChild)
599
-          if label ~= self then
600
-            label:clearSelection()
601
-          end
602
-        end
603
-        self.selectionChildFirst = nil
604
-        self.selectionChildLast = nil
605
-    end
606
-  end
607
-
608 606
   label.name = creatureName
609 607
   label.onMouseRelease = function(self, mousePos, mouseButton)
610 608
     -- TODO: regain lost selection
@@ -612,10 +610,10 @@ function addTabText(text, speaktype, tab, creatureName)
612 610
   end
613 611
   label.onFocusChange = function(self, focused, reason)
614 612
     -- TODO: we are losing focus on context menu and therefore the selection
615
-    if not focused then removeSelectedText(self) end
613
+    if not focused then invalidateSelection(self) end
616 614
   end
617 615
   label.onMousePress = function(self, mousePos, button)
618
-    if button == MouseLeftButton then removeSelectedText(self) end
616
+    if button == MouseLeftButton then invalidateSelection(self) end
619 617
   end
620 618
   label.onMouseMove = function(self, mousePos, mouseMoved)
621 619
     if self:isPressed() then
@@ -625,16 +623,13 @@ function addTabText(text, speaktype, tab, creatureName)
625 623
       local childIndex = parent:getChildIndex(child)
626 624
 
627 625
       -- remove old selection
628
-      removeSelectedText(self)
626
+      invalidateSelection(self)
629 627
 
630 628
       -- choose new selection
631 629
       if child and child ~= self then
632
-        self.selectionChildFirst = math.min(selfIndex, childIndex)
633
-        self.selectionChildLast = math.max(selfIndex, childIndex)
634
-
635
-        for selectionChild = self.selectionChildFirst + 1, self.selectionChildLast - 1 do
636
-          local label = parent:getChildByIndex(selectionChild)
637
-          label:selectAll()
630
+        self.selection = {first = math.min(selfIndex, childIndex), last = math.max(selfIndex, childIndex)}
631
+        for selectionChild = self.selection.first + 1, self.selection.last - 1 do
632
+          parent:getChildByIndex(selectionChild):selectAll()
638 633
         end
639 634
 
640 635
         local textPos = child:getTextPos(mousePos)
@@ -650,7 +645,9 @@ function addTabText(text, speaktype, tab, creatureName)
650 645
   end
651 646
 
652 647
   if consoleBuffer:getChildCount() > MAX_LINES then
653
-    consoleBuffer:getFirstChild():destroy()
648
+    local child = consoleBuffer:getFirstChild()
649
+    if child.selection then invalidateSelection(child) end
650
+    child:destroy()
654 651
   end
655 652
 end
656 653
 
@@ -712,8 +709,9 @@ function processMessageMenu(mousePos, mouseButton, creatureName, text, label, ta
712 709
 
713 710
       menu:addOption(tr('Copy name'), function () g_window.setClipboardText(creatureName) end)
714 711
     end
715
-    if label:hasSelection() then
716
-      menu:addOption(tr('Copy'), function() g_window.setClipboardText(getSelection(label)) end, '(Ctrl+C)')
712
+    local selectedText = getSelection(label)
713
+    if #selectedText > 0 then
714
+      menu:addOption(tr('Copy'), function() g_window.setClipboardText(selectedText) end, '(Ctrl+C)')
717 715
     end
718 716
     menu:addOption(tr('Copy message'), function() g_window.setClipboardText(text) end)
719 717
     menu:addOption(tr('Select all'), function() label:selectAll() end)

Loading…
Cancel
Save