Input string
Blitz3D Forums/Blitz3D Beginners Area/Input string
| ||
Hi there. I'm asking the user to enter an extremely long input string, like a whole paragraph. The app displays in a 640x480 window. How do I get the cursor to wrap so the entered text doesn't go right off the side of the screen? It hard to type where you can't see :( Thanks. |
| ||
Can you post some code? You're probably not using input$... |
| ||
Sure, here you go. comment$ = Input("Type a descrption to appear under 'Comments:' - ") Do I need to put $ next to Input for incoming strings? I didn't realize, since it does work without it. *edit* Nope, doesn't work with Input$ either. |
| ||
For a lot of info, you'll need to write your own input routine, using Keyhit to see which key has been hit, then Printing it yourself, going to another line as needed. And you'll also need to trap Backspace and Delete to delete what's been input and trap Enter to know when input is over. The Locate command will be your best friend during all this. It's certainly do-able, but a bit dull to code. An alternative is to use a GUI (like Yappy's Xlnt GUI) or else use BlitzPlus (which I assume has a TextBox-type object like Visual Basic does). The thing is, Blitz3D (like the old Blitz2D) is aimed at doing game-type stuff, and typing in a paragraph is a rare feature in most games. You can still do it, it just takes a little work. This is the opposite of "business app" languages like Visual Basic, which are *built* to do things like accept many paragraphs of input with a handy TextBox object (or even a rich-text editor, complete with font changes), but which *suck* at creating games. :) My quick visit to the Blitzbasic.com code archives didn't turn up a suitable function (though I might have missed it), and Blitzcoder.com is still in the process of moving, so I can't check there. If I'd ever written what you describe, I'd be glad to share it, but I haven't. So, you could write your own, or maybe someone else has a coded a robust paragraph hander before... Anyone? Bueller? Anyone? :) |
| ||
Second Chance: Is this any help? It's very basic but should give you some ideas (first you'd want to 'add up' all the individual letters to get the total input string, hint: userinput$=userinput$+s$) Next you'll need to process special characters - like backspace,enter,cursor keys etc etc. Graphics 640,480,16,1 fnt=LoadFont("Arial",24) SetFont(fnt) Cls Text 0,0,"Enter a big string then hit return" ;start position of our input string x=0 y=32 While Not KeyDown(28) letter=WaitKey() s$=Chr(letter) ; get the width in pixels of the letter moveright=StringWidth (s$) ;display it Text x,y,s$ ;move print position (X) right by the width ofthe character just printed x=x+moveright ;reached edge? if so go to next line and start at left again If x>640 Then x=0 y=y+24 EndIf Wend WaitKey() End |
| ||
For what you want to do, you'll need to write your own "basic text editor". This is not very difficult: You'll need: a) a routine to read keyboard input. b) interpret the data (check for controls) also insert codes for new line, etc c) store data d) screen printing routine. use "locate" and "write", and a fixed font will avoid a lot of trouble. |
| ||
Well here you have some very basic code, but you can delete and wrap words. ;******* VERY BASIC TEXT EDITOR ******** ; ; By MadMax (Made in 5 minutes) ; ;*************************************** Graphics 640,480,16 ;Include "key_code.bb" Dim novel(5000); This should allow for a page of text curson=CreateImage(8,8) cursoff=CreateImage(8,12); these to are the cursors SetBuffer ImageBuffer(curson) Color 255,0,255 Rect 0,0,8,8,True SetBuffer ImageBuffer(cursoff) Color 0,0,0 Rect 0,0,8,12,True SetBuffer FrontBuffer() Color 255,255,255 MaskImage cursoff,50,50,50 h=10;horizontal cursor location y=10;vertical cursor location ;********** MAIN LOOP ************* While Not KeyDown(1) DrawImage curson,h,y+4 value=GetKey() If value<>0 ;******** DELETE **************** If value=8 DrawImage cursoff,h,y+2 h=h-8 DrawImage cursoff,h,y+2 pos=pos-1 EndIf ;******** CARRIAGE RETURN ****** If value=13 DrawImage cursoff,h,y+2 h=10 y=y+16 pos=pos+1 EndIf ;******** TEXT PRINT ************ If value>31 novel(pos)=value pos=pos+1 DrawImage cursoff,h,y+2 Locate h,y Write Chr$(value) h=h+8 ;*********** CHECK LINE END ********* If h>630 ; Here if the end of the line has been reached ; it starts a new one, but only if the end of line ; coincides with the end of a word. If value=32 h=10 y=y+16 ; Here what to do if the line end doesn't ; coincide with the end of word Else wrap=pos; remember position ;******* delete word ********** Repeat DrawImage cursoff,h,y+2 h=h-8 pos=pos-1 Until novel(pos)=32 h=10 y=y+16 pos=pos+1 ;******* reprint word in new line ***** Repeat Locate h,y Write Chr$(novel(pos)) h=h+8 pos=pos+1 Until pos=wrap EndIf EndIf EndIf EndIf Wend Hope it helps. |
| ||
This text editor will not work if you press the DELETE key until it hits the beginning of the line. It should "reverse-word-wrap" up to the end of the previous line. Instead, it justs goes to a negative horizontal position, and then the text is botched up, because the pos variable is inaccurate, not reflecting the true text location place, and the text on the screen does not reflect what text should really be displayed.. |
| ||
Yeah, that's true. It's just a small sample of how to do what was asked in the question, shouldn't be to difficult to add a few lines to amend this. It also doesn't do many other things and shouldn't be considered a text editor. anyway add these lines If h<10 y=y-16 h=hrem+8 pos=posrem+1 DrawImage cursoff,h,y+2 EndIf in the following routine ;******** DELETE **************** If value=8 DrawImage cursoff,h,y+2 h=h-8 DrawImage cursoff,h,y+2 pos=pos-1 ADD IT HERE EndIf Ooops! almost forgot you'll need to add hrem=h posrem=pos in the routine *** delete word **** just after where it says Until novel(pos)=32 ____________ Off course there are other issues as what happens if you delete when you are at line 1 column 1. But I'd rather be coding my own stuff ;) |
| ||
Whew! What a lot of great help! But that's definitely too much to do for what I want. I was just writing a tiny helper app to speed up finishing my game (nothing to do with writing code). I'm already using XLint for my game interface, so I guess I'll just use that (no reason to re-invent the wheel). I should have thought to do that in the first place. Thanks for all that awsome help though, you guys are great :) |
| ||
Oh... you *are* using Input$... well the reason I originally said you probably weren't was because I tried it and it *did* wrap... ...but that was BlitzPlus, and you're probably using Blitz3D or 2D |
| ||
BlitzPlus's Input$ *wraps*? #$%*! That's just too easy <g>. I now await BlitzMax more eagerly than before. I await it *eagererly*! |
| ||
I recently asked Mark Sibly (via email) "Are the features added to make Blitz2d into BlitzPlus, available to Blitz3d users like myself?" He said, "Hi, No, not yet. Blitz3D currently has no GUI features." Alas, and also, sigh. Here's hoping BlitzMax comes down the line soon. |