Skip to content
Snippets Groups Projects
Commit 4706ae4e authored by dg's avatar dg
Browse files

URLs in the chat are parsed and clickable

git-svn-id: http://svn.net-core.org/repos/t-engine4@5978 51575b47-30f0-44d4-a5cc-537603b46e54
parent d7f19927
No related branches found
No related tags found
No related merge requests found
......@@ -85,9 +85,15 @@ function _M:resize(x, y, w, h)
end
--- Returns the full log
function _M:getLog()
function _M:getLog(extra)
local log = {}
for i = 1, #self.log do log[#log+1] = self.log[i].str end
for i = 1, #self.log do
if not extra then
log[#log+1] = self.log[i].str
else
log[#log+1] = {str=self.log[i].str, src=self.log[i]}
end
end
return log
end
......@@ -103,6 +109,9 @@ function _M:showLogDialog(title, shadow)
game:registerDialog(d)
end
local urlfind = (lpeg.P"http://" + lpeg.P"https://") * (1-lpeg.P" ")^0
local urlmatch = lpeg.anywhere(lpeg.C(urlfind))
--- Appends text to the log
-- This method is set as the call methamethod too, this means it is usable like this:<br/>
-- log = LogDisplay.new(...)<br/>
......@@ -112,7 +121,13 @@ function _M:call(str, ...)
print("[LOG]", str)
local tstr = str:toString()
if self.out_f then self.out_f:write(tstr:removeColorCodes()) self.out_f:write("\n") end
table.insert(self.log, 1, {str=tstr, timestamp = core.game.getTime()})
local url = urlmatch:match(tstr)
if url then
tstr = tstr:lpegSub(urlfind, "#LIGHT_BLUE##{italic}#"..url.."#{normal}##LAST#")
end
table.insert(self.log, 1, {str=tstr, timestamp = core.game.getTime(), url=url})
while #self.log > self.max_log do
local old = table.remove(self.log)
self.cache[old] = nil
......@@ -158,7 +173,12 @@ function _M:mouseEvent(button, x, y, xrel, yrel, bx, by, event)
if citem then
local sub_es = {}
for di = 1, #citem.item._dduids do sub_es[#sub_es+1] = citem.item._dduids[di].e end
self.on_mouse(citem, sub_es, button, event, x, y, xrel, yrel, bx, by)
if citem.url and button == "left" and event == "button" then
util.browserOpenUrl(citem.url)
else
self.on_mouse(citem, sub_es, button, event, x, y, xrel, yrel, bx, by)
end
else
self.on_mouse(nil, nil, button, event, x, y, xrel, yrel, bx, by)
end
......@@ -185,7 +205,7 @@ function _M:display()
self.cache[tstr] = gen
end
for i = #gen, 1, -1 do
self.dlist[#self.dlist+1] = {item=gen[i], date=self.log[z].timestamp}
self.dlist[#self.dlist+1] = {item=gen[i], date=self.log[z].timestamp, url=self.log[z].url}
h = h + self.fh
if h > self.h - self.fh then stop=true break end
end
......@@ -212,6 +232,7 @@ function _M:toScreen()
if fade < self.fading * 1000 then fade = 1
elseif fade < self.fading * 2000 then fade = (self.fading * 2000 - fade) / (self.fading * 1000)
else fade = 0 end
self.dlist[i].faded = fade
end
self.dlist[i].dh = h
......
......@@ -68,10 +68,19 @@ function _M:filterMessage(item)
if config.settings.chat.filter[item.kind] then return true end
end
local urlfind = (lpeg.P"http://" + lpeg.P"https://") * (1-lpeg.P" ")^0
local urlmatch = lpeg.anywhere(lpeg.C(urlfind))
function _M:addMessage(kind, channel, login, name, msg, extra_data, no_change)
local color_name = colors.WHITE
if type(name) == "table" then name, color_name = name[1], name[2] end
local item = {kind=kind, login=login, name=name, color_name=color_name, msg=msg, extra_data=extra_data, timestamp=core.game.getTime()}
local url = urlmatch:match(msg)
if url then
msg = msg:lpegSub(urlfind, "#LIGHT_BLUE##{italic}#"..url.."#{normal}##LAST#")
end
local item = {kind=kind, login=login, name=name, color_name=color_name, msg=msg, url=url, extra_data=extra_data, timestamp=core.game.getTime()}
if self:filterMessage(item) then return end
if self.uc_ext and self.uc_ext.filterMessage then
if self.uc_ext:filterMessage(item) then return end
......@@ -419,7 +428,12 @@ function _M:mouseEvent(button, x, y, xrel, yrel, bx, by, event)
local item = self.dlist[i]
if item.dh and by >= item.dh - self.mouse.delegate_offset_y then citem = self.dlist[i].src ci=i break end
end
self.on_mouse(citem and citem.login and self.channels[self.cur_channel].users[citem.login], citem and citem.login and citem, button, event, x, y, xrel, yrel, bx, by)
if citem and citem.url and button == "left" and event == "button" then
util.browserOpenUrl(citem.url)
else
self.on_mouse(citem and citem.login and self.channels[self.cur_channel].users[citem.login], citem and citem.login and citem, button, event, x, y, xrel, yrel, bx, by)
end
end
end
......
......@@ -1259,6 +1259,8 @@ function _M:setupCommands()
print("===============")
end end,
[{"_g","ctrl"}] = function() if config.settings.cheat then
game.log("Toto foo http://te4.org/ toto")
do return end
local f, err = loadfile("/data/general/events/rat-lich.lua")
print(f, err)
setfenv(f, setmetatable({level=self.level, zone=self.zone}, {__index=_G}))
......
......@@ -1867,15 +1867,18 @@ function _M:setupMouse(mouse)
-- Log tooltips
self.logdisplay:onMouse(function(item, sub_es, button, event, x, y, xrel, yrel, bx, by)
local mx, my = core.mouse.get()
if not item or not sub_es or #sub_es == 0 then game.mouse:delegate(button, mx, my, xrel, yrel, nil, nil, event, "playmap") return end
if ((not item or not sub_es or #sub_es == 0) and (not item or not item.url)) or (item and item.faded == 0) then game.mouse:delegate(button, mx, my, xrel, yrel, nil, nil, event, "playmap") return end
local tooltips = {}
for i, e in ipairs(sub_es) do
if sub_es then for i, e in ipairs(sub_es) do
if e.tooltip then
table.append(tooltips, e:tooltip())
if i < #sub_es then table.append(tooltips, { tstring{ true, "---" } } )
else table.append(tooltips, { tstring{ true } } ) end
end
end end
if item.url then
table.append(tooltips, tstring{"Clicking will open ", {"color", "LIGHT_BLUE"}, {"font", "italic"}, item.url, {"color", "WHITE"}, {"font", "normal"}, " in your browser"})
end
local extra = {}
......@@ -1900,6 +1903,10 @@ function _M:setupMouse(mouse)
str:add({"color","ANTIQUE_WHITE"}, "Playing: ", {"color", "LAST"}, user.current_char, true)
str:add({"color","ANTIQUE_WHITE"}, "Game: ", {"color", "LAST"}, user.module, "(", user.valid, ")",true)
if item.url then
str:add(true, "---", true, "Clicking will open ", {"color", "LIGHT_BLUE"}, {"font", "italic"}, item.url, {"color", "WHITE"}, {"font", "normal"}, " in your browser")
end
local extra = {}
if item.extra_data and item.extra_data.mode == "tooltip" then
local rstr = tstring{item.extra_data.tooltip, true, "---", true, "Linked by: "}
......
......@@ -112,17 +112,24 @@ function _M:mouseEvent(button, x, y, xrel, yrel, bx, by, event)
if button == "wheelup" and event == "button" then self.key:triggerVirtual("MOVE_UP")
elseif button == "wheeldown" and event == "button" then self.key:triggerVirtual("MOVE_DOWN")
elseif event == "motion" then
else
if not self.dlist then return end
local citem = nil
for i = #self.dlist, 1, -1 do
local item = self.dlist[i]
if item.dh and by >= item.dh then citem = self.dlist[i].src break end
end
if citem and citem.extra_data and citem.extra_data.mode == "tooltip" then
game:tooltipDisplayAtMap(game.w, game.h, citem.extra_data.tooltip)
else
game.tooltip_x, game.tooltip_y = nil, nil
if event == "motion" then
if citem and citem.extra_data and citem.extra_data.mode == "tooltip" then
game:tooltipDisplayAtMap(game.w, game.h, citem.extra_data.tooltip)
else
game.tooltip_x, game.tooltip_y = nil, nil
end
elseif event == "button" and button == "left" then
if citem and citem.url then
util.browserOpenUrl(citem.url)
end
end
end
end
......@@ -153,7 +160,7 @@ function _M:switchTo(ui)
for i, ui in ipairs(self.tabs) do ui.ui.selected = false end
ui.ui.selected = true
if ui.tab_channel == "__log" then
self:loadLog(self.log:getLog())
self:loadLog(self.log:getLog(true))
else
local s = nil
if _M.last_tab == ui.tab_channel and self.max and self.max_display and self.scroll < self.max - self.max_display + 1 then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment