// Drag 2 Tshirts around screen
//
// @author Kim Bruce
//

dialect "rtobjectdraw"
import "Tshirt" as ts
type MoreDraggable = ts.MoreDraggable


def DragTshirts: GraphicApplication = object {
    inherit graphicApplicationSize (400 @ 400)

    windowTitle := "Drag 2 Shirts"

    // Create shirts
    var otherShirt: MoreDraggable :=
          ts.tShirtAt (75 @ 50) size (10) on (canvas)
    var selectedShirt: MoreDraggable :=
          ts.tShirtAt (225 @ 50) size (10) on (canvas)

    selectedShirt.color := colorGen.red
    otherShirt.color := colorGen.blue


    // last location of mouse
    var lastPoint: Point

    // Is a tshirt being dragged
    var dragging: Boolean

    // Set selectedShirt to one mouse pressed on (if any)
    // Set dragging to true if pressed on one of them
    method onMousePress (point: Point) -> Done{
        lastPoint := point

        if (selectedShirt.contains(point)) then {
            dragging := true
        } elseif {otherShirt.contains (point)} then {
            def tempShirt: MoreDraggable = selectedShirt
            selectedShirt := otherShirt
            otherShirt := tempShirt
            dragging := true
            selectedShirt.sendToFront
        } else {
            dragging := false
        }
    }

    // drag the selected shirt if dragging
    method onMouseDrag (point: Point) -> Done{
        if (dragging) then {
            selectedShirt.moveBy (point.x-lastPoint.x,point.y-lastPoint.y)
            lastPoint := point
        }
    }

    // return shirts to starting location
    method onMouseRelease (point: Point) -> Done{
        otherShirt.reset
        selectedShirt.reset
    }

    // required to pop up window and start graphics
    startGraphics


}
