Author Topic: How to determine the location of the mouse cursor  (Read 11822 times)

0 Members and 1 Guest are viewing this topic.

Offline Cloudee1

How to determine the location of the mouse cursor
« on: December 24, 2006, 08:41:35 PM »
This is a question that I recently asked at Mega-Tokyo in which Troflip was good enough to write up some code illustrating that exact functionallity. This is a little snippet of code which I had hoped Troflip could enter here, but as he has not signed up yet and I really didn't want to lose this snippet as I plan to find a use for it, I have moved it here.

Troflip's code to get a view to follow the cursor broken up with small explanations added by me.

We first need to create an instance of an Actor, it needs to be an actor so that it can move around the screen. An init method has been included in the instance telling the actor to do what that script says.
Code: [Select]
(instance thing of Act
   (properties
      x 100
      y 100
      view 800
   )
   (method (init)
      (super:init())
      (self:setScript(thingScript))
   )
)
Now that the instance is complete, we also need to create the script which the instances init method references.
Code: [Select]
(instance thingScript of Script
   (properties)
   (method (doit)
      (var myEvent)
      (super:doit())
      (= myEvent Event:new(evNULL))
      (send client:setMotion(MoveTo (send myEvent:x) (send myEvent:y)))
      (send myEvent:dispose())
   )
)

 
...and then of course do (thing:init()) in your room's init method.

* View this how-to script in it's entirety here
« Last Edit: January 21, 2013, 12:35:53 AM by Cloudee1 »


Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline Cloudee1

Re: How to determine the location of the mouse cursor
« Reply #1 on: January 21, 2013, 09:30:07 AM »
So looking at the code for the room1 script in it's entirety as it is posted in the repository, I wonder why it is that I didn't include the doit method in the instance of the actor. I put it in it's own script instance and I'm not sure now that I needed to, makes more sense to have all the bit of code as compact as possible. Also there should be a small memory gain by loading a single instance instead of two different ones.

Looks like it is time to do a little testing to see if there was a reason that I did it the way that I did.
Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition

Offline gumby

Re: How to determine the location of the mouse cursor
« Reply #2 on: August 02, 2013, 05:10:30 PM »
If you just want to track the current cursor location (i.e. if you don't need a view to follow the cursor), create a couple of new global variables in your Main.sc:

Code: [Select]
  gTrackMouse = TRUE
  gMouseX
  gMouseY

Then add the following method in the Script class (Obj.sc):

Code: [Select]
(method (trackMouse)
   (var nullEvent, mouseX, mouseY)
   = nullEvent (Event:new(evNULL))
   = gMouseX (send nullEvent:x)
   = gMouseY (send nullEvent:y)
   (send nullEvent:dispose())
)  

and modify the doit() method for the Script class as well:

Code: [Select]
(method (doit)
   (var theTime)

   (if (== gTrackMouse TRUE)
      (self:trackMouse())
   )
...

You can now check the current position of the mouse anywhere in your game by referencing the gMouseX and gMouseY global variables.  Additionally, you can toggle tracking the mouse movement with the gTrackMouse variable in the Main.sc by setting it TRUE or FALSE, so it can be disabled when you don't need it.
« Last Edit: August 03, 2013, 07:19:35 PM by gumby »
In the Great Underground Empire (Zork port in development)
Winter Break 2012 Rope Prop Competition

Offline claudehuggins

Re: How to determine the location of the mouse cursor
« Reply #3 on: November 23, 2016, 04:52:22 PM »
Is there a way to check if the cursor is on a certain control color?
I could probably do it by having an invisible object follow the cursor and check if that's on the color, but I feel like that would be a cumbersome and memory-eating way to do it...

On the subject of cursors...is it possible to simulate mouse movement? Can the cursor be moved without the player's input (via script etc)?

EDIT: I want to clarify I'm talking about SCI1.1, but being able to do it in SCI0 would be neat and open up some new possibilities for my future projects.
« Last Edit: November 23, 2016, 04:54:58 PM by claudehuggins »
A while ago, at a block party I found myself socially trapped at, I thought to myself: I need a t-shirt that says, "I'd rather be programming".

Offline troflip

Re: How to determine the location of the mouse cursor
« Reply #4 on: November 23, 2016, 05:40:01 PM »
Is there a way to check if the cursor is on a certain control color?

You can get the control (or visual or priority colors) of any point or rect you want:
http://scicompanion.com/Documentation/Kernels/OnControl.html

On the subject of cursors...is it possible to simulate mouse movement? Can the cursor be moved without the player's input (via script etc)?

SetCursor takes an x and y (although I haven't tried it...)
http://scicompanion.com/Documentation/Kernels/SetCursor.html
Check out my website: http://icefallgames.com
Groundhog Day Competition

Offline lskovlun

Re: How to determine the location of the mouse cursor
« Reply #5 on: November 23, 2016, 09:56:24 PM »
I can't believe I didn't respond to this earlier (it appeared in 2006 and references earlier posts...). SCI0 contains an Event instance called uEvt (in user.sc) from which x and y coordinates can be read. It should work in SCI1.1 as well, though not when modal dialogs or the icon bar are active.

Offline Cloudee1

Re: How to determine the location of the mouse cursor
« Reply #6 on: November 25, 2016, 04:17:48 AM »
As far as SCI0 is concerned... using Studio Syntax...

Clicking on a view:
Code: [Select]
(if((> (send pEvent:x) (slime:nsLeft))and
    (< (send pEvent:x) (slime:nsRight))and
    (> (send pEvent:y) (slime:nsTop))and
    (< (send pEvent:y) (slime:nsBottom)))
              Print("You clicked on a view.")
)// end if item

Or, clicking on a Control Color
Code: [Select]
(if(== OnControl(ocSPECIAL (send pEvent:x) (send pEvent:y)) ctlSILVER)      // Silver Control Color
              Print("You clicked on the silver control color.")
)// end if control

In the above snippet, you can change it to check for a visual or priority color by changing the ocSPECIAL....
Code: [Select]
(if(== OnControl(ocPRIORITY (send pEvent:x) (send pEvent:y)) ctlSILVER)      // Silver Priority Color
(if(== OnControl(ocVISUAL (send pEvent:x) (send pEvent:y)) ctlSILVER)      // Silver Visual Color

and finally... clicking inside a rectangle...
Code: [Select]
(if((> (send pEvent:x) 273) and
    (< (send pEvent:x) 320) and
    (> (send pEvent:y) 0) and
    (< (send pEvent:y) 45))
              Print("You clicked inside a rectangle.")
Halloween Competition Brass Lantern Prop Competition Groundhog Day Competition


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.077 seconds with 24 queries.