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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Cauan
Prole
Posts: 22
Joined: Thu Mar 09, 2023 5:58 pm

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

Post 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:
I'm me, and you are you
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
slime
Solid Snayke
Posts: 3129
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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.)
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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
Attachments
2023-03-30.png
2023-03-30.png (23.08 KiB) Viewed 1520 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Cauan
Prole
Posts: 22
Joined: Thu Mar 09, 2023 5:58 pm

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

Post 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
I'm me, and you are you
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests