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

[SOLVED]Menu Bar not working, Please help.

Started by acoollegodude, 12 November 2012 - 01:15 PM
acoollegodude #1
Posted 12 November 2012 - 02:15 PM
I am making an OS that may or may not be very simple depending on how long I work on this project. Anyways, I've been coding this pretty much all day and this is the first bug I've run into. I would appreciate it if you would help.

Code:

function openmenu() -- Opens a menu on top of the start button
local selected = "option1"
local options = {"Shutdown", "Reboot"}
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(1, 16)
print(options[1])
print(options[2])
term.write("WIP")
end
function drawTaskbar() -- makes a small taskbar at the bottom of the screen with a start button just like Windows
term.clear()
paintutils.drawLine(51, 19, 5, 19, colors.blue)
term.setTextColor(colors.black)
term.setBackgroundColor(colors.lime)
term.setCursorPos(1, 19)
term.write("Start")
end
term.setBackgroundColor(colors.white) -- makes the desktop white
shell.run(clear)
drawTaskbar() -- adds task bar to the bottom of the screen
term.setBackgroundColor(colors.white)
local button, xPos, yPos = os.pullEvent("mouse_click") -- detects when you click on start button and opens the menu
if xPos == 1 and yPos == 19, 23 then
openmenu()

Again, Please help.
Edited by
Kingdaro #2
Posted 12 November 2012 - 03:52 PM
I can see what you're trying to do here, but there are too many errors in your general script logic to just, well, fix.

For future reference, you should put your code around
tags so that it's easier to read, or just use pastebin. Plus, you should actually tell us what the error is, if it gives you one.

Here's a commented rewrite: http://pastebin.com/0w6GyeDi
Uncommented version: http://pastebin.com/cYN4LJcx
acoollegodude #3
Posted 12 November 2012 - 04:16 PM
Thanks a lot, I'm fairly new to lua but I just wanted to try making an OS for fun. I didn't think it had that many errors though. Oh and just so you know, it's not finished yet, obviously.
mrSLIMEguy #4
Posted 12 November 2012 - 07:30 PM
Is one opening the menu even when you right click?
To fix it:


local button, xPos, yPos = os.pullEvent("mouse_click") -- detects when you click on start button and opens the menu
if xPos == 1 and yPos == 19, 23 then
openmenu()
--It would detect left clicks if you do:
local button, xPos, yPos = os.pullEvent("mouse_click") -- detects when you click on start button and opens the menu
if xPos == 1 and yPos == 19, 23 and button == "1" then
openmenu()
end --FORGOT THE END!

Also you need to use a

[i]while true do[/i]
[i] blah blah blah[/i]
[i]end[/i]
To make sure you can click it, and then click it again.
Shnupbups #5
Posted 12 November 2012 - 11:13 PM
I took kingdaro's code and made it actually work when you click reboot or shutdown. Also when you click anywhere except the bottom bar it closes the menu.

Teh Code

(or pastebin get Np1QxqSP startmenu)
acoollegodude #6
Posted 13 November 2012 - 02:30 AM
I've worked on the os some more and added a programs tab on the menu but I can't figure out how to find all the programs in the programs folder so that when you click on the tab it opens up a side menu with every program on the computer in it.
Kingdaro #7
Posted 13 November 2012 - 10:47 AM
Listing every file on the system would require some recursiveness.

For a program browser I'm developing, I made a nice little recursive filelist giver thing. Gonna go ahead and drop it your way.

local filelist = {}

function genFiles(cdir)
  cdir = cdir or ''
  for _, path in pairs(fs.list(cdir)) do
    if fs.isDir(cdir .. '/' .. path) then
      genFiles(cdir .. '/' .. path)
    else
      table.insert(filelist, cdir .. '/' .. path)
    end
  end
end

genFiles("")

This should generate a nice filelist in the following fashion:

/somebaseprogram
/rom/programs/someprogram
/rom/programs/someotherprogram
if you were to print them all. There are probably too many to display on the screen at once, though, so figuring that out is up to you.
acoollegodude #8
Posted 13 November 2012 - 10:48 AM
Thanks kingdaro
acoollegodude #9
Posted 13 November 2012 - 03:42 PM
Hey guys I've run into a problem again. I was trying to load an image of a pop-up window but then it came up with this error: paintutils:94: attempt to get the length of nil. I don't know why I'm getting this error as I put in the right arguments and I don't even have 94 lines of code yet(getting close though!). So here is the code

function searchwindow()
  paintutils.loadImage('window')
  paintutils.drawImage('window', 25, 8)
end
local programs = fs.list('rom/programs')
local options = {'Shutdown', 'Reboot','Programs'}
local menuopen = false -- variable for checking if the menu is open, to see if we have to draw it
local programsopen = false
while true do
-- we're going to draw everything before taking mouse input
-- clear the screen and prepare for the new screen to be drawn, with the color black
term.setBackgroundColor(colors.lightBlue)
term.clear()
if search == true then
searchwindow()
end
-- get the width and height of the screen - is more helpful than memorizing numbers
-- it also makes it adaptable to other monitor sizes.
local w,h = term.getSize()
-- draw a line on the bottom of the taskbar from right to left
paintutils.drawLine(1, h, w, h, colors.blue)
-- draw the start button with black text and a lime (light green) background
term.setTextColor(colors.black)
term.setBackgroundColor(colors.lime)
term.setCursorPos(1, h)
write('Start')
-- draw the start menu, but ONLY if menuopen is true.
if menuopen then
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  -- a for loop basically says:
  -- repeat the following, starting at 1 and ending at the number of options (2 in this case)
  for i=1, #options do
   -- set our cursor to the left, and a y position based on i
   -- so the first time, it'll be h (51) - 1, or 50
   -- then h - 2, 49
   term.setCursorPos(1, h - i)
   -- print the ith of options
   -- so on the first loop, i will be 1, and this will say print(options[1])
   -- and then options[2]
   print(options[i])
  end
  if search == true then
  searchwindow()
end
end

-- here's where we take mouse input.
-- use os.pullEvent to get the event pulled, the mouse button pressed, then the mouse x and y
local event, button, mousex, mousey = os.pullEvent('mouse_click')
if menuopen then
if button == 1 and mousex < 9 and mousey == 18 then
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.setCursorPos(1, 18)
term.write(options[1])
sleep(0.5)
os.shutdown()
elseif button == 1 and mousex < 7 and mousey == 17 then
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.setCursorPos(1, 17)
term.write(options[2])
sleep(0.5)
os.reboot()
elseif button == 1 and mousex < 9 and mousey == 16 then
search = true
-- check if we're within bounds of the start button
elseif button == 1 and mousey < 17 or button == 1 and mousey == 17 and mousex > 6 or button == 1 and mousey == 18 and mousex > 8 then
  -- set menuopen to the oppsite of menuopen
  -- so if menuopen is true, menuopen is now false.
  menuopen = not menuopen
end
elseif not menuopen then
if button == 1 and mousey == 19 and mousex < 6 then
menuopen = true
if search == true then
search = true
end
end
end

end
I don't know what the &gt and &lt mean but they don't show up in the game, only when I copied them onto the fourms.
Edited on 13 November 2012 - 02:46 PM