Posted 30 April 2013 - 06:26 PM
Yo.
I got bored and decided to start making a database of my turtles that I could use to know, at least generally, what they were doing(quarry,branch mine, etc..).
The problem is that whenever I right click it draws the second page(menu) when it isn't supposed to. It's also calling other entries when they aren't drawn yet.
I can see why it's calling them, as they have the same button coordinates, but is there a way to stop that?
So the problems in order:
1. Right clicking on the name(label) is supposed to, as of right now, just highlight the name. Except it tries to draw the second menu.
2. Right clicking on the 'Next' and 'Prev' buttons are supposed to draw the next or previous menus accordingly. They don't.
3. More of a question, is there a way to draw and call only the table items that are currently on screen?
I tried using and IF statement to try and determine which to draw but it didn't work and was having the same problem I am having now.
I realize I could just seperate the seperate pages into seperate tables but I'm going to be adding more turtles remotely over time.
It would defeat the purpose of having an on-screen database if I had to continually add tables since each 'page' only has 4 entries.
Pastebin: http://pastebin.com/4xPSYBJr
Code:
Some pictures for your viewing pleasure.
Right click on any valid button and it does this.
http://imgur.com/FLnQS34
Calling table entries that are not drawn.
http://imgur.com/mzNTLXV
As always, thank you and any/all critique is welcome.
I got bored and decided to start making a database of my turtles that I could use to know, at least generally, what they were doing(quarry,branch mine, etc..).
The problem is that whenever I right click it draws the second page(menu) when it isn't supposed to. It's also calling other entries when they aren't drawn yet.
I can see why it's calling them, as they have the same button coordinates, but is there a way to stop that?
So the problems in order:
1. Right clicking on the name(label) is supposed to, as of right now, just highlight the name. Except it tries to draw the second menu.
2. Right clicking on the 'Next' and 'Prev' buttons are supposed to draw the next or previous menus accordingly. They don't.
3. More of a question, is there a way to draw and call only the table items that are currently on screen?
I tried using and IF statement to try and determine which to draw but it didn't work and was having the same problem I am having now.
I realize I could just seperate the seperate pages into seperate tables but I'm going to be adding more turtles remotely over time.
It would defeat the purpose of having an on-screen database if I had to continually add tables since each 'page' only has 4 entries.
Pastebin: http://pastebin.com/4xPSYBJr
Code:
Spoiler
m = peripheral.wrap("top")
x,y = m.getSize()
rednet.open("bottom")
--Table to hold turtle information
-- Label = id, xmin, xmax, ymin, ymax, page, active
local turtle = {
["Quarry 1"] = { id = 19, xmin = 2, xmax = 11, ymin = 4, ymax = 5, page = 1, active = false },
["Quarry 2"] = { id = 18, xmin = 2, xmax = 11, ymin = 6, ymax = 7, page = 1, active = false },
["Branch 1"] = { id = 22, xmin = 2, xmax = 11, ymin = 8, ymax = 9, page = 1, active = false },
["Branch 2"] = { id = 20, xmin = 2, xmax = 11, ymin = 10, ymax = 11, page = 1, active = false },
["Branch 3"] = { id = 29, xmin = 2, xmax = 11, ymin = 4, ymax = 5, page = 2, active = false },
["Branch 4"] = { id = 30, xmin = 2, xmax = 11, ymin = 6, ymax = 7, page = 2, active = false },
["Branch 5"] = { id = 31, xmin = 2, xmax = 11, ymin = 8, ymax = 9, page = 2, active = false },
["Branch 6"] = { id = 32, xmin = 2, xmax = 11, ymin = 10, ymax = 11, page = 2, active = false },
}
--Table to hold GUI. (Buttons)
local menu = {
-- Name, xmin, xmax, ymin, ymax, active
["Prev"] = { xmin = 1, xmax = 5, ymin = 12, ymax = 13, active = false },
["Next"] = { xmin = 9, xmax = 13, ymin = 12, ymax = 13, active = false },
}
--Function to add commas to thousandth place.
-- Not used yet. Utilized when turtles send updates.
function comma(num)
local a,b,c = string.match(num, '^([^%d]*%d)(%d*)(.?%d*)$')
return a..(b:reverse():gsub('%d%d%d','%1,'):reverse())..c
end
-- Function to write to specified points
-- using arguments.
function writeTo(x,y,bColor,tColor,text)
local bColor = bColor or colors.black
local tColor = tColor or colors.white
m.setCursorPos(x,y)
m.setBackgroundColor(bColor)
m.setTextColor(tColor)
m.write(text)
m.setTextColor(colors.white)
m.setBackgroundColor(colors.black)
end
-- Centers text on y line.
function centerPrint(text, tColor, bColor, y)
local mx,my = m.getSize()
local xPos = math.floor((mx/2) - (#text/2))
local tColor = tColor or colors.white
local bColor = bColor or colors.black
m.setTextColor(tColor)
m.setBackgroundColor(bColor)
m.setCursorPos(xPos,y)
m.write(text)
m.setBackgroundColor(colors.black)
m.setTextColor(colors.white)
m.setCursorPos(1,y+1)
end
-- Determines if rednet is enabled or not.
function rEnabled()
local rColor
if rednet.isOpen("bottom") then
rColor = colors.lime
else
rColor = colors.red
rednet.open("bottom")
end
writeTo(24,12,rColor,colors.white,"Rednet")
end
-- Draws main menu.
function drawMain()
m.clear()
local mx,my = m.getSize()
centerPrint("Random List", colors.lime, colors.black, 1)
centerPrint(string.rep("=",x+2), colors.lime, colors.black,2)
writeTo(13,4,colors.black,colors.lime,"+")
m.write(string.rep("-",15))
writeTo(x,4,colors.black,colors.lime,"+")
m.setCursorPos(13,5)
for i = 5,10 do
m.write("|"..string.rep(" ",15).."|")
m.setCursorPos(13,i)
end
writeTo(13,10,colors.black,colors.lime,"+")
m.write(string.rep("-",15))
writeTo(x,10,colors.black,colors.lime,"+")
writeTo(14,5,colors.black,colors.lime,"Fuel:")
writeTo(14,6,colors.black,colors.gray, string.rep("-",15))
writeTo(14,7,colors.black,colors.lime,"Status:")
writeTo(14,8,colors.black,colors.gray, string.rep("-",15))
writeTo(14,9,colors.black,colors.lime,"ID:")
rEnabled()
for k,v in pairs(menu) do
writeTo(v.xmin, v.ymin, colors.black, colors.lime,k)
end
end
-- Draws the buttons for turtles and the buttons
function pageOne()
m.clear()
drawMain()
for k,v in pairs(turtle) do
if v.page == 1 then
writeTo(v.xmin,v.ymin,colors.black,colors.purple,k)
end
end
end
--Draws page 2 of turtle info
function pageTwo()
m.clear()
drawMain()
for k,v in pairs(turtle) do
if v.page == 2 then
writeTo(v.xmin, v.ymin, colors.black, colors.purple,k)
end
end
end
--Check for valid click
function checkTouch()
local e,side,x,y = os.pullEvent("monitor_touch")
for k,v in pairs(turtle) do
if y<=v.ymax and y>=v.ymin and x<=v.xmax and x>=v.xmin then
for i = 1,2 do
v.active = not v.active
toggleButton()
sleep(0.2)
end
print(k)
print(x..":"..y)
end
end
--determines where you clicked for next/prev menu
for k,v in pairs(menu) do
if y<=v.ymax and y>=v.ymin and x<=v.xmax and x>=v.xmin then
for i = 1,2 do
v.active = not v.active
toggleButton()
sleep(0.2)
end
print(k)
print(x..":"..y)
if k == "Next" then
pageTwo()
print(k)
elseif k == "Prev" then
pageOne()
print(k)
end
end
end
end
--Toggles button colors
function toggleButton()
local bColor
local tColor
for k,v in pairs(turtle) do
local on = v.active
if on then
bColor = colors.purple
tColor = colors.lime
else
bColor = colors.black
tColor = colors.purple
end
writeTo(v.xmin,v.ymin,bColor,tColor,k)
end
for k,v in pairs(menu) do
local on = v.active
if on then
bColor = colors.lime
tColor = colors.purple
else
bColor = colors.black
tColor = colors.lime
end
writeTo(v.xmin,v.ymin,bColor,tColor,k)
end
end
while true do
drawMain()
pageOne()
checkTouch()
end
Some pictures for your viewing pleasure.
Right click on any valid button and it does this.
http://imgur.com/FLnQS34
Calling table entries that are not drawn.
http://imgur.com/mzNTLXV
As always, thank you and any/all critique is welcome.