PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/Tukui/modules/chat/copychat.lua

http://github.com/Asphyxia/Tukui
Lua | 121 lines | 101 code | 15 blank | 5 comment | 14 complexity | fa576856bad90ae931c3c1bbde49be77 MD5 | raw file
  1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2-----------------------------------------------------------------------------
  3-- Copy on chatframes feature
  4-----------------------------------------------------------------------------
  5
  6if C["chat"].enable ~= true then return end
  7
  8local lines = {}
  9local frame = nil
 10local editBox = nil
 11local isf = nil
 12
 13local sizes = {
 14	":14:14",
 15	":16:16",
 16	":12:20",
 17	":14",
 18}
 19
 20local function CreatCopyFrame()
 21	frame = CreateFrame("Frame", "CopyFrame", UIParent)
 22	frame:SetTemplate("Default")
 23	if T.lowversion then
 24		frame:Width(TukuiBar1:GetWidth() + 10)
 25	else
 26		frame:Width((TukuiBar1:GetWidth() * 2) + 20)
 27	end
 28	frame:Height(250)
 29	frame:SetScale(1)
 30	frame:Point("BOTTOM", UIParent, "BOTTOM", 0, 10)
 31	frame:Hide()
 32	frame:SetFrameStrata("DIALOG")
 33
 34	local scrollArea = CreateFrame("ScrollFrame", "CopyScroll", frame, "UIPanelScrollFrameTemplate")
 35	scrollArea:Point("TOPLEFT", frame, "TOPLEFT", 8, -30)
 36	scrollArea:Point("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -30, 8)
 37
 38	editBox = CreateFrame("EditBox", "CopyBox", frame)
 39	editBox:SetMultiLine(true)
 40	editBox:SetMaxLetters(99999)
 41	editBox:EnableMouse(true)
 42	editBox:SetAutoFocus(false)
 43	editBox:SetFontObject(ChatFontNormal)
 44	if T.lowversion then
 45		editBox:Width(TukuiBar1:GetWidth() + 10)
 46	else
 47		editBox:Width((TukuiBar1:GetWidth() * 2) + 20)
 48	end
 49	editBox:Height(250)
 50	editBox:SetScript("OnEscapePressed", function() frame:Hide() end)
 51
 52	--EXTREME HACK..
 53	editBox:SetScript("OnTextSet", function(self)
 54		local text = self:GetText()
 55
 56		for _, size in pairs(sizes) do
 57			if string.find(text, size) then
 58				self:SetText(string.gsub(text, size, ":12:12"))
 59			end		
 60		end
 61	end)
 62	
 63	scrollArea:SetScrollChild(editBox)
 64
 65	local close = CreateFrame("Button", "CopyCloseButton", frame, "UIPanelCloseButton")
 66	close:SetPoint("TOPRIGHT", frame, "TOPRIGHT")
 67
 68	isf = true
 69end
 70
 71local function GetLines(...)
 72	--[[		Grab all those lines		]]--
 73	local ct = 1
 74	for i = select("#", ...), 1, -1 do
 75		local region = select(i, ...)
 76		if region:GetObjectType() == "FontString" then
 77			lines[ct] = tostring(region:GetText())
 78			ct = ct + 1
 79		end
 80	end
 81	return ct - 1
 82end
 83
 84local function Copy(cf)
 85	local _, size = cf:GetFont()
 86	FCF_SetChatWindowFontSize(cf, cf, 0.01)
 87	local lineCt = GetLines(cf:GetRegions())
 88	local text = table.concat(lines, "\n", 1, lineCt)
 89	FCF_SetChatWindowFontSize(cf, cf, size)
 90	if not isf then CreatCopyFrame() end
 91	if frame:IsShown() then frame:Hide() return end
 92	frame:Show()
 93	editBox:SetText(text)
 94end
 95
 96local function ChatCopyButtons()
 97	for i = 1, NUM_CHAT_WINDOWS do
 98		local cf = _G[format("ChatFrame%d",  i)]
 99		local button = CreateFrame("Button", format("TukuiButtonCF%d", i), cf)
100		if i == 2 then
101			button:Point("BOTTOMRIGHT", 0, 0)
102		elseif i == 4 then
103			button:Point("TOPRIGHT", 0, 0)
104		else
105			button:Point("TOPRIGHT", 0, 0)
106		end
107		button:Size(20, 20)
108		button:SetNormalTexture(C.media.copyicon)
109		button:SetAlpha(0)
110		button:SetTemplate("Default")
111
112		button:SetScript("OnMouseUp", function(self)
113			Copy(cf)
114		end)
115		button:SetScript("OnEnter", function() 
116			button:SetAlpha(1) 
117		end)
118		button:SetScript("OnLeave", function() button:SetAlpha(0) end)
119	end
120end
121ChatCopyButtons()