All pastes #720986 Raw Edit

Unnamed

public text v1 · immutable
#720986 ·published 2007-09-30 21:09 UTC
rendered paste body

local pi = 3.14159265358979

local circle_cache = {}
local offsetcircle_cache = {}
local ClockOn = true

local function _GetCircle( cx, cy, radius, seg )
	local key = radius .. "/" .. seg
	local key2 = "(" .. cx .. "," .. cy .. ") " .. key
	
	-- did we already calculate the offset of this type of circle?
	if offsetcircle_cache[key2] ~= nil then return offsetcircle_cache[key2] end
	
	-- did we already calculate this type of circle?
	local c = nil
		
	if circle_cache[key] ~= nil then
		c = circle_cache[key]
	else
		c = {}
		
		-- calculate this type of circle.
		local radsperseg = math.rad( 360 / seg )
	
		for i = seg, 1, -1 do
			local r = radsperseg * i - 1
			table.insert( c, { x = math.sin( r ) * radius, y = math.cos( r ) * radius } )
		end
	
		-- cache it for later
		circle_cache[key] = c
	end
	
	local co = {}
	
	-- offset the circle
	for i, v in ipairs( c ) do
		co[i] = {}
		co[i].x = v.x + cx
		co[i].y = v.y + cy
	end
	
	-- cache it
	offsetcircle_cache[key2] = co
	
	return co
end

local function _DrawCircle( cx, cy, r, seg )
	local c = _GetCircle( cx, cy, r, seg )

	surface.DrawPoly( c )
end

local function _DrawCircleOutline( cx, cy, r, seg )
	local c = _GetCircle( cx, cy, r, seg )
	
	for i = 1,#c-1 do
		surface.DrawLine( c[i].x, c[i].y, c[i+1].x, c[i+1].y )
	end
	
	surface.DrawLine( c[#c].x, c[#c].y, c[1].x, c[1].y )
end

local function _DrawRadialPoints( cx, cy, ox, oy, r, num )
	local c = _GetCircle( cx, cy, r, num)
	
	for i = 1, #c do
		draw.SimpleText( tostring(i), "ScoreboardText", c[i].x + ox, c[i].y - oy, Color(255,255,255,255), 1, 3 )
	end
end

function ToggleClock()
	ClockOn = not ClockOn
end

concommand.Add("ToggleClock", ToggleClock)

function tmrClock()
	if !ClockOn then return end
    local dblSecond, dblMinute, dblHour
    local strSec, strMin, strHr
	
    dblSecond = ( os.date("%S") * 6 - 90 ) * -1
    dblMinute = ( ( os.date("%M") + os.date("%S")  / 60)) * 6 - 90
    dblHour   = ( ( os.date("%H") + os.date("%M")  / 60)) * 30 - 90
	
	local lineSecondX1, lineSecondX2, lineMinuteX1, lineMinuteX2, lineHourX1, lineHourX2
	local lineSecondY1, lineSecondY2, lineMinuteY1, lineMinuteY2, lineHourY1, lineHourY2
	
	lineSecondX1, lineMinuteX1, lineHourX1 = surface.ScreenWidth()  / 8, surface.ScreenWidth()  / 8, surface.ScreenWidth()  / 8
	lineSecondY1, lineMinuteY1, lineHourY1 = surface.ScreenHeight() / 5, surface.ScreenHeight() / 5, surface.ScreenHeight() / 5

	
    lineSecondX2 = 100 * -math.cos(dblSecond * pi / 180) + lineSecondX1
    lineSecondY2 = 100 * math.sin(dblSecond * pi / 180) + lineSecondY1
    lineMinuteX2 = 90  * math.cos(dblMinute * pi / 180) + lineMinuteX1
    lineMinuteY2 = 90  * math.sin(dblMinute * pi / 180) + lineMinuteY1
    lineHourX2 	 = 70  * math.cos(dblHour   * pi / 180) + lineHourX1
    lineHourY2 	 = 70  * math.sin(dblHour   * pi / 180) + lineHourY1
	
	
	surface.SetDrawColor( 0, 0, 0, 170 )
	_DrawCircle(lineHourX1, lineHourY1, 110, 150)
	
	surface.SetDrawColor( 0, 0, 0, 170 )
	--_DrawCircleOutline(lineHourX1, lineHourY1, 110, 12)
	_DrawRadialPoints( lineHourX1, lineHourY1, 0, 10, 110, 12 )
	
	surface.SetDrawColor(0,0,255,255)
	surface.DrawLine( lineSecondX1, lineSecondY1, lineSecondX2, lineSecondY2 )
	
	surface.SetDrawColor(0,255,0,255)
	surface.DrawLine( lineMinuteX1, lineMinuteY1, lineMinuteX2, lineMinuteY2 )
	
	surface.SetDrawColor(255,0,0,255)
	surface.DrawLine( lineHourX1, lineHourY1, lineHourX2, lineHourY2 )
    
end 

hook.Add("HUDPaint", "lolClock", tmrClock)