Author Topic: Re: Rat Problem  (Read 649 times)

0 Members and 1 Guest are viewing this topic.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Rat Problem
« on: January 25, 2019, 10:32:47 am »
The following seems to work -
Code: QB64: [Select]
  1. CONST True = -1
  2. CONST False = 4 'try different value for false as all variables have 0 as their initial value
  3.  
  4. TYPE newMouse
  5.     oldX AS INTEGER
  6.     oldY AS INTEGER
  7.  
  8.     newX AS INTEGER
  9.     newY AS INTEGER
  10.  
  11.     clicked AS INTEGER
  12.     dragged AS INTEGER
  13.     pressed AS INTEGER
  14.  
  15. DIM SHARED rat AS newMouse
  16.  
  17. 'rat.clicked = False
  18. 'rat.dragged = False
  19.  
  20. DIM getInput AS STRING
  21.  
  22.  
  23. WHILE NOT Quit
  24.     _LIMIT 60
  25.  
  26.     getInput = getRat
  27.     IF getInput <> "{Nil" THEN LOCATE 12, 2: PRINT getInput
  28.  
  29.     _DISPLAY
  30.  
  31. FUNCTION getRat$
  32.     DIM temp AS STRING
  33.     temp = "{Nil"
  34.  
  35.         rat.newX = _MOUSEX
  36.         rat.newY = _MOUSEY
  37.  
  38.         rat.pressed = _MOUSEBUTTON(1)
  39.     WEND
  40.  
  41.     IF rat.pressed THEN
  42.         temp = "{Press"
  43.         rat.clicked = False
  44.  
  45.         IF rat.newX <> rat.oldX OR rat.newY <> rat.oldY THEN
  46.             temp = "{Drag "
  47.             rat.dragged = True
  48.         END IF
  49.     ELSE
  50.         IF rat.clicked = False THEN
  51.             temp = "{Click": BEEP
  52.             rat.clicked = True
  53.             rat.dragged = False
  54.         END IF
  55.     END IF
  56.  
  57.     rat.oldX = rat.newX
  58.     rat.oldY = rat.newY
  59.  
  60.     getRat = temp
  61.  
  62.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Rat Problem
« Reply #1 on: January 27, 2019, 04:15:42 am »
It beeps because at startup the mouse is both released and not pressed.

IF Rat.pressed THEN....  (rat.pressed = 0 so it’s not — go into the ELSE )

ELSE
    IF rat.clicked = FALSE THEN... (rat.pressed = 0, so we do the stuff here)
         temp = “”: BEEP <— and you beep


  Set a flag to check to see if the mouse has been clicked down at least once before BEEPING for a release.

If rat.pressed THEN
     RatPressedOnce = TRUE
     ‘More stuff
ELSE
    IF rat.clicked = FALSE AND RatPressedOnce = TRUE THEN
         ‘Do stuff
    ENDIF
END IF
« Last Edit: January 27, 2019, 04:17:29 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Rat Problem
« Reply #2 on: January 27, 2019, 11:18:09 am »
Sample mouse code to wait until left mouse button is released before tacking action...

Code: QB64: [Select]
  1.     CALL mouse(mi%, mx%, my%, lb%)
  2.     IF lb% THEN LOCATE 1, 1: PRINT "Left mouse button down...     ": mflag% = -1
  3.     WHILE _MOUSEBUTTON(1): CALL mouse(mi%, mx%, my%, lb%): WEND
  4.     IF mflag% AND lb% = 0 THEN
  5.         LOCATE 1, 1: PRINT "Left mouse button released...     ": mflag% = 0
  6.     END IF
  7.  
  8. SUB mouse (mi%, mx%, my%, lb%)
  9.     mi% = _MOUSEINPUT
  10.     mx% = _MOUSEX
  11.     my% = _MOUSEY
  12.     lb% = _MOUSEBUTTON(1)

If you don't want a sub...

Code: QB64: [Select]
  1.     mi% = _MOUSEINPUT: lb% = _MOUSEBUTTON(1)
  2.     IF lb% THEN LOCATE 1, 1: PRINT "Left mouse button down...     ": mflag% = -1
  3.     WHILE _MOUSEBUTTON(1): mi% = _MOUSEINPUT: lb% = _MOUSEBUTTON(1): WEND
  4.     IF mflag% AND lb% = 0 THEN
  5.         LOCATE 1, 1: PRINT "Left mouse button released...     ": mflag% = 0
  6.     END IF

It is the ...

WHILE _MOUSEBUTTON(1): mi% = _MOUSEINPUT: lb% = _MOUSEBUTTON(1): WEND

...that waits for the button to be released.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/