Author Topic: Sprite Problem  (Read 704 times)

Brian Reynolds

  • Newbie
  • *
  • Posts: 20
    • View Profile
Sprite Problem
« on: December 16, 2022, 02:11:37 am »
I've struck a problem with Sprites. Using the following code, I get nothing for the Sprite name and a zero of each of the X& and Y&.
I am running Windows 11.

      CASE %EZ_Click
         a$ = EZ_TestSpriteClick("Form1", %Form1_Canvas1, X&, Y&)
         MSGBOX a$ +STR$(X&)+STR$(Y& )

Can you offer help?

Chris Boss

  • Administrator
  • Newbie
  • *****
  • Posts: 49
    • View Profile
Re: Sprite Problem
« Reply #1 on: December 16, 2022, 10:00:22 pm »
EZ_TestSpriteClick does not work the way you think.

The X& and Y& parameters are not return values, but are passed BYVAL telling the function where the coordinates to test are.

In the Canvas control's %EZ_Click event you have to call first:

EZ_GetCanvasXY

to get the coordinates for the last mouse click.

Then you can pass them to EZ_TestSpriteClick to see if the click occurred on a sprite.


Brian Reynolds

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Sprite Problem
« Reply #2 on: December 17, 2022, 02:18:43 am »
Thank you for your explanation, Chris.
From that as well as reading other "GetSprite" info, I take it there is no way of clicking on a Sprite and retrieving its name or X or Y position.
 

Chris Boss

  • Administrator
  • Newbie
  • *****
  • Posts: 49
    • View Profile
Re: Sprite Problem
« Reply #3 on: December 19, 2022, 12:30:39 pm »
No, you can get a sprite click. It is demonstrated in the sprite example in the codeclips folder.

Here is the code from that sample app:

Code: [Select]
SUB FORM1_CANVAS1_Click()
     LOCAL X&,Y&
     EZ_GetCanvasXY  "{ME}",  %FORM1_CANVAS1, X&, Y&, 0,0
     MSGBOX "You clicked me:"+STR$(X&)+STR$(Y&)+" "+EZ_TestSpriteClick("{ME}", %FORM1_CANVAS1, X&,Y&)
END SUB


Chris Boss

  • Administrator
  • Newbie
  • *****
  • Posts: 49
    • View Profile
Re: Sprite Problem
« Reply #4 on: December 19, 2022, 12:54:42 pm »
EZ_TestSpriteClick does not actually test for an actual click. It simply is passed the coordinates of the last click and it just calculates from that position which sprite the coordinates are on.

You have to get the actual mouse click position for the canvas control first, which is done using the EZ_GetCanvasXY command which you can call in the Canvas controls %EZ_Click event.

EZGUI stores the last X, Y position when the left mouse button is pressed down. It generates the %EZ_Click event so you can process it there.

Now let's say you want to drag a sprite around, you can subclass the Canvas control and then in the %EZ_LButtonDown event get the X,Y position of the mouse and then call EZ_TestSpriteClick and then start a drag process to draw the sprite around.

You can use EZ_GetMouseXY instead of EZ_GetCanvasXY when processing it in the %EZ_LButtonDown event.

Here is some old EZGUI 4.0 code (should work) where I drag a sprite around by processing the %EZ_LButtonDown event and using EZ_TestSpriteClick to figure out which sprite needs to be dragged:

Code: [Select]
UB Form1_Events(CID&, CMsg&, CVal&, Cancel&)
    LOCAL X&, Y&, SX&, SY&
    STATIC SPN$
    SELECT CASE CID&
        CASE %EZ_Window
            IF CMsg&=%EZ_Started THEN
                MakeSprites
            END IF
        CASE  %FORM1_CANVAS1
            IF CMsg&=%EZ_SelectCursor THEN
                IF App_MoveFlag&=0 THEN
                    SPN$ = EZ_TestSpriteClick ("Form1", %FORM1_CANVAS1, App_MouseX&, App_MouseY&)
                    IF SPN$<>"" THEN
                        EZ_SetCursor "", 4
                        Cancel&=1
                    END IF
                END IF
            END IF
            IF CMsg&=%EZ_LButtonDown THEN
                EZ_GetMouseXY CVal&, X&, Y&
                SPN$ = EZ_TestSpriteClick ("Form1", %FORM1_CANVAS1, X&, Y&)
                IF SPN$<>"" THEN
                    IF EZ_GetSpriteXY (SPN$, SX&, SY&) THEN
                        EZ_SetCaptureEx EZ_Handle("Form1", %FORM1_CANVAS1)
                        EZ_SetCursor "", 1
                        App_MoveFlag&=1
                        App_OffsetX&=X&-SX&
                        App_OffsetY&=Y&-SY&
                    END IF
                END IF
            END IF
            IF CMsg&=%EZ_MouseMove THEN
                EZ_GetMouseXY CVal&, X&, Y&
                App_MouseX&=X&
                App_MouseY&=Y&
                IF App_MoveFlag& THEN
                    EZ_MoveSprites SPN$, X&-App_OffsetX&, Y&-App_OffsetY&, 0
                    EZ_UpdateClient "Form1", %FORM1_CANVAS1
                END IF
            END IF
            IF CMsg&=%EZ_LButtonUp THEN
                App_MoveFlag&=0
                EZ_SetCaptureEx 0
            END IF
        CASE ELSE
    END SELECT
END SUB             

Notice when dragging a sprite you need to capture the mouse once you process the %EZ_LButtonDown event.