Post by u9 on Feb 26, 2008 10:25:47 GMT -5
Here's a little class which you can use to get the user to enter an IP address. Who knows, maybe it is useful now that Brutus has network support.
' This class allows the user to enter an ip address using the keyboard.
' To use, call Update() in your game loop until it returns true (user pressed
' enter). Display the currently entered IP in the ip member variable.
' A demo program is at the bottom of the file.
'
' Public Domain. Done Feb 2008 by u9. u9 AT kallnet D0T fo
class IPRequester
Public ip
Private keyDown
Public Sub Initialize( newip )
If newip <> "" Then
ip = newip
Else
ip = net.GetmasterServerIP()
End If
keyDown = False
End Sub
' Reads the keyboard and writes the required char to end of ip string
Public Function Update()
' Read what key is pressed
Dim any : any = key.pressedAny
If any = vk_return Or any = 156 Then ' 156 is enter
If isValid() Then
Update = False
keyDown = vk_return
End If
ElseIf keyDown = vk_return Then
Update = True
ElseIf keyDown = False Then
keyDown = any
If any >= 2 And any <= 11 Then ' numbers
ip = ip & ( ( any - 1 ) Mod 10 )
ElseIf any = 52 Or any = 83 And len( ip ) > 0 Then ' the periods
If right( ip, 1 ) <> "." Then
ip = ip & "."
End If
ElseIf any = 82 Then ' numpad 0
ip = ip & "0"
ElseIf any >= 79 And any <= 81 Then ' numpad 1-3
ip = ip & (any-78)
ElseIf any >= 75 And any <= 77 Then ' numpad 4-6
ip = ip & (any-71)
ElseIf any >= 71 And any <= 73 Then ' numpad 7-9
ip = ip & (any-64)
ElseIf any = vk_backspace And len( ip ) > 0 Then
ip = left( ip, len( ip ) - 1 )
Else
keyDown = False
End If
ElseIf keyDown <> any Or any = 0 Then
keyDown = False
End If
' Check for max 3 dots
Dim arr, i
arr = split( ip, "." )
If UBound( arr ) > 3 Then
ip = left( ip, len( ip ) - 1 )
End If
' Check current number max 255 and max 3 digits
arr = split( ip, "." )
If UBound( arr ) >= 0 Then
If isNumeric( arr( UBound( arr ) ) ) Then
If arr( UBound( arr ) ) > 255 Or len( arr( UBound( arr ) ) ) > 3 Then
ip = left( ip, len( ip ) - 1 )
End If
End If
End If
End Function
Private Function isValid()
isValid = True
Dim arr : arr = split( ip, "." )
If UBound( arr ) = 3 Then
Dim i
For i = 0 To 3
If isNumeric( arr( i ) ) Then
If arr( i ) > 255 Then
isValid = False
End If
Else
isValid = False
End If
Next
Else
isValid = False
End If
End Function
End class
' --- Example of use ---
graphics.initialize
key.initialize
Call graphics.settitle( "Press enter to accept IP" )
Dim font : font = graphics.createfont( "System", 14 )
Dim i : Set i = New IPRequester
Call i.Initialize( "" )
Do While Not key.pressed( vk_windowx ) And Not key.pressed( vk_escape )
Dim isDone : isDone = i.Update()
If isDone Then Exit Do
Call graphics.clear()
Call graphics.settext( i.ip, 10, 10, font, argb(255,255,255,255) )
Call graphics.display()
Call system.processMessages()
Loop
key.terminate
graphics.terminate