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

making a simple mouse api/class?

Started by etopsirhc, 11 December 2012 - 02:08 PM
etopsirhc #1
Posted 11 December 2012 - 03:08 PM
ok so first off idk if its a class or api i'm trying to make , part of it may be class but at the same time its loaded by the load api and yet idk how to make a lua class but kinda know how to make an api
note: if your confused with that just imagine how confused i am XD

but yeah i have a mouse api/class i'm trying to make , where it can detect and return a table of info , or ( later configureable ) you can just use it like click.row to get the row , but i cant seem to get the row, col , ect to actualy work when i call them like that. so yeah , if you can let me know what i should be creating to work how i want lemme know , as well as how i should make it work like that ^^;

heres what i have
local mouseDown = {}
local mouseUp = {}
local mouseScroll = {}
event = ""
row = 0
col = 0
button = 0
direction = 0

os.loadAPI("rom/apis/parallel")

function down()
  while true do
    local e,b,c,r = os.pullEvent("mouse_click")
    if e == "mouse_click" then
	  mouseDown.button = b
	  mouseDown.col = c
	  mouseDown.row = r
	  mouseDown.event = e
	  event = e
	  row = r
	  col = c
	  button = b
	  direction = 0
	  return mouseDown
    end
  end
end

function up()
  while true do
    local e,b,c,r = os.pullEvent("mouse_drag")
    if e == "mouse_drag" then
	  mouseUp.button = b
	  mouseUp.col = c
	  mouseUp.row = r
	  mouseUp.event = e
	  event = e
	  row = r
	  col = c
	  button = b
	  dicrection = 0
	  mouseEvent = mouseUp
	  return mouseUp
    end
  end
end

function scroll()
  while true do
    local e,d,c,r = os.pullEvent("mouse_scroll")
    if e == "mouse_scroll" then
	  mouseScroll.direction = direction
	  mouseScroll.col = col
	  mouseScroll.row = row
	  mouseScroll.event = e
	  event = e
	  direction = d
	  col = c
	  row = r
	  button = 0
	  mouseEvent = mouseScroll
	  return mouseScroll
    end
  end
end

function getLastDown()
  return mouseDown
end

function getLastUp()
  return mouseUp
end

function getLastScroll()
  return mouseScroll
end

function listen()
  parallel.waitForAny(down,up,scroll)
end
 
Bubba #2
Posted 11 December 2012 - 03:21 PM
ok so first off idk if its a class or api i'm trying to make , part of it may be class but at the same time its loaded by the load api and yet idk how to make a lua class but kinda know how to make an api

Lua doesn't have classes and I am very confused by your code. There is no mouseUp, mouseScroll, or mouseDown API which is what you are trying to call (unless I've just completely missed it - but I don't think I have). What exactly are you trying to do?
etopsirhc #3
Posted 11 December 2012 - 03:34 PM
this is the api containing that XP

essentialy what i want to do is make a api work kinda like a class
you alreay call API.function() when you want to but where i'm getting stuck is trying to get the variables col, row , and the others not labeled as local at the top in a simmilar manner API.variable

edit: also i've seen refrences of lua classes , they kinda exist but in the sam way as javascript ones as tables but i dont want a class cause you'd then have to instantiate it , witch is pointless unless you have 2 mice :P/>

edit2: the mouseDown is a table that i'm adding a referancable variable instead of an indexed variable
so instead of mouseDown[1] you'd use mouseDown.col to get the coloums
Bubba #4
Posted 11 December 2012 - 03:42 PM
this is the api containing that XP

essentialy what i want to do is make a api work kinda like a class
you alreay call API.function() when you want to but where i'm getting stuck is trying to get the variables col, row , and the others not labeled as local at the top in a simmilar manner API.variable

edit: also i've seen refrences of lua classes , they kinda exist but in the sam way as javascript ones as tables but i dont want a class cause you'd then have to instantiate it , witch is pointless unless you have 2 mice :P/>

edit2: the mouseDown is a table that i'm adding a referancable variable instead of an indexed variable
so instead of mouseDown[1] you'd use mouseDown.col to get the coloums

Still confused by what you are saying. I suppose that you are thinking of metatables as classes, in which case yeah they do kind of work similarly to classes in other languages. But unless you have already made the mouseDown/mouseUp/mouseScroll APIs then the code you posted will not work. If you want to get the x,y positions of mouse clicks/drags then you can do something like this:


function getMouseClick()
  while true do
    local event, x, y = os.pullEvent()
    if event == "mouse_click" or event == "mouse_drag" then
       return x,y
    end
  end
end

local x,y = getMouseClick()
print("You clicked at: "..x..", "..y)
etopsirhc #5
Posted 11 December 2012 - 04:03 PM
lemme try and clarify , what you think are api's ( the mouseDown and that ) are just the metatables (now that i know the name ) to store the info gotten by the event
so the mouseDown.event is storeing the event string not getting it.
the entire code i posted is to be the api, i just cant get the vairables at the very top ( not labeld local ) to be accessable like the api was a metatable

so in an outside code i would use APIName.col and that would return the col variable from this api-thing

edit: looking at the code you posted i should change my listen one to somthing simmilar
Bubba #6
Posted 11 December 2012 - 04:13 PM
lemme try and clarify , what you think are api's ( the mouseDown and that ) are just the metatables (now that i know the name ) to store the info gotten by the event
so the mouseDown.event is storeing the event string not getting it.
the entire code i posted is to be the api, i just cant get the vairables at the very top ( not labeld local ) to be accessable like the api was a metatable

so in an outside code i would use APIName.col and that would return the col variable from this api-thing

edit: looking at the code you posted i should change my listen one to somthing simmilar

Okay I think I might get what you are saying now. You want to be able to load this as an api and then access the mouseDown/mouseUp/etc. tables from the calling program. You can do that a few different ways, but here are two.

First one:

  --Add this to your api code
  function getVars()
    return mouseDown, mouseUp, mouseScroll
  end

Second one:

--Change relevant sections of your api code
_G["mouseDown"] = {}
_G["mouseUp"] = {}
_G["mouseScroll"] = {}

mouseDown, mouseUp, and mouseScroll are now global variables and you can access them from any script.
etopsirhc #7
Posted 11 December 2012 - 04:26 PM
the second is closer to what i'm looking for * testing some stuff while typing* but would it work with the apiName before it …


edit: did some changes with the new info , but still having troubles ><
maybe i should re-write this as more class based than api , though somehow still loadable though api ?



_G["event"] = ""
_G["row"] = 0
_G["col"] = 0
_G["button"] = 0
_G["direction"] = 0

function down()
  while true do
    local e,b,c,r = os.pullEvent("mouse_click")
    if e == "mouse_click" then
	  _G["click.event"] = e
	  row = r
	  col = c
	  button = b
	  direction = 0
	  break
    end
  end
end

function up()
  while true do
    local e,b,c,r = os.pullEvent("mouse_drag")
    if e == "mouse_drag" then
	  _G["event"] = e
	  row = r
	  col = c
	  button = b
	  dicrection = 0
	  break
    end
  end
end

function scroll()
  while true do
    local e,d,c,r = os.pullEvent("mouse_scroll")
    if e == "mouse_scroll" then
	  _G["click.event"] = e
	  direction = d
	  col = c
	  row = r
	  button = 0
	  break
    end
  end
end

Bubba #8
Posted 11 December 2012 - 04:33 PM
the second is closer to what i'm looking for * testing some stuff while typing* but would it work with the apiName before it …

Not sure why you want the api name in front of it, but okay.


--Add to api code
_G["apiname"] = {
mouseUp = {}
mouseDown = {}
mouseScroll = {}
}
etopsirhc #9
Posted 11 December 2012 - 04:40 PM
its an OOP thing XP
example

system.out.println("java print line code");
calls system's out class and it's println function
it just feels right to me *cant explain it*
Bubba #10
Posted 11 December 2012 - 04:41 PM
its an OOP thing XP
example

system.out.println("java print line code");
calls system's out class and it's println function
it just feels right to me *cant explain it*

Ah I see. Java fanatic then :P/>
I'm still learning Java but yeah I can see why you would prefer that over the Lua way. Keeps your code neater I guess
Bubba #11
Posted 11 December 2012 - 04:46 PM
edit: did some changes with the new info , but still having troubles ><
maybe i should re-write this as more class based than api , though somehow still loadable though api ?

Just saw your edit. What troubles are you having exactly? Could you post errors?

Also, no need to continue using _G["varname"] after your first initialization of it. It's in the global table now, meaning that you could do this:

_G["thisvar"] = 5

And from another program:

print(thisvar)

Of course that is assuming that you run the program initializing the variable first.
Bubba #12
Posted 11 December 2012 - 04:51 PM
Oh and let me point you towards this tutorial from the Lua PIL. It explains what I am doing much more in depth.
etopsirhc #13
Posted 11 December 2012 - 04:55 PM
after i got the api name part working i tested somthing and fixed it , at first i didnt think i needed to call it from the gloabl to change it from local file but had to any ways :P/>

my finished code =D TY for all the help

_G["click"] = {
  event = "",
  row = 0,
  col = 0,
  button = 0,
  direction = 0
}

function down()
  while true do
	local e,b,c,r = os.pullEvent("mouse_click")
	if e == "mouse_click" then
	  click.event = e
	  click.row = r
	  click.col = c
	  click.button = b
	  click.direction = 0
	  break
	end
  end
end

function up()
  while true do
	local e,b,c,r = os.pullEvent("mouse_drag")
	if e == "mouse_drag" then
	  click.event = e
	  click.row = r
	  click.col = c
	  click.button = b
	  click.dicrection = 0
	  break
	end
  end
end

function scroll()
  while true do
	local e,d,c,r = os.pullEvent("mouse_scroll")
	if e == "mouse_scroll" then
	  click.event = e
	  click.direction = d
	  click.col = c
	  click.row = r
	  click.button = 0
	  break
	end
  end
end

function listen()
  while true do
	local e,d,c,r = os.pullEvent()
	if e == "mouse_scroll" then
	  click.event = e
	  click.direction = d
	  click.col = c
	  click.row = r
	  click.button = 0
	  break
	elseif e == "mouse_drag" then
	  click.event = e
	  click.row = r
	  click.col = c
	  click.button = b
	  click.dicrection = 0
	  break
	elseif e == "mouse_click" then
	  click.event = e
	  click.row = r
	  click.col = c
	  click.button = b
	  click.direction = 0
	  break
	end
  end
end

use

os.loadAPI("click")
click.down()
print(click.row .. " " .. click.col)
Bubba #14
Posted 11 December 2012 - 04:57 PM
after i got the api name part working i tested somthing and fixed it , at first i didnt think i needed to call it from the gloabl to change it from local file but had to any ways :P/>/>

my finished code =D TY for all the help

_G["click"] = {
  event = "",
  row = 0,
  col = 0,
  button = 0,
  direction = 0
}

function down()
  while true do
	local e,b,c,r = os.pullEvent("mouse_click")
	if e == "mouse_click" then
	  click.event = e
	  click.row = r
	  click.col = c
	  click.button = b
	  click.direction = 0
	  break
	end
  end
end

function up()
  while true do
	local e,b,c,r = os.pullEvent("mouse_drag")
	if e == "mouse_drag" then
	  click.event = e
	  click.row = r
	  click.col = c
	  click.button = b
	  click.dicrection = 0
	  break
	end
  end
end

function scroll()
  while true do
	local e,d,c,r = os.pullEvent("mouse_scroll")
	if e == "mouse_scroll" then
	  click.event = e
	  click.direction = d
	  click.col = c
	  click.row = r
	  click.button = 0
	  break
	end
  end
end

function listen()
  while true do
	local e,d,c,r = os.pullEvent()
	if e == "mouse_scroll" then
	  click.event = e
	  click.direction = d
	  click.col = c
	  click.row = r
	  click.button = 0
	  break
	elseif e == "mouse_drag" then
	  click.event = e
	  click.row = r
	  click.col = c
	  click.button = b
	  click.dicrection = 0
	  break
	elseif e == "mouse_click" then
	  click.event = e
	  click.row = r
	  click.col = c
	  click.button = b
	  click.direction = 0
	  break
	end
  end
end

use

os.loadAPI("click")
click.down()
print(click.row .. " " .. click.col)

Cool :)/> Glad I could be of assistance.