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

[SOLVED] ccDHD - having to 'hard-code' formatting for every page, want to avoid this

Started by Dog, 06 February 2014 - 03:50 PM
Dog #1
Posted 06 February 2014 - 04:50 PM
Greetings, all :)/>

I've run into a brick wall with my ccDHD program and have exhausted my remaining brain cells trying to work out a solution. The program displays 24 gates/page in 4 columns of 6 gates. This works fine if I 'hard code' for each page. That's clearly no good as even supporting five pages is silly (as show below).

Here is the specific page formatting code:
Spoiler

local function drawDialUI() -- Gate Address Book
  local bColor = black
  local xPos = 2
  local yPos = 4
  local j = ((pageNum - 1) * 23) + pageNum

  local k = ((pageNum - 1) * 23)
  local l = ((pageNum - 1) * 24)
  local m = ((pageNum - 1) * 12)

  term.setBackgroundColor(bColor)
  for i = j,#allGates,1 do	-- for i = j,j+23,1 do (fixed to 24 gates per page, but avoids need for 'break')
	if (i > 0 and i < 7) or (i > 24 and i < 31) or (i > 48 and i < 55) or (i > 72 and i < 79) or (i > 96 and i < 103) then		-- Column 1
	  xPos = 2
	  yPos = 4
	elseif (i > 6 and i < 13) or (i > 30 and i < 37) or (i > 54 and i < 61) or (i > 78 and i < 85) or (i > 102 and i < 109) then  -- Column 2
	  xPos = 12
	  yPos = -8
	elseif (i > 12 and i < 19) or (i > 36 and i < 43) or (i > 60 and i < 67) or (i > 84 and i < 91) or (i > 108 and i < 115) then -- Column 3
	  xPos = 22
	  yPos = -20
	elseif (i > 18 and i < 25) or (i > 42 and i < 49) or (i > 66 and i < 73) or (i > 90 and i < 97) or (i > 114 and i < 121) then -- Column 4
	  xPos = 32
	  yPos = -32
	end
	term.setCursorPos(xPos,yPos + ((i - ((pageNum - 1) * 24)) * 2))
	term.setTextColor(white)
	bColor = assignColor(i)
	term.setBackgroundColor(bColor)
	if dialAddress == allGates[i].addr then
	  term.setTextColor(black)
	end
	if i > (pageNum * 24) or i > #allGates then
	  break
	else
	  if #allGates[i].addr == 7 then
		term.write(" " .. allGates[i].addr .. " ")
	  elseif #allGates[i].addr == 8 then
		term.write(" " .. allGates[i].addr)
	  elseif #allGates[i].addr == 9 then
		term.write(allGates[i].addr)
	  elseif #allGates[i].addr < 7 then
		term.write("*" .. allGates[i].addr .. "*")  -- * = address is less than 7 characters
	  else
		term.write("ERROR #10")  -- ERROR #10 = address is 10 or more characters
	  end
	end
  end
  term.setBackgroundColor(black)
end

I'm looking for a way to properly format the pages without having to hardcode a specific set of values for every page. I've managed to get some code working for the first column, but it breaks at page 5. My algorithm-fu is not strong. Any help would be greatly appreciated :)/>

EDIT: I figured out a solution - the code is below:
Spoiler


local function drawDialUI() -- Gate Address Book
  local bColor = black
  local xPos = 2
  local yPos = 4
  local j = ((pageNum - 1) * 23) + pageNum
  local k = ((pageNum - 1) * 23)
  local l = ((pageNum - 1) * 24)
  term.setBackgroundColor(bColor)
  for i = j,#allGates,1 do    -- for i = j,j+23,1 do (fixed to 24 gates per page, but avoids need for 'break')
    if i > l and i < l + 7 then           -- Column 1
      xPos = 2
      yPos = 4
    elseif i > l + 6 and i < l + 13 then  -- Column 2
      xPos = 12
      yPos = -8
    elseif i > l + 12 and i < l + 19 then -- Column 3
      xPos = 22
      yPos = -20
    elseif i > l + 18 and i < l + 25 then -- Column 4
      xPos = 32
      yPos = -32
    end
    term.setCursorPos(xPos,yPos + ((i - ((pageNum - 1) * 24)) * 2))
    term.setTextColor(white)
    bColor = assignColor(i)
    term.setBackgroundColor(bColor)
    if dialAddress == allGates[i].addr then
      term.setTextColor(black)
    end
    if i > (pageNum * 24) or i > #allGates then
      break
    else
      if #allGates[i].addr == 7 then
        term.write(" " .. allGates[i].addr .. " ")
      elseif #allGates[i].addr == 8 then
        term.write(" " .. allGates[i].addr)
      elseif #allGates[i].addr == 9 then
        term.write(allGates[i].addr)
      elseif #allGates[i].addr < 7 then
        term.write("*" .. allGates[i].addr .. "*")  -- * = address is less than 7 characters
      else
        term.write("ERROR #10")  -- ERROR #10 = address is 10 or more characters
      end
    end
  end
  term.setBackgroundColor(black)
end
[size=4]

Bomb Bloke #2
Posted 06 February 2014 - 08:42 PM
I spotted this earlier and started writing something out. I see you've come up with a solution yourself, but I figure this may still prove useful to you in some way. Note that I haven't specifically tested it, but I believe it should act in a similar manner to your current implementation.

local function drawDialUI() -- Gate Address Book
  local i = (pageNum - 1) * 24

  for xPos = 2,32,10 do for yPos = 6,16,2 do
        i = i + 1
        if i > #allGates then
        	term.setBackgroundColor(black)
        	return
        end

        term.setCursorPos(xPos,yPos)
        term.setBackgroundColor(assignColor(i))
        if dialAddress == allGates[i].addr then term.setTextColor(black) else term.setTextColor(white) end

        if #allGates[i].addr < 10 then
              term.write(string.rep(" ",math.ceil((9-#allGates[i].addr)/2)) .. allGates[i].addr .. string.rep(" ",math.floor((9-#allGates[i].addr)/2)))  -- Center the text.
        else
              term.write(allGates[i].addr:sub(1,7).."..")  -- ... or truncate it if it won't fit.
        end
  end end

  term.setBackgroundColor(black)
end
Dog #3
Posted 06 February 2014 - 10:07 PM
Thank you, Bomb Bloke :)/>

It looks like I could learn quite a bit from how you approached the whole function. Much cleaner and more elegant. Thank you, again :D/>
Edited on 06 February 2014 - 09:07 PM