Gauge functions

Share your scripts and packages with other Mudlet users.
Post Reply
johndahlstrom
Posts: 9
Joined: Tue Aug 11, 2009 8:45 am
Location: Sweden

Gauge functions

Post by johndahlstrom »

Here's some code I wrote a moment ago on request from the IRC channel. It'll make creating gauges easier for you. Hopefully! You'll see these in the next release in the LuaGlobal.lua file, but for the ones who want bleeding edge, here you go:

Code: Select all

-- Return the numbers of a RGB colour by using the names used in our color_table. 
--
-- Example: 
--
-- local red, green, blue = getRGB("green")
-- echo(red .. "." .. green .. "." .. blue ) 
--
-- This would then display 0.255.0 on your screen.

function getRGB(colorName)

	local red = color_table[colorName][1]
	local green = color_table[colorName][2]
	local blue = color_table[colorName][3]

	return red, green, blue
	
end


-- Make your very own customized gauge with this function.
--
-- Example: 
--
-- createGauge("healthBar", 300, 20, 30, 300, nil, 0, 255, 0) 
-- or 
-- createGauge("healthBar", 300, 20, 30, 300, nil, "green")
--
-- This would make a gauge at that's 300px width, 20px in height, located at Xpos and Ypos and is green.
-- The second example is using the same names you'd use for something like fg() or bg().
--
-- If you wish to have some text on your label, you'll change the nil part and make it look like this:
-- createGauge("healthBar", 300, 20, 30, 300, "Now with some text", 0, 255, 0) 
-- or 
-- createGauge("healthBar", 300, 20, 30, 300, "Now with some text", "green")

gaugesTable = {} -- first we need to make this table which will be used later to store important data in...

function createGauge(gaugeName, width, height, Xpos, Ypos, gaugeText, color1, color2, color3)
	

	-- make a nice background for our gauge
	createLabel(gaugeName .. "_back",0,0,0,0,1)
	if color2 == nil then
		local red, green, blue = getRGB(color1)
		setBackgroundColor(gaugeName .. "_back", red , green, blue, 100)		
	else
		setBackgroundColor(gaugeName .. "_back", color1 ,color2, color3, 100)
	end
	moveWindow(gaugeName .. "_back", Xpos, Ypos)
	resizeWindow(gaugeName .. "_back", width, height)
	showWindow(gaugeName .. "_back")

	-- make a nicer front for our gauge
	createLabel(gaugeName,0,0,0,0,1)
	if color2 == nil then
		local red, green, blue = getRGB(color1)
		setBackgroundColor(gaugeName, red , green, blue, 255)		
	else
		setBackgroundColor(gaugeName, color1 ,color2, color3, 255)
	end
	moveWindow(gaugeName, Xpos, Ypos)
	resizeWindow(gaugeName, width, height)
	showWindow(gaugeName)

	-- put some text on our label
	if gaugeText ~= nil then
		echo(gaugeName .. "_back", gaugeText)
		echo(gaugeName, gaugeText)
	else
		-- just so that it'll get rid of the text if there already was a label like this
		echo(gaugeName .. "_back", "")
		echo(gaugeName, "")
	end

	-- store important data in a table
	table.insert(gaugesTable, {name = gaugeName, width = width, height = height, color1 = color1, color2 = color2, color3 = color3})
	
end


-- Use this function when you want to change the gauges look according to your values.
--
-- Example: 
--
-- setGauge("healthBar", 200, 400)
--
-- In that example, we'd change the looks of the gauge named healthBar and make it fill
-- to half of its capacity. The height is always remembered.
--
-- If you wish to change the text on your gauge, you'd do the following:
--
-- setGauge("healthBar", 200, 400, "some text")
--
-- Typical usage would be in a prompt with your current health or whatever value, and throw
-- in some variables instead of the numbers.

function setGauge(gaugeName, currentValue, maxValue, gaugeText)	


	-- search through our gaugesTable for our name input and change according to the values
	for _,v in pairs(gaugesTable) do
		if v.name == gaugeName then
			resizeWindow(gaugeName, v.width/100*((100/maxValue)*currentValue), v.height)
		end
	end

	-- if we wanted to change the text, we do it
	if gaugeText ~= nil then
		echo(gaugeName .. "_back", gaugeText)
		echo(gaugeName, gaugeText)
	end
	
end

-- UPDATED
I did some changes to the createGauge() and setGauge(). I also made a new function for pulling out RGB numbers from the color_table[] which allowed some of the changes made in the two other functions. Hopefully it'll add some usefulness to some other script you make.

User avatar
demonnic
Posts: 884
Joined: Sat Dec 05, 2009 3:19 pm

Re: Gauge functions

Post by demonnic »

created a resizeGauge function... cuz doing it by hand sucks.

Code: Select all

-- Resizes a custom gauge built by createGauge(...)
-- example: resizeGauge("healthbar", 150, 15)
-- This would make the healthbar 150 pix wide by 15 pix tall
function resizeGauge(gaugeName, width, height)
	for _,g in pairs(gaugesTable) do
		if g.name == gaugeName then
			resizeWindow(gaugeName, width, height)
			resizeWindow(gaugeName .. "_back", width, height)
		end
	end
end

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

Re: Gauge functions

Post by Vadi »

Code: Select all

function setGaugeStyleSheet(gaugeName, css, cssback)
if not setLabelStyleSheet then return end-- mudlet 1.0.5 and lower compatibility

   for _,g in pairs(gaugesTable) do
      if g.name == gaugeName then
         setLabelStyleSheet(gaugeName, css)
         setLabelStyleSheet(gaugeName .. "_back", cssback or css)
      end
   end
end

User avatar
Alexander Divine
Posts: 65
Joined: Mon Dec 21, 2009 7:01 pm

Re: Gauge functions

Post by Alexander Divine »

Heeey, that is sweet. This would answer the problem I was having with the font of my gauges always being black. You would encase your CSS in the [[]] double brackets, right? Also, what do I call the gauges in CSS? I know that you have to use QPushButton, etc. with buttons.

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

Re: Gauge functions

Post by Vadi »

Alexander Divine wrote:Heeey, that is sweet. This would answer the problem I was having with the font of my gauges always being black. You would encase your CSS in the [[]] double brackets, right? Also, what do I call the gauges in CSS? I know that you have to use QPushButton, etc. with buttons.
Yes, in [[]]. You dont have to supply a name either, just use the css directly. heres my example of a bar that emulates nexus ones:

setGaugeStyleSheet("vital_H", [[background-color: rgb(221, 0, 51);
border-top: 1px solid black;
border-left: 1px solid black;
border-bottom: 1px solid black;
border-radius: 3px;]],
[[border: 1px solid black;
background-color: rgba(221, 0, 51, 50%);
border-radius: 3px;]])

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

Re: Gauge functions

Post by Vadi »

Oh just note that this will work in 1.0.6, 1.0.5 doesn't have setLabelStyleSheet :P

Post Reply