45 posts
Posted 22 February 2013 - 03:57 PM
Hello, I'm using Direwolf20's general button code to get some practice and learn how to use them to control programs. I modified the code slightly:
Spoiler
--Monitor Setup
local mon = peripheral.wrap("top")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)
--Functions
function setTable(name, func, xmin, xmax, ymin, ymax)
button[name] = {}
button[name]["func"] = func
button[name]["active"] = true
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
end
function fill(text, color, bData)
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
if k == xspot then
mon.write(text)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end
function screen()
local currColor
for name,data in pairs(button) do
local on = data["active"]
if on == true then currColor = colors.lime else currColor = colors.red end
fill(name, currColor, data)
end
end
function checkxy(x, y)
for name, data in pairs(button) do
if y>=data["ymin"] and y <= data["ymax"] then
if x>=data["xmin"] and x<= data["xmax"] then
data["func"]()
data["active"] = not data["active"]
print(name)
end
end
end
end
function heading(text)
w, h = mon.getSize()
mon.setCursorPos((w-string.len(text))/2+1, 1)
mon.write(text)
end
function next()
local nextScreen = not nextScreen
end
--Program
local nextScreen = false
setTable("Begin", next, 5, 35, 20, 25)
while nextScreen == false do
mon.clear()
heading("Dummy Test Screen")
screen()
local e,side,x,y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x,y)
sleep(.1)
end
next()
heading("Success!!")
My problem arises because the touches aren't registering. I have my monitor on top (as indicated in the peripheral wrap statement), but I can't figure out why the code only registers touches when the monitor is placed to the right of the computer. What am I missing? Other than not registering touches from the os.pullEvent it seems to be functioning fine.
EDIT: I will probably use this thread for all my touchscreen issues, as I'm sure there will be many. I'll try to update the code and I progress.
Edited by
8543 posts
Posted 23 February 2013 - 06:45 AM
Where do you define the "next" function?
758 posts
Location
Budapest, Hungary
Posted 23 February 2013 - 07:00 AM
Ummm… next in Lua is something like rawset, isn't it? A global variable or like that… I'm not sure that CC uses it but in Notepad++ "next" has the same color as "rawset".
EDIT: I knew that I've seen next somewhere before - this is the pairs function from CC 1.481
function pairs( _t )
local typeT = type( _t )
if typeT ~= "table" then
error( "bad argument #1 to pairs (table expected, got "..typeT..")", 2 )
end
return next, _t, nil
end
2088 posts
Location
South Africa
Posted 23 February 2013 - 07:14 AM
Where do you define the "next" function?
function next()
local nextScreen = not nextScreen
end
he has defined it…
I really can't find any problems with the code o.O
Does it print the name when you click it?
And how does it only work if you put the monitor on the right, really makes no sense!
758 posts
Location
Budapest, Hungary
Posted 23 February 2013 - 07:24 AM
Hmmm… I have a strange idea. Rename "next" to "toggleNextScreen", save, then reboot the computer. Don't ask, just try it.
Nevermind… There is nothing wrong with next. (But you still shouldn't call a function "next"…)
2088 posts
Location
South Africa
Posted 23 February 2013 - 07:29 AM
Hmmm… I have a strange idea. Rename "next" to "toggleNextScreen", save, then reboot the computer. Don't ask, just try it.
Don't think that that is the problem because I made a test code defining a function 'next' and using ipairs afterwards and it worked…
function next(text, yPos)
term.setCursorPos( (51 - #text)/2, yPos)
write(text)
end
next('lol', 3)
for i, v in pairs( {'lol', 'derp', 'xd', 'meow'} ) do
term.setCursorPos( 1, i + 5 )
write(v)
end
45 posts
Posted 23 February 2013 - 08:28 AM
I believe my problem is somewhere in the checkxy() function. It draws the button and centers the text, but then does nothing afterwards. When I click the monitor it doesn't even register in the terminal. I'll try and take some screenshots later if that would help. All I changed as far as orientation was the "top" in the peripheral wrap. By default it was set to "right."
EDIT: If I run the program with the monitor on the right, it will register touch, but not draw the button. However the coordinates are written in the terminal every time I touch the monitor. Same code, all I did was change the peripheral wrapping. The button does not exit the while loop though, like it should. Theotetically, when the button is pressed, it should exit the while loop and only display a screen saying "Success!", correct?
Edited on 23 February 2013 - 01:47 PM
45 posts
Posted 26 February 2013 - 04:13 AM
Still need help with this problem…Would screenshots help diagnose the issue?
758 posts
Location
Budapest, Hungary
Posted 26 February 2013 - 04:46 AM
Tested the code and… It works fine for me… I really don't know what the problem is…
45 posts
Posted 26 February 2013 - 08:19 AM
when you tested, did it exit out of the while loop properly and display the Success screen?
758 posts
Location
Budapest, Hungary
Posted 26 February 2013 - 08:34 AM
Ohhhhh… Missed that line. But in this case, solved! Use
local nextScreen = false
function next()
nextScreen = not nextScreen
end
instead of
function next()
local nextScreen = not nextScreen
end
local nextScreen = false
45 posts
Posted 26 February 2013 - 09:00 AM
Ohhhhh… Missed that line. But in this case, solved! Use
local nextScreen = false
function next()
nextScreen = not nextScreen
end
instead of
function next()
local nextScreen = not nextScreen
end
local nextScreen = false
Ooo! I have to have a variable defined before the function can use it properly, correct!? I feel silly now…lol thank you sir!
758 posts
Location
Budapest, Hungary
Posted 26 February 2013 - 09:06 AM
Hey, no probs! :D/> Keep in mind, that either if you have declared nextScreen before declaring the function or not, if the function changes nextScreen only in its own environment (due to the "local" keyword"), nextScreen outside the function won't ever change.
45 posts
Posted 26 February 2013 - 09:33 AM
Hey, no probs! :D/> Keep in mind, that either if you have declared nextScreen before declaring the function or not, if the function changes nextScreen only in its own environment (due to the "local" keyword"), nextScreen outside the function won't ever change.
Actually that was the issue! I had already changed the position of the variable declaration. But because it was all local, it wasn't exiting the while loop. Works perfectly fine now! Thanks so much Hacker!!