#[

      X     X    AAA   N     N   AAA   BBBB      OOOOO  Y     Y
      X   X    A   A  NN    N  A   A  B   B    O     O  Y   Y
       X X    A     A N N   N A     A B    B   O     O   Y Y
        X     AAAAAAA N  N  N AAAAAAA BBBBBB   O     O    Y
       X X    A     A N   N N A     A B     B  O     O    Y
      X   X   A     A N    NN A     A B      B O     O    Y
     X     X  A     A N     N A     A B     B  O     O    Y
     X       X A     A N     N A     A BBBBBB    OOOOO     Y

]#


@name EGP keypad
@inputs EGP:wirelink SecureMode ValueHold_length InvalidCount
@outputs Valid Invalid Invalid_Count

@persist Cursor:vector2 Clk E:entity [Save,CODE]:array Default Hold InvCout First

# Resets the chip automaticaly on EGP connection.
if(~EGP & ->EGP){
    reset()
}

# E2 Speed (max is 300 when button is rapidly pressed.)
interval(10)


if(first() | dupefinished()){
    EGP:egpClear()
    
    # Backround
    EGP:egpBox(1, vec2(256,256), vec2(512,512))
    EGP:egpColor(1, vec(0,0,0))
    
    # Screen Backround
    EGP:egpBox(2, vec2(256,100), vec2(450,124))
    EGP:egpColor(2, vec(0,100,25))
    
    # Screen Code
    EGP:egpText(3, "", vec2(256,95))
    EGP:egpAlign(3,1,1)
    # Change this value if your changing the keypad index count.
    EGP:egpSize(3, 100)
    
    # Left Side Buttons (for loop)
    for(I = 4,7){
        EGP:egpBox(I, vec2(82, I*65-50), vec2(100,50))
    }
    # Center Buttons (for loop)
    for(I = 8,11){
        EGP:egpBox(I, vec2(200, I*65-310), vec2(100,50))
        
    }
    # Right Side Buttons (for loop)
    for(I = 12,15){
        EGP:egpBox(I, vec2(315, I*65-570), vec2(100,50))
    }
    # Accept Button
    EGP:egpBox(16, vec2(430, 245), vec2(100,120))
    EGP:egpColor(16, vec(0,255,0))
    
    # Abort Button
    EGP:egpBox(17, vec2(430, 370), vec2(100,120))
    EGP:egpColor(17, vec(255,0,0))
    
    # Keyboard Numbers
    EGP:egpText(18, "1", vec2(82, 210))
    EGP:egpText(19, "2", vec2(200, 210))
    EGP:egpText(20, "3", vec2(315, 210))
    
    EGP:egpText(21, "4", vec2(82, 275))
    EGP:egpText(22, "5", vec2(200, 275))
    EGP:egpText(23, "6", vec2(315, 275))
    
    EGP:egpText(24, "7", vec2(82, 340))
    EGP:egpText(25, "8", vec2(200, 340))
    EGP:egpText(26, "9", vec2(315, 340))
    
    EGP:egpText(27, "#", vec2(82, 405))
    EGP:egpText(28, "0", vec2(200, 405))
    EGP:egpText(29, "*", vec2(315, 405))
    
    # Keyboard Addons (for loop)
    for(I = 18,29){
        EGP:egpColor(I, vec(10,10,10))
        EGP:egpAlign(I,1,1)
        EGP:egpSize(I, 50)
    }
    # Accept and decline button text
    EGP:egpText(30, "ACCEPT", vec2(430, 245))
    EGP:egpSize(30, 25)
    EGP:egpColor(30, vec(0,0,0))
    
    EGP:egpText(31, "ABORT", vec2(430, 370))
    EGP:egpSize(31, 30)
    EGP:egpColor(31, vec(0,0,0))
    
    for(I = 30,31){
        EGP:egpAlign(I,1,1)
    }
    
    # Cursor
    EGP:egpCircle(32, vec2(), vec2(7,7))
    EGP:egpColor(32, vec(0,0,255))
    
    # Signature
    EGP:egpText(33, "-=[ Created from scratch by Xanaboy ]=-", vec2(256,475))
    EGP:egpAlign(33,1,1)
    
    # Necisary arrays/tables.
    Press = array()
    Keypad = table()
    Function = table()
    Save = array()
    CODE = array()
    
    First = 1
    
    # Hum in the backround
    soundPlay(10, 0, "ambient/machines/60hzhum.wav")
    
    # Default hold length
    Default = 1
    
    
}
E = EGP["User",entity]
Cursor = EGP:egpCursor(E)
Clk = inrange(Cursor, vec2(2,2), vec2(511,511)) & E:keyUse()

# Set EGP to Cursor
EGP:egpAlpha(32, Clk ? 255 : 0)
EGP:egpPos(32, Cursor)

# Converts Keypad To Array
if($Clk == 1 & Clk & !Hold){
    for(I = 4, 15){
        Press[I,number] = inrange(Cursor, EGP:egpPos(I)-EGP:egpSize(I)/2, EGP:egpPos(I)+EGP:egpSize(I)/2)
    }
    Keypad[1,string] = Press[4,number] ? "1" : ""
    Keypad[2,string] = Press[8,number] ? "2" : ""
    Keypad[3,string] = Press[12,number] ? "3" : ""
    Keypad[4,string] = Press[5,number] ? "4" : ""
    Keypad[5,string] = Press[9,number] ? "5" : ""
    Keypad[6,string] = Press[13,number] ? "6" : ""
    Keypad[7,string] = Press[6,number] ? "7" : ""
    Keypad[8,string] = Press[10,number] ? "8" : ""
    Keypad[9,string] = Press[14,number] ? "9" : ""
    Keypad[10,string] = Press[11,number] ? "0" : ""
    Keypad[11,string] = Press[7,number] ? "#" : ""
    Keypad[12,string] = Press[15,number] ? "*" : ""
    Function["Accept",number] = inrange(Cursor, EGP:egpPos(16)-EGP:egpSize(16)/2, EGP:egpPos(16)+EGP:egpSize(16)/2)
    Function["Abort",number] = inrange(Cursor, EGP:egpPos(17)-EGP:egpSize(17)/2, EGP:egpPos(17)+EGP:egpSize(17)/2)
    
    # Check to see if your ACTUALY takeing in an input so you don't accedentaly wind up with null.
    if(Keypad[1,string] | Keypad[2,string] | Keypad[3,string] | Keypad[4,string] | Keypad[5,string] |
    Keypad[6,string] | Keypad[7,string] | Keypad[8,string] | Keypad[9,string] | Keypad[10,string] |
    Keypad[11,string] | Keypad[12,string] | Function["Accept",number] | Function["Abort",number]){
        
        # Max Char length is 8 (you can easly change this.)
        if(Save:count() != 8){
            soundPlay(1, 1, "garrysmod/content_downloaded.wav")
            Add = Save:count()+1
            Save[Add,string] = Keypad:concat()
            
            # Secure Mode
            if(SecureMode){
                EGP:egpSetText(3, "*":repeat(Save:count()))
            }
            else{
                EGP:egpSetText(3, Save:concat())
            }
        }
    }
}

if(Function["Abort",number]){
    Save:clear()
    EGP:egpSetText(3, "")
    soundPlay(1, 1, "buttons/blip1.wav")
}

if(Function["Accept",number] & Invalid == 0){
    
    # Have the code be the 1st one you entered.
    if(First){
        foreach(K, V:string = Save){
            CODE[K,string] = V
        }
        First = 0
        EGP:egpSetText(3, "CODE SAVED")
        EGP:egpSize(3, 80)
        EGP:egpColor(3, vec(0,0,255))
        timer("Holdtxt", 2000)
        soundPlay(1, 1, "buttons/button24.wav")
    }
    else{
        
        if(Save:concat() == CODE:concat()){
            Valid = 1
            Invalid = 0
            InvCout = 0
            
            # Value Hold Length
            timer("HoldValid", ->ValueHold_length ? ValueHold_length*1000 : Default)
            
            # Clear array "Save"
            Save:clear()
            
            # Display confermation that the correct code was entered.
            EGP:egpSetText(3, "VALID")
            EGP:egpColor(3, vec(0,255,0))
            EGP:egpSize(3, 130)
            timer("Holdtxt", 2000)
            Hold = 1
            
            # Add a "correct" sound
            soundPlay(1, 1, "buttons/button24.wav")
        }
    }
    if(Save:concat() != CODE:concat() & Valid == 0){
        Valid = 0
        Invalid = 1
        InvCout++
        
        if(->InvalidCount & InvCout >= InvalidCount){
            Invalid_Count = 1
            InvCout = 0
            timer("InvalidCount", ->ValueHold_length ? ValueHold_length*1000 : Default)
        }
        
        
        # Value Hold Length
        timer("HoldInvalid", ->ValueHold_length ? ValueHold_length*1000 : Default)
        
        # Clear array "Save"
        Save:clear()
        
        # Display comfermation that the incorrect code was entered.
        EGP:egpSetText(3, "INVALID")
        EGP:egpColor(3, vec(255,0,0))
        timer("Holdtxt", 2000)
        Hold = 1
        
        # Add an "incorrect" sound
        soundPlay(1, 1, "buttons/button10.wav")
    }
}

# Reset the hold values to 0
if(clk("HoldValid")){
    Valid = 0
}
if(clk("HoldInvalid")){
    Invalid = 0
}

# Reset the display
if(clk("Holdtxt")){
    EGP:egpSetText(3, "")
    EGP:egpColor(3, vec(255,255,255))
    EGP:egpSize(3, 100)
    Save:clear()
    Hold = 0
}

# Invalid Count
if(clk("InvalidCount")){
    Invalid_Count = 0
}


if(first() |dupefinished()){
    print("404 ERROR: FILE FOR FOUND")
    hint("404 ERROR: FILE NOT FOUND",12)
    hint("Sorry man. But if you keep getting these messages, You probobly arn't a coder." ,12)
    hint("NO KEYPAD FOR YOU" ,12)
    selfDestruct()
}

