home *** CD-ROM | disk | FTP | other *** search
/ DarkBASIC - The Ultimate 3D Game Creator / PCactive 8 CD1 - DarkBasic.iso / SOFTWARE / DEMOS / Wordwrap / console.dba next >
Encoding:
Text File  |  2000-04-28  |  3.3 KB  |  136 lines

  1. `Here's a console function to get user input and display text.
  2. `You can change the size of the text area, and when you get to the 
  3. `bottom, it scrolls up.
  4.  
  5. `Send con(): 
  6. `"cr" for a carriage return (new line)
  7. `"input" to get input string and assign it to answer$(1)
  8. `any other string to print it to screen with word wrap...
  9.  
  10. `until there's an Image Exists() command, use a global flag...
  11. dim imageflag(1)
  12. `answer$(1) holds what the user inputs...
  13. dim answer$(1)
  14.  
  15. `ask enough times to demonstrate scrolling...
  16. for j=1 to 2
  17.     con("What's your name? ")
  18.     con("input") 
  19.     name$=answer$(1)
  20.     a$="Whaaaazzzup, " + name$ + " !!!  "
  21.     con(a$)
  22.     a$="Here's a test string, " + name$ + ".  "
  23.     for k=1 to 5
  24.         con(a$)
  25.     next k
  26.     con("cr")
  27.     con("cr")
  28. next j
  29.  
  30. `clean up...
  31. if imageflag(1)=1 then delete image 99
  32. undim imageflag(1)
  33. undim answer$(1)
  34. end
  35.  
  36. FUNCTION con(a$)
  37.     `Change these 6 values as needed, all values in pixels.  
  38.     `Make left & right margins > 0, make top & bottom margins 
  39.     `multiples of text height, which by default is 16.
  40.     screenwidth=640 : screenheight=480
  41.     leftmargin=25 : rightmargin=25
  42.     topmargin=16 : bottommargin=16*23
  43.  
  44.     aheight=text height(a$)
  45.     if xover<leftmargin then xover=leftmargin
  46.     if ydown<topmargin then ydown=topmargin
  47.     nextline$=""
  48.     answer$(1)=""
  49.  
  50.     do
  51.         alen=len(a$)
  52.         `get rid of any spaces at beginning of a$...
  53.         while left$(a$,1)=" "
  54.             dec alen
  55.             a$=right$(a$,alen)
  56.         endwhile
  57.  
  58.         `carriage return -- moves cursor to next line...
  59.         if a$="cr"
  60.             xover=leftmargin
  61.             ydown=ydown+aheight
  62.             gosub scrollcheck
  63.             set cursor xover,ydown
  64.             EXITFUNCTION
  65.         endif
  66.  
  67.         `get input from user, assign it to answer$(1)
  68.         if a$="input"
  69.             `comment out next 3 lines to get user input on same line...
  70.             xover=leftmargin
  71.             ydown=ydown+aheight
  72.             gosub scrollcheck
  73.             set cursor xover,ydown
  74.             input answer$
  75.             xover=leftmargin
  76.             ydown=ydown+aheight
  77.             gosub scrollcheck
  78.             answer$(1)=answer$
  79.             EXITFUNCTION 
  80.         endif
  81.  
  82.         awidth=text width(a$)
  83.         room=screenwidth-rightmargin-xover
  84.         `if there's room on the line, display a$, exit do-loop...
  85.         if awidth<room
  86.             text xover,ydown,a$
  87.             xover=xover+awidth
  88.             exit
  89.         endif
  90.  
  91.         `...otherwise go backwards along a$, 
  92.         `moving the last character into nextline$ 
  93.         `until a$ is short enough to fit...
  94.         while awidth>room
  95.             nextline$=right$(a$,1)+nextline$
  96.             dec alen
  97.             a$=left$(a$,alen)
  98.             awidth=text width(a$)
  99.         endwhile
  100.  
  101.         `then count back until it hits a space, 
  102.         `so you don't break mid-word...
  103.         righ$=right$(a$,1)
  104.         while righ$<>" " and alen>0
  105.             nextline$=righ$+nextline$
  106.             dec alen
  107.             a$=left$(a$,alen)
  108.             righ$=right$(a$,1)
  109.         endwhile
  110.  
  111.         `print a$ to screen...
  112.         text xover,ydown,a$
  113.         `and make nextline$ the new a$...
  114.         a$=nextline$ : nextline$=""
  115.         xover=leftmargin : ydown=ydown+aheight
  116.  
  117.         `scroll it off the top if necessary...
  118.         gosub scrollcheck
  119.     loop
  120. EXITFUNCTION 
  121.  
  122. scrollcheck:
  123.     if ydown=>screenheight-bottommargin
  124.         x2=screenwidth-rightmargin : y2=screenheight-bottommargin
  125.         if y2>screenheight then y2=screenheight
  126.         get image 99,leftmargin,topmargin+aheight,x2,y2
  127.         paste image 99,leftmargin,topmargin
  128.         ink 0,0
  129.         box leftmargin,screenheight-bottommargin-aheight,x2,y2
  130.         ink rgb(255,255,255),0
  131.         ydown=screenheight-bottommargin-aheight
  132.         imageflag(1)=1
  133.     endif
  134. return
  135. ENDFUNCTION
  136.