New Gauge Functions

Post Reply
Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

New Gauge Functions

Post by Jor'Mox »

I took the gauge functions from GUIUtils, modified them to use the different orientations that are available in Geyser (horizontal, vertical, goofy, and batty) and added a third layer on top for text display to avoid the text getting squished when the gauge value gets too small.
Function call for createGauge is now: createGauge(gaugeName, width, height, x, y, gaugeText, orientation, r, g, b)
Function call for setGaugeStyleSheet is now: setGaugeStyleSheet(gaugeName, css, cssback, csstext)
All the other functions for gauges are called in exactly the same manner.

Here is the code for them:
Code: [show] | [select all] lua
function createGauge(gaugeName, width, height, x, y, gaugeText, orientation, r, g, b)
	gaugeText = gaugeText or ""
	orientation = orientation or "horizontal"
	assert(table.contains({"horizontal","vertical","goofy","batty"},orientation), "createGauge: orientation must be horizontal, vertical, goofy, or batty")
	if r ~= nil then
		if g == nil then
			r,g,b = getRGB(r)
		end
	else
		-- default colors
		r,g,b = 128,128,128
	end
	local tbl = {width = width, height = height, x = x, y = y, text = gaugeText, r = r, g = g, b = b, orientation = orientation, value = 1}
	createLabel(gaugeName .. "_back", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_back", r, g, b, 100)
	
	createLabel(gaugeName .. "_front", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_front", r, g, b, 255)
	
	createLabel(gaugeName .. "_text", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_text", 0, 0, 0, 0)
	
	-- save new values in table
	gaugesTable[gaugeName] = tbl
	resizeGauge(gaugeName, tbl.width, tbl.height)
	moveGauge(gaugeName, tbl.x, tbl.y)
	setGaugeText(gaugeName, gaugeText, "black")
	showGauge(gaugeName)
end

function hideGauge(gaugeName)
	assert(gaugesTable[gaugeName], "hideGauge: no such gauge exists.")
	hideWindow(gaugeName .. "_back")
	hideWindow(gaugeName .. "_front")
	hideWindow(gaugeName .. "_text")
end

function showGauge(gaugeName)
	assert(gaugesTable[gaugeName], "showGauge: no such gauge exists.")
	showWindow(gaugeName .. "_back")
	showWindow(gaugeName .. "_front")
	showWindow(gaugeName .. "_text")
end

function resizeGauge(gaugeName, width, height)
	assert(gaugesTable[gaugeName], "resizeGauge: no such gauge exists.")
	assert(width and height, "resizeGauge: need to have both width and height.")
	resizeWindow(gaugeName .. "_back", width, height)
	resizeWindow(gaugeName .. "_text", width, height)
	-- save new values in table
	gaugesTable[gaugeName].width, gaugesTable[gaugeName].height = width, height
	setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
end

function moveGauge(gaugeName, x, y)
	assert(gaugesTable[gaugeName], "moveGauge: no such gauge exists.")
	assert(x and y, "moveGauge: need to have both X and Y dimensions.")
	moveWindow(gaugeName .. "_back", x, y)
	moveWindow(gaugeName .. "_text", x, y)
	-- save new values in table
	gaugesTable[gaugeName].x, gaugesTable[gaugeName].y = x, y
	setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
end

function setGaugeStyleSheet(gaugeName, css, cssback, csstext)
	if not setLabelStyleSheet then return end -- mudlet 1.0.5 and lower compatibility
	assert(gaugesTable[gaugeName], "setGaugeStyleSheet: no such gauge exists.")
	setLabelStyleSheet(gaugeName .. "_back", cssback or css)
	setLabelStyleSheet(gaugeName .. "_front", css)
	setLabelStyleSheet(gaugeName .. "_text", csstext or "")
end

function setGaugeText(gaugeName, gaugeText, r, g, b)
	assert(gaugesTable[gaugeName], "setGaugeText: no such gauge exists.")
	if r ~= nil then
		if g == nil then
			r,g,b = getRGB(r)
		end
	else
		r,g,b = 0,0,0
	end
	gaugeText = gaugeText or ""
	local echoString = [[<font color="#]] .. RGB2Hex(r,g,b) .. [[">]] .. gaugeText .. [[</font>]]
	echo(gaugeName .. "_text", echoString)
	-- save new values in table
	gaugesTable[gaugeName].text = echoString
end

function setGauge(gaugeName, currentValue, maxValue, gaugeText)
	assert(gaugesTable[gaugeName], "setGauge: no such gauge exists.")
	assert(currentValue and maxValue, "setGauge: need to have both current and max values.")
	local value = currentValue / maxValue
	-- save new values in table
	gaugesTable[gaugeName].value = value
	local info = gaugesTable[gaugeName]
	local x,y,w,h = info.x, info.y, info.width, info.height
	
	if info.orientation == "horizontal" then
		resizeWindow(gaugeName .. "_front", w * value, h)
		moveWindow(gaugeName .. "_front", x, y)
	elseif info.orientation == "vertical" then
		resizeWindow(gaugeName .. "_front", w, h * value)
		moveWindow(gaugeName .. "_front", x, y + h * (1 - value))
	elseif info.orientation == "goofy" then
		resizeWindow(gaugeName .. "_front", w * value, h)
		moveWindow(gaugeName .. "_front", x + w * (1 - value), y)
	elseif info.orientation == "batty" then
		resizeWindow(gaugeName .. "_front", w, h * value)
		moveWindow(gaugeName .. "_front", x, y)
	end

	setGaugeText(gaugeName, gaugeText)
end

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: New Gauge Functions

Post by Vadi »

Could you move the orientation argument for createGauge to the end, so we can keep backwards compatibility with the existing users code that uses createGauge()?

Thanks for making this!

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: New Gauge Functions

Post by Jor'Mox »

Okay, tweaked so the argument order is changed, as requested. I went and did a quick retest to be sure that everything worked as expected, but you should probably give it a test as well just to be sure. The only significant thing of note is that the text layer doesn't take any CSS styling unless given one explicitly (so that it doesn't cover up the rest of the gauge). This can leave the text somewhat awkwardly positioned on gauges that have big borders, unless the text layer is styled similarly.

The function createGauge can now be successfully called in any of these ways:
createGauge(name, width, height, x, y)
createGauge(name, width, height, x, y, text)
createGauge(name, width, height, x, y, text, color_word)
createGauge(name, width, height, x, y, text, color_word, orientation)
createGauge(name, width, height, x, y, text, r, g, b)
createGauge(name, width, height, x, y, text, r, g, b, orientation)
Code: [show] | [select all] lua
function createGauge(gaugeName, width, height, x, y, gaugeText, r, g, b, orientation)
	gaugeText = gaugeText or ""
	if type(r) == "string" then
		orientation = g
		r,g,b = getRGB(r)
	elseif r == nil then
		-- default colors
		r,g,b = 128,128,128
	end
	orientation = orientation or "horizontal"
	assert(table.contains({"horizontal","vertical","goofy","batty"},orientation), "createGauge: orientation must be horizontal, vertical, goofy, or batty")
	local tbl = {width = width, height = height, x = x, y = y, text = gaugeText, r = r, g = g, b = b, orientation = orientation, value = 1}
	createLabel(gaugeName .. "_back", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_back", r, g, b, 100)
	
	createLabel(gaugeName .. "_front", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_front", r, g, b, 255)
	
	createLabel(gaugeName .. "_text", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_text", 0, 0, 0, 0)
	
	-- save new values in table
	gaugesTable[gaugeName] = tbl
	resizeGauge(gaugeName, tbl.width, tbl.height)
	moveGauge(gaugeName, tbl.x, tbl.y)
	setGaugeText(gaugeName, gaugeText, "black")
	showGauge(gaugeName)
end

function hideGauge(gaugeName)
	assert(gaugesTable[gaugeName], "hideGauge: no such gauge exists.")
	hideWindow(gaugeName .. "_back")
	hideWindow(gaugeName .. "_front")
	hideWindow(gaugeName .. "_text")
end

function showGauge(gaugeName)
	assert(gaugesTable[gaugeName], "showGauge: no such gauge exists.")
	showWindow(gaugeName .. "_back")
	showWindow(gaugeName .. "_front")
	showWindow(gaugeName .. "_text")
end

function resizeGauge(gaugeName, width, height)
	assert(gaugesTable[gaugeName], "resizeGauge: no such gauge exists.")
	assert(width and height, "resizeGauge: need to have both width and height.")
	resizeWindow(gaugeName .. "_back", width, height)
	resizeWindow(gaugeName .. "_text", width, height)
	-- save new values in table
	gaugesTable[gaugeName].width, gaugesTable[gaugeName].height = width, height
	setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
end

function moveGauge(gaugeName, x, y)
	assert(gaugesTable[gaugeName], "moveGauge: no such gauge exists.")
	assert(x and y, "moveGauge: need to have both X and Y dimensions.")
	moveWindow(gaugeName .. "_back", x, y)
	moveWindow(gaugeName .. "_text", x, y)
	-- save new values in table
	gaugesTable[gaugeName].x, gaugesTable[gaugeName].y = x, y
	setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
end

function setGaugeStyleSheet(gaugeName, css, cssback, csstext)
	if not setLabelStyleSheet then return end -- mudlet 1.0.5 and lower compatibility
	assert(gaugesTable[gaugeName], "setGaugeStyleSheet: no such gauge exists.")
	setLabelStyleSheet(gaugeName .. "_back", cssback or css)
	setLabelStyleSheet(gaugeName .. "_front", css)
	setLabelStyleSheet(gaugeName .. "_text", csstext or "")
end

function setGaugeText(gaugeName, gaugeText, r, g, b)
	assert(gaugesTable[gaugeName], "setGaugeText: no such gauge exists.")
	if r ~= nil then
		if g == nil then
			r,g,b = getRGB(r)
		end
	else
		r,g,b = 0,0,0
	end
	gaugeText = gaugeText or ""
	local echoString = [[<font color="#]] .. RGB2Hex(r,g,b) .. [[">]] .. gaugeText .. [[</font>]]
	echo(gaugeName .. "_text", echoString)
	-- save new values in table
	gaugesTable[gaugeName].text = echoString
end

function setGauge(gaugeName, currentValue, maxValue, gaugeText)
	assert(gaugesTable[gaugeName], "setGauge: no such gauge exists.")
	assert(currentValue and maxValue, "setGauge: need to have both current and max values.")
	local value = currentValue / maxValue
	-- save new values in table
	gaugesTable[gaugeName].value = value
	local info = gaugesTable[gaugeName]
	local x,y,w,h = info.x, info.y, info.width, info.height
	
	if info.orientation == "horizontal" then
	resizeWindow(gaugeName .. "_front", w * value, h)
	moveWindow(gaugeName .. "_front", x, y)
	elseif info.orientation == "vertical" then
	resizeWindow(gaugeName .. "_front", w, h * value)
	moveWindow(gaugeName .. "_front", x, y + h * (1 - value))
	elseif info.orientation == "goofy" then
	resizeWindow(gaugeName .. "_front", w * value, h)
	moveWindow(gaugeName .. "_front", x + w * (1 - value), y)
	elseif info.orientation == "batty" then
	resizeWindow(gaugeName .. "_front", w, h * value)
	moveWindow(gaugeName .. "_front", x, y)
	end
	if gaugeText then
		setGaugeText(gaugeName, gaugeText)
	end
end

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: New Gauge Functions

Post by Jor'Mox »

Hmm. I just realized that I didn't allow for a nil argument in place of a color name which can then be followed by an orientation. Not a huge deal, but I'll see if I can't fix that.

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: New Gauge Functions

Post by Jor'Mox »

Fixed.
Code: [show] | [select all] lua
function createGauge(gaugeName, width, height, x, y, gaugeText, r, g, b, orientation)
	gaugeText = gaugeText or ""
	if type(r) == "string" then
		orientation = g
		r,g,b = getRGB(r)
	elseif r == nil then
		orientation = orientation or g
		-- default colors
		r,g,b = 128,128,128
	end
	orientation = orientation or "horizontal"
	assert(table.contains({"horizontal","vertical","goofy","batty"},orientation), "createGauge: orientation must be horizontal, vertical, goofy, or batty")
	local tbl = {width = width, height = height, x = x, y = y, text = gaugeText, r = r, g = g, b = b, orientation = orientation, value = 1}
	createLabel(gaugeName .. "_back", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_back", r, g, b, 100)
	
	createLabel(gaugeName .. "_front", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_front", r, g, b, 255)
	
	createLabel(gaugeName .. "_text", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_text", 0, 0, 0, 0)
	
	-- save new values in table
	gaugesTable[gaugeName] = tbl
	resizeGauge(gaugeName, tbl.width, tbl.height)
	moveGauge(gaugeName, tbl.x, tbl.y)
	setGaugeText(gaugeName, gaugeText, "black")
	showGauge(gaugeName)
end

function hideGauge(gaugeName)
	assert(gaugesTable[gaugeName], "hideGauge: no such gauge exists.")
	hideWindow(gaugeName .. "_back")
	hideWindow(gaugeName .. "_front")
	hideWindow(gaugeName .. "_text")
end

function showGauge(gaugeName)
	assert(gaugesTable[gaugeName], "showGauge: no such gauge exists.")
	showWindow(gaugeName .. "_back")
	showWindow(gaugeName .. "_front")
	showWindow(gaugeName .. "_text")
end

function resizeGauge(gaugeName, width, height)
	assert(gaugesTable[gaugeName], "resizeGauge: no such gauge exists.")
	assert(width and height, "resizeGauge: need to have both width and height.")
	resizeWindow(gaugeName .. "_back", width, height)
	resizeWindow(gaugeName .. "_text", width, height)
	-- save new values in table
	gaugesTable[gaugeName].width, gaugesTable[gaugeName].height = width, height
	setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
end

function moveGauge(gaugeName, x, y)
	assert(gaugesTable[gaugeName], "moveGauge: no such gauge exists.")
	assert(x and y, "moveGauge: need to have both X and Y dimensions.")
	moveWindow(gaugeName .. "_back", x, y)
	moveWindow(gaugeName .. "_text", x, y)
	-- save new values in table
	gaugesTable[gaugeName].x, gaugesTable[gaugeName].y = x, y
	setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
end

function setGaugeStyleSheet(gaugeName, css, cssback, csstext)
	if not setLabelStyleSheet then return end -- mudlet 1.0.5 and lower compatibility
	assert(gaugesTable[gaugeName], "setGaugeStyleSheet: no such gauge exists.")
	setLabelStyleSheet(gaugeName .. "_back", cssback or css)
	setLabelStyleSheet(gaugeName .. "_front", css)
	setLabelStyleSheet(gaugeName .. "_text", csstext or "")
end

function setGaugeText(gaugeName, gaugeText, r, g, b)
	assert(gaugesTable[gaugeName], "setGaugeText: no such gauge exists.")
	if r ~= nil then
		if g == nil then
			r,g,b = getRGB(r)
		end
	else
		r,g,b = 0,0,0
	end
	gaugeText = gaugeText or ""
	local echoString = [[<font color="#]] .. RGB2Hex(r,g,b) .. [[">]] .. gaugeText .. [[</font>]]
	echo(gaugeName .. "_text", echoString)
	-- save new values in table
	gaugesTable[gaugeName].text = echoString
end

function setGauge(gaugeName, currentValue, maxValue, gaugeText)
	assert(gaugesTable[gaugeName], "setGauge: no such gauge exists.")
	assert(currentValue and maxValue, "setGauge: need to have both current and max values.")
	local value = currentValue / maxValue
	-- save new values in table
	gaugesTable[gaugeName].value = value
	local info = gaugesTable[gaugeName]
	local x,y,w,h = info.x, info.y, info.width, info.height
	
	if info.orientation == "horizontal" then
	resizeWindow(gaugeName .. "_front", w * value, h)
	moveWindow(gaugeName .. "_front", x, y)
	elseif info.orientation == "vertical" then
	resizeWindow(gaugeName .. "_front", w, h * value)
	moveWindow(gaugeName .. "_front", x, y + h * (1 - value))
	elseif info.orientation == "goofy" then
	resizeWindow(gaugeName .. "_front", w * value, h)
	moveWindow(gaugeName .. "_front", x + w * (1 - value), y)
	elseif info.orientation == "batty" then
	resizeWindow(gaugeName .. "_front", w, h * value)
	moveWindow(gaugeName .. "_front", x, y)
	end
	if gaugeText then
		setGaugeText(gaugeName, gaugeText)
	end
end

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: New Gauge Functions

Post by Vadi »

Works well - thanks a ton! I've added it in.

Jor'Mox
Posts: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: New Gauge Functions

Post by Jor'Mox »

Updated as requested to change name of the foremost label (used for text display) to the name assigned to the gauge.
Code: [show] | [select all] lua
function createGauge(gaugeName, width, height, x, y, gaugeText, r, g, b, orientation)
	gaugeText = gaugeText or ""
	if type(r) == "string" then
		orientation = g
		r,g,b = getRGB(r)
	elseif r == nil then
		orientation = g
		-- default colors
		r,g,b = 128,128,128
	end
	orientation = orientation or "horizontal"
	assert(table.contains({"horizontal","vertical","goofy","batty"},orientation), "createGauge: orientation must be horizontal, vertical, goofy, or batty")
	local tbl = {width = width, height = height, x = x, y = y, text = gaugeText, r = r, g = g, b = b, orientation = orientation, value = 1}
	createLabel(gaugeName .. "_back", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_back", r, g, b, 100)
	
	createLabel(gaugeName .. "_front", 0,0,0,0,1)
	setBackgroundColor(gaugeName .. "_front", r, g, b, 255)
	
	createLabel(gaugeName, 0,0,0,0,1)
	setBackgroundColor(gaugeName, 0, 0, 0, 0)
	
	-- save new values in table
	gaugesTable[gaugeName] = tbl
	resizeGauge(gaugeName, tbl.width, tbl.height)
	moveGauge(gaugeName, tbl.x, tbl.y)
	setGaugeText(gaugeName, gaugeText, "black")
	showGauge(gaugeName)
end

function hideGauge(gaugeName)
	assert(gaugesTable[gaugeName], "hideGauge: no such gauge exists.")
	hideWindow(gaugeName .. "_back")
	hideWindow(gaugeName .. "_front")
	hideWindow(gaugeName)
end

function showGauge(gaugeName)
	assert(gaugesTable[gaugeName], "showGauge: no such gauge exists.")
	showWindow(gaugeName .. "_back")
	showWindow(gaugeName .. "_front")
	showWindow(gaugeName)
end

function resizeGauge(gaugeName, width, height)
	assert(gaugesTable[gaugeName], "resizeGauge: no such gauge exists.")
	assert(width and height, "resizeGauge: need to have both width and height.")
	resizeWindow(gaugeName .. "_back", width, height)
	resizeWindow(gaugeName, width, height)
	-- save new values in table
	gaugesTable[gaugeName].width, gaugesTable[gaugeName].height = width, height
	setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
end

function moveGauge(gaugeName, x, y)
	assert(gaugesTable[gaugeName], "moveGauge: no such gauge exists.")
	assert(x and y, "moveGauge: need to have both X and Y dimensions.")
	moveWindow(gaugeName .. "_back", x, y)
	moveWindow(gaugeName, x, y)
	-- save new values in table
	gaugesTable[gaugeName].x, gaugesTable[gaugeName].y = x, y
	setGauge(gaugeName, gaugesTable[gaugeName].value, 1)
end

function setGaugeStyleSheet(gaugeName, css, cssback, csstext)
	if not setLabelStyleSheet then return end -- mudlet 1.0.5 and lower compatibility
	assert(gaugesTable[gaugeName], "setGaugeStyleSheet: no such gauge exists.")
	setLabelStyleSheet(gaugeName .. "_back", cssback or css)
	setLabelStyleSheet(gaugeName .. "_front", css)
	setLabelStyleSheet(gaugeName, csstext or "")
end

function setGaugeText(gaugeName, gaugeText, r, g, b)
	assert(gaugesTable[gaugeName], "setGaugeText: no such gauge exists.")
	if r ~= nil then
		if g == nil then
			r,g,b = getRGB(r)
		end
	else
		r,g,b = 0,0,0
	end
	gaugeText = gaugeText or ""
	local echoString = [[<font color="#]] .. RGB2Hex(r,g,b) .. [[">]] .. gaugeText .. [[</font>]]
	echo(gaugeName, echoString)
	-- save new values in table
	gaugesTable[gaugeName].text = echoString
end

function setGauge(gaugeName, currentValue, maxValue, gaugeText)
	assert(gaugesTable[gaugeName], "setGauge: no such gauge exists.")
	assert(currentValue and maxValue, "setGauge: need to have both current and max values.")
	local value = currentValue / maxValue
	-- save new values in table
	gaugesTable[gaugeName].value = value
	local info = gaugesTable[gaugeName]
	local x,y,w,h = info.x, info.y, info.width, info.height
	
	if info.orientation == "horizontal" then
	resizeWindow(gaugeName .. "_front", w * value, h)
	moveWindow(gaugeName .. "_front", x, y)
	elseif info.orientation == "vertical" then
	resizeWindow(gaugeName .. "_front", w, h * value)
	moveWindow(gaugeName .. "_front", x, y + h * (1 - value))
	elseif info.orientation == "goofy" then
	resizeWindow(gaugeName .. "_front", w * value, h)
	moveWindow(gaugeName .. "_front", x + w * (1 - value), y)
	elseif info.orientation == "batty" then
	resizeWindow(gaugeName .. "_front", w, h * value)
	moveWindow(gaugeName .. "_front", x, y)
	end
	if gaugeText then
		setGaugeText(gaugeName, gaugeText)
	end
end

Post Reply