Dk75 isobar3d.module (en)

From Conky PitStop

Jump to: navigation, search

dk75_isobar3d.module

User:DK75

 Language   English   Français   


Isometric bars for One4All project

Dk75 isobar3d.module.png

dk75_isobar3d.module-1.0.tar.gz

--[[
isobar3d.module by dk75
concept - isobar3d by mpeachy

changelog:
	v1.0 - initial release (19-10-2012)
--]]

local modname = ...
local M = {}
_G[modname] = M
package.loaded[modname] = M



--[[
###############################################################################
###                              SETTINGS                                   ###
###############################################################################
--]]
local settings_table = {
	{
	object	=	{
			name	= '${cpu cpu0}',
			conky	= true,
			value	=	{
					warning	= 10,		-- value.warnig along with fill.colorW and fill.colorA decides if there will be pattern or not (when empty = no pattern)
					max	= 100,		-- maximum value
					scale	= 'log',	-- possible values: nil/'linear' (for linear scale), 'log' (for logaritmic scale) and 'exp' (for inverted logaritmic scale)
					print	= true,		-- bolean: whatever to print value on bar or not (true/false)
					},
			},
	pos	=	{
			x	= 240,
			y	= 440,
			},
	width	= 40,
	height	= 200,
	iso	=	{
			x	= 10,				-- number of pixels in X axis that bar will be moved to (positive - to the right; negative - to the left)
			y	= 20,				-- number of pixels in X axis that bar will be moved to (positive - to the down; negative - to the up)
			},
	frames	= true,						-- whatever frames will be drawn (true/false)
	line	=	{
			width	= 0.2,
			color	= 0xffffff,
			alpha	= 1,
			},
	fill	=	{
			color	= 0x00ff00,
			alpha	= 0.5,
			colorW	= 0xffae00,			-- color of warning part on pattern (no pattern when empty)
			colorA	= 0xff0000,			-- color of alarm part on pattern (no pattern when empty)
			},
	text	=	{
			string	= "CPU",			-- name displayed below the bar
			font	= "White Rabbit",
			size	= 14,
			style	= "normal",			-- font style: 'normal', 'italic' and 'oblique'
			weight	= "normal",			-- font weight: 'normal' or 'bold'
			color	= 0xffffff,
			alpha	= 1,
			align	= "center",			-- align of text: 'center', 'left, or 'right'
			},
 	},

	{
	object	=	{
			name	= "${hddtemp /dev/sda}",
			conky	= true,
			value	=	{
					warning	= 50,
					max	= 100,
					scale	= 'linear',
					print	= true,
					},
			interval= 15,
			},
	pos	=	{
			x	= 320,
			y	= 440,
			},
	width	= 40,
	height	= 200,
	iso	=	{
			x	= -10,
			y	= -20,
			},
	frames	= true,
	line	=	{
			width	= 0.2,
			color	= 0xffffff,
			alpha	= 1,
			},
	fill	=	{
			color	= 0x00ff00,
			alpha	= 0.5,
			colorW	= 0xffae00,
			colorA	= 0xff0000,
			},
	text	=	{
			string	= "/dev/sda",
			font	= "White Rabbit",
			size	= 14,
			style	= "normal",
			weight	= "normal",
			color	= 0xffffff,
			alpha	= 1,
			align	= "center",
			},
 	},
}
--[[
###############################################################################
###                           END OF SETTINGS                               ###
###############################################################################
--]]



--[[
###############################################################################
###                              INITIALIZE                                 ###
###############################################################################
--]]
M.color = one4all_cairo.rgb2rgba
M.exec = one4all_main.os_capture
M.print_text = one4all_cairo.print_text
local _locale = os.setlocale(nil, 'ctype')
os.setlocale(_locale, 'all')
local matrix = cairo_matrix_t:create(); tolua.takeownership(matrix)
local i = nil
for i in pairs(settings_table) do
	local t = settings_table[i]
	t.fill.value = nil
	t.pos.Xiso, t.pos.Yiso = t.pos.x+t.iso.x, t.pos.y+t.iso.y
	if t.iso.y>0 then
		t.text.x = t.pos.Xiso+t.width/2
		t.text.y = t.pos.Yiso+t.text.size/4
		t.text.xV = t.pos.x+t.width/2+t.iso.x/2
		t.text.yV = t.pos.y+t.text.size/4
	else
		t.text.x = t.pos.x+t.width/2
		t.text.y = t.pos.y+t.text.size/4
		t.text.xV = t.pos.Xiso+t.width/2-t.iso.x/2
		t.text.yV = t.pos.Yiso+t.text.size/4
	end
	if not t.line.width then t.line.width=0.5 end
	if not t.object.value.scale then t.object.value.scale = nil end
	if not tonumber(t.object.interval) then t.object.interval=1 end
	if not t.object.value.current then t.object.value.current = 0 end
end
--[[
###############################################################################
--]]


 
function M.execi(object, interval)
	if (tonumber(conky_parse('${updates}')) % interval) == 0 then return M.exec(object, raw)
	else return nil end
end

function M.parser(t)
	local object_value=nil
	if t.object.conky then object_value = tonumber(conky_parse(t.object.name))
	else object_value = tonumber(M.execi(t.object.name, t.object.interval)) end
	if object_value then t.object.value.current=object_value end
end

function M.scale(value, max, height, scale)
	if scale=="log" then value = (height/math.log(max))*math.log(value)
	elseif scale=="exp" then value = (height/math.exp(max))*math.exp(value)
	else value = (height/max)*value end
	if not value or value<=0 or value ~= value then value=0 end
	if value>height then value=height end
	return value
end
 
function M.calc_color(t)
	local _rS, _gS, _bS, _a, _rE, _gE, _bE = nil
	local _percent=M.scale(t.object.value.current, t.object.value.warning, 1, t.object.value.scale)
	if _percent<=1 then
		_rS, _gS, _bS, _a = M.color(t.fill.color, 1)
		_rE, _gE, _bE, _a = M.color(t.fill.colorW, 1)
	else
		_percent=M.scale(t.object.value.current-t.object.value.warning, t.object.value.max-t.object.value.warning, 1, t.object.value.scale)
		_rS, _gS, _bS, _a = M.color(t.fill.colorW, 1)
		_rE, _gE, _bE, _a = M.color(t.fill.colorA, 1)
	end
	local _red = tonumber(_rS+(_rE-_rS)*_percent); if _red>1 then _red=1 elseif _red<0 then _red=0 end
	local _green = tonumber(_gS+(_gE-_gS)*_percent); if _green>1 then _green=1 elseif _green<0 then _green=0 end
	local _blue = tonumber(_bS+(_bE-_bS)*_percent); if _blue>1 then _blue=1 elseif _blue<0 then _blue=0 end
	return _red, _green, _blue, t.fill.alpha
end

function M.draw_box(t, x, y, width, height, trans, frames)
	cairo_set_line_width(cr, t.line.width)
	cairo_set_source_rgba(cr, M.color(t.line.color, t.line.alpha))
	cairo_save(cr)
	cairo_translate(cr, x, y)
	if trans=="y" then
		cairo_matrix_init(matrix,
			1,	t.iso.y/width,
			0,	1,
			0,	0)
		cairo_transform(cr, matrix)
	elseif trans=="x" then
		cairo_matrix_init(matrix,
			1,		0,
			t.iso.x/height,	1,
			0,		0)
		cairo_transform(cr, matrix)
	end
	cairo_rectangle(cr, 0, 0, width, -height)
	if not frames then
		if t.fill.pat then
			if y==t.pos.Yiso-t.fill.value then cairo_set_source_rgba(cr, M.calc_color(t))
			else cairo_set_source(cr, t.fill.pat) end
		else cairo_set_source_rgba(cr, M.color(t.fill.color, t.fill.alpha)) end
		cairo_fill_preserve(cr)
		cairo_set_source_rgba(cr, M.color(t.line.color, t.line.alpha))
	end
	cairo_stroke(cr)
	cairo_restore(cr)
end

function M.isobar3d(t)
	t.fill.value=M.scale(t.object.value.current, t.object.value.max, t.height, t.object.value.scale)
	if t.frames then M.draw_box(t, t.pos.x, t.pos.y, t.width, t.height, nil, true) end -- back frame
	--M.draw_box(t, t.pos.x, t.pos.y, t.width, t.fill.value, false, false) -- back fill
	if t.iso.x>0 and t.iso.y>0 then -- right/down
		if t.frames then M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.iso.y, "x", true) end -- bottom frame
		M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.iso.y, "x") -- bottom fill
		M.draw_box(t, t.pos.x, t.pos.y, t.iso.x, t.fill.value, "y") -- left side fill
		M.draw_box(t, t.pos.Xiso, t.pos.Yiso-t.fill.value, t.width, t.iso.y, "x") -- top fill
		if t.frames then M.draw_box(t, t.pos.Xiso, t.pos.Yiso-t.height, t.width, t.iso.y, "x", true) end -- top frame
	end
	if t.iso.x<0 and t.iso.y>0 then --left/down
		if t.frames then M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.iso.y, "x", true) end -- bottom frame
		M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.iso.y, "x") -- bottom fill
		M.draw_box(t, t.pos.x+t.width, t.pos.y, t.iso.x, t.fill.value, "y") -- right side fill
		M.draw_box(t, t.pos.Xiso, t.pos.Yiso-t.fill.value, t.width, t.iso.y, "x") -- top fill
		if t.frames then M.draw_box(t, t.pos.Xiso, t.pos.Yiso-t.height, t.width, t.iso.y, "x", true) end -- top frame
	end
	if t.iso.x>0 and t.iso.y<0 then -- right/up
		if t.frames then M.draw_box(t, t.pos.Xiso, t.pos.Yiso-t.height, t.width, t.iso.y, "x", true) end -- top frame
		M.draw_box(t, t.pos.Xiso, t.pos.Yiso-t.fill.value, t.width, t.iso.y, "x") -- top fill
		M.draw_box(t, t.pos.x, t.pos.y, t.iso.x, t.fill.value, "y") -- left side fill
		M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.iso.y, "x") -- bottom fill
		if t.frames then M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.iso.y, "x", true) end -- bottom frame
	end
	if t.iso.x<0 and t.iso.y<0 then --left/up
		if t.frames then M.draw_box(t, t.pos.Xiso, t.pos.Yiso-t.height, t.width, t.iso.y, "x", true) end -- top frame
		M.draw_box(t, t.pos.Xiso, t.pos.Yiso-t.fill.value, t.width, t.iso.y, "x") -- top fill
		M.draw_box(t, t.pos.x+t.width, t.pos.y, t.iso.x, t.fill.value, "y") -- right side fill
		M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.iso.y, "x") -- bottom fill
		if t.frames then M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.iso.y, "x", true) end -- bottom frame
	end
	M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.fill.value, nil) -- front fill
	if t.frames then M.draw_box(t, t.pos.Xiso, t.pos.Yiso, t.width, t.height, nil, true) end -- front frame
	M.print_text(cr, t.text.x, t.text.y, t.text.string, t.text.color, t.text.alpha, t.text.font, t.text.size, t.text.style, t.text.weight, t.text.align, "top")
	if t.object.value.print then
		M.print_text(cr, t.text.xV, t.text.yV-t.fill.value, tostring(t.object.value.current), t.text.color, t.text.alpha, t.text.font, t.text.size, t.text.style, t.text.weight, t.text.align, "top")
	end
end



--[[
###############################################################################
###                              INITIALIZE                                 ###
###############################################################################
--]]
local i = nil
for i in pairs(settings_table) do
	local t = settings_table[i]
	if t.object.value.warning and t.object.value.max and t.fill.colorW and t.fill.colorA then
		local _offset = nil
		t.fill.pat = cairo_pattern_create_linear(0, 0, 0, -t.height)
		cairo_pattern_add_color_stop_rgba(t.fill.pat, 0, M.color(t.fill.color, t.fill.alpha))
		cairo_pattern_add_color_stop_rgba(t.fill.pat, M.scale(t.object.value.warning, t.object.value.max, 1, t.object.value.scale), M.color(t.fill.colorW, t.fill.alpha))
		cairo_pattern_add_color_stop_rgba(t.fill.pat, 1, M.color(t.fill.colorA, t.fill.alpha))
	end
end
--[[
###############################################################################
###                            MODULE MAIN LOOP                             ###
###############################################################################
--]]
function M.main()
	for i in pairs(settings_table) do
		M.parser(settings_table[i])
		M.isobar3d(settings_table[i])
	end
end
Personal tools
Namespaces
Variants
Actions
Navigation
English
Français
Toolbox