Page 1 of 1

mouse cursor / object position

Posted: Mon Sep 24, 2012 4:44 am
by Zdeno Bielik
Hi Roger,

one customer asked from me solution for task like this:
I need to know object, over which just mouse is while user move mouse’s cursor or cursor stops over any object – e.g. it is over green arrow or over yellow star...
The problem is I cannot put each ones objects on dcstatic area, but I can only put one whole image(see attached example) on dcstatic object.
Do you have idea how to solve this?

TIA & Regards
Zdeno

Re: mouse cursor / object position

Posted: Mon Sep 24, 2012 9:42 am
by rdonnay
I would probably approach such a project with a simple solution, making the assumption that each object occupies a rectangular area of the screen, even if it does not look rectangular.

In other words, I would determine the rectangular coordinates that would best define the object on the screen.

Examples ;
1. The upper-left object ( the right-pointing arrow ) occupies the rectangular coordinates from 40, 20 to 380, 80.
2. The upper-middle object ( the star ) occupies the rectangular coordinates from 395,17 to 488,100.
3. The pentagon occupies the rectangular coordinates from 508, 49 to 648,172.

There are 13 total objects, so I would create a multi-dimensional array of 13 elements.
It would look like this:

Code: Select all

aObjects := { ;
   { 40,20,380,80,'Right pointing arrow' }, ;
   { 395,17,488,100,'Star' }, ;
   { 508,49,648,172,'Pentagon' }, ;
   .... }
I would then use the :motion callback slot of the static object to call a function every time the mouse is moved.

Code: Select all

@ .. DCSTATIC oStatic TYPE XBPSTATIC_TYPE_BITMAP CAPTION 'Objects.Bmp' ;
      EVAL {|o|o:motion := {|aMousePos|DisplayObjectName( aMousePos, aObjects, oObjectName )}

@ .. DCSTATIC oObjectName TYPE XBPSTATIC_TYPE_TEXT ...

* ------------

STATIC FUNCTION DisplayObjectName( aMousePos, aObjects, oObjectName )

nObject := FindObject( aMousePos, aObjects )

IF nObject > 0
  oObjectName:setCaption( aObjects[nObject,5] )
ELSE
  oObjectName:setCaption( '' )
ENDIF

RETURN nil

* ------------

STATIC FUNCTION FindObject( aMousePos, aObjects ) 

LOCAL i, nObject := 0

FOR i := 1 TO Len(aObjects)

  IF aMousePos[1] >= aObjects[i,1] .AND. aMousePos[1] <= aObjects[i,3] .AND. ;
     aMousePos[2] >= aObjects[i,2] .AND. aMousePos[2] <= aObjects[i,4]
    nObject := i
    EXIT
  ENDIF

NEXT[

RETURN nObject
This is a rather simplistic approach but it may satisfy the needs of your customer.

Re: mouse cursor / object position

Posted: Tue Sep 25, 2012 10:59 pm
by Zdeno Bielik
Roger,

thank you for example, it works great!

Zdeno