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
:
Thanks for your help
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