Page 3 of 3

Re: text based game

Posted: Tue Jul 20, 2010 4:50 pm
by kikito
I feel obliged to post this: ascii sector.

Re: text based game

Posted: Fri Jul 23, 2010 3:34 pm
by pwniknoobz
Hey supertails - heres a class I made for Love2d that simulates textboxes to gather user input. It's just a basic start, but should give you a starting point for handling text based user input on the gui.

I have attached the 2 necessary lua files to be required in main.lua.

Here is a simple example of how to implement the textbox class:

Code: Select all

function love.load()

txtNewBox[1] = TextBox:new()
txtNewBox[2] = TextBox:new()
txtNewBox[0] = 2 -- INDEX 0 OF A TEXTBOX ARRAY HOLDS THE VALUE OF THE TOTAL NUMBER OF TEXT BOXES IN THE ARRAY, MANUALLY SET THIS

txtNewBox[1]:setParams (100,100,12,0,'textbox.png','textbox_selected.png',true,true,'(Enter Nick Name)')
txtNewBox[2]:setParams (100,200,5,0,'textbox.png','textbox_selected.png',true,true,'(Click to enter value)')

end

function love.draw()

drawTextBoxes(txtNewBox)

end

function love.mousepressed(x, y, button)

checkTextBoxesClicked (txtNewBox, x, y)

end

function love.keypressed(k)

updateTextBoxesText (txtNewBox, k)

end
text_box.lua is set up to handle an array of textboxes. Index[0] of each array must hold the total number of textboxes in the array (starting with 1).

Parameters:

setParams (x,y,maxChars,txtType,txtBoxImg,txtBoxImgSelected,enabled,visible,sleepText)

x: The x position of the textbox.
y: The y position of the textbox.
maxChars: The maximum number of characters allowed in the textbox.
txtType: N/A at this point, to be used with further development of the class, may leave blank for now.
txtBoxImg: A file path string to the textbox image you would like to use.
txtBoxImgSelected: A file path string to the textbox image you would like the textbox to change to when it is selected.
enabled: Self explanatory, as of now the only impact this variable makes is that if the textbox is not enabled, it will not draw, and can not be selected/typed in.
visible: Same as enabled for now.
sleepText: When a textbox loses focus, it's text will be reset to sleepText.

This class still needs some development, such as variables to control the specific placement of the cursor inside the textbox image in order to adapt to various textbox images. Also, this class does not limit what keys can be typed into it; I.E. if you press the spacebar it will type "spacebar" per Love2d's key constants.

If you would like to manually adapt the class for YOUR specific textbox image, and align the cursor into the correct position, you can play around with the love.graphics.print calls in the TextBox:draw() function. There are 3 instances of print in TextBox:draw(), such as this:

love.graphics.print(self.text, self.x + 8, self.y + self.height - 10)

To adjust the placement of the cursor, modify the "self.x + 8" and the "self.height - 10" to correctly align the cursor within your textbox image.

Let me know if this helps!

Re: text based game

Posted: Sat Jul 24, 2010 12:08 am
by Robin
pwniknoobz wrote:

Code: Select all

txtNewBox[0] = 2 -- INDEX 0 OF A TEXTBOX ARRAY HOLDS THE VALUE OF THE TOTAL NUMBER OF TEXT BOXES IN THE ARRAY, MANUALLY SET THIS
Why? What is wrong with #txtNewBox?