[Solved] I'm not entirely sure whats wrong...

Started by theawesomegoldfish, Feb 28, 2020, 11:52 AM

Previous topic - Next topic

theawesomegoldfish

I started with computercraft sometime last week, and I've been trying to make a basic button setup for the main screen. The button I'm working on now is the "shutdown" button and it draws everything properly, but the loop I used on the main screen to dectect if a button is pressed doesnt work on the shutdown menu... Thanks in advance :)

Code for the Main Screen:
os.loadAPI("/disk/FOSAPI")
FOSAPI.drawMainMenu()
local choice1 = " Forge Status "
local choice2 = " Shutdown "
local choice3 = " Reboot "
local x, y = 1, 1
local x1, y1 = 1, 2
local x2, y2 = 1, 3
while true do
  local event, button, cx, cy = os.pullEvent()
  if event == "mouse_click" then
    if cx >= x and cx < choice1:len() and cy == y and button == 1 then
      --Add Forge Check Program
    elseif cx >= x1 and cx < choice2:len() and cy == y1 and button == 1 then
      term.clear()
      shell.run("/disk/shutDown")
    break
    elseif cx >= x2 and cx < choice3:len() and cy == y2 and button == 1 then
      --Add Reboot Prompt
    end
  end
end

Code for Shutdown:
os.loadAPI("/disk/FOSAPI")
local yes = " Yes "
local no = " No "
local x, y = 18,9
local x1, y1 = 27,9
while true do
  FOSAPI.drawShutdown()
  local event, button, cx, cy = os.pullEvent()
  if event == "mouse_click" then
    if cx >= x and cx < x1 and cx < yes:len() and cy == y and button == 1 then
      term.clear()
      term.setCursorPos(15,10)
      write("Shutting Down...")
    elseif cx >= x1 and cx < no:len() and cy == y1 and button == 1 then
      term.clear()
      term.setCursorPos(1,1)
      shell.run("/disk/mainMenu")
    end
  end
end

The API im using:
function drawBackwall() --Draws default backdrop
  backwall = paintutils.loadImage("/disk/backwall")
  paintutils.drawImage(backwall, 1, 1)
end
function drawMainMenu() --Draws main menu screen
  term.clear()
  term.setCursorPos(1,1)
  local x, y = 1,1
  local x1, y1 = 1,2
  local x2, y2 = 1,3
  local choice1 = " Forge Status "
  local choice2 = " Shutdown "
  local choice3 = " Reboot "
  term.setBackgroundColor(colors.black)
  term.setTextColor(colors.white)
  term.setCursorPos(x,y)
  write(choice1)
  term.setCursorPos(x1,y1)
  write(choice2)
  term.setCursorPos(x2,y2)
  write(choice3)
end
function drawShutdown() --Draws shutdown menu
  local choice1 = " Yes "
  local choice2 = " No "
  local x, y = 20,7
  local x1, y1 = 18,9
  local x2, y2 = 27,9
  term.setBackgroundColor(colors.black)
  term.setTextColor(colors.white)
  drawBackwall()
  term.setCursorPos(x,y)
  write("Shutdown?")
  term.setCursorPos(x1,y1)
  write(choice1)
  term.setCursorPos(x2,y2)
  write(choice2)
end

Dog

#1
Part of the issue appears to be your x checking code.  You have:
if cx >= x and cx < choice1:len()
when it should be...
if cx >= x and cx < x + choice1:len()
Notice the addition of the x in the second check - make this change to all your x checking code and it *should* work.

theawesomegoldfish

#2
Thanks!
Fixing the x dectection solved the yes button, the no button still doesn't work but I have a feeling I won't be able to put both buttons on the same y using this method...

Edit: Nevermind, I got the no button to work as well! Thank you!

Dog

You should be able to put as many buttons as will fit on a line - so long as your x and y checks are in bounds of the button you are checking you should have no problems.  Most of your issues are with your x checks.

pjals

Quote from: Dog on Feb 28, 2020, 04:58 PMPart of the issue appears to be your x checking code.  You have:
if cx >= x and cx < choice1:len()
when it should be...
ix cx >= x and cx < x + choice1:len()
Notice the addition of the x in the second check - make this change to all your x checking code and it *should* work.
if*
hi i am T79, bye