Making a MaxGUI Application: StripAnimMakerLite Part 7b - Capturing Image to AnimStrip
(c) Assari Jan 29 2006

The next big piece we need to add is the ability to capture the image under to the cursor to the AnimStrip.
The user will be using the right click mouse button to transfer the image to the Animstrip.

We also need a data structure to hold our transferred strips. The simplest is to simply paste these images directly onto the canvas but this method will make it more difficult for us later when we want to do re-sequencing, deletes etc.

What we are going to use is a data structure called lists. Fortunately for us, Blitzmax already have a nice set of built in list functions that we can use.

New Additions to Part 1 - Variable Definitions

To handle the anim strips we will require 3 new variables
    Global AnimList:TList=CreateList()
Global AnimSize:Int=0 '---------Current size of the animation strip
Global AnimListPtr:TLink=Null


New Additions to Part 2 - Gadget Creation Block

Nothing to change here

New Additions to Part 3 - MAIN LOOP

No addition to the Main Loop.

New Additions to Part 4 - Function Definitions

The Changes here are:-

Function Process_MouseDownEvent()

This function requires only a small modification, that is to add in a case statement to capture the right click event and then call the required function to handle it.
    Function Process_MouseDownEvent:Int()

    Select EventSource()
        Case Canvas1
               Select EventData()
                   Case MOUSE_LEFT
                    If CursorResize=False 'We are not normal mode
                          CursorResize=True    'user click left mouse
                        xsize_old=xsize
                        ysize_old=ysize
                          mx1_Old=mx1
                          my1_Old=my1
                          MoveMouse(mx1+xsize,my1+ysize)
                    Else 'We are in change size mode, so back out
                          CursorResize=False       
                        xsize=mx1a-mx1_Old
                          ysize=my1a-my1_Old
                        If xsize>MaxCursorSize Then xsize=MaxCursorSize
                        If xsize>MaxCursorSize Then xsize=MaxCursorSize
                          MoveMouse(mx1_Old,my1_Old)
                    EndIf
                Case MOUSE_RIGHT
                    CopyPixUnderCursorToAnimStrip()
                   
               End Select 'eventdata() 
       RedrawGadget(Canvas1)
    End Select 'eventsource()

End Function




Function CopyPixUnderCursorToAnimStrip

Not only is the name of this function long but the code is long and complicated too. Just take it from me that it works as its name suggests. I'm not going to try to explain this one for now :)
    Function CopyPixUnderCursorToAnimStrip:Int()

  CursorResize=False
  mx1_Old=mx1; my1_Old=my1

  Local x:Int=Mx1
  Local y:Int=My1
  Local mask:Int

  If xsize>MaxCursorSize Then xsize=MaxCursorSize
  If ysize>MaxCursorSize Then ysize=MaxCursorSize
 
  If xsize>AnimSize AnimSize=xsize
  If ysize>AnimSize AnimSize=ysize

  Local Tmp:TPixmap=CreatePixmap(AnimSize,AnimSize,PixmapFormat(PixMap1))
  'set all backgrounf to black
  For Local i:Int=0 To AnimSize-1
    For Local j:Int=0 To AnimSize-1
      WritePixel(Tmp,i,j,0)
    Next
  Next

  Local YOffset:Int=(AnimSize-ysize)/2
  Local XOffset:Int=(AnimSize-xsize)/2
  mask=ReadPixel(Pix,x,y)

  For Local i:Int=0 To xsize-1
    For Local j:Int=0 To ysize-1
'-----make sure we are within bounds
      If i+x<PixmapWidth(Pix) And j+y<PixmapHeight(Pix)
        Local rgb:Int = ReadPixel(Pix,i+x,j+y)
        WritePixel(Tmp,i+XOffset,j+YOffset,rgb)
      EndIf
    Next
  Next

  Local r:Int, g:Int, b:Int, a:Int
  GetRGB(mask,a,r,g,b)
  Local Anim:TPixmap=MaskPixmap(Tmp,r,g,b)

  If AnimListPtr=Null Then
     AnimListPtr:TLink=ListAddLast(AnimList, Anim:TPixmap)
  Else
     AnimListPtr:TLink=AnimList.InsertAfterLink(Anim:TPixmap, AnimListPtr:TLink)
  EndIf

   MoveMouse(mx1_Old,my1_Old)
   RedrawGadget(Canvas3)

End Function


Function Process_GadgetPaintEvent()

We now need to add the code to display images from our Animation List to our Canvas3 paint area. Basically it iterates through each image in the Animation List and draw the image as well as a square frame around the image.
    Function Process_GadgetPaintEvent:Int()

    Select EventSource()
    Case Canvas1
        SetGraphics CanvasGraphics (Canvas1)
        Cls
        DrawPixmap Pix,0,0
        If MouseInCanvas1 Then
            DrawCursor(mx1,my1,xsize, ysize)
            If CursorResize=True
                SetColor 255,0,0
                DrawCursor(mx1_old,my1_old,xsize_old, ysize_old)
                SetColor 0,0,0
            EndIf
            RedrawGadget(Canvas2)
        EndIf
        Flip

    Case Canvas2
        SetGraphics CanvasGraphics(Canvas2)
        Cls
        If MouseInCanvas1=True Then
            If CursorResize=False Then DrawPixUnderCursor(Canvas2,Pix,mx1,my1)
            If CursorResize=True Then DrawPixUnderCursor(Canvas2,Pix,mx1_Old,my1_Old)

        EndIf
        Flip

    Case Canvas3
        SetGraphics CanvasGraphics(Canvas3)
        Cls
        If AnimListPtr<>Null Then
            Local j:Int=0
            For Local p:TPixMap=EachIn AnimList
                  Local YOffset:Int=(AnimSize-PixmapHeight(p))/2
                  Local XOffset:Int=(AnimSize-PixmapWidth(p))/2
                DrawPixmap p,j*AnimSize+XOffset,YOffset
                DrawCursor(j*AnimSize,0,AnimSize,AnimSize)
                j=j+1
            Next
        EndIf
        Flip
   
    End Select 'EventSource()

End Function


Download the source (link) and compile and run the program. Who should be able to duplicate the screenshot below.

   



That's all for now. Return to Main Index.