local U = require "scriptbank\\utillib" -- Simple script to work with BOD's buttons -- Look at button and click LMB to press it, global variable -- will have name of button pressed. g_buttonPressed = '' local buttons = {} local rad = math.rad local sin = math.sin local cos = math.cos g_FingerSprite = g_FingerSprite or nil function button_init_name(e, name) Include("utillib.lua") buttons[e] = {name = name} if g_FingerSprite == nil then g_FingerSprite = CreateSprite(LoadImage ( "scriptbank\\images\\finger.png")) SetSpriteOffset(g_FingerSprite, -1 , 5) SetSpriteSize (g_FingerSprite, -1 , 10) SetSpriteDepth (g_FingerSprite, 0) SetSpritePosition (g_FingerSprite, 200, 200) end end local downFrame = 18 -- button 'down' frame local upFrame = 1 -- button 'up' frame local butDelay = 30 -- milliseconds between frames local chckDelay = 100 -- milliseconds between checks for player looking at button local fingerShowing = nil function button_main(e) thisButton = buttons[e] if thisButton == nil then return end if thisButton.frame == nil then -- first time initialisation -- set button 'up', i.e. first frame thisButton.frame = 1 SetAnimationFrame(e, thisButton.frame) -- set button state to 'up' thisButton.state = 'up' -- get object id of button thisButton.obj = g_Entity[e].obj -- set delay timer thisButton.timer = g_Time + chckDelay -- mouse flag thisButton.mousePressed = false elseif g_Time > thisButton.timer then if U.PlayerLookingNear( e, 100, 10 ) then if fingerShowing ~= e then fingerShowing = e SetSpritePosition (g_FingerSprite, 50, 45) end if g_MouseClick == 1 then if not thisButton.mousePressed then thisButton.mousePressed = true thisButton.state = 'down' thisButton.timer = g_Time + butDelay end elseif thisButton.mousePressed then thisButton.mousePressed = false thisButton.state = 'up' thisButton.timer = g_Time + butDelay else thisButton.timer = g_Time + chckDelay end elseif fingerShowing == e then SetSpritePosition (g_FingerSprite, 200, 200) fingerShowing = nil end if fingerShowing ~= e then if thisButton.mousePressed then thisButton.mousePressed = false thisButton.state = 'up' thisButton.timer = g_Time + butDelay else thisButton.timer = g_Time + chckDelay end end -- animate button if thisButton.state == 'down' then if thisButton.frame < downFrame then thisButton.frame = thisButton.frame + 1 SetAnimationFrame(e, thisButton.frame) thisButton.timer = g_Time + butDelay return elseif g_buttonPressed ~= thisButton.name then g_buttonPressed = thisButton.name end else if thisButton.frame > upFrame then thisButton.frame = thisButton.frame - 1 SetAnimationFrame(e, thisButton.frame) thisButton.timer = g_Time + butDelay return elseif g_buttonPressed == thisButton.name then g_buttonPressed = '' end end thisButton.timer = g_Time + chckDelay end end