[15 Feb 2006] drag a sprite

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
User avatar
lazi
Posts: 572
Joined: Thu Feb 24, 2011 11:08 pm

[15 Feb 2006] drag a sprite

Post by lazi »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Wed, 15 Feb 2006 22:25:40 +0100

If anybody interested, here is a little example that makes some sprites that could be drag by mouse.

You have to change the filenames in the keptar array to make it work on your config (and &brush 10, of course).

I am very impressed of the new "lua" syntax and the way of handling events.

Code: Select all

@display { Sizeable = True, Title = "Szózeum", X = #CENTER, Y = #CENTER, Width = 320, Height = 240, Mode = "ask"}
/* This is the brush+button of the check function */
@BRUSH 10, "Classic:Data/Pictures/DockBrushes/Radial124-176/Picture.png"
Const #CHECK = 10

CreateGradientBGPic(2,#LINEAR,#BLACK,#BLUE)
DisplayBGPic(2)

Const #LASTITEM =   2
Const #CIMKEW   = 100
Const #CIMKEH   =  10

keptar={
    { file="Classic:Data/Pictures/clips/used_clips/pencil.lbm",
      cim ="ceruza",
      lang="pencil",
      xpos= 20,
      ypos= 40,
      xsize=80,
      ysize=100,
      cx  = Rnd(220),
      cy  = Rnd(50)+180
    },

    { file="Classic:Data/Pictures/clips/Chiks/Chickt.gif",
      cim ="csirke",
      lang="chicken",
      xpos= 120,
      ypos= 40,
      xsize=80,
      ysize=100,
      cx  = Rnd(220),
      cy  = Rnd(50)+180
    },

    { file="Classic:Data/Pictures/clips/1clips/magglass.lbm",
      cim ="nagyító",
      lang="magnifier",
      xpos= 220,
      ypos= 40,
      xsize=80,
      ysize=100,
      cx  = Rnd(220),
      cy  = Rnd(50)+180
      }
    }

s_id=-1

/*
** Handling the user input. Moving sprites and so on...
*/

function p_button(msg)

    switch (msg.action)

    case "OnMouseDown":
        s_id=msg.id
        If s_id <= #LASTITEM
           dx=Mousex()-keptar[s_id].cx
           dy=Mousey()-keptar[s_id].cy
        Endif

    case "OnMouseMove":
        if s_id>=0
            if IsleftMouse()
                keptar[s_id].cx=Mousex()-dx
                keptar[s_id].cy=MouseY()-dy
                displaysprite (s_id,keptar[s_id].cx,keptar[s_id].cy)
                Makebutton(s_id,#SIMPLEBUTTON,keptar[s_id].cx,keptar[s_id].cy,#CIMKEW,#CIMKEH,{Onmousedown=p_button})
            else
                s_id=-1
            endif
        endif

    endswitch

endfunction

/*
** Rendering initial graphics
*/

function p_renderb()
    For i=0 to #LASTITEM
        LoadBrush(i,keptar[i].file)
        ScaleBrush   ( i,keptar[i].xsize, keptar[i].ysize )
        DisplayBrush ( i, keptar[i].xpos, keptar[i].ypos )
        CreateBrush(#LASTITEM+1,#CIMKEW,#CIMKEH,#RED)
        SelectBrush(#LASTITEM+1)
        TextOut(#CENTER,#CENTER,keptar[i].lang)
        CreateSprite(i,#BRUSH,#LASTITEM+1)
        EndSelect()
        DisplaySprite(i,keptar[i].cx,keptar[i].cy)
        Makebutton(i,#SIMPLEBUTTON,keptar[i].cx,keptar[i].cy,#CIMKEW,#CIMKEH,{OnmOuseDown=p_button})
        DisplayBrush (#CHECK, 320-56, 240-46)
        MakeButton (#CHECK, #SIMPLEBUTTON, 320-56, 240-46,56,46,{OnMouseDown=p_check})
    Next
endfunction

/*
** To check if the sprite is on the right picture
** If so then align to it.
*/

function p_check()
    For i=0 to #LASTITEM
        local xm = Abs ( (keptar[i].cx + ( #CIMKEW / 2 )) - (keptar[i].xpos + (keptar[i].xsize / 2)) )
        local ym = Abs ( (keptar[i].cy + ( #CIMKEH / 2 )) - (keptar[i].ypos + (keptar[i].ysize / 2)) )
      If xm < (keptar[i].xsize / 2) and ym < (keptar[i].ysize / 2)
            Local newx= keptar[i].xpos
            Local newy= keptar[i].ypos+keptar[i].ysize
            MoveSprite (i,keptar[i].cx, keptar[i].cy, newx, newy ,1,5)
            Keptar[i].cx=newx
            Keptar[i].cy=newy
      endif
    Next
endfunction

p_renderb

InstallEventHandler({OnMouseMove=p_button, SizeWindow=p_renderb})

Repeat
 Waitevent()
Forever

Locked