mirror of
https://github.com/linux-man/LoveFrames.git
synced 2024-11-26 05:14:21 +00:00
base code for text object clickable links
This commit is contained in:
parent
c9f3d785cb
commit
4734d92376
@ -13,12 +13,16 @@ Version 0.9.6.4 - Alpha (Release Date TBD)
|
||||
[ADDED] a new textinput method: GetMasked()
|
||||
[ADDED] a new textinput method: SetMaskChar(char)
|
||||
[ADDED] a new textinput method: GetMaskChar()
|
||||
[ADDED] a new text method: SetLinksEnabled(enabled)
|
||||
[ADDED] a new text method: GetLinksEnabled()
|
||||
[ADDED] a new text event callback: OnClickLink(object, text)
|
||||
[ADDED] a new util library function: TableHasValue(table, value)
|
||||
[ADDED] a new util library function: GetHoverObject()
|
||||
[ADDED] a new callback: textinput(text)
|
||||
[ADDED] basic clipboard support for the textinput object (0.9.0 only)
|
||||
[ADDED] custom cursor support for the textinput object
|
||||
[ADDED] support for love.filesystem changes to loveframes.util.GetDirectoryContents (0.9.0 only)
|
||||
[ADDED] support for text object link processing
|
||||
[ADDED] loveframes.downobject
|
||||
|
||||
[FIXED] error when pressing a function key while a textinput was focused
|
||||
|
131
objects/text.lua
131
objects/text.lua
@ -16,7 +16,7 @@ local newobject = loveframes.NewObject("text", "loveframes_object_text", true)
|
||||
- desc: initializes the object
|
||||
--]]---------------------------------------------------------
|
||||
function newobject:initialize()
|
||||
|
||||
|
||||
self.type = "text"
|
||||
self.text = ""
|
||||
self.font = loveframes.basicfont
|
||||
@ -29,9 +29,14 @@ function newobject:initialize()
|
||||
self.original = {}
|
||||
self.defaultcolor = {0, 0, 0, 255}
|
||||
self.shadowcolor = {0, 0, 0, 255}
|
||||
self.linkcolor = {0, 102, 255, 255}
|
||||
self.linkhovercolor = {0, 0, 255, 255}
|
||||
self.ignorenewlines = false
|
||||
self.linkcursorset = false
|
||||
self.shadow = false
|
||||
self.internal = false
|
||||
self.linksenabled = false
|
||||
self.OnClickLink = nil
|
||||
|
||||
end
|
||||
|
||||
@ -63,12 +68,52 @@ function newobject:update(dt)
|
||||
|
||||
self:CheckHover()
|
||||
|
||||
local hover = self.hover
|
||||
local linksenabled = self.linksenabled
|
||||
local linkcol = false
|
||||
if hover and linksenabled then
|
||||
local formattedtext = self.formattedtext
|
||||
local x = self.x
|
||||
local y = self.y
|
||||
for k, v in ipairs(formattedtext) do
|
||||
local link = v.link
|
||||
if link then
|
||||
local mx, my = love.mouse.getPosition()
|
||||
local font = v.font
|
||||
local linkx = v.x
|
||||
local linky = v.y
|
||||
local text = v.text
|
||||
local twidth = font:getWidth(text)
|
||||
local theight = font:getHeight()
|
||||
local col = loveframes.util.BoundingBox(x + linkx, mx, y + linky, my, twidth, 1, theight, 1)
|
||||
v.hover = false
|
||||
if col then
|
||||
local linkcursorset = self.linkcursorset
|
||||
v.hover = true
|
||||
if not linkcursorset then
|
||||
local newcursor = love.mouse.getSystemCursor("hand")
|
||||
love.mouse.setCursor(newcursor)
|
||||
self.linkcursorset = true
|
||||
end
|
||||
linkcol = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- move to parent if there is a parent
|
||||
if parent ~= base then
|
||||
self.x = self.parent.x + self.staticx
|
||||
self.y = self.parent.y + self.staticy
|
||||
end
|
||||
|
||||
local linkcursorset = self.linkcursorset
|
||||
if not linkcol and linkcursorset then
|
||||
self.linkcursorset = false
|
||||
love.mouse.setCursor()
|
||||
end
|
||||
|
||||
if update then
|
||||
update(self, dt)
|
||||
end
|
||||
@ -134,12 +179,35 @@ function newobject:mousepressed(x, y, button)
|
||||
end
|
||||
|
||||
local hover = self.hover
|
||||
|
||||
if hover and button == "l" then
|
||||
local baseparent = self:GetBaseParent()
|
||||
if baseparent and baseparent.type == "frame" then
|
||||
baseparent:MakeTop()
|
||||
end
|
||||
local linksenabled = self.linksenabled
|
||||
if linksenabled then
|
||||
local formattedtext = self.formattedtext
|
||||
local x = self.x
|
||||
local y = self.y
|
||||
for k, v in ipairs(formattedtext) do
|
||||
local link = v.link
|
||||
if link then
|
||||
local linkx = v.x
|
||||
local linky = v.y
|
||||
local font = v.font
|
||||
local text = v.text
|
||||
local twidth = font:getWidth(text)
|
||||
local theight = font:getHeight()
|
||||
local col = loveframes.util.BoundingBox(x + linkx, x, y + linky, y, twidth, 1, theight, 1)
|
||||
if col then
|
||||
local onclicklink = self.OnClickLink
|
||||
if onclicklink then
|
||||
onclicklink(self, text)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@ -251,10 +319,16 @@ function newobject:SetText(t)
|
||||
local largestwidth = 0
|
||||
local largestheight = 0
|
||||
local initialwidth = 0
|
||||
local linksenabled = self.linksenabled
|
||||
|
||||
for k, v in ipairs(textdata) do
|
||||
local text = v.text
|
||||
local color = v.color
|
||||
if linksenabled then
|
||||
if #text > 7 and text:sub(1, 7) == "http://" then
|
||||
v.link = true
|
||||
end
|
||||
end
|
||||
if type(text) == "string" then
|
||||
self.text = self.text .. text
|
||||
local width = v.font:getWidth(text)
|
||||
@ -374,28 +448,44 @@ function newobject:DrawText()
|
||||
local inlist, list = self:IsInList()
|
||||
|
||||
for k, v in ipairs(textdata) do
|
||||
local textx = v.x
|
||||
local texty = v.y
|
||||
local text = v.text
|
||||
local color = v.color
|
||||
local font = v.font
|
||||
local link = v.link
|
||||
local theight = font:getHeight("a")
|
||||
if inlist then
|
||||
if (y + v.y) <= (list.y + list.height) and self.y + ((v.y + theight)) >= list.y then
|
||||
local listy = list.y
|
||||
local listhieght = list.height
|
||||
if (y + texty) <= (listy + listhieght) and y + ((texty + theight)) >= listy then
|
||||
love.graphics.setFont(font)
|
||||
if shadow then
|
||||
love.graphics.setColor(unpack(shadowcolor))
|
||||
love.graphics.print(text, x + v.x + shadowxoffset, y + v.y + shadowyoffset)
|
||||
love.graphics.print(text, x + textx + shadowxoffset, y + texty + shadowyoffset)
|
||||
end
|
||||
love.graphics.setColor(unpack(color))
|
||||
love.graphics.print(text, x + v.x, y + v.y)
|
||||
if link then
|
||||
local linkcolor = self.linkcolor
|
||||
local linkhovercolor = self.linkhovercolor
|
||||
local hover = v.hover
|
||||
if hover then
|
||||
love.graphics.setColor(linkhovercolor)
|
||||
else
|
||||
love.graphics.setColor(linkcolor)
|
||||
end
|
||||
else
|
||||
love.graphics.setColor(unpack(color))
|
||||
end
|
||||
love.graphics.print(text, x + textx, y + texty)
|
||||
end
|
||||
else
|
||||
love.graphics.setFont(font)
|
||||
if shadow then
|
||||
love.graphics.setColor(unpack(shadowcolor))
|
||||
love.graphics.print(text, x + v.x + shadowxoffset, y + v.y + shadowyoffset)
|
||||
love.graphics.print(text, x + textx + shadowxoffset, y + texty + shadowyoffset)
|
||||
end
|
||||
love.graphics.setColor(unpack(color))
|
||||
love.graphics.print(text, x + v.x, y + v.y)
|
||||
love.graphics.print(text, x + textx, y + texty)
|
||||
end
|
||||
end
|
||||
|
||||
@ -586,10 +676,33 @@ end
|
||||
|
||||
--[[---------------------------------------------------------
|
||||
- func: GetDefaultColor()
|
||||
- desc: gets the object's default text color
|
||||
- desc: gets whether or not the object should draw a
|
||||
shadow behind its text
|
||||
--]]---------------------------------------------------------
|
||||
function newobject:GetDefaultColor()
|
||||
|
||||
return self.defaultcolor
|
||||
|
||||
end
|
||||
|
||||
--[[---------------------------------------------------------
|
||||
- func: SetLinksEnabled(enabled)
|
||||
- desc: sets whether or not the object should process
|
||||
urls into clickable links
|
||||
--]]---------------------------------------------------------
|
||||
function newobject:SetLinksEnabled(enabled)
|
||||
|
||||
self.linksenabled = enabled
|
||||
|
||||
end
|
||||
|
||||
--[[---------------------------------------------------------
|
||||
- func: GetLinksEnabled()
|
||||
- desc: gets whether or not the object should process
|
||||
urls into clickable links
|
||||
--]]---------------------------------------------------------
|
||||
function newobject:GetLinksEnabled()
|
||||
|
||||
return self.linksenabled
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user