Advertisement
MagmaLP

Test Mon

May 18th, 2025 (edited)
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.29 KB | None | 0 0
  1. -- Schritt 1: Eingaben für Monitor
  2. term.setCursorPos(1, 1)
  3. term.setTextColor(colors.lightBlue)
  4. term.setBackgroundColor(colors.black)
  5. term.clear()
  6. print("Du stehst vor dem Computer, auf welcher Seite")
  7. print("des Computers befindet sich der Monitor")
  8. print("Schreibe: left, right, back, front, top, bottom")
  9. print(" ")
  10. local side = read()
  11.  
  12. term.setCursorPos(1, 7)
  13. print("Textgrösse wählen: (von 1 in 0.5er Schritten bis 5) ")
  14. local scaleInput = read()
  15. local scale = tonumber(scaleInput)
  16. if not scale or scale < 0.5 or scale > 5 then
  17.   scale = 1.0
  18. end
  19.  
  20. -- Schritt 2: Hintergrundfarbe wählen
  21. term.setCursorPos(1, 10)
  22. print("Hintergrundfarbe des Bildschirms wählen:")
  23.  
  24. local MonbgColors = {
  25.   colors.white, colors.orange, colors.magenta, colors.lightBlue,
  26.   colors.yellow, colors.lime, colors.pink, colors.gray,
  27.   colors.black, colors.brown, colors.red, colors.green,
  28.   colors.blue, colors.purple, colors.cyan, colors.lightGray
  29. }
  30.  
  31. for i, color in ipairs(MonbgColors) do
  32.   term.setBackgroundColor(color)
  33.   term.setCursorPos((i - 1) * 2 + 1, 11)
  34.   term.write("  ")
  35. end
  36.  
  37. term.setBackgroundColor(colors.black)
  38. term.setCursorPos(1, 13)
  39. write("Klicke eine Farbe an...")
  40.  
  41. local currentMonBgColor = colors.black
  42. while true do
  43.   local event, button, x, y = os.pullEvent()
  44.   if event == "mouse_click" and y == 11 then
  45.     for i, color in ipairs(MonbgColors) do
  46.       local startX = (i - 1) * 2 + 1
  47.       if x >= startX and x <= startX + 1 then
  48.         currentMonBgColor = color
  49.         break
  50.       end
  51.     end
  52.     if currentMonBgColor then break end
  53.   end
  54. end
  55.  
  56. -- Monitor initialisieren
  57. local mon = peripheral.wrap(side)
  58. mon.setTextScale(scale)
  59. mon.setBackgroundColor(currentMonBgColor)
  60. mon.clear()
  61.  
  62. local width, height = mon.getSize()
  63.  
  64. -- Hilfstexte
  65. term.setCursorPos(1, 1)
  66. term.setTextColor(colors.lightBlue)
  67. term.setBackgroundColor(colors.black)
  68. term.clear()
  69.  
  70. term.setCursorPos(1,12)
  71. term.write("Mit den Pfeiltasten kannst du auf dem Monitor an")
  72. term.setCursorPos(1,13)
  73. term.write("die Stelle navigieren an der dein Text stehen soll.")
  74. term.setCursorPos(1,14)
  75. term.write("Schreibe via Tastatureingabe den Text direkt ")
  76. term.setCursorPos(1,15)
  77. term.write("an die gewünschte Stelle, du kannst für jeden")
  78. term.setCursorPos(1,16)
  79. term.write("Buchstaben Text- und Hintergrundfarbe festlegen.")
  80. term.setCursorPos(1,18)
  81. term.write("[Enter] drücken um zu speichern oder beenden")
  82.  
  83. -- Farben
  84. local textColors = MonbgColors
  85. local bgColors = MonbgColors
  86.  
  87. local currentTextColor = colors.white
  88. local currentBgColor = colors.black
  89.  
  90. -- Ordner vorbereiten
  91. if not fs.exists("screens") then
  92.   fs.makeDir("screens")
  93. end
  94.  
  95. -- Speichern
  96. local function saveScreen(name, content)
  97.   local f = fs.open("screens/" .. name .. ".txt", "w")
  98.   if not f then
  99.     print("Fehler beim Speichern.")
  100.     return
  101.   end
  102.   f.writeLine(table.concat({side, scale, currentMonBgColor}, ","))
  103.   for _, data in ipairs(content) do
  104.     f.writeLine(table.concat({
  105.       data.x,
  106.       data.y,
  107.       data.textColor,
  108.       data.bgColor,
  109.       data.char
  110.     }, ","))
  111.   end
  112.   f.close()
  113.   print("Gespeichert als: " .. name)
  114. end
  115.  
  116. -- Cursor-Logik
  117. local x, y = 1, 1
  118. local cursorVisible = true
  119. local timerID = os.startTimer(0.5)
  120. local content = {}
  121.  
  122. local function clamp(val, min, max)
  123.   if val < min then return min end
  124.   if val > max then return max end
  125.   return val
  126. end
  127.  
  128. local function drawCursor()
  129.   mon.setCursorPos(x, y)
  130.   local oldChar = " "
  131.   for i = #content, 1, -1 do
  132.     local c = content[i]
  133.     if c.x == x and c.y == y then
  134.       oldChar = c.char
  135.       mon.setTextColor(c.textColor)
  136.       mon.setBackgroundColor(c.bgColor)
  137.       break
  138.     end
  139.   end
  140.  
  141.   if cursorVisible then
  142.     mon.setTextColor(currentTextColor)
  143.     mon.setBackgroundColor(currentMonBgColor)
  144.     mon.write("_")
  145.   else
  146.     mon.write(oldChar)
  147.   end
  148.  
  149.   mon.setCursorPos(x, y)
  150. end
  151.  
  152. local function drawColorBoxes()
  153.   term.setCursorPos(1, 1)
  154.   term.setTextColor(colors.lightBlue)
  155.   term.setBackgroundColor(colors.black)
  156.   print("Schrift - Farbe:")
  157.   for i, color in ipairs(textColors) do
  158.     term.setBackgroundColor(color)
  159.     term.setCursorPos(i * 2 - 1, 2)
  160.     term.write("  ")
  161.   end
  162.  
  163.   term.setCursorPos(1, 4)
  164.   term.setTextColor(colors.lightBlue)
  165.   term.setBackgroundColor(colors.black)
  166.   print("Schrift - Hintergrundfarbe:")
  167.   for i, color in ipairs(bgColors) do
  168.     term.setBackgroundColor(color)
  169.     term.setCursorPos(i * 2 - 1, 5)
  170.     term.write("  ")
  171.   end
  172. end
  173.  
  174. local function checkColorClick(mx, my)
  175.   if my == 2 then
  176.     for i = 1, #textColors do
  177.       if mx >= i * 2 - 1 and mx <= i * 2 then
  178.         currentTextColor = textColors[i]
  179.       end
  180.     end
  181.   elseif my == 5 then
  182.     for i = 1, #bgColors do
  183.       if mx >= i * 2 - 1 and mx <= i * 2 then
  184.         currentBgColor = bgColors[i]
  185.       end
  186.     end
  187.   end
  188. end
  189.  
  190. -- Startanzeige
  191. drawColorBoxes()
  192. drawCursor()
  193.  
  194. -- Eingabeschleife
  195. while true do
  196.   local event, p1, p2, p3 = os.pullEvent()
  197.  
  198.   if event == "char" then
  199.     local char = p1
  200.     mon.setCursorPos(x, y)
  201.     mon.setTextColor(currentTextColor)
  202.     mon.setBackgroundColor(currentBgColor)
  203.     mon.write(char)
  204.     table.insert(content, {
  205.       x = x, y = y,
  206.       textColor = currentTextColor,
  207.       bgColor = currentBgColor,
  208.       char = char
  209.     })
  210.     x = clamp(x + 1, 1, width)
  211.     drawCursor()
  212.  
  213.   elseif event == "key" then
  214.     local key = p1
  215.     cursorVisible = false
  216.     drawCursor()
  217.  
  218.     if key == keys.enter then
  219.       term.setCursorPos(1, 8)
  220.       term.setTextColor(colors.white)
  221.       term.setBackgroundColor(colors.black)
  222.       term.clearLine()
  223.       write("Speichern? (j/n): ")
  224.       local ans = read()
  225.       if ans == "j" then
  226.         term.clearLine()
  227.         write("Speichername: ")
  228.         local name = read()
  229.         saveScreen(name, content)
  230.       end
  231.       break
  232.  
  233.     elseif key == keys.left then
  234.       x = clamp(x - 1, 1, width)
  235.     elseif key == keys.right then
  236.       x = clamp(x + 1, 1, width)
  237.     elseif key == keys.up then
  238.       y = clamp(y - 1, 1, height)
  239.     elseif key == keys.down then
  240.       y = clamp(y + 1, 1, height)
  241.  
  242.     elseif key == keys.backspace then
  243.       if x > 1 then
  244.         x = x - 1
  245.       elseif y > 1 then
  246.         y = y - 1
  247.         x = width
  248.       end
  249.       for i = #content, 1, -1 do
  250.         local c = content[i]
  251.         if c.x == x and c.y == y then
  252.           table.remove(content, i)
  253.           break
  254.         end
  255.       end
  256.       mon.setCursorPos(x, y)
  257.       mon.setTextColor(currentTextColor)
  258.       mon.setBackgroundColor(currentBgColor)
  259.       mon.write(" ")
  260.  
  261.     elseif key == keys.delete then
  262.       for i = #content, 1, -1 do
  263.         local c = content[i]
  264.         if c.x == x and c.y == y then
  265.           table.remove(content, i)
  266.           break
  267.         end
  268.       end
  269.       mon.setCursorPos(x, y)
  270.       mon.setTextColor(currentTextColor)
  271.       mon.setBackgroundColor(currentBgColor)
  272.       mon.write(" ")
  273.     end
  274.  
  275.     cursorVisible = true
  276.     drawCursor()
  277.  
  278.   elseif event == "mouse_click" then
  279.     local btn, mx, my = p1, p2, p3
  280.     checkColorClick(mx, my)
  281.  
  282.   elseif event == "timer" and p1 == timerID then
  283.     cursorVisible = not cursorVisible
  284.     drawCursor()
  285.     timerID = os.startTimer(0.5)
  286.   end
  287. end
  288.  
  289. cursorVisible = false
  290. drawCursor()
  291. mon.setCursorPos(1, height)
  292. mon.write("Programm beendet.")
  293.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement