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

Button Acts Differently Each Time It's Run

Started by OReezy, 05 February 2014 - 06:00 PM
OReezy #1
Posted 05 February 2014 - 07:00 PM
I wrote up a quick button program to test how I wanted to implement it in my main program and it is acting oddly. The first time I run the program it prints the button in the wrong position. The second time it prints the button in the correct position.

The code http://pastebin.com/tq75PA7H
Spoiler
local m = peripheral.wrap("right")
local monx, mony = m.getSize()

local button = {}
  button.label = "Manual Override"
  button.x = monx/2 - #button.label/2
  button.y = mony/2
  button.w = #button.label+2
  button.h = 3
  button.state = true

m.setTextScale(.5)

local function drawButton(b, color)
  m.setBackgroundColor(color)
  for row = 1,b.h do
	m.setCursorPos(b.x,b.y+row-1)
	m.write(string.rep(" ", b.w))
  end
  m.setCursorPos(b.x+1, b.y+1)
  m.write(b.label)
end

drawButton(button, colors.lime)

while true do
  local event, x, y = os.pullEvent()
  if event == "monitor_touch" then
	if button.state == true then
	  button.state = false
	  drawButton(button, colors.red)
	elseif button.state == false then
	  button.state = true
	  drawButton(button, colors.lime)
	end
  end
end


And here is a picture for clarification. The red button is where it prints the first time the program is run. The second time it prints where the green button is and continues working after that. I have also tried it with different sized screens and it prints in the wrong position the first time no matter what the size. Also, the button changes color correctly no matter which position it is in.

Spoiler
Kashin #2
Posted 05 February 2014 - 08:15 PM
Hi there OReezy.

The reason your program is failing is because you change the textScale after having retrieved the size.

Try
m.setTextScale(.5)

before you run
local monx, mony = m.getSize()

So you should end up with something like this
Spoiler

local m = peripheral.wrap("right")
m.setTextScale(.5)
local monx, mony = m.getSize()

local button = {}
  button.label = "Manual Override"
  button.x = monx/2 - #button.label/2
  button.y = mony/2
  button.w = #button.label+2
  button.h = 3
  button.state = true

local function drawButton(b, color)
  m.setBackgroundColor(color)
  for row = 1,b.h do
	m.setCursorPos(b.x,b.y+row-1)
	m.write(string.rep(" ", b.w))
  end
  m.setCursorPos(b.x+1, b.y+1)
  m.write(b.label)
end

drawButton(button, colors.lime)

while true do
  local event, x, y = os.pullEvent()
  if event == "monitor_touch" then
	if button.state == true then
	  button.state = false
	  drawButton(button, colors.red)
	elseif button.state == false then
	  button.state = true
	  drawButton(button, colors.lime)
	end
  end
end

Let me know if it works for you :)/>
OReezy #3
Posted 05 February 2014 - 08:45 PM
facepalm. Thanks, I knew i had to be doing something dumb somewhere.
Kashin #4
Posted 05 February 2014 - 09:27 PM
You're quite welcome :)/>

Good luck with your program!
surferpup #5
Posted 05 February 2014 - 11:10 PM
facepalm. Thanks, I knew i had to be doing something dumb somewhere.

We've all been there, OReezy.