Author Topic: Rat Problem  (Read 153 times)

Rat Problem
« on: January 23, 2019, 06:53:15 PM »
I would appreciate some help with this:

Code: [Select]
CONST True = -1
CONST False = 0

TYPE newMouse
  oldX AS INTEGER
  oldY AS INTEGER

  newX AS INTEGER
  newY AS INTEGER

  clicked AS INTEGER
  dragged AS INTEGER
  pressed AS INTEGER
END TYPE

DIM SHARED rat AS newMouse

rat.clicked = False
rat.dragged = False

DIM getInput AS STRING

WHILE _MOUSEINPUT: WEND

WHILE NOT Quit
  _LIMIT 60

  getInput = getRat
  IF getInput <> "{Nil" THEN LOCATE 12, 2: PRINT getInput

  _DISPLAY
WEND

FUNCTION getRat$
DIM temp AS STRING
temp = "{Nil"

WHILE _MOUSEINPUT
  rat.newX = _MOUSEX
  rat.newY = _MOUSEY

  rat.pressed = _MOUSEBUTTON(1)
WEND

IF rat.pressed THEN
  temp = "{Press"
  rat.clicked = False

  IF rat.newX <> rat.oldX OR rat.newY <> rat.oldY THEN
    temp = "{Drag "
    rat.dragged = True
  END IF
ELSE
  IF rat.clicked = False THEN
    temp = "{Click": BEEP
    rat.clicked = True
    rat.dragged = False
  END IF
END IF

rat.oldX = rat.newX
rat.oldY = rat.newY

getRat = temp
END FUNCTION

It 'seems' to work, yes, but why does it beep at the start of the program?

"WHILE _MOUSEINPUT: WEND" <<<< Doesn't that remove any of the old Mouse Inputs?

Offline Ashish

  • The joy of coding is endless.
Re: Rat Problem
« Reply #1 on: January 25, 2019, 10:32:47 AM »
The following seems to work -
Code: [Select]
CONST True = -1
CONST False = 4 'try different value for false as all variables have 0 as their initial value

TYPE newMouse
    oldX AS INTEGER
    oldY AS INTEGER

    newX AS INTEGER
    newY AS INTEGER

    clicked AS INTEGER
    dragged AS INTEGER
    pressed AS INTEGER
END TYPE

DIM SHARED rat AS newMouse

'rat.clicked = False
'rat.dragged = False

DIM getInput AS STRING

WHILE _MOUSEINPUT: WEND

WHILE NOT Quit
    _LIMIT 60

    getInput = getRat
    IF getInput <> "{Nil" THEN LOCATE 12, 2: PRINT getInput

    _DISPLAY
WEND

FUNCTION getRat$
    DIM temp AS STRING
    temp = "{Nil"

    WHILE _MOUSEINPUT
        rat.newX = _MOUSEX
        rat.newY = _MOUSEY

        rat.pressed = _MOUSEBUTTON(1)
    WEND

    IF rat.pressed THEN
        temp = "{Press"
        rat.clicked = False

        IF rat.newX <> rat.oldX OR rat.newY <> rat.oldY THEN
            temp = "{Drag "
            rat.dragged = True
        END IF
    ELSE
        IF rat.clicked = False THEN
            temp = "{Click": BEEP
            rat.clicked = True
            rat.dragged = False
        END IF
    END IF

    rat.oldX = rat.newX
    rat.oldY = rat.newY

    getRat = temp
END FUNCTION

if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories

Re: Rat Problem
« Reply #2 on: January 27, 2019, 01:30:11 AM »
The following seems to work -
Code: [Select]
CONST True = -1
CONST False = 4 'try different value for false as all variables have 0 as their initial value

TYPE newMouse
    oldX AS INTEGER
    oldY AS INTEGER

    newX AS INTEGER
    newY AS INTEGER

    clicked AS INTEGER
    dragged AS INTEGER
    pressed AS INTEGER
END TYPE

DIM SHARED rat AS newMouse

'rat.clicked = False
'rat.dragged = False

DIM getInput AS STRING

WHILE _MOUSEINPUT: WEND

WHILE NOT Quit
    _LIMIT 60

    getInput = getRat
    IF getInput <> "{Nil" THEN LOCATE 12, 2: PRINT getInput

    _DISPLAY
WEND

FUNCTION getRat$
    DIM temp AS STRING
    temp = "{Nil"

    WHILE _MOUSEINPUT
        rat.newX = _MOUSEX
        rat.newY = _MOUSEY

        rat.pressed = _MOUSEBUTTON(1)
    WEND

    IF rat.pressed THEN
        temp = "{Press"
        rat.clicked = False

        IF rat.newX <> rat.oldX OR rat.newY <> rat.oldY THEN
            temp = "{Drag "
            rat.dragged = True
        END IF
    ELSE
        IF rat.clicked = False THEN
            temp = "{Click": BEEP
            rat.clicked = True
            rat.dragged = False
        END IF
    END IF

    rat.oldX = rat.newX
    rat.oldY = rat.newY

    getRat = temp
END FUNCTION


Thank you Ashish, but that doesn't work for me.  I even tried making True/Fase AS STRING { T/F } and even that doesn't work.

Can anyone provide a good mouse example where the mouse isn't recognized UNTIL the mouse button is released and NOT pressed?

Offline SMcNeill

  • QB64 Developer
Re: Rat Problem
« Reply #3 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 »

Offline Pete

  • Cuz I sez so, varmint!
Re: Rat Problem
« Reply #4 on: January 27, 2019, 11:18:09 AM »
Sample mouse code to wait until left mouse button is released before tacking action...

Code: [Select]
DO
    CALL mouse(mi%, mx%, my%, lb%)
    IF lb% THEN LOCATE 1, 1: PRINT "Left mouse button down...     ": mflag% = -1
    WHILE _MOUSEBUTTON(1): CALL mouse(mi%, mx%, my%, lb%): WEND
    IF mflag% AND lb% = 0 THEN
        LOCATE 1, 1: PRINT "Left mouse button released...     ": mflag% = 0
    END IF
LOOP

SUB mouse (mi%, mx%, my%, lb%)
    mi% = _MOUSEINPUT
    mx% = _MOUSEX
    my% = _MOUSEY
    lb% = _MOUSEBUTTON(1)
END SUB

If you don't want a sub...

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

It is the ...

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

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

Pete