@name EGP Mouse
@inputs EGP:wirelink
@outputs Coords:vector2 Click
@persist [Coords1 Coords2]:vector2 Dir E:entity RepeatTime

interval(10)

if(first() | duped() | ~EGP | changed(numPlayers()))
{
    E=EGP:entity()
    RepeatTime=1000
    
    #BG
    EGP:egpBox(1,vec2(),vec2(512,512),vec4(0,200,255,255))
    
    #Button1
    EGP:egpBox(2,vec2(66,386),vec2(132,68),vec4(0,0,0,255))
    EGP:egpBox(3,vec2(64,384),vec2(128,64),vec4(0,100,255,255))
    EGP:egpText(4,"Test",vec2(80,395),vec4(0,255,50,255))
    EGP:egpSetFont(4,"coolvetica",50)
    
    #Offsets
    if( E:model() == "models/props/cs_office/tv_plasma.mdl") {
        Coords1=vec2(-28.5,36)
        Coords2=vec2(28.5,2)
        Dir=1
    }
    elseif( E:model() == "models/props/cs_office/computer_monitor.mdl") {
        Dir=1
        Coords1=vec2(-10.5,24.7)
        Coords2=vec2(10.5,8.6)
    }   
    elseif( E:model() == "models/props_lab/monitor01b.mdl") {
        Dir=1
        Coords1=vec2(-5.535,5.091)
        Coords2=vec2(3.5,-4.1)
    }   
    elseif( E:model() == "models/kobilica/wiremonitorsmall.mdl") {
        Dir=1
        Coords1=vec2(-4.4,9.5)
        Coords2=vec2(4.5,0.6)
    }   
    elseif( E:model() == "models/kobilica/wiremonitorbig.mdl") {
        Dir=1
        Coords1=vec2(-11.6,24.5)
        Coords2=vec2(11.6,1.5)
    }   
    elseif( E:model() == "models/hunter/plates/plate2x2.mdl" ){
        Dir=2
        Coords1=vec2(-46.5,-46.5)
        Coords2=vec2(46.5,46.5)
    }   
    elseif( E:model() == "models/hunter/plates/plate4x4.mdl" ){
        Dir=2
        Coords1=vec2(-94.8,-94.8)
        Coords2=vec2(94.8,94.8)
    }   
    elseif( E:model() == "models/hunter/plates/plate1x1.mdl" ){
        Dir=2
        Coords1=vec2(-23,-23)
        Coords2=vec2(23,23)
    }   
    elseif( E:model() == "models/hunter/plates/plate8x8.mdl" ){
        Dir=2
        Coords1=vec2(-189.8,-189.8)
        Coords2=vec2(189.8,189.8)
    }   
}

#Find closest player
findByClass("player")
findSortByDistance(E:massCenter())
Ply = find()
Aim = E:toLocal(Ply:aimPos())

if (Dir==1){
    Coords=(Coords1 - vec2(Aim:y(),Aim:z()))/(Coords1-Coords2)*513
    Z = Aim:x()
}
elseif (Dir==2) {
    Coords=(Coords1 - vec2(Aim:y(),Aim:x()))/(Coords1-Coords2)*513
    Z = Aim:z()
}
    
Dist=Ply:shootPos():distance(E:massCenter())
IsAimingOn=(Dist<max(E:radius()*2,30) & Ply:aimEntity()==E  & inrange(Coords,vec2(1,1),vec2(513,513)))

if (IsAimingOn) {
    Click=Ply:keyUse()
}
else {
    Coords=vec2()
}

if (inrange(Coords,vec2(64,384),vec2(192,448))) {
    EGP:egpBox(3,vec2(65,385),vec2(128,64),vec4(0,100,200,255))
    EGP:egpText(4,"Test",vec2(81,396),vec4(0,255,50,255))
    EGP:egpSetFont(4,"coolvetica",50)
    if (Click){
        EGP:egpBox(3,vec2(66,386),vec2(128,64),vec4(200,100,0,255))
        EGP:egpText(4,"Test",vec2(82,397),vec4(0,255,50,255))
        EGP:egpSetFont(4,"coolvetica",50)
        if ($Click) {
            soundPlay(0,0,"buttons/blip1.wav")
        }
    }
}
else {
    EGP:egpBox(3,vec2(64,384),vec2(128,64),vec4(0,100,255,255))
    EGP:egpText(4,"Test",vec2(80,395),vec4(0,255,50,255))
    EGP:egpSetFont(4,"coolvetica",50)
}

#EGP:egpTriangle(40,Coords,Coords+vec2(6,5),Coords+vec2(0,10),Click ? vec4(255,0,0,200) : vec4(0,0,255,200))

Cursor = array(Coords,Coords+vec2(16,0),Coords+vec2(9,7),Coords+vec2(16,14),Coords+vec2(14,16),Coords+vec2(8,9),Coords+vec2(0,16))
EGP:egpPoly(40,Cursor)
EGP:egpDraw()
 
