Author Topic: Extended Input  (Read 6067 times)

0 Members and 1 Guest are viewing this topic.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Extended Input
« Reply #15 on: February 02, 2021, 01:27:34 am »
Aright here's a bug for ya.

In "enter a float"... type something with a decimal, and then backspace through the decimal... you can never type a decimal again after that
« Last Edit: February 02, 2021, 01:31:18 am by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Extended Input
« Reply #16 on: February 02, 2021, 01:33:56 am »
Ahem, where's OPTION _EXPLICIT?
I dunno.  In somebody else’s code??  Personally, I never use it.  It defeats the purpose of having BASIC allocate and define your variables on the fly for you.  :P

Feel free to add it if you need it.  I usually don’t get around to stuff like that until I’m completely finished tinkering with the code.

I’m thinking for floats to have them in a F12.2 format.  The example there would allow up to 12 digits before the decimal, 2 after.

I'll also add a command to move cursor to after a paste.

Maybe even add an option to auto-comma values...

So still some more tweaking to go here.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Extended Input
« Reply #17 on: February 02, 2021, 01:34:37 am »
Aright here's a bug for ya.

In "enter a float"... type something with a decimal, and then backspace through the decimal... you can never type a decimal again after that

Good catch.  I'll address that tomorrow as well. ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Extended Input
« Reply #18 on: February 02, 2021, 01:40:27 am »
While the hood is open, maybe it's worth dressing up the whole thing in an ascii-box too? Then it'll be an alternative to this box used in several projects around here:

 
ss.png
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Extended Input
« Reply #19 on: February 02, 2021, 10:41:17 am »
@STxAxTIC -- Try this out and see if it's working as intended.  We now limit floats by digits before and after the decimal point, and that period shouldn't be a problem anymore, like you reported up above.

Code: QB64: [Select]
  1. OEInput "{F-.2}Enter a float (Unlimited.2)=>", a$
  2. OEInput "{F0.2}Enter a float (0.2) =>", a$
  3. OEInput "{F9.2}Enter a float (9.2) =>", a$
  4. OEInput "{F9.-}Enter a float (9.unlimited) =>", a$
  5.  
  6. OEInput "{P}Enter a password =>", a$
  7.  
  8. OEInput "{UI}Enter an unsigned integer =>", a$
  9.  
  10. OEInput "{I}Enter an integer=>", a$
  11.  
  12. OEInput "{IL10}Enter an integer of less than 10 digits =>", a$
  13.  
  14. OEInput "{X15,Y15}Enter whatever at loc 15,15 =>", a$
  15.  
  16. OEInput "{D}Check to make certain paste is disabled here =>", a$
  17.  
  18. OEInput "Enter whatever here, but use paste.  See if the cursor remains BEFORE the paste. => ", a$
  19.  
  20. OEInput "{V}Enter whatever here, but use paste.  See if the cursor moves AFTER the paste. => ", a$
  21.  
  22. PRINT "Press <ANY KEY> to clear the screen -- we're getting full!"
  23.  
  24. OEInput "{H}This should hide itself, after you enter whatever =>", a$
  25. PRINT "Is the question and input gone?  This line should be at the top left corner of your screen."
  26. PRINT "And here's your input => "; a$
  27.  
  28.  
  29.  
  30. SUB OEInput (prompt$, result$) 'Over Engineered Input
  31.     'limit VALUES:
  32.     '1 = Unsigned
  33.     '2 = Integer
  34.     '4 = Float
  35.     '8 = Who cares. It's handled via internal variables and we don't need to know a type for it.
  36.  
  37.     PCOPY 0, 1
  38.     A = _AUTODISPLAY: X = POS(0): Y = CSRLIN
  39.     OX = X: OY = Y 'original x and y positions
  40.     CP = 0: OldCP = 0 'Cursor Position
  41.     _KEYCLEAR
  42.     length_limit = -1 'unlimited length input, by default
  43.  
  44.     IF LEFT$(prompt$, 1) = "{" THEN 'possible limiter
  45.         i = INSTR(prompt$, "}")
  46.         IF i THEN 'yep, we have something!
  47.             limiter$ = UCASE$(MID$(prompt$, 2, i - 2))
  48.             IF INSTR(limiter$, "U") THEN limit = limit OR 1 'Unsigned
  49.             IF INSTR(limiter$, "I") THEN 'can't limit to BOTH an integer AND a float
  50.                 limit = limit OR 2 'Integer
  51.             ELSEIF INSTR(limiter$, "F") THEN
  52.                 limit = limit OR 4 'Float
  53.                 float_before_limit = GetValue(limiter$, "F")
  54.                 float_after_limit = GetValue(MID$(limiter$, INSTR(limiter$, "F") + 1), ".")
  55.             END IF
  56.         END IF
  57.         IF INSTR(limiter$, "P") THEN password_protected = -1: limit = limit OR 8 'don't show passwords.
  58.         IF INSTR(limiter$, "L") THEN 'Length Limitation
  59.             limit = limit OR 8
  60.             length_limit = GetValue(limiter$, "L")
  61.         END IF
  62.         IF INSTR(limiter$, "X") THEN 'X position on screen
  63.             limit = limit OR 8
  64.             X = GetValue(limiter$, "X")
  65.         END IF
  66.         IF INSTR(limiter$, "Y") THEN 'Y position on scren
  67.             limit = limit OR 8
  68.             Y = GetValue(limiter$, "Y")
  69.         END IF
  70.         IF INSTR(limiter$, "D") THEN disable_paste = -1: limit = limit OR 8 'disable paste
  71.         IF INSTR(limiter$, "V") THEN cursor_after_paste = -1: limit = limit OR 8 'disable paste
  72.         IF INSTR(limiter$, "H") THEN clean_exit = -1: limit = limit OR 8 'disable paste
  73.     END IF
  74.     IF limit <> 0 THEN prompt$ = MID$(prompt$, i + 1)
  75.  
  76.  
  77.     DO
  78.         PCOPY 1, 0
  79.         IF _KEYDOWN(100307) OR _KEYDOWN(100308) THEN AltDown = -1 ELSE AltDown = 0
  80.         k = _KEYHIT
  81.         IF AltDown THEN
  82.             SELECT CASE k 'ignore all keypresses except ALT-number presses
  83.                 CASE -57 TO -48: AltWasDown = -1: alt$ = alt$ + CHR$(-k)
  84.             END SELECT
  85.         ELSE
  86.             SELECT CASE k 'without alt, add any keypresses to our input
  87.                 CASE 8
  88.                     oldin$ = in$
  89.                     IF CP > 0 THEN OldCP = CP: CP = CP - 1
  90.                     in$ = LEFT$(in$, CP) + MID$(in$, CP + 2) 'backspace to erase input
  91.                 CASE 9
  92.                     oldin$ = in$
  93.                     in$ = LEFT$(in$, CP) + SPACE$(4) + MID$(in$, CP + 1) 'four spaces for any TAB entered
  94.                     OldCP = CP
  95.                     CP = CP + 4
  96.                 CASE 32 TO 128
  97.                     IF _KEYDOWN(100305) OR _KEYDOWN(100306) THEN
  98.                         IF k = 118 OR k = 86 THEN
  99.                             IF disable_paste = 0 THEN
  100.                                 oldin$ = in$
  101.                                 temp$ = _CLIPBOARD$
  102.                                 in$ = LEFT$(in$, CP) + temp$ + MID$(in$, CP + 1) 'ctrl-v paste
  103.                                 'CTRL-V leaves cursor in position before the paste, without moving it after.
  104.                                 'Feel free to modify that behavior here, if you want it to move to after the paste.
  105.                                 IF cursor_after_paste THEN CP = CP + LEN(temp$)
  106.                             END IF
  107.                         END IF
  108.                         IF k = 122 OR k = 90 THEN SWAP in$, oldin$: SWAP OldCP, CP 'ctrl-z undo
  109.                     ELSE
  110.                         check_input:
  111.                         oldin$ = in$
  112.                         IF limit AND 1 THEN 'unsigned
  113.                             IF k = 43 OR k = 45 THEN _CONTINUE 'remove signs +/-
  114.                         END IF
  115.                         IF limit AND 2 THEN 'integer
  116.                             IF k = 45 AND CP = 0 THEN GOTO good_input 'only allow a - sign for the first digit
  117.                             IF k < 48 OR k > 57 THEN _CONTINUE 'remove anything non-numeric
  118.                         END IF
  119.                         IF limit AND 4 THEN 'float
  120.                             IF k = 45 AND CP = 0 THEN GOTO good_input 'only allow a - sign for the first digit
  121.                             IF k = 46 AND INSTR(in$, ".") = 0 THEN GOTO good_input 'only one decimal point
  122.                             IF k < 48 OR k > 57 THEN _CONTINUE 'remove anything non-numeric
  123.                             IF LEFT$(in$, 1) = "-" THEN temp$ = MID$(in$, 2) ELSE temp$ = in$
  124.                             IF INSTR(in$, ".") = 0 OR CP < INSTR(in$, ".") THEN
  125.                                 IF LEN(temp$) < float_before_limit OR float_before_limit = -1 THEN
  126.                                     in$ = LEFT$(in$, CP) + CHR$(k) + MID$(in$, CP + 1) 'add input to our string
  127.                                     OldCP = CP
  128.                                     CP = CP + 1
  129.                                 END IF
  130.                             ELSE
  131.                                 temp$ = MID$(in$, INSTR(in$, ".") + 1)
  132.                                 IF LEN(temp$) < float_after_limit OR float_after_limit = -1 THEN
  133.                                     in$ = LEFT$(in$, CP) + CHR$(k) + MID$(in$, CP + 1) 'add input to our string
  134.                                     OldCP = CP
  135.                                     CP = CP + 1
  136.                                 END IF
  137.                             END IF
  138.                             _CONTINUE
  139.                         END IF
  140.                         good_input:
  141.                         IF CP < length_limit OR length_limit < 0 THEN
  142.                             in$ = LEFT$(in$, CP) + CHR$(k) + MID$(in$, CP + 1) 'add input to our string
  143.  
  144.                             OldCP = CP
  145.                             CP = CP + 1
  146.                         END IF
  147.                     END IF
  148.                 CASE 18176 'Home
  149.                     CP = 0
  150.                 CASE 20224 'End
  151.                     CP = LEN(in$)
  152.                 CASE 21248 'Delete
  153.                     oldin$ = in$
  154.                     in$ = LEFT$(in$, CP) + MID$(in$, CP + 2)
  155.                 CASE 19200 'Left
  156.                     CP = CP - 1
  157.                     IF CP < 0 THEN CP = 0
  158.                 CASE 19712 'Right
  159.                     CP = CP + 1
  160.                     IF CP > LEN(in$) THEN CP = LEN(in$)
  161.             END SELECT
  162.         END IF
  163.         alt$ = RIGHT$(alt$, 3)
  164.         IF AltWasDown = -1 AND AltDown = 0 THEN
  165.             v = VAL(alt$)
  166.             IF v >= 0 AND v <= 255 THEN
  167.                 k = v
  168.                 alt$ = "": AltWasDown = 0
  169.                 GOTO check_input
  170.             END IF
  171.         END IF
  172.         blink = (blink + 1) MOD 30
  173.         LOCATE Y, X
  174.         PRINT prompt$;
  175.         IF password_protected THEN
  176.             PRINT STRING$(LEN(LEFT$(in$, CP)), "*");
  177.             IF blink \ 15 THEN PRINT " "; ELSE PRINT "_";
  178.             PRINT STRING$(LEN(MID$(in$, CP + 1)), "*")
  179.         ELSE
  180.             PRINT LEFT$(in$, CP);
  181.             IF blink \ 15 THEN PRINT " "; ELSE PRINT "_";
  182.             PRINT MID$(in$, CP + 1)
  183.         END IF
  184.  
  185.         _DISPLAY
  186.         _LIMIT 30
  187.     LOOP UNTIL k = 13
  188.  
  189.     PCOPY 1, 0
  190.     LOCATE OY, OX
  191.     IF clean_exit = 0 THEN
  192.         LOCATE Y, X
  193.         IF password_protected THEN
  194.             PRINT prompt$; STRING$(LEN(in$), "*")
  195.         ELSE
  196.             PRINT prompt$; in$
  197.         END IF
  198.     END IF
  199.     result$ = in$
  200.  
  201.  
  202. FUNCTION GetValue (limiter$, what$)
  203.     jstart = INSTR(limiter$, what$): j = 0
  204.     IF MID$(limiter$, INSTR(limiter$, what$) + 1, 1) = "-" THEN
  205.         GetValue = -1 'unlimited
  206.         EXIT FUNCTION
  207.     END IF
  208.  
  209.     DO
  210.         j = j + 1
  211.         m$ = MID$(limiter$, jstart + j, 1)
  212.     LOOP UNTIL m$ < "0" OR m$ > "9"
  213.     GetValue = VAL(MID$(limiter$, jstart + 1, j - 1))

Personally, I think even what we have here so far, is a pretty neat little bit of input handling for us.  Being able to limit input by types (integer, unsigned, float, or string), is a pretty nice little option for any input routine, as it helps prevent errors from the user-entry level -- and that can stop a whole lot of error checking from ever being necessary.  After all, if I *KNOW* that all an user can input is values from 0 to 9 for a numeric input, then I don't have to error-check later for dollar signs, commas, or stray sneeze input.

We can now limit floats by a before the decimal and after the decimal syntax.  We have an option to move our cursor to after a paste (default has it staying before).  We have an option to turn our text into a pop-up style input, where it completely disappears off the screen once we're done with it.

I guess all the basic legwork is done now.  Now, I guess next, I'll work up an {A} option to create an ASCII-Box similar to the one STx presented above.  Unless  someone comes across a glitch somewhere for me to sort out?

(Which wouldn't surprise me one bit.  After all, OEInput is starting to become a rather complex little set of work here!  The more complex something gets, the more likely it is to have unexpected glitches pop-up in things!)
« Last Edit: February 02, 2021, 11:17:05 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Extended Input
« Reply #20 on: February 02, 2021, 11:57:17 am »
While the hood is open, maybe it's worth dressing up the whole thing in an ascii-box too? Then it'll be an alternative to this box used in several projects around here:

 
ss.png


🎶🎵 Don't go ditching my box
I know it's not so alright
Oh honey if it gets used then
there is something of good... pam pam, pam pam, pam 🎶🎵

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Extended Input
« Reply #21 on: February 02, 2021, 02:47:58 pm »
That box^ is the best one anyone bothered to make. Still that way, so thank you muchly Fellippe. My versions of the input box are lazy and inferior.

But I don't want Steve to fall into the same trap. Now there's a milestone to *pass*, not just drive under.
« Last Edit: February 02, 2021, 02:56:43 pm by STxAxTIC »
You're not done when it works, you're done when it's right.