Analog clock.module (en)

From Conky PitStop

Jump to: navigation, search

analog_clock.module

User:DK75

 Language   English   Français   


Analog clock for One4All project

Analog clock.module.png

Analog clock.module-1.2.tar.gz

--[[
Conky Cairo analog clock one4all module

changelog:

v1.2	- (21.06.2012) compatibility restored to new version of One4All function libraries
v1.0	- (..) initial release
--]]

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



--[[
###############################################################################
###                              SETTINGS                                   ###
###############################################################################
--]]

local settings_table = {
	{	x 	= 200,	-- X-axis clock center value
		y 	= 200,	-- Y-axis clock center value
		radius 	= 100,	-- radius of the clock
		factor 	= 10,	-- determine thick of clock rim and clock hands as of 'radius/factor'
	}
}
		
-- end of configuration 

--[[
###############################################################################
###                           END OF SETTINGS                               ###
###############################################################################
--]]



function M.clock_marks(cr, x, y, number, radius, thick)
	local delta_angle = 360/number
	local a = (thick/2)+1
	local pins, b = nil
	for pins = 0, 360-delta_angle, delta_angle do
		if (pins == 0) or (pins == 90) or (pins == 180) or (pins == 270) then
			b = thick/2
			cairo_set_line_width(cr, thick/2)
		else
			b = thick/4
			cairo_set_line_width(cr, thick/4)
		end
		cairo_move_to(cr, one4all_main.pointATcircle(x, y, pins, radius-a))
		cairo_line_to(cr, one4all_main.pointATcircle(x, y, pins, radius-a-b))
		cairo_stroke(cr)
	end
end

function M.clock_check_size(x, y, radius, factor)
	--print ("hook: ", cr, "\twidth: ", w, "\theight: ", h)
	local thick = radius/factor
	local w, h = conky_window.width, conky_window.height
	--if w or h then return end
	if ((radius+thick)*2 > w) then radius = (w/2)-(w/2/factor); thick = radius/factor end
	if ((radius+thick)*2 > h) then radius = (h/2)-(h/2/factor); thick = radius/factor end
	if (radius+thick > x) then x = radius+thick end
	if (radius+thick > y) then y = radius+thick end
	if (x+radius+thick > w) then x = w-radius-thick end; if (y+radius+thick > h) then y = h-radius-thick end
	return x, y, radius, thick, w, h
end

function M.draw_clock_hands(cr, x, y, radius, factor, opacity)
	local x, y, radius, thick, w, h = M.clock_check_size(x, y, radius, factor)
	--if x or y or radius or thick or w or h then return end
	
	local h_alpha, m_alpha, s_alpha = one4all_main.time2angle()
	cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND)
	
	cairo_set_line_width(cr, thick*1.5)
	cairo_set_source_rgba(cr, one4all_cairo.rgb2rgba(0x606060, opacity))
	cairo_move_to(cr, one4all_main.pointATcircle(x, y, h_alpha, thick*3))
	cairo_line_to(cr, one4all_main.pointATcircle(x, y, h_alpha, radius*0.5))
	cairo_stroke(cr)
	
	cairo_set_line_width(cr, thick*0.8)
	cairo_set_source_rgba(cr, one4all_cairo.rgb2rgba(0xFFFFFF, opacity))
	cairo_move_to(cr, one4all_main.pointATcircle(x, y, m_alpha, thick*4))
	cairo_line_to(cr, one4all_main.pointATcircle(x, y, m_alpha, radius*0.75))
	cairo_stroke(cr)
	
	local s_thick = thick*.2; if ( s_thick < 2 ) then s_thick = 2 end
	cairo_set_line_width(cr, s_thick)
	cairo_set_source_rgba(cr, one4all_cairo.rgb2rgba(0xff0000, opacity))
	cairo_move_to(cr, one4all_main.pointATcircle(x, y, s_alpha, thick*6))
	cairo_line_to(cr, one4all_main.pointATcircle(x, y, s_alpha, radius*0.90))
	cairo_stroke(cr)
end

function M.draw_clock(cr, x, y, radius, factor)
	local x, y, radius, thick, w, h = M.clock_check_size(x, y, radius, factor)
	--if x or y or radius or thick or w or h then return end
	
	cairo_set_line_width(cr, thick)
	cairo_set_source_rgba(cr, one4all_cairo.rgb2rgba(0xFFFFFF, 1))
	cairo_arc(cr, x, y, radius, 0/180*math.pi, 360/180*math.pi)
	cairo_stroke(cr)
	
	M.clock_marks(cr, x, y, 12, radius, thick)
		cairo_set_line_width(cr, thick)
	cairo_set_source_rgba(cr, one4all_cairo.rgb2rgba(0xFFFFFF, 0.5))
	cairo_arc(cr, x, y, thick, 0, 2*math.pi)
	cairo_stroke(cr)
end
	
function M.analog_clock(t)
	if (t.radius == 0) or (t.factor == 0) or (t.x == 0) or (t.y == 0) then return end
	M.draw_clock(cr, t.x, t.y, t.radius, t.factor)
	M.draw_clock_hands(cr, t.x, t.y, t.radius, t.factor, .5)
end



--[[
###############################################################################
###                            MODULE MAIN LOOP                             ###
###############################################################################
--]]
function M.main()
        for i in pairs(settings_table) do
            M.analog_clock(settings_table[i])
        end
end
Personal tools
Namespaces
Variants
Actions
Navigation
English
Français
Toolbox