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

Advanced Monitors made Simple

Started by KingofGamesYami, 18 March 2014 - 06:29 PM
KingofGamesYami #1
Posted 18 March 2014 - 07:29 PM
This is my first (public) program! It is intended to make your advanced monitor easy to use, without having to code complex stuff all the time.
Video[media]http://youtu.be/M9-x4HwOSp8[/media]

Code

t = term
t.sCP = t.setCursorPos
t.w = t.write
t.clear()
t.sCP(1, 1)
print("This program is provided by KingofGamesYami for personal use, please do not distribute.  If your program is not working, check that you are using the correct syntax.  If you use the scroll function, you must hold down control+t to stop the program.  Press any key to continue")
os.pullEvent(key)
sleep(0.1)
t.clear()
t.sCP(1, 1)
t.w("Side: ") --side of your monitor
local side = string.lower(read())
if side == "top" or "right" or "left" or "back" or "bottom" or "front" then --check side is valid
 --do nothing
else
 print("Invalid side!")
 sleep(10)
 os.shutdown()
end
m = peripheral.wrap(side)
m.clear()
m.setCursorPos(1, 1)
t.sCP(1, 2)
t.w("Text Color: ") --Color of text
local input = string.lower(read())
local tcolor
if (colors[input] and type(colors[input]) == "number") or (colours[input] and type(colours[input]) == "number") then
  tcolor = colors[input] or colours[input]
elseif tonumber(input) then
  tcolor = tonumber(input)
else
  print("Not a number or color!")
end
m.setTextColor(tcolor)
t.sCP(1, 3)
t.w("Background Color: ")
local input = string.lower(read())
local bcolor
if (colors[input] and type(colors[input]) == "number") or (colours[input] and type(colours[input]) == "number") then
  bcolor = colors[input] or colours[input]
elseif tonumber(input) then
  bcolor = tonumber(input)
else
  print("Not a number or color!")
end
m.setBackgroundColor(bcolor) --Set Background Color
m.clear()
t.sCP(1, 4)
t.w("Text Size: ") --Size of Text (1 - 5)
local size = tonumber(read())
if size >= 0.5 and size <= 5 then --check size is valid
 --do nothing
else
 print("Your size isn't within the limits... 0.5 - 5")
 sleep(10)
 os.shutdown()
end
m.setTextScale(size)
t.sCP(1, 5)
t.w("Text: ") --Text that will be printed
local stuff = read()
t.sCP(1, 6) --Scroll
t.w("Would you like your text to scroll?: ")
local scroll = string.lower(read())
if scroll == "yes" then
 t.sCP(1, 7)
 t.w("Speed: ")
 speed = tonumber(read())
 local tlen = string.len(stuff)
 local mwidth, mheight = m.getSize()
 if mheight/2 == 0.5 then
  mheight = 2
 end
 local tpos = mwidth
 while true do --loop
  if tpos == 0-tlen then --reset position
   tpos = mwidth
  end
  m.clear()
  m.setCursorPos(tpos, mheight/2)
  m.write(stuff)
  tpos = tpos-1
  os.sleep(0.5/speed)
 end
elseif scroll == "no" then
 sleep(0.1)
else
 print("Invalid answer, use yes or no")
end
t.redirect(m)
print(stuff)
t.restore()
ChangelogChangelog:
1.0 - initial release!
2.0 - added scrolling text &amp; bugfixes
2.1 - bugfixes added check-for-valid stuff
Future Plans:
Adding a scrolling text utility (need help with debugging current code for this, please enter "no" when asked) Done!
Maybe auto detect monitor? (problem is, what if you have multiple monitors??)

http://pastebin.com/ii02ZH2g
pastebin get ii02ZH2g mon
Edited on 22 March 2014 - 02:34 AM
ZeeSays #2
Posted 18 March 2014 - 08:03 PM
The code for this is a bit excessive, don't you think?

And in the video, you say something about the background colour. The term.setBackgroundColor doesn't actually change the terminal's background colour, it changes the text's background colour.

The way I set my background color(I haven't found a default API of this as of yet) is by using a function as so:
  • function drawBackground(color)
  • local x, y = term.getSize()
  • for i=1, y do
  • paintutils.drawLine(1, i, 53, i, color)
  • end
  • end
Lyqyd #3
Posted 18 March 2014 - 09:23 PM
Changing the whole screen to be blank and have one background color is quite simply:


term.setBackgroundColor(colors.lime)
term.clear()
KingofGamesYami #4
Posted 18 March 2014 - 09:56 PM
The code for this is a bit excessive, don't you think?

And in the video, you say something about the background colour. The term.setBackgroundColor doesn't actually change the terminal's background colour, it changes the text's background colour.

The way I set my background color(I haven't found a default API of this as of yet) is by using a function as so:
  • function drawBackground(color)
  • local x, y = term.getSize()
  • for i=1, y do
  • paintutils.drawLine(1, i, 53, i, color)
  • end
  • end
I don't use term.setBackgroundColor, I used peripheral.wrap("right").setBackgroundColor. Basically the same thing, but for monitors.
Also, what part is excessive? The only part I would consider largely unneeded is the part that allows you to use the color names instead of the numbers.

Changing the whole screen to be blank and have one background color is quite simply:


term.setBackgroundColor(colors.lime)
term.clear()
Thanks, I will try that. All I need to add is the m.clear() after m.setBackgroundColor
Edited on 18 March 2014 - 08:59 PM
KingofGamesYami #5
Posted 21 March 2014 - 04:13 AM
Announcing A.M.M.S. v2.0! This version is new and improved…
+Scroll text feature
+Scroll Speed
-Program no longer breaks with capital letters in colors &amp; side
-Background Color is now seamless
-For those of you that used the program while I was adding this stuff:
-program doesn't ask for # of monitors
-program doesn't ask for text length
Main post has been updated with this new and improved code. If you have any future suggestions, I would love to hear them. I will add it if it seems useful and is related to monitors.