Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can I implement mouse drag from one Studio plugin GUI to another?

Asked by
aschepler 135
6 years ago

I would like to be able to drag objects between two DockWidgetPluginGuis created by the same Studio plugin. I have dragging within a window working, but it seems I don't get any events for the other window while the mouse button is still held down.

Example code:

local win1 = plugin:CreateDockWidgetPluginGui(
    "MouseTest_Window1",
    DockWidgetPluginGuiInfo.new(Enum.InitialDockState.Float, true, true)
)
win1.Name = "MouseTest_Window1"
win1.Title = "Mouse Test 1"
local frame1 = Instance.new("Frame", win1)
frame1.Size = UDim2.new(1,0,1,0)
frame1.BackgroundColor3 = Color3.new(.5, .5, .5)
local square = Instance.new("Frame", frame1)
square.Size = UDim2.new(0,50,0,50)
square.BackgroundColor3 = Color3.new(.1, 1, .1)

local win2 = plugin:CreateDockWidgetPluginGui(
    "MouseTest_Window2",
    DockWidgetPluginGuiInfo.new(Enum.InitialDockState.Float, true, true)
)
win2.Name = "MouseTest_Window2"
win2.Title = "Mouse Test 2"
local frame2 = Instance.new("Frame", win2)
frame2.Size = UDim2.new(1,0,1,0)
frame2.BackgroundColor3 = Color3.new(.5, .5, .5)

-- nil when not currently dragging
local mouseToSquare

local function MoveSquare(mouseX, mouseY)
    print("Move", mouseX, mouseY)
    if mouseToSquare then
        square.Position = UDim2.new(0, mouseX + mouseToSquare.X, 0, mouseY + mouseToSquare.Y)
    end
end

local function OnEnterFrame(frame, x, y)
    print("Enter", frame.Parent.Name, x, y)
    if mouseToSquare then
        square.Parent = frame
        MoveSquare(x, y)
        square.BackgroundTransparency = 0
    end
end

local function OnLeaveFrame()
    print("Leave")
    if mouseToSquare then
        -- Fade the square
        square.BackgroundTransparency = 0.5
    end
end

square.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        mouseToSquare = square.AbsolutePosition - Vector2.new(input.Position.X, input.Position.Y)
        local event
        event = input.Changed:Connect(function()
            print("input object:", input.UserInputState, input.UserInputType)
            if input.UserInputState == Enum.UserInputState.End then
                square.BackgroundTransparency = 0
                mouseToSquare = nil
                event:Disconnect()
            end
        end)
    end
end)

frame1.MouseEnter:Connect(function(x,y) OnEnterFrame(frame1, x, y) end)
frame1.MouseMoved:Connect(MoveSquare)
frame1.MouseLeave:Connect(OnLeaveFrame)
frame2.MouseEnter:Connect(function(x,y) OnEnterFrame(frame2, x, y) end)
frame2.MouseMoved:Connect(MoveSquare)
frame2.MouseLeave:Connect(OnLeaveFrame)

The green square can be dragged around window 1, and fades if the mouse leaves that window, but I don't get any print output at all when the mouse moves into or through window 2. If I release the mouse button in window 2, I do start getting input events there, so I could "snap" the object into the window at that time, but it would be much nicer to be able to see where you're placing it.

I also tried looking at the plugin mouse from plugin:Activate(true) and plugin:GetMouse(), but didn't see any events from it during this sort of drag either.

Is there some way to detect a mouse drag into another window, or is this just not possible?

Answer this question