Timed "Warning" Labels.

Share your scripts and packages with other Mudlet users.
Post Reply
User avatar
kevutian
Posts: 217
Joined: Fri Aug 20, 2010 8:18 pm
Location: United Kingdom
Contact:

Timed "Warning" Labels.

Post by kevutian »

This function will provide Mudlet users with easy access to "warning" style labels.

The function currently accepts two arguments, label name and label text. The function is called as standard, i.e.

Code: Select all

visWarning("test", "test")
It currently accepts two concurrent labels, with the second receiving a 75 pixel offset.
Code: [show] | [select all] lua
function visWarning(name, text)
	local width, height = getMainWindowSize()     -- Get our current window dimensions
	local strLen = text:len()     -- Find the character length of our "warning"
	if windowExists == false then     -- Does a warning window already exist?
		createLabel(name, (width / 2) - 300, (height / 2) - 100, 250, 150, 1)
		resizeWindow(name, strLen * 25, 70)
		moveWindow(name, (width - (strLen * 25)) / 2, (height / 2) - 100) -- Dynamically place the window dependent on the length of our "warning"
		windowExists = true
	else
		moveWindow(name, (width - (strLen * 25)) / 2, (height / 2) - 175)
	end
	setBackgroundColor(name, 255, 51, 51, 200)
	echo(name, [[<p style="font-size:35px"><b><center><font color="white"> ]] .. text .. [[</font></center></b></p>]])
	showWindow(name)
	tempTimer(3, [[hideWindow("]] .. name .. [[");windowExists = false]])     -- Remove the window after 3 seconds
	resetFormat()
end
As always, this code is provided, "as-is" - Should there be any glaring issues, please do report them on this thread.

Thanks, and enjoy!
Last edited by kevutian on Sat Nov 10, 2012 11:42 pm, edited 1 time in total.

User avatar
chris
Posts: 493
Joined: Fri Jun 17, 2011 5:39 am

Re: Timed "Warning" Labels.

Post by chris »

It looks cool. Here are some suggestions for it:
visWarning(text) -> would make a random label id and raise the raise (so no need to define the label name)
raiseWarning('label') -> would recall a priorly made label

User avatar
kevutian
Posts: 217
Joined: Fri Aug 20, 2010 8:18 pm
Location: United Kingdom
Contact:

Re: Timed "Warning" Labels.

Post by kevutian »

Ahh, thanks for the feedback. Never even noticed you commented on here.

I actually already implemented the random id element. I'll get onto the recall at some point.
Code: [show] | [select all] lua
function visWarn(text)
	local width, height = getMainWindowSize()
	local strLen = text:len()
	local pre = function () local hex = { "a", "b", "c", "d", "e", "f" } return tostring(hex[math.random(#hex)] .. hex[math.random(#hex)] .. math.random(999, 9999)) end
	local label = pre()

	tmp.labels[label] = text
	createLabel(label, (width / 2) - 300, (height / 2) - 100, 250, 150, 1)
	setLabelStyleSheet(label, [[
		color: #333;
		border: 2px solid #555;
		border-radius: 11px;
		padding: 5px;
		background: qradialgradient(cx: 0.3, cy: -0.4,
		fx: 0.3, fy: -0.4,
		radius: 1.35, stop: 0 #fff, stop: 1 #888);
		min-width: 80px;
	]])
		
	resizeWindow(label, strLen * 25, 70)

	local offsetTable = { 100, 200, 300 }
	local tabLen, offset = countTable(tmp.labels), 100

	if countTable(tmp.labels) == 1 then	
		moveWindow(label, (width - (strLen * 25)) / 2, (height / 2) - offset) 
	else
		moveWindow(label, (width - (strLen * 25)) / 2, (height / 2) - offsetTable[tabLen]) 
	end

	setBackgroundColor(label, 255, 51, 51, 200)
	echo(label, [[<p style="font-size:35px"><b><center><font color="blue"> ]] .. text .. [[</font></center></b></p>]])
	showWindow(label)
	tempTimer(5, [[hideWindow(next(tmp.labels));keyRemove(tmp.labels, next(tmp.labels))]])
	resetFormat()
end

countTable():
Code: [show] | [select all] lua
function countTable(tbl)
	local c = 0
	for k, v in pairs(tbl) do
		c = c + 1
	end
	return c
end
keyRemove():
Code: [show] | [select all] lua
function keyRemove(tbl, key)
    local element = tbl[key]
    tbl[key] = nil
    return element
end

Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Re: Timed "Warning" Labels.

Post by Delrayne »

I suggest that they be made see through, or at least given the option to make them see through.

User avatar
kevutian
Posts: 217
Joined: Fri Aug 20, 2010 8:18 pm
Location: United Kingdom
Contact:

Re: Timed "Warning" Labels.

Post by kevutian »

Sure thing. I'll get on that today.

Delrayne
Posts: 159
Joined: Tue Jun 07, 2011 7:07 pm
Contact:

Re: Timed "Warning" Labels.

Post by Delrayne »

Any update on the transparent labels?

User avatar
kevutian
Posts: 217
Joined: Fri Aug 20, 2010 8:18 pm
Location: United Kingdom
Contact:

Re: Timed "Warning" Labels.

Post by kevutian »

Oh, sorry. I just happened upon this thread, heh.

Here's the latest code for them. It's composed of two parts; a function and a global timer.

Function:
Code: [show] | [select all] lua
--- Display high visibility warning labels.
-- For each string passed, this function will display a highly visible, timed warning label.
-- You may specify how long the label will remain by passing an optional second argument.
-- @param text The text to display.
-- @param duration How long the label should remain.
-- @usage visWarn("I am a warning label.", 3)
-- @usage visWarn("I am a warning label.")
function visWarn(text, duration)
	local width, height = getMainWindowSize()
	local strLen = text:len()
	local label = rndStr(8, "%l%d")

	tmp.labels[label] = {label = label, text = text, duration = (duration or 5)}
	createLabel(label, 0, 0, 0, 0, 1)
	setLabelStyleSheet(label, [[
		background-color: #333300;
		border: 5px double #339900;
		border-radius: 12px;
		color: #ff99ff;
		font-size: 12px;
		font-family: Zekton;
		font-style: normal;
		padding: 3px;
	]])
                
	resizeWindow(label, strLen * 25, 70)
	local tabLen, offset = countTable(tmp.labels), 100
	local topPos = (height / 2.0) - (tabLen * 75)
	if topPos > 0 then
		moveWindow(label, (width - (strLen * 18)) / 3, topPos)
	end

	echo(label, [[<p style="font-size:35px"><b><center><font color="yellow"> ]] .. text .. [[</font></center></b></p>]])
	if topPos > 0 then
		showWindow(label)
		table.insert(tmp.displayedLabels, label)
	else
		hideWindow(label)
		table.insert(tmp.labelQueue, label)
	end

	resetFormat()
end
And the timer function:
Code: [show] | [select all] lua
function viswarn_loop()
	if not tmp.labels then return end
	local toHide = {}
	local needRedraw = false

	for index, label in pairs(tmp.displayedLabels) do
		tmp.labels[label].duration = tmp.labels[label].duration - 0.5
		if tmp.labels[label].duration <= 0 then
			toHide[label] = true
			needRedraw = true
		end
	end

	for i = 1, #(tmp.displayedLabels) do
		if not tmp.displayedLabels[i] then break end
		if toHide[tmp.displayedLabels[i]] then
			hideWindow(tmp.displayedLabels[i])
			tmp.labels[tmp.displayedLabels[i]] = nil
			table.remove(tmp.displayedLabels, i)
			i = i - 1
		end
	end 
	local width, height = getMainWindowSize()
	if needRedraw or (#(tmp.displayedLabels) == 0 and #(tmp.labelQueue) > 0) then
		local brk = false
		local iter = 1
		while not brk do
			local topPos = (height / 1.5) - ((iter) * 75)
			if tmp.displayedLabels[iter] then
				local label = tmp.displayedLabels[iter]
				moveWindow(label, (width - (#(tmp.labels[label].text) * 25)) / 3, topPos)
			elseif topPos >= 0 and #(tmp.labelQueue) > 0 then
				local label = table.remove(tmp.labelQueue, 1)
				table.insert(tmp.displayedLabels, label)
				moveWindow(label, (width - (#(tmp.labels[label].text) * 25)) / 3, topPos)
				showWindow(label)
			else
				brk = true
				break;
			end
			iter = iter + 1
		end
	end
end
Enjoy :)

User avatar
Akaya
Posts: 414
Joined: Thu Apr 19, 2012 1:36 am

Re: Timed "Warning" Labels.

Post by Akaya »

This isn't working for me. Pasted the two functions into two separate scripts on a fresh profile then tried running the example usage listed in the first script. Is this the proper way to do so?

fetaera
Posts: 191
Joined: Sat Aug 03, 2013 7:00 pm

Re: Timed "Warning" Labels.

Post by fetaera »

Oh? What's the problem? Have a precise error for me? These should simply 'just work' but should they not, it's almost certainly nothing more than a missing table declaration.

Post Reply