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

Monitor touch not working on todo list program

Started by Sweenu, 28 December 2016 - 08:23 PM
Sweenu #1
Posted 28 December 2016 - 09:23 PM
Hello,

I'm working on a little todo list project but I'm having a problem : it should delete the item when I right-click the red cross and add an item when i click the green + But nothing happens (It's not a problem of where I clicked as I tried to click everywhere on the screen with the same result).

Here's my code :

monitor = assert(peripheral.find("monitor"), "No screen attached")
dimX, dimY = monitor.getSize()
if dimX < 20 then error("The screen is not large enough.") end
if dimY < 6 then error("The screen is not tall enough.") end
if not fs.exists("/todo") then fs.makeDir("/todo") end

monitor.setTextScale(1)
monitor.setCursorPos(1,1)
monitor.setTextColor(colors.blue)
monitor.write("TODO List :")

-- Functions
function drawSections(myList)
	local y = 3
	for i=1, #myList, 1 do
		monitor.setCursorPos(1, y)
		monitor.setTextColor(colors.red)
		monitor.write('x')
		monitor.setCursorPos(3, y)
		monitor.setTextColor(colors.white)
		monitor.write(myList[i])
		y = y + 2
	end
	if #myList < (dimY+1)/2 - 1 then
		monitor.setCursorPos(1, y)
		monitor.setTextColor(colors.green)
		monitor.write('+')
	end
end

function addItem(myList)
	input = read()
	table.insert(myList, input)
end

function rmItem(myList, y)
	if y % 2 ~= 0 then table.remove(myList, (y-1) / 2) end
	updateFile(myList)
end

function initFile(myList)
	local listFile = fs.open("/todo/list", fs.exists("/todo/list") and 'r' or 'w')
	while true do
		local line = listFile.readLine()
		if line == nil then break end
		table.insert(myList, line)
	end
	listFile.close()
end

function updateFile(myList)
	local listFile = fs.open("/todo/list", 'w')
	for item in myList do
		listFile.writeLine("item")
	end
	listFile.close()
end

-- Main
listItem = {}
initFile(listItem)
while true do
	drawSections(listItem)
	local name, touchX, touchY = os.pullEvent("monitor_touch")
	if (touchX == 1 or touchX == 2) and (touchY == #listItem * 2 + 3) then
		addItem(listItem)
	elseif (touchX == 1 or touchX == 2) and touchY < #listItem * 2 + 3 and touchY > 2 then
		rmItem(listItem, touchY)
	end
end

Here's what it looks like : http://imgur.com/4KfdfbZ
Bomb Bloke #2
Posted 29 December 2016 - 11:05 AM
local event, side, touchX, touchY = os.pullEvent("monitor_touch")