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

I think I broke my GUI

Started by Cranium, 16 February 2013 - 08:46 AM
Cranium #1
Posted 16 February 2013 - 09:46 AM
Ok, so I'm working on a door program to open doors when I click on a screen. I have made something like this before, and it worked okay, but that was with a lot more tables and code, since it was more complicated. The problem I am having with my code is that the color states of the diagram on the left, as well as the alarm on the roght do not change when I click them. I know that the states are changing because the status is updated on the right, but it won't update the diagram on the left.
can someone tell my why it's broken?

local shutterState = false
local entranceState = false
local processingState = false
local alarmState = false
local x,y = term.getSize()
local statusLights = {
 shutter = {
  light = {"[--------------]"},
  color = shutterState and colors.black or colors.white,
  BGcolor = shutterState and colors.red or colors.lime,
  action = function()
	if shutterState then
	 shutterState = false
	 rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.white)
	else
	 shutterState = true
	 rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") + colors.white)
	end
   end,
  x1 = 5,
  x2 = 20,
  y1 = 2,
  y2 = 2},
 entrance = {
  light = {"[url="file://\\====/"]\\====/[/url]"},
  color = entranceState and colors.black or colors.white,
  BGcolor = entranceState and colors.red or colors.lime,
  action = function()
	if entranceState then
	 entranceState = false
	 rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.white)
	else
	 entranceState = true
	 rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") + colors.white)
	end
   end,
  x1 = 10,
  x2 = 15,
  y1 = 3,
  y2 = 3},
 processing = {
  light = {"|====|"},
  color = processingState and colors.black or colors.white,
  BGcolor = processingState and colors.red or colors.lime,
  action = function()
	if processingState then
	 processingState = false
	 rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") - colors.white)
	else
	 processingState = true
	 rs.setBundledOutput("bottom", rs.getBundledOutput("bottom") + colors.white)
	end
   end,
  x1 = 10,
  x2 = 15,
  y1 = 18,
  y2 = 18},
 alarm = {
  light = {"+--------+",
   "|	    |",
   "| ALARM! |",
   "|	    |",
   "+--------+"},
  color = alarmState and colors.black or colors.white,
  BGcolor = alarmState and colors.red or colors.lime,
  action = function()
	if rs.getBundledInput("bottom", colors.red) then
	 alarmState = false
	 rs.setBundledOutput("bottom", rs.getBundledInput("bottom") - colors.red)
	else
	 alarmState = true
	 rs.setBundledOutput("bottom", rs.getBundledInput("bottom") + colors.red)
	end
   end,
  x1 = 35,
  x2 = 43,
  y1 = 14,
  y2 = 18}
 }
local function drawBG()
 term.clear()
 for i = 1, 19 do
  paintutils.drawLine(1, i, 24, i, colors.white)
  paintutils.drawLine(28, i, 51, i, colors.white)
  term.setBackgroundColor(colors.red)
  term.setCursorPos(25, i)
  write("[")
  term.setBackgroundColor(colors.blue)
  term.setCursorPos(26, i)
  write("X")
  term.setBackgroundColor(colors.red)
  term.setCursorPos(27, i)
  write("]")
 end
 for i = 2, 18 do
  paintutils.drawLine(2, i, 23, i, colors.lightGray)
  term.setBackgroundColor(colors.gray)
  term.setCursorPos(23, i)
  write("|")
  term.setCursorPos(2, i)
  write(i == 2 and ".--------------------." or
   i == 18 and "'--------------------'" or
   "|")
 end
end
local function drawGUI()
 for _,v in pairs(statusLights) do
  for i = 1, #v.light do
   term.setBackgroundColor(v.BGcolor)
   term.setTextColor(v.color)
   term.setCursorPos(v.x1, v.y1 + (i - 1))
   write(v.light[i])
  end
 end
 term.setBackgroundColor(colors.white)
 term.setTextColor(colors.black)
 term.setCursorPos(29, 3)
 write("Shutter: ")
 term.setTextColor(shutterState and colors.red or colors.lime)
 write(shutterState and "OPEN  " or "CLOSED")
 term.setTextColor(colors.black)
 term.setCursorPos(29, 4)
 write("Entrance: ")
 term.setTextColor(entranceState and colors.red or colors.lime)
 write(entranceState and "OPEN  " or "CLOSED")
 term.setTextColor(colors.black)
 term.setCursorPos(29, 5)
 write("Processing: ")
 term.setTextColor(processingState and colors.red or colors.lime)
 write(processingState and "OPEN  " or "CLOSED")
end
if rs.getBundledInput("bottom", colors.red) then
 alarmState = true
end
drawBG()
drawGUI()
while true do
 local events = {os.pullEvent()}
 if events[1] == "mouse_click" and events[2] == 1 then
  for _,v in pairs(statusLights) do
   if events[3] >= v.x1 and events[3] <= v.x2 and events[4] >= v.y1 and events[4] <= v.y2 then
	v.action()
	drawGUI()
   end
  end
 elseif events[1] == "redstone" then
  if rs.getBundledInput("bottom", colors.red) then
   alarmState = true
  end
 end
end
Lyqyd #2
Posted 16 February 2013 - 09:53 AM
Looks like you need to update BGColor in your action() function.
Cranium #3
Posted 16 February 2013 - 09:56 AM
And how exactly would I do that?
remiX #4
Posted 16 February 2013 - 10:19 AM
When you call v.action() just change the colour of v.BGcolor right then.



if events[3] >= v.x1 and events[3] <= v.x2 and events[4] >= v.y1 and events[4] <= v.y2 then
    v.action()
    v.BGcolor = v.BGcolor == colours.lime and colours.red or colours.lime -- you should know how this works, just does the opposite of what it currently is
    drawGUI()
end
Pinkishu #5
Posted 16 February 2013 - 10:22 AM
probably change v.action() to v:action()
change the action = function() parts to action = function(self)
and then do
self.BGColor = processingState and colors.red or colors.lime
and such
Cranium #6
Posted 16 February 2013 - 10:25 AM
When you call v.action() just change the colour of v.BGcolor right then.



if events[3] >= v.x1 and events[3] <= v.x2 and events[4] >= v.y1 and events[4] <= v.y2 then
	v.action()
	v.BGcolor = v.BGcolor == colours.lime and colours.red or colours.lime -- you should know how this works, just does the opposite of what it currently is
	drawGUI()
end
Well, it came close…..but not quite.
All that did was change all of the colors……to the opposite. and each time i click, it changes color to all of them :I


EDIT: Nevermind. I had the call in the wrong place.
Edited on 16 February 2013 - 09:27 AM