Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --API for regions on advanced monitors
- --All local func are not meant to be usable from outside the file
- --Returns a table containing a point's coordinates for use in addReg() (Makes it more readable)
- function p(x, y)
- return {["x"]=x, ["y"]=y}
- end--p()
- --Get the region's table from file
- local function getTable()
- local t = {}
- if fs.exists("data/regions") then
- local f = fs.open("data/regions", 'r')
- t = textutils.unserialize(f.readLine())
- f.close()
- end
- return t
- end--getTable()
- --Writes the table in file
- local function writeTable(t)
- fs.makeDir("data")
- local f = fs.open("data/regions", 'w')
- f.writeLine(textutils.serialize(t))
- f.close()
- end--writeTable
- --Adds regions between A(x, y) and B(x2, y2)
- function addReg(n, A, B)
- local t = getTable()
- --Adds a table inside the table
- table.insert(t, {
- ["name"]=n,
- ["xS"]=A.x,
- ["yS"]=A.y,
- ["xE"]=B.x,
- ["yE"]=B.y})
- writeTable(t)
- end --addReg()
- --Deletes reg from list
- function delReg(n)
- local t = getTable()
- --Read table
- for k,v in ipairs(t) do
- --Find match
- if v.name == n then
- table.remove(t, k) --Delete the table from the list
- end
- end
- writeTable(t)
- end --delReg()
- --Clears the table
- function clear()
- writeTable({})
- end--clear()
- --Checks if a regions was clicked, must run in parralel with the other script
- function run()
- local t, event, side, x, y
- --Main loop
- while true do
- t = getTable()
- event, side, x, y = os.pullEventRaw("monitor_touch") --Get screen event. We use Raw just in case the main program wants to use it to prevent the termination event
- --Reads the table
- for k,v in ipairs(t) do
- --Search if we clicked in the region we're checking
- if x >= v.xS and x <= v.xE and y >= v.yS and y <= v.yE then --We're in it
- --We queue an event that will be read by the main script with the region's name
- os.queueEvent("regions", v.name)
- end--If
- end--For
- end--while
- end--run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement