beads 1 program tiles

record tile_move
    dg : yesno
    r : a_rect
    board : array of num

const
    op = 0.5
    
var
    tn : tile_move
    ti // tile index
    tv // tile value
    
    
grid d_board
    horz slice
        skip 5 pt
        loop reps:4
            add 50 pt
            skip 5 pt
    vert slice
        skip 5 pt
        loop reps:4
            add 50 pt
            skip 5 pt
    under
        draw_rect(fill:KHAKI)
    cell
        //draw_rect(fill:GRAY2,thick:1 pt)
        draw_rect(fill:GRAY2,thick:1 pt,opacity:op)
        if tn.board[b.cell_seq] <> 0
            draw_str("{tn.board[b.cell_seq]}",size:0.5,bold:Y) //,opacity:op)
    over
        if tn.dg
            draw_rect(box:tn.r,fill:RED,thick:1 pt)
            if tv <> 0
                draw_str("{tv}",color:WHITE,box:tn.r,size:0.5,bold:Y)

track
    case e.evkind
    | EV_DRAG_BEGIN
        log "begin drag {b.cell_seq}"
        if is_numeric(b.cell_seq)
            tn.dg = Y
            ti = b.cell_seq
            tv = tn.board[b.cell_seq]
            adjust_drag_cell(b.box,e.global_x,e.global_y,50 pt,50 pt)
    | EV_DRAG_MOVE
        log "{tn.dg}"
        if tn.dg
            adjust_drag_cell(b.box,e.global_x,e.global_y,50 pt,50 pt)
    | EV_DRAG_END
        log "end drag"
        if tn.dg
            if is_numeric(b.cell_seq)
                swap tn.board[ti] <=> tn.board[b.cell_seq]
            tn.dg = N
    
calc adjust_drag_cell(
    r : a_rect
    x
    y
    w
    h
    )
    r.left = x - w /. 8
    r.top = y - h /. 8
    tn.r <=== solve_rect(basis:r,width:w, height:h)

vert slice main_draw
    tn.dg = N
    tn.board <=== [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0]
    add 50 al d_board()
    add 50 al
        draw_rect(fill:DARK_CYAN)