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

Need help with my OS

Started by PrinzJuliano, 02 February 2013 - 04:30 AM
PrinzJuliano #1
Posted 02 February 2013 - 05:30 AM
Well hi guys,
thanks for your help!

I got a confusing problem,
I made an pogram which shows the current Minecraft time and it has a button.
When u click on the button an menu is drawn and each item on this menu should be click-able to run a function and when u click somewhere else, the menu should disapear.

My problem is now, if i click on any item the menu disapears.

Here is the code
:

w,h = term.getSize()
running = true
timeR = nil
refreshRate = 0.2
isDrawed = false
done = false
items = {
{
   name="Exit",
   func = function()
   running = false
   shell.run("clear")
  end,
  ypos = 0,
  width = 0},
{
   name="Refresh",
   func = function()
   end,
   ypos = 0,
   width = 0
}
}
function drawTask()
term.setCursorPos(1,h-1)
term.setBackgroundColor(colors.blue)
term.write(string.rep(" ",w))

term.setCursorPos(1,h)
term.write(string.rep(" ",w))

time = textutils.formatTime(os.time(),true)
term.setCursorPos(w-string.len(time),h)
term.write(time)

term.setCursorPos(1,h-1)
term.write("+-+")
term.setCursorPos(1,h)
term.write("+-+")
term.setBackgroundColor(colors.black)
end
function onDrawMenu()
term.setCursorPos(1,h-2-table.getn(items))
term.setBackgroundColor(colors.gray)
term.setTextColor(colors.black)
large = 0
for i=1,table.getn(items) do
  if string.len(items[i].name) > large then
   large = string.len(items[i].name)
  end
end
for i=1,table.getn(items) do
  items[i].width = large
end
for i=1,table.getn(items) do
term.setCursorPos(1,h-1-i)
term.write(string.rep(" ",large))
end
for i=1,table.getn(items) do
  term.setCursorPos(1,h-1-i)
  term.write(items[i].name)
  items[i].xpos = h-1-i
end
isDrawed = true
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
end
function updateMouse(button,xpos,ypos)
if button == 1 then
  if xpos == 1 and isDrawed == false or xpos == 2 and isDrawed == false or xpos == 3 and isDrawed == false then
   if ypos == h-1 or ypos == h then
	onDrawMenu()
   end
  elseif isDrawed then
   done = false
   for j=1,table.getn(items) do
	if xpos <= items[j].width and ypos == items[j].ypos then
	 term.clear()
	 items[i].func()
	 isDrawed = false
	 done = true
	 break;
	end
   end
   if done == false then
	isDrawed = false
	drawAll()
   end
  end
end
end
function drawAll()
shell.run("clear")
drawTask()
if isDrawed == true then
  onDrawMenu()
end
term.setCursorPos(1,1)
end
function updateAll()
e,p1,p2,p3 = os.pullEvent()
if e == "timer" and p1 == timeR then
  drawTask()
  timeR = os.startTimer(refreshRate)
elseif e == "mouse_click" then
  updateMouse(p1,p2,p3)
end
end
timeR = os.startTimer(refreshRate)
while running do

drawAll()
updateAll()
end


Thanks for your help
remiX #2
Posted 02 February 2013 - 06:10 AM
Well…
if xpos <= items[j].width and ypos == items[j].ypos then
It will never be true because width and ypos (from table) are both 0, which is impossible to click on

edit:
I see you do attempt to change 'xpos' value of the items in the table but not 'width' value. Also, you're changing to the Y value, the X value is actually 1.

This works (Read my comments):

Spoiler

w,h = term.getSize()
running = true
timeR = nil
refreshRate = 0.2
isDrawed = false
done = false
items = {
	{
		name="Exit",
		func = function()
		running = false
		shell.run("clear")
		end,
		ypos = 0,
		xpos = 0 -- Changed this
	},
	{
		name="Refresh",
		func = function()
		end,
		ypos = 0,
		xpos = 0 -- Changed this
	}
}

function drawTask()
	term.setCursorPos(1,h-1)
	term.setBackgroundColor(colors.blue)
	term.write(string.rep(" ",w))

	term.setCursorPos(1,h)
	term.write(string.rep(" ",w))

	time = textutils.formatTime(os.time(),true)
	term.setCursorPos(w-string.len(time),h)
	term.write(time)

	term.setCursorPos(1,h-1)
	term.write("+-+")
	term.setCursorPos(1,h)
	term.write("+-+")
	term.setBackgroundColor(colors.black)
end

function onDrawMenu()
	term.setCursorPos(1,h-2-table.getn(items))
	term.setBackgroundColor(colors.gray)
	term.setTextColor(colors.black)
	large = 0
	for i=1,table.getn(items) do
		if string.len(items[i].name) > large then
			large = string.len(items[i].name)
		end
	end
	--[[
		It changes the xpos and ypos for where it actually prints, although
		it changes each time it prints which isn't good practice.
		Make the default value of xpos and ypos in the table to where it must print
		and then use term.setCursorPos(items[i].xpos, items[i].ypos) to set the 
		position it must print
	]]--
	for i=1,table.getn(items) do
		term.setCursorPos(1,h-1-i)
		term.write(string.rep(" ",large))
		term.setCursorPos(1,h-1-i)
		term.write(items[i].name)
		items[i].xpos = 1
		items[i].ypos = h-1-i
	end
	isDrawed = true
	term.setBackgroundColor(colors.black)
	term.setTextColor(colors.white)
end

function updateMouse(button,xpos,ypos)
	if button == 1 then
		if xpos == 1 and isDrawed == false or xpos == 2 and isDrawed == false or xpos == 3 and isDrawed == false then
			if ypos == h-1 or ypos == h then
				onDrawMenu()
			end
		elseif isDrawed then
			done = false
			for j=1, #items do
				if xpos >= items[j].xpos and xpos <= #items[j].name and ypos == items[j].ypos then -- this is the way to do it :D/>
					term.clear()
					items[j].func()
					isDrawed = false
					done = true
					break
				end
			end
			if done == false then
				isDrawed = false
				drawAll()
			end
		end
	end
end

function drawAll()
	shell.run("clear")
	drawTask()
	if isDrawed == true then
	  onDrawMenu()
	end
	term.setCursorPos(1,1)
end

function updateAll()
	e,p1,p2,p3 = os.pullEvent()
	if e == "timer" and p1 == timeR then
		drawTask()
		timeR = os.startTimer(refreshRate)
	elseif e == "mouse_click" then
		updateMouse(p1,p2,p3)
	end
end

timeR = os.startTimer(refreshRate)

while running do
	drawAll()
	updateAll()
end
PrinzJuliano #3
Posted 02 February 2013 - 06:46 AM
thanks