Skip to content

Instantly share code, notes, and snippets.

@NanoAi
Last active April 19, 2023 19:39
Show Gist options
  • Save NanoAi/865901b41c86029ce5787728f87e8949 to your computer and use it in GitHub Desktop.
Save NanoAi/865901b41c86029ce5787728f87e8949 to your computer and use it in GitHub Desktop.
A nice little spinner for Garry's Mod GUI.
local function mouseToAngle(panel, w, h)
local x, y = gui.MousePos()
local globalX, globalY = panel:LocalToScreen(w / 2, h / 2)
local aX = x - globalX
local aY = y - globalY
local cosX = aX / math.sqrt(math.pow(aX, 2) + math.pow(aY, 2))
local ang = ((math.atan2(aY, aX) * 180 / math.pi) * -1) -- math.deg(math.acos(cosX))
ang = (ang - 90)%360
return math.floor( ang )
end
local function CreateSpinner(text, bind)
local convar = GetConVar(bind)
local col = SKIN.Colours.Label.Dark or Color(0, 0, 0, 255)
local img = Material('widgets/disc.png')
local lastRotation = 0
local p = vgui.Create("DButton")
p:SetText(text)
p.rotation = 0
function p:Paint(w, h)
local ow, oh = 0, 0
local angle = p.rotation .. "°"
if p.down then
lastRotation = p.rotation
p.rotation = mouseToAngle(self, w, h)
if lastRotation ~= p.rotation then
self:UpdateConVar()
end
end
surface.SetMaterial(img)
surface.SetDrawColor(col.r, col.g, col.b)
surface.DrawTexturedRectRotated(w/2, h/2, h, h, self.rotation)
surface.SetFont( "DermaDefault" )
surface.SetTextColor( col.r, col.g, col.b, col.a )
ow, oh = surface.GetTextSize( angle )
surface.SetTextPos( w/2 - ow/2, h/2 - 20 )
surface.DrawText( angle )
return false
end
function p:UpdateConVar()
convar:SetInt(p.rotation)
end
function p:OnMousePressed()
p.down = true
end
function p:OnMouseWheeled(sd)
local val = (p.rotation + sd)
p.rotation = val%360
return true
end
function p:OnMouseReleased()
p.down = false
end
p:SetSize(100, 100)
return p
end
@NanoAi
Copy link
Author

NanoAi commented Apr 8, 2018

Special thanks to DBotThePone for helping me with the math because my brain stopped working at 5 AM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment