View Single Post
06-07-16, 08:05 PM   #9
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Originally Posted by Ketho View Post
If I don't call SetMultiLine it already seems to almost completely reduce the lag, if I just wanted to copy/paste stuff.
SetText doesn't behave the same way as actually typing characters into the edit box, so pasting large amounts of text will still lag your game while calling SetText does not.

Unfortunately, I just tried this and disabling word wrap doesn't appear to solve this problem any more. It was working about a week ago, but things change.

If you need to paste large amounts of text into the game, you can call editbox:SetMaxBytes(1) to limit the length of the string, and hook the edit box's OnChar function which will fire for every character in the paste regardless of whether it's being displayed.

Just insert the characters into a table OnChar and use OnUpdate to call table.concat on it on the next frame after the paste is finished to get your string.

Here's a relatively long example which accepts pastes and displays a truncated portion of it, something similar is being used in weak auras for importing strings.
Lua Code:
  1. local f = CreateFrame('frame')
  2. f:SetBackdrop({
  3.     bgFile = 'Interface/Tooltips/UI-Tooltip-Background',
  4.     edgeFile = 'Interface/Tooltips/UI-Tooltip-Border', edgeSize = 16,
  5.     insets = {left = 4, right = 4, top = 4, bottom = 4}
  6. })
  7. f:SetBackdropColor(0.2, 0.2, 0.2)
  8. f:SetBackdropBorderColor(0.2, 0.2, 0.2)
  9. f:SetPoint('CENTER')
  10. f:SetSize(400, 300)
  11.  
  12. local cursor = f:CreateTexture() -- make a fake blinking cursor, not really necessary
  13. cursor:SetTexture(1, 1, 1)
  14. cursor:SetSize(4, 8)
  15. cursor:SetPoint('TOPLEFT', 8, -8)
  16. cursor:Hide()
  17.  
  18. local editbox = CreateFrame('editbox', nil, f)
  19. editbox:SetMaxBytes(1) -- limit the max length of anything entered into the box, this is what prevents the lag
  20. editbox:SetAutoFocus(false)
  21.  
  22. local timeSince = 0
  23. local function UpdateCursor(self, elapsed)
  24.     timeSince = timeSince + elapsed
  25.     if timeSince >= 0.5 then
  26.         timeSince = 0
  27.         cursor:SetShown(not cursor:IsShown())
  28.     end
  29. end
  30.  
  31. local fontstring = f:CreateFontString(nil, nil, 'GameFontHighlightSmall')
  32. fontstring:SetPoint('TOPLEFT', 8, -8)
  33. fontstring:SetPoint('BOTTOMRIGHT', -8, 8)
  34. fontstring:SetJustifyH('LEFT')
  35. fontstring:SetJustifyV('TOP')
  36. fontstring:SetWordWrap(true)
  37. fontstring:SetNonSpaceWrap(true)
  38. fontstring:SetText('Click me!')
  39. fontstring:SetTextColor(0.6, 0.6, 0.6)
  40.  
  41. local textBuffer, i, lastPaste = {}, 0, 0
  42.  
  43. local function clearBuffer(self)
  44.     self:SetScript('OnUpdate', nil)
  45.     if i > 10 then -- ignore shorter strings
  46.         print(i, 'characters pasted')
  47.         local paste = strtrim(table.concat(textBuffer))
  48.         -- the longer this font string, the more it will lag trying to draw it
  49.         fontstring:SetText(strsub(paste, 1, 2500))
  50.         editbox:ClearFocus()
  51.     end
  52. end
  53.  
  54. editbox:SetScript('OnChar', function(self, c) -- runs for every character being pasted
  55.     if lastPaste ~= GetTime() then -- a timestamp can be used to track how many characters have been added within the same frame
  56.         textBuffer, i, lastPaste = {}, 0, GetTime()
  57.         self:SetScript('OnUpdate', clearBuffer)
  58.     end
  59.    
  60.     i = i + 1
  61.     textBuffer[i] = c -- store entered characters in a table to concat into a string later
  62. end)
  63.  
  64. editbox:SetScript('OnEditFocusGained', function(self)
  65.     fontstring:SetText('')
  66.     timeSince = 0
  67.     cursor:Show()
  68.     f:SetScript('OnUpdate', UpdateCursor)
  69. end)
  70.  
  71. editbox:SetScript('OnEditFocusLost', function(self)
  72.     f:SetScript('OnUpdate', nil)
  73.     cursor:Hide()
  74. end)
  75.  
  76. editbox:SetScript('OnEscapePressed', editbox.ClearFocus)
  77.  
  78. f:EnableMouse()
  79. f:SetScript('OnMouseDown', function()
  80.     editbox:SetFocus()
  81. end)

Last edited by semlar : 06-07-16 at 08:08 PM.