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

Troubles with GUI reactor managemente program

Started by X3ME, 27 February 2016 - 05:28 PM
X3ME #1
Posted 27 February 2016 - 06:28 PM
Hey, im making a program to run my nuclear reactor,
It has a simple GUI
Recently I tried to incorporate the feature to disable the reactor in case of a rednet message signaling overheating..
I don't get why it doesn't work atm…

--Desktop
slc = 0
tBarC = 8
tBartC = 1
backColor = 1
term.setBackgroundColor(backColor)
term.clear()
function titleBar()
  term.setCursorPos(1,1)
  term.setBackgroundColor(tBarC)
  term.setTextColor(tBartC)
  term.clearLine()
  term.setCursorPos(2, 1)
  print("[Start Menu]")
  term.setCursorPos(15, 1)
  print("Reactor Commands")
end
function drawDesktop()
  term.setBackgroundColor(backColor)
  term.clear()
  bground = paintutils.loadImage(".background")
  paintutils.drawImage(bground,1,1)
  titleBar()
end
function drawMenu1()
term.setTextColor(256)
term.setBackgroundColor(128)
term.setCursorPos(1,2)
print("		  ")
term.setCursorPos(1,3)
print(" Shutdown ")
term.setCursorPos(1,4)
print(" Restart  ")
term.setCursorPos(1,5)
print("		  ")
end
function drawMenu2()
term.setTextColor(256)
term.setBackgroundColor(128)
term.setCursorPos(15,2)
print("			   ")
term.setCursorPos(15,3)
print(" Start Reactor ")
term.setCursorPos(15,4)
print(" Stop Reactor  ")
term.setCursorPos(15,5)
print(" Lock Room	 ")
term.setCursorPos(15,6)
print("			   ")
end
drawDesktop()
while true do
local event, button, X, Y = os.pullEventRaw()
  if slc == 0 then
    if event == "mouse_click" then
	  if X >=2 and X <=15 and Y==1 and button ==1 then
	  drawMenu1()
	  slc = 1
	  elseif X >=15 and X <=30 and Y==1 and button ==1 then
	  drawMenu2()
	  slc = 1
	    else
	    drawDesktop()
	  end
    end
   elseif slc == 1 then
    if event == "mouse_click" then
		  if X >=1 and X <=11 and button == 1 and Y== 3 then slc = 0
	   os.shutdown()
	   elseif X>=1 and X<=11 and Y==4 and button ==1 then slc = 0
	   os.reboot()
	   elseif X>=15 and X<=25 and Y==3 and button ==1 then slc = 0
	   rednet.open("top")
	   rednet.broadcast("Reactor 1 On")
	   rednet.close("top")
	   rs.setOutput("back", true)
	   elseif X>=15 and X<=25 and Y==4 and button ==1 then slc = 0
	   rednet.open("top")
	   rednet.broadcast("Reactor 1 Off")
	   rednet.close("top")
	   rs.setOutput("back", false)
	   elseif X>=15 and X<=20 and Y==5 and button ==1 then slc = 0
	   os.shutdown()
	   else
	   slc = 0
	   drawDesktop()
	 end
  end
  end
local arg1, arg2 = os.pullEvent("rednet.message")
if arg2 == "Reactor 1 Overheat" then
  rs.setOutput("back", false)
  os.reboot()
else
  drawDesktop()
end
end

Thanks! :)/>
Cheers
KingofGamesYami #2
Posted 27 February 2016 - 08:39 PM
1) "rednet_message", not "rednet.message"
2) the first pullEvent gets an event… and removes it from the queue. The second pullEvent (with the filter) promptly pulls all events out of queue until it gets a rednet message. At which point, it repeats.
You can only use os.pullEvent once. If you use it multiple times, you're doing something wrong.
X3ME #3
Posted 27 February 2016 - 10:48 PM
Thanks! Then how do I do what I want to?
HPWebcamAble #4
Posted 27 February 2016 - 11:29 PM
Thanks! Then how do I do what I want to?

In your main loop, you should have one call to os.pullEvent() (usually the first line of the loop)

Below it, you check what event was pulled, and handle it accordingly:

while true do

  local event, p1, p2, p3, p4 = os.pullEvent() --# I use the variable names because you don't know for sure what each variable contains yet.

  if event == "mouse_click" then
    --# The mouse was clicked, do stuff with the parameters (that's our variables p1, p2, ...)
  elseif event == "rednet_message" then
    --# Recieved a rednet message
 end --# You can check other events if you need to, but those you don't handle will simple by discarded, which is fine

end
X3ME #5
Posted 27 February 2016 - 11:58 PM
Ok, so this is what I got now:

--Desktop
slc = 0
tBarC = 8
tBartC = 1
backColor = 1
term.setBackgroundColor(backColor)
term.clear()
--Functions
function titleBar()
  term.setCursorPos(1,1)
  term.setBackgroundColor(tBarC)
  term.setTextColor(tBartC)
  term.clearLine()
  term.setCursorPos(2, 1)
  print("[Start Menu]")
  term.setCursorPos(15, 1)
  print("Reactor Commands")
end
function drawDesktop()
  term.setBackgroundColor(backColor)
  term.clear()
  bground = paintutils.loadImage(".background")
  paintutils.drawImage(bground,1,1)
  titleBar()
end
function drawMenu1()
term.setTextColor(256)
term.setBackgroundColor(128)
term.setCursorPos(1,2)
print("		  ")
term.setCursorPos(1,3)
print(" Shutdown ")
term.setCursorPos(1,4)
print(" Restart  ")
term.setCursorPos(1,5)
print("		  ")
end
function drawMenu2()
term.setTextColor(256)
term.setBackgroundColor(128)
term.setCursorPos(15,2)
print("			   ")
term.setCursorPos(15,3)
print(" Start Reactor ")
term.setCursorPos(15,4)
print(" Stop Reactor  ")
term.setCursorPos(15,5)
print(" Lock Room	 ")
term.setCursorPos(15,6)
print("			   ")
end
drawDesktop()
while true do
local event, arg1, arg2, arg3 = os.pullEvent()
  if event == "mouse_click" then
  if slc == 0 then
    if event == "mouse_click" then
	  if arg2 >=2 and arg2 <=15 and arg3==1 and arg1 ==1 then
	  drawMenu1()
	  slc = 1
	  elseif arg2 >=15 and arg2 <=30 and arg3==1 and arg1 ==1 then
	  drawMenu2()
	  slc = 1
	    else
	    drawDesktop()
	  end
    end
   elseif slc == 1 then
    if event == "mouse_click" then
		  if arg2 >=1 and arg2 <=11 and arg1 == 1 and arg3== 3 then slc = 0
	   os.shutdown()
	   elseif arg2>=1 and arg2<=11 and arg3==4 and arg1 ==1 then slc = 0
	   os.reboot()
	   elseif arg2>=15 and arg2<=25 and arg3==3 and arg1 ==1 then slc = 0
	   rednet.open("top")
	   rednet.broadcast("Reactor 1 On")
	   rednet.close("top")
	   rs.setOutput("back", true)
	   elseif arg2>=15 and arg2<=25 and arg3==4 and arg1 ==1 then slc = 0
	   rednet.open("top")
	   rednet.broadcast("Reactor 1 Off")
	   rednet.close("top")
	   rs.setOutput("back", false)
	   elseif arg2>=15 and arg2<=20 and arg3==5 and arg1 ==1 then slc = 0
	   os.shutdown()
	   else
	   slc = 0
	   drawDesktop()
	 end
  end
  end
  elseif event == "rednet_message" and arg2 == "Reactor 1 Overheat" then
  rs.setOutput("back", false)
  rednet.open("top")
  rednet.broadcast("Reactor 1 Off")
  rednet.close("top")
 
  else
  drawDesktop()
end
end

Only one problem, the menu's disapeer as soon as I click them..
HPWebcamAble #6
Posted 28 February 2016 - 01:09 AM
Ok, a few things:
  1. I'm not sure if it's the forum software, but your indenting is strange, I've fixed it for you and uploaded it to pastebin here: http://pastebin.com/AjmGJPsN (Note I didn't change how it works, just the spacing and such)
  2. You CAN use the numerical color values, but using the Colors API is much easier
  3. Your 'event if-statement' (we'll call it that) has three options: mouse_click, rednet_message, and an else for all other events. But within your mouse_click statement, you check again that the event was a mouse click. You don't need to.
  4. I'd recommend using a button API, so that you don't have to check 4 values to see what button was clicked. You could use my Simple Screen Maker to create it with an interface, or Lyqyd's Touchpoint API (there are others too)
Finally, I didn't check to see what causes the menu to disappear. Take another look, perhaps change a few of the things I mentioned, and you might find it.
X3ME #7
Posted 28 February 2016 - 09:30 AM
Hey dude! :)/>
So
1. Yeah it was the forum that messed up my indenting, oh well thanks anyways!
2. Ok, will do!
3. Yeah let me fix that
4. Im probably going to use your screen api and ssm interface then, I've tried touchpoint api before and never got around to actually using it
X3ME #8
Posted 28 February 2016 - 03:43 PM
Neither does your screen api does touchpoint work for me… Being to think api's hate me.. can't figure out why it does work. pm me
Lyqyd #9
Posted 28 February 2016 - 04:44 PM
Touchpoint should work fine as long as you're on a version of ComputerCraft with advanced monitors (or 1.65ish and higher if you want to put buttons on the terminal). If you post the issues you're having with it, we can get that working for you as well.
X3ME #10
Posted 28 February 2016 - 09:28 PM
Touchpoint should work fine as long as you're on a version of ComputerCraft with advanced monitors (or 1.65ish and higher if you want to put buttons on the terminal). If you post the issues you're having with it, we can get that working for you as well.
Making the thread now, btw i use cc 1.74
X3ME #11
Posted 28 February 2016 - 09:38 PM
Nevermind then!
All it took was a system reboot, its working now! :)/>

It was the event handling function in all api's that was giving me errors, a reboot fixed it ;)/>