Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DAY 016: _MOUSEINPUT
#1
Today's Keyword of the Day is the first which has poor Steve shaking his head and sighing deeply over our documentation and wiki entries on it.  Guys, do me a favor and toss the wiki examples out of your brains, as they're NOT what you want to do with _MOUSEINPUT at all.  



Let me illustrate with the first example from the wiki:



Code: (Select All)
DO
  DO WHILE _MOUSEINPUT '      Check the mouse status
    PRINT _MOUSEX, _MOUSEY, _MOUSEBUTTON(1), _MOUSEWHEEL
  LOOP
LOOP UNTIL INKEY$ <> ""


Seems to work as advertised, so what's the problem here?



Nothing should go inside your _MOUSEINPUT loop, except for a _MOUSEWHEEL counter!



Let me show you why:



Code: (Select All)
Do
    Locate 1
    i = 1
    Do While _MouseInput '      Check the mouse status
        Locate i, 1: Print _MouseX, _MouseY, _MouseButton(1), _MouseWheel
        i = i + 1
    Loop
    If i <> 1 Then For z = i To 20: Locate z, 1: Print Space$(60): Next
    Locate 21, 1: Print _MouseX, _MouseY, _MouseButton(1)
    _Limit 120
Loop Until InKey$ <> ""




Take the first code and imagine trying to actually do something with it.  Printing it inside that example loop isn't actually doing anything constructive for us -- it's just displaying all the mouse's minute movements as it crosses the screen.  If we're going to check the mouse against a button, or some such, that we've draw upon our screen, we'll need to settle on o single value for that mouse position which we compare against -- and that's going to be AFTER WE EXIT THE LOOP.

Code: (Select All)
Do While _MOUSEINPUT
Loop


^The above says we're not going to actually do anything with our _MOUSEX and _MOUSEY values...  We'll only respond to them outside the loop.



And, as you can see from my second set of code, the end result is *always* going to print the same values as the last pass inside the loop did...



So why would we waste time processing a couple of dozen events, assigning them to overwrite the same values over and over and whatnot, when we're just going to toss those values out the door at the end of the loop??



IF both sets of the following code produce the same results, which is more efficient for us?



Code: (Select All)
While _MouseInput
    x = _MOUSEX:  y = _MOUSEY
Wend


Code: (Select All)
While _MouseInput; Wend
x = _MouseX: y = _MouseY


Both are going to generate the exact same values for x and y, so which is more efficient for us?  The first code is going to assign several dozen temporary values to x and y, before exiting and giving us a final value for x and y.  The second code skips all that intermittent assignment and just gives us the final result after the mouseinput loop.






Our second example in the wiki is just as poorly written, and is just as bad of an example.



Code: (Select All)
SCREEN 12

DO ' main program loop

  ' your program code

  DO WHILE _MOUSEINPUT'mouse status changes only
    x = _MOUSEX
    y = _MOUSEY
    IF x > 0 AND x < 640 AND y > 0 AND y < 480 THEN
      IF _MOUSEBUTTON(2) THEN
        PSET (x, y), 15
        LOCATE 1, 1: PRINT x, y
      END IF
    END IF
  LOOP

  ' your program code

LOOP UNTIL INKEY$ = CHR$(27)



Once again, the example is processing all sorts of crap inside that _MOUSEINPUT loop that it doesn't need to.  Compile it.  Run it.  Watch how it behaves.   And then compare to the following:



Code: (Select All)
Screen 12

Do ' main program loop

    ' your program code

    Do While _MouseInput: Loop 'mouse status changes only

    ' your program code
    x = _MouseX
    y = _MouseY
    If x > 0 And x < 640 And y > 0 And y < 480 Then
        If _MouseButton(2) Then
            PSet (x, y), 15
            Locate 1, 1: Print x, y
        End If
    End If

Loop Until InKey$ = Chr$(27)




The only thing that needs to go inside a _MOUSEINPUT loop is possibly a counter to deal with _MOUSEWHEEL.  Everything else should go outside it, or else you're going to end up lagging up your program to the point of uselessness.



MouseInput code should generally look like one of the two varieties below:



Code: (Select All)
While _MouseInput: Wend


Code: (Select All)
While _MouseInput
    scrollwheel = scrollwheel + _MOUSEWHEEL
Wend


Attempting to process other code inside that mouseinput loop is just asking for trouble.  Our wiki examples work for what they're showing, but what they're showing is about the worst possible structure I could imagine for use with _MOUSEINPUT.  Honestly, they must've just written into the wiki when _MOUSEINPUT was first added into the wiki and nobody knew much about it..  As it stands now, those examples need an overhaul to show how to actually use them properlyWink
Reply


Messages In This Thread
DAY 016: _MOUSEINPUT - by SMcNeill - 11-21-2022, 11:27 PM
RE: DAY 016: _MOUSEINPUT - by bplus - 11-21-2022, 11:41 PM
RE: DAY 016: _MOUSEINPUT - by SMcNeill - 11-22-2022, 12:09 AM
RE: DAY 016: _MOUSEINPUT - by PhilOfPerth - 11-22-2022, 12:16 AM
RE: DAY 016: _MOUSEINPUT - by james2464 - 11-22-2022, 12:20 AM
RE: DAY 016: _MOUSEINPUT - by SMcNeill - 11-22-2022, 12:32 AM
RE: DAY 016: _MOUSEINPUT - by mnrvovrfc - 11-21-2022, 11:54 PM
RE: DAY 016: _MOUSEINPUT - by PhilOfPerth - 11-22-2022, 12:08 AM
RE: DAY 016: _MOUSEINPUT - by james2464 - 11-22-2022, 12:38 AM
RE: DAY 016: _MOUSEINPUT - by SMcNeill - 11-22-2022, 12:49 AM
RE: DAY 016: _MOUSEINPUT - by mnrvovrfc - 11-22-2022, 12:46 AM
RE: DAY 016: _MOUSEINPUT - by Pete - 11-22-2022, 01:32 AM
RE: DAY 016: _MOUSEINPUT - by SMcNeill - 11-22-2022, 02:00 AM
RE: DAY 016: _MOUSEINPUT - by OldMoses - 12-06-2022, 01:39 AM
RE: DAY 016: _MOUSEINPUT - by Pete - 12-06-2022, 02:28 AM
RE: DAY 016: _MOUSEINPUT - by Pete - 11-22-2022, 02:59 AM
RE: DAY 016: _MOUSEINPUT - by SMcNeill - 11-22-2022, 03:47 AM
RE: DAY 016: _MOUSEINPUT - by Pete - 11-22-2022, 07:54 AM
RE: DAY 016: _MOUSEINPUT - by mnrvovrfc - 12-05-2022, 02:03 PM
RE: DAY 016: _MOUSEINPUT - by Pete - 12-05-2022, 04:02 PM
RE: DAY 016: _MOUSEINPUT - by SMcNeill - 12-05-2022, 04:55 PM
RE: DAY 016: _MOUSEINPUT - by Pete - 12-05-2022, 07:45 PM
RE: DAY 016: _MOUSEINPUT - by OldMoses - 12-06-2022, 02:26 PM
RE: DAY 016: _MOUSEINPUT - by Pete - 12-06-2022, 03:23 PM
RE: DAY 016: _MOUSEINPUT - by NasaCow - 12-10-2022, 12:28 PM
RE: DAY 016: _MOUSEINPUT - by Pete - 12-10-2022, 05:08 PM



Users browsing this thread: 6 Guest(s)