view View.lua @ 22:8df5d8ef2e27 tip

switch back to LibQTip-1.0 as LQTC is depreciated Fixed up the line resizing as LQT does this automatically now
author wobin
date Wed, 22 Jul 2009 23:16:42 +1000
parents 431f2fce08f2
children
line wrap: on
line source
local View = Squawk.View
local Model = Squawk.Model
local Settings = Model.UserSettings
local Controller = Squawk.Controller
local Squawks = Model.Squawks

-- View --

function View:UpdateSquawkList()
	self:Print("Updated Squawk List")
	self:ShowMeMySquawks()
end

function View:NotifyOfPending(Name)
	self:Print(Name.." will have to approve your request")
end

function View:NotifyOfPendingRequest(Name)
	self:Print(Name.." wants to follow you.")
end

function View:NotifyOfNewFollowing(Name)
	self:Print("You are now following "..Name)
end

function View:NotifyOfUnfollowing(Name)
	self:Print("You are no longer following "..Name)
end

function View:NotifyOfNewFollower(Name)
	self:Print(Name.." is now following you")
end

function View:ShowMeMySquawks()
	for _,squawk in ipairs(Model.Squawks.Main) do
		self:Print(squawk.Message)
	end
end

function View:ShowMeMyFollowers()
	self:Print("My followers are:")
	for name,_ in pairs(Settings.Follower) do
		self:Print(name)
	end
end

function View:ShowMeWhoImFollowing()
	self:Print("I am following:")
	for name,_ in pairs(Settings.Following) do
		self:Print(name)
	end
end

function View:ShowMeWhoIveBlocked()
	self:Print("I've blocked:")
	for name,_ in pairs(Settings.Blocked) do
		self:Print(name)
	end
end

local TimeSpan = {	[1] = {"second",  60, 1},
										[2] = {"minute", 3600, 60}, 
										[3] = {"hour", 86400, 3600} }

function View:GetTime(stime)
	local lapsed = difftime(time(), stime)
	if lapsed < 86400 then -- if we're still in the same day...
		for inc,span in ipairs(TimeSpan) do
			if lapsed < span[2] then
				local timespan = math.floor(lapsed/span[3])
				if inc == 3 then
					timespan = tonumber(string.format("%.1f", lapsed/span[3]))
				end
				if timespan == 1 then
					timespan = timespan .." ".. span[1]
				else
					timespan = timespan .. " ".. span[1].."s"
				end
				return timespan.. " ago"
			end
		end
	end
	return date("%I:%M %p %b %d", stime)
end

local LDBFeed = LibStub("LibDataBroker-1.1"):NewDataObject("Squawk", {type = "data source", text = "Awk!"})
local QTip = LibStub("LibQTip-1.0")
local QTipClick = LibStub("LibQTipClick-1.0")
local tooltip = {}
local editbox = CreateFrame('EditBox', nil, UIParent)

local function HideTooltip()
	if not tooltip then return end
	if MouseIsOver(tooltip) or MouseIsOver(editbox) then return end
	tooltip:SetScript("OnLeave", nil)
	tooltip:Hide()
	QTip:Release(tooltip)
	tooltip = nil
end

local function ReplyToMe(cell, Owner, event)
	editbox.Timestamp = Owner[2]
	LDBFeed.OnClick(LDBFeed, "LeftButton", "@"..Owner[1]:gsub("|c%x%x%x%x%x%x%x%x(.-)|r", "%1"))
end

local function AddLine(tooltip, Line, Number, Owner, TimeStamp)
	local x,y
	if #Line < 79 then
		y,x = tooltip:AddNormalLine(Number, Owner, Line, View:GetTime(TimeStamp))
	else
		y,x = tooltip:AddNormalLine(Number, Owner, Line:sub(1, 80).."-", View:GetTime(TimeStamp))
		AddLine(tooltip, Line:sub(81))
	end
	if not TimeStamp then return end

	-- Now add the reply clickback
	tooltip:SetCell(y, 5, "|TInterface\\Addons\\Squawk\\reply:0|t", {Owner, TimeStamp})
	-- Reply clickback finished
end

function LDBFeed:OnEnter()
	tooltip = QTipClick:Acquire("Squawk",5, "LEFT", "CENTER", "LEFT", "RIGHT", "RIGHT")
	tooltip.type = "Squawk"
	tooltip:Clear()
	tooltip:SetCallback("OnMouseDown", ReplyToMe)
	self.tooltip = tooltip
	for i,squawk in ipairs(Squawks:GetLast10()) do
			local head = true
			local message = {strsplit("\n",squawk.Message)}
			for _,line in ipairs(message) do
				if head then
					AddLine(tooltip, line, i..".", Squawks:OwnerString(squawk), squawk.Time)
					head = false
				else
					AddLine(tooltip, line)
				end
			end
	end
	tooltip:SmartAnchorTo(self)
	tooltip:SetScript("OnLeave", HideTooltip)
	tooltip:Show()
end

function LDBFeed:OnLeave()
	HideTooltip()
end



local function GetTipAnchor(frame)
	local x,y = frame:GetCenter()	
	local hhalf = (x > UIParent:GetWidth()*2/3) and "RIGHT" or (x < UIParent:GetWidth()/3) and "LEFT" or ""
	local vhalf = (y > UIParent:GetHeight()/2) and "TOP" or "BOTTOM"
	return vhalf..hhalf, frame, (vhalf == "TOP" and "BOTTOM" or "TOP")..hhalf
end

function LDBFeed:OnClick(button, reply)
	editbox:ClearAllPoints()
	local frame  -- begin terrible hack to place the editbox somewhere close to the tooltip/ldb
	if GetMouseFocus():GetParent().type == "Squawk" then -- if we're a tooltip as designated above
		frame = GetMouseFocus():GetParent() -- parent to the tooltip
	else
		frame = GetMouseFocus() -- else parent to the ldb feeder
	end
	editbox:SetPoint(GetTipAnchor(frame))
	editbox:Show()
	editbox:SetText("")
	if reply then
		editbox:SetText(reply)
	end
	HideTooltip(true)
end


editbox:Hide()
editbox:SetAutoFocus(true)
editbox:SetHeight(32)
editbox:SetWidth(350)
editbox:SetMaxLetters(140)
editbox:SetFrameStrata("HIGH")
editbox:SetFontObject('GameFontHighlightSmall')
LDBFeed.editbox = editbox

editbox:SetScript("OnEscapePressed", function() 
																				editbox:SetText("") 
																				editbox.Timestamp = nil 
																				editbox:ClearFocus() 
																			end)
editbox:SetScript("OnEnterPressed", function() 
																				if #editbox:GetText() > 0 then 
																					View:Print(editbox.Timestamp)
																					Controller:SendNewSquawk(editbox:GetText(), editbox.Timestamp) 
																				end 
																				editbox.Timestamp = nil
																				editbox:SetText("")
																				editbox:ClearFocus() 
																			end) 
editbox:SetScript("OnEditFocusLost", editbox.Hide)
editbox:SetScript("OnEditFocusGained", editbox.HighlightText)
editbox:SetScript("OnTextChanged", function(self)
end)

	local left = editbox:CreateTexture(nil, "BACKGROUND")
	left:SetWidth(8) left:SetHeight(20)
	left:SetPoint("LEFT", -5, 0)
	left:SetTexture("Interface\\Common\\Common-Input-Border")
	left:SetTexCoord(0, 0.0625, 0, 0.625)

	local right = editbox:CreateTexture(nil, "BACKGROUND")
	right:SetWidth(8) right:SetHeight(20)
	right:SetPoint("RIGHT", 0, 0)
	right:SetTexture("Interface\\Common\\Common-Input-Border")
	right:SetTexCoord(0.9375, 1, 0, 0.625)

	local center = editbox:CreateTexture(nil, "BACKGROUND")
	center:SetHeight(20)
	center:SetPoint("RIGHT", right, "LEFT", 0, 0)
	center:SetPoint("LEFT", left, "RIGHT", 0, 0)
	center:SetTexture("Interface\\Common\\Common-Input-Border")
	center:SetTexCoord(0.0625, 0.9375, 0, 0.625)

function LDBFeed.OpenEditbox(self)
  editbox:SetText(self.val)
  editbox:SetParent(self)
  editbox:SetPoint("LEFT", self)
  editbox:SetPoint("RIGHT", self)
  editbox:Show()
end