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

Easy Mouse GUI

Started by Sammich Lord, 13 October 2012 - 09:39 AM
Sammich Lord #1
Posted 13 October 2012 - 11:39 AM
I have been messing around with the new mouse support and I made this really cool function.
Here is a example program with it.

local running = true
--[[Menu Functions]]--
local function stop()
  running = false
end
local function hey()
  print("Hey")
end
--[[Menu Table]]--
local buttons = {
  [1] = {buttonType = 1, startX = 1, endX = 1, startY = 1, endY = 1, buttonText = "&", command = hey},
  [2] = {buttonType = 2, startX = 2, endX = 5, startY = 2, endY = 5, buttonText = "Hello", command = stop}
}
--[[Main Function]]--
function menu(menu)
  for k,v in ipairs(menu) do
    term.setCursorPos(menu[k].startX, menu[k].startY)
    print(menu[k].buttonText)
  end
  while running do
    event, p1, p2, p3 = os.pullEvent()
    if event == "click" then
	  for k,v in ipairs(menu) do
	    if p3 == menu[k].buttonType then
		  if p1 >= menu[k].startX and p1 <= menu[k].endX and p2 >= menu[k].startY and p2 <= menu[k].endY then
		    menu[k].command()
		  end 
	    end
	  end
    end
  end 
end
term.clear()
menu(buttons)
It is pretty simple but very awesome!
Exerro #2
Posted 13 October 2012 - 04:58 PM
what does it do? does it actually detect clicks?
Sammich Lord #3
Posted 13 October 2012 - 05:00 PM
what does it do? does it actually detect clicks?
Well what it does is first print the text that is defined as a var in table. Then it waits for a mouse click then it checks if that click was made with the cords set in the table. If all those are right it runs the function defined in the table.
It is quite simple but it is the first mouse function I have seen on the forums.
Tiin57 #4
Posted 13 October 2012 - 05:01 PM
WHOA. HOLD UP. New mouse support? Holy mother of God, thank you! My prayers have been answered! (If this is a troll, I will reach through the screen and slap you.)
Sammich Lord #5
Posted 13 October 2012 - 05:08 PM
WHOA. HOLD UP. New mouse support? Holy mother of God, thank you! My prayers have been answered! (If this is a troll, I will reach through the screen and slap you.)
In the snapshot gold computers have mouse support.
It returns the event "click". Test it in the Lua prompt and see how it works.
Lettuce #6
Posted 13 October 2012 - 05:45 PM
Wait, snapshot? Where do I get that? That sounds AWESOME.
Kadecamz #7
Posted 13 October 2012 - 06:03 PM
hi.
Will it work when editing programs?

also, can you make a GUI api?
Sammich Lord #8
Posted 13 October 2012 - 06:27 PM
hi.
Will it work when editing programs?

also, can you make a GUI api?
Well to make a GUI just copy the function and add a table with the vars that are used in the above code and add functions for each selection.
I plan on making something a lot more complex for CommuteOS though.
Exerro #9
Posted 13 October 2012 - 08:22 PM
omg how do you get the snapshot this looks amazing…does it track the mouse as you move it so you can have it do stuff with hovering?
Sammich Lord #10
Posted 13 October 2012 - 08:47 PM
omg how do you get the snapshot this looks amazing…does it track the mouse as you move it so you can have it do stuff with hovering?
Well it is a beta not a snapshot. Go to the beta testing section in the forums.
There is no function to get the mouse position so you can only tell when a mouse is clicked.
Exerro #11
Posted 13 October 2012 - 09:17 PM
Well it is a beta not a snapshot. Go to the beta testing section in the forums.
There is no function to get the mouse position so you can only tell when a mouse is clicked.
ok…thanks i got it and its flipping amazing! this will open up so many possibilities
Lettuce #12
Posted 13 October 2012 - 10:17 PM
Did that just get added? Like, how did I never see that? Checkin' it out too…
ChaddJackson12 #13
Posted 14 October 2012 - 01:29 AM
This seems to be very awesome. Also, I didn't know that there was a "click" event for computercraft
Exerro #14
Posted 14 October 2012 - 03:18 PM
This seems to be very awesome. Also, I didn't know that there was a "click" event for computercraft
the click event is new in the beta release…its awesome but i think there are a couple of bugs :l
ardera #15
Posted 14 October 2012 - 04:22 PM
WHOA. HOLD UP. New mouse support? Holy mother of God, thank you! My prayers have been answered! (If this is a troll, I will reach through the screen and slap you.)

Oh no…….
I made an mouse support for 1.4/1.42… It has 2 big files and is for CommuteOS… But if the mouse is now added I can delete them…