Page 1 of 1

Issues of A drag 'n' drop using tables and for loops

Posted: Wed Mar 29, 2023 4:52 pm
by Cauan
Hi, I'm having a small problem with the Drag 'n' drop system that when I hover the mouse over another rectangle, they automatically join, I don't know how to solve it, because if I put a variable that is activated when I hover the mouse over it, it activates drag and drop, activates and the two join again in the same way, can you help me my code is this:

Code: Select all

-- create some rectangles
local rects = {}
local numRects = 15
for i = 1, numRects do
    rects[i] = {
        x = math.random(0, love.graphics.getWidth()),
        y = math.random(0, love.graphics.getHeight()),
        w = 50,
        h = 50,
    }
end

function love.update(dt)
    if love.mouse.isDown(1) then -- if left mouse button is pressed
        -- get mouse position
        local mx, my = love.mouse.getPosition()

        -- check if mouse is over any rectangle
        for i = 1, numRects do
            local rect = rects[i]

            if mx > rect.x and mx < rect.x + rect.w and my > rect.y and my < rect.y + rect.h then
                -- if mouse is over rectangle, then update position
                rect.x = mx - rect.w/2
                rect.y = my - rect.h/2
            end
        end
    end
end

function love.draw()
for i = 1, numRects do
            local rect = rects[i]
love.graphics.rectangle("fill", rect.x, rect.y, rect.w, rect.h)
end
end
Help me pls:cry::cry::cry: :cry: :cry: :cry:

Re: Issues of A drag 'n' drop using tables and for loops

Posted: Wed Mar 29, 2023 7:02 pm
by darkfrei
Pseudocode:
If mouseRect then update rectangle position
Elseif mouseHover and mouseDown then mouseRect = mouseHover
else iterate all rectangles if collision then mouseHover = rectangle, if nothing was hovered then mouseHover = nil

And if not mouseDown then mouseRect = nil

Re: Issues of A drag 'n' drop using tables and for loops

Posted: Wed Mar 29, 2023 8:19 pm
by slime
As I mentioned in one of your other threads, this isn't the right subforum for general support questions. Please use the Support and Development subforum instead.

(edit: I moved all your threads for you. Please stop posting general support questions in the Ports subforum.)

Re: Issues of A drag 'n' drop using tables and for loops

Posted: Thu Mar 30, 2023 9:33 am
by darkfrei
Cauan wrote: Wed Mar 29, 2023 4:52 pm Help me pls:cry::cry::cry: :cry: :cry: :cry:
Possible optimizations:
move some code to love.keypressed, love.mousemoved, love.mousereleased
store variables out of rectangles

Code: Select all

-- License CC0 (Creative Commons license) (c) darkfrei, 2023

Width, Height = love.graphics.getDimensions( )

function love.load()
	Rectangles = {}
	for i = 1, 10 do
		local w = love.math.random (Width/8)+1/8*Width
		local h = love.math.random (Height/8)+1/16*Height
		local x = love.math.random (Width-w)
		local y = love.math.random (Height-h)
		local rectangle = {x=x,y=y,w=w,h=h,dx=0,dy=0}
		table.insert(Rectangles, rectangle)
	end
end

local function isOn (mx, my, rect)
	return mx > rect.x and mx < rect.x + rect.w and my > rect.y and my < rect.y + rect.h or false
end
 
function love.update(dt)
	local mx, my = love.mouse.getPosition()
	local mouseDown = love.mouse.isDown(1)
	local hovered = false
	for i = 1, #Rectangles, 1 do
		local rect = Rectangles[i]
		if not hovered and isOn (mx, my, rect) then
			rect.hovered = true
			hovered = rect
		else
			rect.hovered = false
		end
		if not mouseDown then
			rect.taken = false
		end
	end
	
	
	if hovered and mouseDown then
		if hovered.taken then
			hovered.x = mx-hovered.dx
			hovered.y = my-hovered.dy
		else
			hovered.taken = true
			hovered.dx = mx-hovered.x
			hovered.dy = my-hovered.y
		end
	end
end


function love.draw()
	-- draw backwards!
	for i = #Rectangles, 1, -1 do
		local rect = Rectangles[i]
		if rect.taken then
			love.graphics.setColor (0.5,0.5,0.5)
			love.graphics.rectangle('fill', rect.x, rect.y, rect.w, rect.h)
			love.graphics.setLineWidth(3)
			love.graphics.setColor (0,1,0)
			love.graphics.rectangle('line', rect.x, rect.y, rect.w, rect.h)
		elseif rect.hovered then
			love.graphics.setColor (0.5,0.5,0.5)
			love.graphics.rectangle('fill', rect.x, rect.y, rect.w, rect.h)
			love.graphics.setLineWidth(2)
			love.graphics.setColor (1,1,0)
			love.graphics.rectangle('line', rect.x, rect.y, rect.w, rect.h)
		else -- not active
			love.graphics.setColor (0.5,0.5,0.5)
			love.graphics.rectangle('fill', rect.x, rect.y, rect.w, rect.h)
			love.graphics.setLineWidth(1)
			love.graphics.setColor (1,1,1)
			love.graphics.rectangle('line', rect.x, rect.y, rect.w, rect.h)
		end
		love.graphics.print ('x:'..rect.x..' y:'..rect.y, rect.x, rect.y)
		love.graphics.print ('dx:'..rect.dx..' dy:'..rect.dy, rect.x, rect.y+14)
	end
end

Re: Issues of A drag 'n' drop using tables and for loops

Posted: Tue May 16, 2023 3:31 pm
by Cauan
slime wrote: Wed Mar 29, 2023 8:19 pm (edit: I moved all your threads for you. Please stop posting general support questions in the Ports subforum.)
Ok thank you