This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
wilcomega's profile picture

work around for a silly problem

Started by wilcomega, 25 August 2014 - 04:38 PM
wilcomega #1
Posted 25 August 2014 - 06:38 PM
the problem is that i am making a program where you drag boxes around.

for every mouse_drag or mouse_click event, i am checking if the previous cursor coords or the current ones are in the box,
i am keeping track of the prev coords by at the end of the loop setting them to the current one, i need this because when i started dragging inside the box and then moved my cursor while dragging out of the box it would not see that its dragging anymore because its outside.

so the problem is that it only checks if the cursor is in the box or the prev cursor every mouse click or drag, so what happens is, i release the box and the prev coords stay in the box of course, so the next time i click it will move the box to where i clicked, and not dragged. and as you can image, this keeps happening until the universe ends( or my laptop runs out of juice )

i know i have done it before but i just cant find my old code back.

i dont care if you fix my current system or make a new system to drag the boxes around.

since i dont want to spoil to much of the project i am working on, i will only post this bit of code that is relevant

Spoiler

if ev[1] == "mouse_drag" or ev[1] == "mouse_click" then
  if ev[2] == 1 then
	for k,v in pairs( tiles ) do
	  local posx, posy = v.window.getPosition()
	  if inrange2( ev[3], ev[4], posx + scroll, posy, posx + scroll + v.width * theme.start.appSizeX, posy + v.height * theme.start.appSizeY ) or
	  inrange2( lastX, lastY, posx + scroll, posy, posx + scroll + v.width * theme.start.appSizeX, posy + v.height * theme.start.appSizeY ) then
		v.dragged = true
		dragOffsetX = -(ev[3] - posx)
		dragOffsetY = -(ev[4] - posy)
		v.window.reposition( ev[3] + dragOffsetX + ( ev[3] - lastX ), ev[4] + dragOffsetY + ( ev[4] - lastY ), v.width * theme.start.appSizeX, v.height *		   theme.start.appSizeY )
	  else
		v.dragged = false
	  end
	end
  end
end

the inrange2() function is a function i made to see if a pair of coords are indisde 2 others wih means 3 arguments for each axis and 6 in total
Edited on 25 August 2014 - 04:39 PM
Writer #2
Posted 25 August 2014 - 06:46 PM
Can you show us what does the tables 'ev' 'tiles' do in the code? It could be relevant.
Edited on 25 August 2014 - 04:47 PM
Bomb Bloke #3
Posted 25 August 2014 - 07:36 PM
I would use a separate "dragging" variable (as opposed to the ones you have in your tile tables), set to nil by default. If a mouse_click event is detected on a box, set it to the index of that box. As soon as another mouse_click event is detected, set it to nil again before repeating the check to see if a box was clicked.

Whenever a drag is detected, if the "dragging" variable points to an index, move the indicated box around. If it's nil, then, well, don't.
TheOddByte #4
Posted 25 August 2014 - 07:48 PM
Hmm, I've actually done a script like this that I used to create message boxes that we're draggable
This is a part of the script that I used, as you can see I have a variable to check for dragging.

local drag, click_x = false, nil
while true do
    local e = { os.pullEvent() }
    if e[1] == "mouse_click" then
        -- Change this code to whatever you use to see if it was inside the box
        if e[3] >= self.x and e[3] <= self.x + self.w - 1 and e[4] == self.y then
            drag = true
            click_x = e[3]
        else
            -- the click was outside the box, set drag to false
            drag = false
            x = nil;
        end

    elseif e[1] == "mouse_drag" then
        if drag then
            self.x = self.x + ( e[3] - click_x )
            self.y = e[4]
            x = e[3]
        end
    end
end
As you can see I handle the events separately instead of handling them in the same chunk of code
wilcomega #5
Posted 25 August 2014 - 08:34 PM
thanks for all of your reply's ill be checking it all out tommorow as for today i am done with it ;)/>