Jump to content




[ADV. Comp] Mouse drag

utility computer lua

  • You cannot reply to this topic
4 replies to this topic

#1 zacpier

  • Members
  • 44 posts
  • LocationUS

Posted 27 January 2013 - 08:55 PM

I'm going to assume that you can at least understand this:
colorOptions = {colors.red, colors.white, colors.blue, colors.green, colors.orange}
function dPixed(x,y)
  local lastX, lastY = term.getCursorPos()
  term.setBackgroundColor(colorOptions[math.random(1,#colorOptions])
  term.setCursorPos(x,y)
  write(" ")
  term.setCursorPos(lastX,lastY)
end
while true do
  type, btn, x, y = os.pullEvent("mouse_click")
  dPixel(x,y)
end

If you didn't get that, then here's an explanation.
Spoiler

As you may know, you can use os.startTimer(paramTime) to queue a timed event. What you may not know is: it returns a unique timer ID.
With this id and a little ingenuity, you can make it only call a function when the last timer is called. EX:
--CurrentTimer
cT = 0 --Init the var (so we don't crash)
--Let us draw text at mouse's last pos
lastX = 0
lastY = 0
while true do
  type, v1, v2, v3 = os.pullEvent()
  if (type=="timer" and v1==cT) then
	term.setCursorPos(lastX,lastY)
	print("X")
	term.setCursorPos(0,0)
  end
  if (type=="mouse_click") then
	cT = os.startTimer(2)
	lastX = v2
	lastY = v3
  end
end

If you need an explanation:
Spoiler

I hope you're following me, this is my first tutorial in awhile :P

Now, to make it a drag function.

Initialize the currentTimer as 0 so we don't get errors.
Initialize drag so we don't get errors.
cTimer = 0
drag = false

Let's make a function that runs when we perform a "proper drag"
function myDragFunc(x,y)
  term.setCursorPos(x,y)
  write("O")
end

Next, I'll start our loop
while true do
  --In the loop
end
Everything from here on out is inside that loop.

Get input
-- Event type, argument 1, argument 2, argument 3 = os.pullEvent()
type,v1,v2,v3 = os.pullEvent()

Let's see if the timer went off.
If it did, let's tell the program that we can no longer drag, and tell the same to the user.
if (type=="timer" and v1==cTimer) then
  term.setCursorPos(1,1)
  write("dragEnd")
  drag=false
end

Now let's see if the input is a mouse_drag.

if (type=="mouse_drag") then


end

Everything from here on out will be inside the if.

See if it's where a drag can start. (In this example, a box starting at 5,5 and ending at 15,10)
if (v2>4 and v2<16 and v3>4 and v3<11) then
  drag = true
  cTimer = os.startTimer(1)
  myDragFunc(v2,v3)

  --Let's clear the "dragEnd" text
  term.setCursorPos(1,1)
  write("		")
end

And finially let's put in code for when drag==true.

if (drag) then
  cTimer = os.startTimer(1)
  myDragFunc(v2,v3)
end

All assembled, that would be:
Spoiler

Hopefully, you learned something.
If you use this knowledge in any way, I ask that you (not require) post what you made in my topic. I love seeing creativity in action!

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 28 January 2013 - 09:52 AM

if (type=="timer" and v1==cT) then

Quote

As you may know, you can use os.startTimer(paramTime) to queue a timed event.

You may want to fix that :P

#3 Andan

  • New Members
  • 1 posts

Posted 16 April 2014 - 02:17 PM

Sorry 2 reply after 1 year but I need to know something. Is it possible to get the x,y of the starting pixel and the x,y of the last pixel? Like "You dragged from x,y to x,y which means x pixels up and y pixels down"

#4 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 16 April 2014 - 02:32 PM

Yes you can, you simply record the first known position when the drag starts, and the last recorded position after a drag.

Edited by Cranium, 16 April 2014 - 02:33 PM.


#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 19 April 2014 - 07:44 AM

Well, you sorta can. Unfortunately you can't directly determine where the "last" position is, as there's no "mouse_release" event (for whatever reason).

This means you'll need to infer (... or guess) as to when the drag operation ended, by eg using a timer or by waiting for another click event.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users