GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2022-12-16 00:56:33

felres
Game Designer
From: CR
Registered: 2021-05-22
Posts: 4
Website

drag_and_drop (for quickie debugging)

Sometimes when testing stuff I need to move objects around that dont normally move. I always wished I could just drag them like a window so I made this script you can just drop at the step event.

Expand/// drag_and_drop([button?])
//
//  Enables the object to be drag and dropped with the mouse around the room.
//
//      button?         Optional. Can be enabled if the sprite works as a button.
//                      ie. contains 2 images of a button being pressed.
//
/// GMLscripts.com/license
{
    var xx, yy, isbutton, hold, pressed, released;
    // Implement global vars
    if !variable_global_exists("cursor_dnd")
    {
        global.cursor_hover = noone;
        global.cursor_dnd = noone;
        global.cursor_xoffset = 0;
        global.cursor_yoffset = 0;
    }
    
    // variables
    xx = mouse_x;
    yy = mouse_y;
    isbutton = false;
    if(argument_count>0) isbutton = argument[0];
    if isbutton image_speed = 0;
    
    // inputs
    hold = mouse_check_button(mb_left);
    pressed = mouse_check_button_pressed(mb_left);
    released = mouse_check_button_released(mb_left);
    
    // mark as hover instance
    if position_meeting(xx, yy, id) && !instance_exists(global.cursor_hover)
    {
        global.cursor_hover = id;
    }
    if(global.cursor_hover == id)
    {
        if(!position_meeting(xx, yy, id))
            global.cursor_hover = noone;
    }
    
    // detect click
    if position_meeting(xx, yy, id) && pressed && !instance_exists(global.cursor_dnd)
    {
        global.cursor_dnd = id;
        global.cursor_xoffset = x-xx;
        global.cursor_yoffset = yy-y;
    }
    // dragging
    if(global.cursor_dnd == id)
    {
        x = xx+global.cursor_xoffset;
        y = yy-global.cursor_yoffset;
        if isbutton image_index = 1;
        if released
        {
            global.cursor_dnd = noone;
            if isbutton image_index = 0;
        }
    }
}

Offline

Board footer

Powered by FluxBB