Posted 05 April 2013 - 03:03 PM
I'm trying to make a button API that I can use for my menus and games. It worked the first time I used it but I've changed something that makes the button be drawn quite oddly. Instead of making the rectangle button it makes a rectangle with the last line of it being colored all the way to the edge of the terminal. I can't seem to figure out what I've changed or done to the code to make it behave like this. I believe that it only has to do with the drawing part of the createButton() function but I'm going to post the entirety of the code just in case. Some of the code may not make sense as much of it is not finished and there are many unused variables. Sorry about that!
If someone could locate the problem that would be great!
Help is appreciated!
Thanks!
m = term
--m = peripheral.wrap("top")
if m == term then
detect = "click"
else
detect = "touch"
end
local btns = {
["btn1"] ={
["xMin"] = 1,
["xMax"] = 10,
["yMin"] = 1,
["yMax"] = 5,
["text"] = "Start!",
["active"] = false,
},
-- ["btn2"] = {
-- ["xMin"] = 11,
-- ["xMax"] = 10,
-- ["yMin"] = 1,
-- ["yMax"] = 5,
-- ["text"] = "Quit",
-- ["active"] = false,
-- }
}
m.clear()
m.setCursorPos(1,7)
m.setBackgroundColor(colors.green)
function findCenter()
xPos = math.floor((btns["btn1"]["xMax"]- btns["btn1"]["xMin"] - string.len(btns["btn1"]["text"])) /2)+1
yPos = math.floor((btns["btn1"]["yMin"] + btns["btn1"]["yMax"])/2)
end
function createBtn(btn)
xMin = btns["btn1"]["xMin"]
xMax = btns["btn1"]["xMax"]
yMin = btns["btn1"]["yMin"]
yMax = btns["btn1"]["yMax"]
if btns[btn]["active"] == false then
color = colors.red
else
color = colors.lime
end
m.setBackgroundColor(color)
for i = yMin, yMax do
m.setCursorPos(xMin, i)
if i == yPos then
for k = 0,xMax-xMin-string.len(text)+1 do
if k == xPos then
m.write(text)
else
m.write(" ")
end
end
else
for o = xMin, xMax do
m.write(" ")
end
end
end
m.setBackgroundColor(colors.black)
end
function click()
if detect == "click" then
event, button, x, y = os.pullEvent("mouse_click")
elseif detect == "touch" then
event, button, x, y = os.pullEvent("monitor_touch")
end
if x >= btns["btn1"]["xMin"] and x <= btns["btn1"]["xMax"] and y >= btns["btn1"]["yMin"] and y <= btns["btn1"]["yMax"] then
clicked = Start
--elseif x >=
end
end
findCenter("btn1")
createBtn("btn1")
term.setCursorPos(1,10)
If someone could locate the problem that would be great!
Help is appreciated!
Thanks!