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

[SOLVED] Weird results with term.setCursorPos() when in a tab

Started by Dog, 11 April 2014 - 04:51 AM
Dog #1
Posted 11 April 2014 - 06:51 AM
I'm hitting up Ask A Pro instead of the bug thread because I'm not sure if this is actually a bug, or something I'm doing wrong and I'm not sure how to proceed.

Assuming the following:

ComputerCraft 1.63pr1 (possibly earlier 1.6x versions)

Using an advanced wireless portable computer

termX,termY = term.getSize()

Output positioned at (termY/2)-1 (when in a CraftOS tab) sometimes seems to 'disappear'. I've had to hard code termY values in one of my programs because things randomly won't show up when using (termY/2)-1 in a tab.

The routine I'm using to draw everything is drawElement() (x,y = cursor pos, h = height, w = width)
Spoiler

local function drawElement(x,y,w,h,txColor,bgColor,text)
  local deText,i = tostring(text)
  term.setCursorPos(x,y)
  term.setBackgroundColor(bgColor)
  if w > #deText or h > 1 then	   -- We're 'drawing' something more than text
	for i = 1,h,1 do				 --
	  term.write(string.rep(" ",w))  -- Draw the 'element' (box/rectangle/line-seg)
	  term.setCursorPos(x,y+i)	   --
	end
  end
  if deText ~= "" and deText ~= "nil" then
	term.setTextColor(txColor)
	if w < #deText then w = #deText end -- Ensure minimum length
      local xW = (x + w/2) - #deText/2 -- Center the text horizontally
      local xH = y + h/2			   -- Center the text vertically
      term.setCursorPos(xW,xH)
      term.write(deText)
    end
  end
end

Does anyone see something in my drawElement routine that would cause it to fail or weird-out with (termY/2)-1 vs. a hard value of '8' or '9' when running in a CraftOS tab?

Question part 'the second':

I'm also noticing that my readInput() function is behaving strangely in CraftOS tabs
Spoiler

local function readInput(cX,cY,cO)  -- cursor X,Y and color
  local word = ""
  local curX, curY = cX, cY
  term.setTextColor(cO)
  term.setCursorBlink(true)
  term.setCursorPos(curX, curY)
  while true do
	local kEvent = { os.pullEvent() }
	if kEvent[1] == "key" then
	  if kEvent[2] == keys.backspace then
		if curX > cX then
		  curX = curX - 1
		  word = word:sub(1,#word-1)
		end
		drawElement(curX,curY,1,1,cO,black," ")
	  elseif kEvent[2] == keys.enter then
		term.setCursorBlink(false)
		return word
	  end
	elseif kEvent[1] == "char" then
	  drawElement(curX,curY,1,1,cO,black,kEvent[2])
	  word = word .. kEvent[2]
	  curX = curX + 1
	end
	term.setCursorPos(curX, curY)
  end
end

In this case, feeding it (termY/2)-1 (while in a tab) causes the text it writes to the screen to be 1 line above the blinking cursor position.

So are my routines to blame for these problems or is there something in all of this I should take to the bug thread? And if so, how do I describe it so it makes sense?

If necessary I can provide a working program that demonstrates the problem, but it's a two piece setup that requires a turtle and an advanced wireless portable.

Apologies for the vague nature of my request - I'm rather stumped.
Edited on 11 April 2014 - 07:25 AM
theoriginalbit #2
Posted 11 April 2014 - 07:41 AM
okay so I would say the 'cause of this problem would be to do with the following logic

local xH = y + h/2 
however before I can confirm and explain, what is the height you're providing? I'm going to take a guess and say 3?
Dog #3
Posted 11 April 2014 - 08:05 AM
Height of 1

This makes a box with the appropriate label…

if tDig == "tunnel" then
  drawElement(termX/2-5,9,10,1,white,gray,"Length")
elseif tDig == "excavate" then
  drawElement(termX/2-5,9,10,1,white,gray,"Size")
end
drawElement(termX/2-5,10,10,2,white,gray,"")
drawElement(termX/2-4,10,8,1,white,black,"")

This results in the proper box, but the text is 'missing'; which is odd since the top line of the box is drawn in the same command as the text (and the text is 'drawn' after the gray line)…

if tDig == "tunnel" then
  drawElement(termX/2-5,(termY/2)-1,10,1,white,gray,"Length")
elseif tDig == "excavate" then
  drawElement(termX/2-5,(termY/2)-1,10,1,white,gray,"Size")
end
drawElement(termX/2-5,termY/2,10,2,white,gray,"")
drawElement(termX/2-4,termY/2,10,1,white,black,"")

Based on your question, I'm guessing something along the lines of the following might be worth exploring…

local xH = y + math.floor(h/2)

EDIT: Holy Moley, I *think* that fixed it. I've tested all of my programs that are using drawElement() with the math.floor tweak and I'm seeing no unexpected results - thanks, yet again, bit :)/>
Edited on 11 April 2014 - 03:28 PM