|
Post by Blaze on Oct 21, 2007 17:49:46 GMT -5
Hello, I am attempting to create a type of text editor. I was able to save the typed text to a variable and then display it. I was also successful on saving it to a file. But when I added a way to delete characters(with backspace) Typing text no longer works anymore. When you press a button nothing shows up, but if you press a whole bunch of buttons at the same time, then certain buttons show up, while others don't. I also commented on all my code(I mean all of it). The comments became unaligned when copied into post. Here is the source code:
TextMode(TEXT_BUFFERED) 'Text Mode
Dim file Dim text as String 'Variables Dim dif
while ScanKeyDown(vk_ESCAPE) = 0 'Button to exit if Inkey$() <> vk_BACK then 'Avoid Backspace to be added to 'text' string text = text + Inkey$ () 'Add characters to 'text' string endif cls 'Avoid string 'text' from continously being added print text 'Display string 'text' DrawText() 'Update screen if ScanKeyDown(vk_F2) then 'Button to save to file file = OpenFileWrite ("SavedFile.txt") 'Open(create) file for saving WriteString(file, text) 'Save string 'text' to file printr " " printr "File Saved" 'Tell user that the file has been saved DrawText() 'Update screen sleep(3000) ' Wait 3 seconds before text diapears endif if ScanKeyDown(vk_BACK) then 'Code to be able to delete text dif = len(text) - 1 text = Left$(text, dif) endif wend
|
|
|
Post by matthew on Oct 22, 2007 4:40:48 GMT -5
Try taking a look at this post by davy and this post by nicky. Both of them show ways of handling backspaces. EditHave a look at this post as well, it shows a text editor that davy made.
|
|
|
Post by Nicky Peter Hollyoake on Oct 22, 2007 8:12:54 GMT -5
The one I did before wasn't to good I found out that its easier to use "ClearKeys()" when backspacing so another word won't be added on this is a nice & easy way of doing it (sorry if theres any mistakes the computer i'm using doesn't have B4GL and I can't download it ...).
TextMode (TEXT_BUFFERED) SetTextScroll(FALSE)
Dim Word$
While TRUE: CLS
If ScanKeyDown (VK_BACK) Then Word$ = Left$(Word$, Len(Word$)) ClearKeys()
Else Word$ = (Word$ + Inkey$()) EndIf
Locate 0, TextRows: Print Right$(Word$, TextCols())
DrawText() Wend
EDIT:
I'm on my computer now and i've corrected the mistakes and added the file write you wanted.
TextMode (TEXT_BUFFERED) SetTextScroll(FALSE)
Dim Word$ Dim File
While TRUE: CLS
If ScanKeyDown (VK_BACK) Then Word$ = Left$(Word$, Len(Word$) - 1) ClearKeys()
Else Word$ = (Word$ + Inkey$()) EndIf
Locate 0, TextRows(): Print Right$(Word$, TextCols())
If Word$ <> "" Then Gosub __ADD_TEXT_TO_FILE EndIf
DrawText() Wend
__ADD_TEXT_TO_FILE: File = OpenFileWrite("Data/TextFile.TXT") WriteLine(File, Word$) CloseFile(File) Return
|
|
|
Post by Blaze on Oct 22, 2007 14:47:14 GMT -5
Thanks, I will have to look at all the links you gave me. And the code. I looked quickly through your(nicky) post. So I am guessing the ClearKeys() will clear the Backspace before it is added to the string? I am gonna check the documentation. EDIT: I looked for a tutorial on making a text editor. But I couldn't find one. Oh well, I think I learned more having to do it all myself. EDIT2: Ok, now I am trying to load the Saved File. I can load it, but all the blank lines are deleted. So if you had for example: Hello John,
How are you doing?
It would show up as: Hello John, How are you doing?
How would I fix it? I tried using an if statement to see if the line was empty, then put a space there. But it ended up deleting more. What is the problem? Source: TextMode(TEXT_OVERLAID) 'Text Mode Dim file Dim line$(100)
Dim lnum Dim key Dim L Dim i Dim w Dim o Dim fopen Dim load$ Dim con
struc STextbox dim Width dim Height dim X dim Y endstruc
dim STextbox Textbox Textbox.Y=0 Textbox.X=0 Textbox.Width=40 Textbox.Height=20
fopen = OpenFileRead("SavedFile.txt") if FileError () = "" then printr "Saved File found." printr " Would you like to load it? (y/n)" INPUT ">", load$ if load$ = "y" then for o = 0 to Textbox.Height - 1 line$(o) = ReadLine(fopen) next Gosub Update2 INPUT "Press enter to continue...", con else printr "Ok, if you save this file, the old one will be deleted." Gosub Update2 INPUT "Press enter to continue...", con CloseFile(fopen) endif else printr "No saved file found. Make sure it is in the same directory." Gosub Update2 INPUT "Press enter to continue...", con CloseFile(fopen) endif
while ScanKeyDown(vk_ESCAPE) = 0 'Button to exit
key=asc(inkey$()) if key>31 and key<127 then if lnum<=Textbox.Height-1 then Line$(lnum)=Line$(lnum)+chr$(key) Gosub Update else beep() endif endIf
if key = 8 then if L > 0 then L=Len(Line$(lnum))-1 Line$(lnum)=Left$(Line$(lnum), L) L=L-1 Gosub Update else if lnum > 0 then lnum = lnum - 1 L=Len(Line$(lnum))-1 Line$(lnum)=Left$(Line$(lnum), L) L = L - 1 Gosub Update else Beep() endif endif endif
if key = 13 then if lnum < Textbox.height - 1 then lnum = lnum + 1 L = Len(Line$(lnum)) - 1 Gosub Update else Beep() endif endif
L = Len(line$(lnum)) if L = Textbox.Width then lnum = lnum + 1 endif 'Update screen if ScanKeyDown(vk_F2) then 'Button to save to file file = OpenFileWrite ("SavedFile.txt") 'Open(create) file for saving if FileError () <> "" then print FileError (): end endif for w = 0 to lnum WriteLine(file, Line$(w)) 'Save string 'text' to file next CloseFile(file) printr " " printr "File Saved" 'Tell user that the file has been saved DrawText() 'Update screen SwapBuffers() sleep(500) ' Wait 3 seconds before text disapears endif wend Update: cls glClear(GL_COLOR_BUFFER_BIT) Locate L+1, lnum Print "|" DrawText() for i=0 to Textbox.Height Locate 0+Textbox.X, i+Textbox.Y Print Line$(i) next DrawText() SwapBuffers() Return
Update2: DrawText() SwapBuffers() ClearKeys() Return Update3: cls glClear(GL_COLOR_BUFFER_BIT) ClearKeys() Return
Edit3: I fixed it. Instead of converting the blank lines when I load it, I just do it when it is saved. Edit4: Does anyone know what the default text color is? In my text editor I change it, then I need to change it back.
|
|
|
Post by Nicky Peter Hollyoake on Oct 25, 2007 10:32:48 GMT -5
The default color is: 255, 255, 255
So it will be like ...
Color(255, 255, 255)
|
|
|
Post by Blaze on Oct 25, 2007 19:36:40 GMT -5
That would be white. But the default is a little grayer. I just used 220, 220, 220. It's not perfect but it works. Although I am gonna change it soon. I am gonna but a white background so the text will be darker.
|
|