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

Custom Write Function

Started by augustas656, 12 June 2014 - 06:16 PM
augustas656 #1
Posted 12 June 2014 - 08:16 PM
I have made my own API which has a tool that adds a window for you, I'm making my own window API sort of. Now, instead of like the regular terminal screen when you write text if it's longer than the width it will keep snapping off in word style until it fits the screen, if more space is needed it will scroll along the Y axis. It doesn't scroll to the X, it does scroll to the Y. MY window is customisable, so you can choose wether you want X to scroll or not and Y to scroll or not. If X scrolls, writing text will keep writing it on the same line unless there's an \n or manually use a scroll function to scroll Y or something. If neither X or Y can scroll, then text will snap off the X axis and when it reaches it's end, it will just stop writing unless you change position or something.

My write function will not need to split by words, it will just split along the area of the window even if it has to split words in half. I can take care of the splitting with already preset functions in my API. I can also take care of scrolling with ScrollX, ScrollY or ScrollPos(x, y). I just need sort of logical help for how I could make this work.

Also I use a two-dimensional pixel table for storing each character and colour data of my window. For example if the window is 10 width and 6 height and at the 10, 6 there is a letter C written. win.charsData[6][10] (reversed, it's easier for X handling) will equal to "C".

Can someone provide help here on how I could make such a function that would write as described? I don't want to write over the borders of the window either, I can't think of a way.

Regards
Augustas
Edited on 12 June 2014 - 06:34 PM
Lyqyd #2
Posted 12 June 2014 - 08:30 PM
I think you forgot to ask your question. Are you getting an error? Where is the code?
CometWolf #3
Posted 12 June 2014 - 10:14 PM
You should probably atleast give this stuff a go yourself first, but i'd imagine it would be something like

while #sString > 0 do
  local line = sString:sub(1,window.length-window.cursX)
  sString = sString:sub(#line+1)
  write(line)
  window.lines[window.cursY] = (
    window.lines[window.cursY]
    and window.lines[window.cursY]..string.rep(" ",math.max(0,window.cursX-#window.lines[window.cursY]))..line
    or string.rep(" ",window.cursX-1)..line
  )
  for i=window.cursX,window.cursX+#line do
	window.pixels[i][window.cursY] = window.backgroundColor
  end
  window.cursY = window.cursY+1
  window.cursX = 1
  if window.curY > window.height+window.scrollY then
	window.scroll(window.curY-window.height)
  end
end
I threw this together just now because i like this sort of thing :P/> but if you really can't figure it out, you should probably go with something easier first.
Edited on 12 June 2014 - 08:20 PM