Okay, here's my proposal for a function to get the player's name or initials for a high score list. The code is documented, and should be very easy to use.
I'm looking for comments, crits, suggestions for improvements, etc.
baxslash has final approval on using the function for ADG Arcade!, but I'm releasing the code for *anyone* to use and abuse, so hack away. The attached project is a demo on how it looks and works.
` ****************************************
` THE ACD_get_name() FUNCTION ************
` ****************************************
` PARAMETERS:
` characters - this is how many characters you want the player to be able
` to input. The minimum is 3 characters, the maximum is 9 characters.
` prompt$ - what the player sees above the character input boxes.
` For example, "You made the high score board! Enter your initials below."
` If the whole prompt$ is too wide for the window, put a chr(10) in the middle
` to force a line break, but only two lines worth will fit.
` The prompt is centered above the character input boxes.
` default$ - appears in the character boxes at the start, for instance "XXX",
` or the player's previous input, if you have kept track of it. You can set this
` to a null string to start the boxes empty. If you do not set it to null, and the
` string length does not match the number of characters parameter, the string
` will be adjusted to fit.
` fontID - the ID of the image you are using for text. Set to zero to use Avenir.
` fontsize - how big the characters are. Set to zero to match the box size.
function ACD_get_name(characters as integer, prompt$ as string, default$ as string, fontID as integer, fontSize as integer)
charTimer# = timer()
spinnerImg1 = LoadImage("ACD_Spinner1.png")
spinnerImg2 = LoadImage("ACD_Spinner2.png")
defaultFnt = LoadImage("Avenir.png")
boxImg = LoadImage("ACD_Box.png")
if characters < 3 then characters = 3
if characters > 9 then characters = 9
if len(default$) > characters then default$ = left(default$, characters)
if len(default$) < characters then default$ = default$ + spaces(characters - len(default$))
vWidth = GetVirtualWidth()
vHeight = GetVirtualHeight()
boxSize = (vWidth / 11)
boxStart = (vWidth / 2) - (((characters * (boxSize + 10) / 2)))
dim letter[characters]
dim sprite[3, characters]
dim char[characters]
for s = 1 to characters
sprite[1, s] = CreateSprite(spinnerImg1)
SetSpriteSize(sprite[1, s], boxSize, -1)
SetSpritePosition(sprite[1, s], boxStart + (s - 1) * (boxSize + 10), (vHeight / 2) - (boxSize * 1.5) - 5)
SetSpriteDepth(sprite[1, s], 1)
sprite[2, s] = CreateSprite(boxImg)
SetSpriteSize(sprite[2, s], boxSize, boxSize)
SetSpritePosition(sprite[2, s], boxStart + (s - 1) * (boxSize + 10), (vHeight / 2) - (boxSize / 2))
SetSpriteDepth(sprite[1, s], 1)
sprite[3, s] = CreateSprite(spinnerImg2)
SetSpriteSize(sprite[3, s], boxSize, -1)
SetSpritePosition(sprite[3, s], GetSpriteX(sprite[2, s]), GetSpriteY(sprite[2, s]) + boxSize + 5)
SetSpriteDepth(sprite[3, s], 1)
char[s] = asc(mid(default$, s - 1, 1))
letter[s] = CreateText(mid(default$, s - 1, 1))
if fontID = 0
SetTextFontImage(letter[s], defaultFnt)
SetTextColor(letter[s], 0, 0, 0, 255)
else
SetTextFontImage(letter[s], fontID)
endif
SetTextAlignment(letter[s], 1)
if fontSize = 0 then fontSize = boxSize
SetTextSize(letter[s], fontSize)
SetTextPosition(letter[s], GetSpriteX(sprite[2, s]) + boxSize / 2, GetSpriteY(sprite[2, s]) + (boxSize / 2) - (fontSize / 2.5))
SetTextDepth(letter[s], 0)
next s
promptTxt = CreateText(prompt$)
if fontID = 0
SetTextFontImage(promptTxt, defaultFnt)
SetTextColor(promptTxt, 255, 255, 0, 255)
SetTextSpacing(PromptTxt, 3)
else
SetTextFontImage(promptTxt, fontID)
endif
SetTextAlignment(promptTxt, 1)
SetTextDepth(promptTxt, 0)
SetTextSize(promptTxt, fontSize / 1.5)
SetTextPosition(promptTxt, vWidth / 2, vWidth / 10)
enterSpr = CreateSprite(boxImg)
SetSpriteSize(enterSpr, vWidth / 4, vHeight / 8)
SetSpriteDepth(enterSpr, 1)
SetSpritePosition(enterSpr, (vWidth / 2) - (GetSpriteWidth(enterSpr) / 2), (vHeight / 8) * 5.5)
enterTxt = CreateText("DONE")
SetTextFontImage(enterTxt, defaultFnt)
SetTextColor(enterTxt, 0, 0, 0, 255)
SetTextAlignment(enterTxt, 1)
SetTextDepth(enterTxt, 0)
SetTextSpacing(enterTxt, 5)
SetTextSize(enterTxt, GetSpriteHeight(enterSpr) * 0.75)
SetTextPosition(enterTxt, (vWidth / 2), GetSpriteY(enterSpr) + GetSpriteHeight(enterSpr) / 6)
do
for s = 1 to characters
SetSpriteColor(sprite[1, s], 255, 255, 255, 255)
SetSpriteColor(sprite[3, s], 255, 255, 255, 255)
next s
if GetPointerState() = 1
x# = GetPointerX()
y# = GetPointerY()
hit = GetSpriteHit(x#, y#)
for s = 1 to characters
if hit = sprite[1, s] OR hit = sprite[3, s]
SetSpriteColor(hit, 255, 55, 55, 255)
endif
next s
for s = 1 to characters
if hit = sprite[1, s]
if timer() > charTimer# + 0.3
charTimer# = timer()
inc char[s]
if char[s] = 91 then char[s] = 32
SetTextString(letter[s], chr(char[s]))
endif
endif
if hit = sprite[3, s]
if timer() > charTimer# + 0.3
charTimer# = timer()
dec char[s]
if char[s] = 31 then char[s] = 90
SetTextString(letter[s], chr(char[s]))
endif
endif
next s
endif
if GetPointerReleased() then charTimer# = timer() - .5
if GetPointerReleased() AND hit = enterSpr then exit
sync()
loop
name$ = ""
for s = 1 to characters
name$ = name$ + chr(char[s])
DeleteText(letter[s])
DeleteSprite(sprite[1, s])
DeleteSprite(sprite[2, s])
DeleteSprite(sprite[3, s])
next s
DeleteImage(spinnerImg1)
DeleteImage(spinnerImg2)
DeleteImage(defaultFnt)
DeleteImage(boxImg)
DeleteSprite(enterSpr)
DeleteText(promptTxt)
DeleteText(enterTxt)
` All variables used are local, and are destroyed on function end.
` All images, sprites and text objects used by the function have been deleted.
` name$ holds the return value.
endfunction name$