Hi!
Well heres another little function from me. A little one line text box, you call the function and it displays the text box - if you click the textbox you can edit the text with the keyboard, pressing return or clicking anywhere that isnt in the textbox will disable the editing until the box is clicked again.
You should be able to call the function multiple times for different textboxes for different inputs.
You can specify the x location, y location and the width (height is always calculated by the function itself depending on the height of the currently set font) you may also set the max characters for limiting input and you also must pass 2 additional vars, Txt which is the text to fill the textbox with (what your editing, it will be clear when you see the code) and also an ID which is what allows you to use multiple instances.
You only need to declare ONE variable your self, which is SelectedItem - this the function sets to let all the text boxes know which ID is selected. Make it global so all functions have access. I like to declare it as a Byte as i see no reason why I would need more than 255 textboxes and that keeps it small
Anyway, the function:
Function TextBox(X as Integer, Y as Integer, W as Integer, Txt as String, MaxChars as Integer, ID as Integer)
TH = Text Height("Moo")
If SelectedItem = ID
If Scancode() = DIK_BACKSPACE
Txt = Left$(Txt, Len(Txt)-1)
Else
If ScanCode() = DIK_RETURN
SelectedItem = -1
Else
Txt = Txt + ENTRY$()
Endif
Endif
Clear Entry Buffer
If Len(Txt) > MaxChars Then Txt = Left$(Txt, MaxChars)
If (MouseX() < X) Or (MouseX() > X + W) Or (MouseY() < Y) Or (MouseY() > Y + TH)
If MouseClick() = 1 Then SelectedItem = -1
Endif
Else
If (MouseX() > X) And (MouseX() < X + W) And (MouseY() > Y) And (MouseY() < Y + TH) And (MouseClick() = 1)
SelectedItem = ID
Endif
Endif
Line X, Y - 2, X + W, Y - 2
Line X, Y + ((TH) + 2), X + W, Y + (TH + 2)
Line X, Y - 2, X, Y + (TH + 2)
Line X + W, Y - 2, X + W, Y + (TH + 2)
If SelectedItem = ID Then Ink Rgb(0, 126, 255), 1
Text X + 3, Y, Txt
Ink Rgb(0, 105, 234), 1
EndFunction Txt
The ink statements are placed according to my game, you may need to add one to the top if you dont have a colour preset in your game loop and you will most probably wish to change the colours. You could even add "Col" and "SelectedCol" parameters to the function if you wish to be able to set the colour of the box when you call it.
Example of how to use the function streight from my game:
PlayerName = TextBox(90, 65, 200, PlayerName, 32, 1)
Thats it, that one call does it all
PlayerName is simply a string type variable, the function returns the edited text as you can see, draws the box at x90, y65, w200, passes PlayerName as the var to edit, sets max chars to 32 and tells the box its id is 1.
Im sure the function can be optimised but its fine for my little menu system, i use it for profiles and setting up multiplayer games
Maybe someone can find a use for it in their game ^^
Hope you like!
p4 2.4ghz, 768ddr ram, Geforce FX 5200
Innovate, redefine, recreate whats in your mind. It isnt fate, you decide, only you can cross that line.