Advertisement
MagmaLP

Test Mon 2

May 19th, 2025 (edited)
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.96 KB | None | 0 0
  1. -- Hilfsfunktionen
  2.  
  3. if not fs.exists("startup") then
  4.   local f = fs.open("startup", "w")
  5.   f.writeLine('shell.run("Screen-Conf")')
  6.   f.close()
  7. end
  8.  
  9.  
  10. local function clear()
  11.   term.setBackgroundColor(colors.black)
  12.   term.setTextColor(colors.lightBlue)
  13.   term.clear()
  14.   term.setCursorPos(1, 1)
  15. end
  16.  
  17. local function waitEnter()
  18.   term.setTextColor(colors.gray)
  19.   print("\nDrücke [Enter], um fortzufahren...")
  20.   term.setTextColor(colors.lightBlue)
  21.   read()
  22. end
  23.  
  24. local function listScreens()
  25.   if not fs.exists("screens") then fs.makeDir("screens") end
  26.   local files = fs.list("screens")
  27.   local screens = {}
  28.   for _, f in ipairs(files) do
  29.     if not fs.isDir("screens/" .. f) then
  30.       table.insert(screens, f)
  31.     end
  32.   end
  33.   return screens
  34. end
  35.  
  36. local function displayScreen(filename)
  37.   local path = "screens/" .. filename
  38.   local file = fs.open(path, "r")
  39.   if not file then return false, "Datei konnte nicht geöffnet werden." end
  40.  
  41.   local header = file.readLine()
  42.   local side, scaleStr, bgColorStr = header:match("([^,]+),([^,]+),([^,]+)")
  43.   local scale = tonumber(scaleStr)
  44.   local bgColor = tonumber(bgColorStr)
  45.  
  46.   if not (side and scale and bgColor) then
  47.     file.close()
  48.     return false, "Ungültiger Header in Datei."
  49.   end
  50.  
  51.   local monitor = peripheral.wrap(side)
  52.   if not monitor then
  53.     file.close()
  54.     return false, "Monitor auf Seite '" .. side .. "' nicht gefunden."
  55.   end
  56.  
  57.   monitor.setTextScale(scale)
  58.   monitor.setBackgroundColor(bgColor)
  59.   monitor.clear()
  60.  
  61.   while true do
  62.     local line = file.readLine()
  63.     if not line then break end
  64.     local x, y, textColor, charBgColor, char = line:match("([^,]+),([^,]+),([^,]+),([^,]+),(.+)")
  65.     x = tonumber(x)
  66.     y = tonumber(y)
  67.     textColor = tonumber(textColor)
  68.     charBgColor = tonumber(charBgColor)
  69.     if x and y and textColor and charBgColor and char then
  70.       monitor.setCursorPos(x, y)
  71.       monitor.setTextColor(textColor)
  72.       monitor.setBackgroundColor(charBgColor)
  73.       monitor.write(char)
  74.     end
  75.   end
  76.  
  77.   file.close()
  78.   return true
  79. end
  80.  
  81.  
  82.  
  83.  
  84.  
  85. -- [3] Auto-Start Bildschirmanzeige
  86. local function autoStartScreen()
  87.   if not fs.exists("auto_start") then return end
  88.  
  89.   local file = fs.open("auto_start", "r")
  90.   local screenFile = file.readLine()
  91.   file.close()
  92.  
  93.   if not screenFile or screenFile == "" then return end
  94.  
  95.   local success, err = displayScreen(screenFile)
  96.   if not success then
  97.     term.setTextColor(colors.red)
  98.     print("Auto-Start Fehler: " .. err)
  99.     term.setTextColor(colors.lightBlue)
  100.     sleep(2)
  101.   end
  102. end
  103.  
  104. autoStartScreen()
  105.  
  106. -- [1] Gespeicherten Bildschirm anschauen
  107. local function promptScreenStart()
  108.   clear()
  109.   print("Gespeicherte Screens:\n")
  110.  
  111.   local screens = listScreens()
  112.   for i, s in ipairs(screens) do
  113.     print(i .. ") " .. s)
  114.   end
  115.  
  116.   io.write("\nNummer eingeben zum Starten: ")
  117.   local n = tonumber(read())
  118.  
  119.   if not (n and screens[n]) then
  120.     term.setTextColor(colors.red)
  121.     print("Ungültige Eingabe.")
  122.     term.setTextColor(colors.lightBlue)
  123.     waitEnter()
  124.     return
  125.   end
  126.  
  127.   local success, err = displayScreen(screens[n])
  128.   if not success then
  129.     term.setTextColor(colors.red)
  130.     print(err)
  131.     term.setTextColor(colors.lightBlue)
  132.     waitEnter()
  133.     return
  134.   end
  135. end
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. -- [2] Neuen Bildschirm Konfigurieren
  143. local function screenConfig()
  144.   term.setCursorPos(1, 1)
  145. term.setTextColor(colors.lightBlue)
  146. term.setBackgroundColor(colors.black)
  147. term.clear()
  148. print("Du stehst vor dem Computer, auf welcher Seite")
  149. print("des Computers befindet sich der Monitor")
  150. print("Schreibe: left, right, back, front, top, bottom")
  151. print(" ")
  152. local side = read()
  153.  
  154. term.setCursorPos(1, 7)
  155. print("Textgrösse wählen: (von 1 in 0.5er Schritten bis 5) ")
  156. local scaleInput = read()
  157. local scale = tonumber(scaleInput)
  158. if not scale or scale < 0.5 or scale > 5 then
  159.   scale = 1.0
  160. end
  161.  
  162. -- Schritt 2: Hintergrundfarbe wählen
  163. term.setCursorPos(1, 10)
  164. print("Hintergrundfarbe des Bildschirms wählen:")
  165.  
  166. local MonbgColors = {
  167.   colors.white, colors.orange, colors.magenta, colors.lightBlue,
  168.   colors.yellow, colors.lime, colors.pink, colors.gray,
  169.   colors.black, colors.brown, colors.red, colors.green,
  170.   colors.blue, colors.purple, colors.cyan, colors.lightGray
  171. }
  172.  
  173. for i, color in ipairs(MonbgColors) do
  174.   term.setBackgroundColor(color)
  175.   term.setCursorPos((i - 1) * 2 + 1, 11)
  176.   term.write("  ")
  177. end
  178.  
  179. term.setBackgroundColor(colors.black)
  180. term.setCursorPos(1, 13)
  181. write("Klicke eine Farbe an...")
  182.  
  183. local currentMonBgColor = colors.black
  184. while true do
  185.   local event, button, x, y = os.pullEvent()
  186.   if event == "mouse_click" and y == 11 then
  187.     for i, color in ipairs(MonbgColors) do
  188.       local startX = (i - 1) * 2 + 1
  189.       if x >= startX and x <= startX + 1 then
  190.         currentMonBgColor = color
  191.         break
  192.       end
  193.     end
  194.     if currentMonBgColor then break end
  195.   end
  196. end
  197.  
  198. -- Monitor initialisieren
  199. local mon = peripheral.wrap(side)
  200. mon.setTextScale(scale)
  201. mon.setBackgroundColor(currentMonBgColor)
  202. mon.clear()
  203.  
  204. local width, height = mon.getSize()
  205.  
  206. -- Hilfstexte
  207. term.setCursorPos(1, 1)
  208. term.setTextColor(colors.lightBlue)
  209. term.setBackgroundColor(colors.black)
  210. term.clear()
  211.  
  212. term.setCursorPos(1,12)
  213. term.write("Mit den Pfeiltasten kannst du auf dem Monitor an")
  214. term.setCursorPos(1,13)
  215. term.write("die Stelle navigieren an der dein Text stehen soll.")
  216. term.setCursorPos(1,14)
  217. term.write("Schreibe via Tastatureingabe den Text direkt ")
  218. term.setCursorPos(1,15)
  219. term.write("an die gewünschte Stelle, du kannst für jeden")
  220. term.setCursorPos(1,16)
  221. term.write("Buchstaben Text- und Hintergrundfarbe festlegen.")
  222. term.setCursorPos(1,18)
  223. term.write("[Enter] drücken um zu speichern oder beenden")
  224.  
  225. -- Farben
  226. local textColors = MonbgColors
  227. local bgColors = MonbgColors
  228.  
  229. local currentTextColor = colors.white
  230. local currentBgColor = colors.black
  231.  
  232. -- Ordner vorbereiten
  233. if not fs.exists("screens") then
  234.   fs.makeDir("screens")
  235. end
  236.  
  237. -- Speichern
  238. local function saveScreen(name, content)
  239.   local f = fs.open("screens/" .. name .. ".txt", "w")
  240.   if not f then
  241.     print("Fehler beim Speichern.")
  242.     return
  243.   end
  244.   f.writeLine(table.concat({side, scale, currentMonBgColor}, ","))
  245.   for _, data in ipairs(content) do
  246.     f.writeLine(table.concat({
  247.       data.x,
  248.       data.y,
  249.       data.textColor,
  250.       data.bgColor,
  251.       data.char
  252.     }, ","))
  253.   end
  254.   f.close()
  255.   print("Gespeichert als: " .. name)
  256. end
  257.  
  258. -- Cursor-Logik
  259. local x, y = 1, 1
  260. local cursorVisible = true
  261. local timerID = os.startTimer(0.5)
  262. local content = {}
  263.  
  264. local function clamp(val, min, max)
  265.   if val < min then return min end
  266.   if val > max then return max end
  267.   return val
  268. end
  269.  
  270. local function drawCursor()
  271.   mon.setCursorPos(x, y)
  272.   local oldChar = " "
  273.   for i = #content, 1, -1 do
  274.     local c = content[i]
  275.     if c.x == x and c.y == y then
  276.       oldChar = c.char
  277.       mon.setTextColor(c.textColor)
  278.       mon.setBackgroundColor(c.bgColor)
  279.       break
  280.     end
  281.   end
  282.  
  283.   if cursorVisible then
  284.     mon.setTextColor(currentTextColor)
  285.     mon.setBackgroundColor(currentMonBgColor)
  286.     mon.write("_")
  287.   else
  288.     mon.write(oldChar)
  289.   end
  290.  
  291.   mon.setCursorPos(x, y)
  292. end
  293.  
  294. local function drawColorBoxes()
  295.   term.setCursorPos(1, 1)
  296.   term.setTextColor(colors.lightBlue)
  297.   term.setBackgroundColor(colors.black)
  298.   print("Schrift - Farbe:")
  299.   for i, color in ipairs(textColors) do
  300.     term.setBackgroundColor(color)
  301.     term.setCursorPos(i * 2 - 1, 2)
  302.     term.write("  ")
  303.   end
  304.  
  305.   term.setCursorPos(1, 4)
  306.   term.setTextColor(colors.lightBlue)
  307.   term.setBackgroundColor(colors.black)
  308.   print("Schrift - Hintergrundfarbe:")
  309.   for i, color in ipairs(bgColors) do
  310.     term.setBackgroundColor(color)
  311.     term.setCursorPos(i * 2 - 1, 5)
  312.     term.write("  ")
  313.   end
  314. end
  315.  
  316. local function checkColorClick(mx, my)
  317.   if my == 2 then
  318.     for i = 1, #textColors do
  319.       if mx >= i * 2 - 1 and mx <= i * 2 then
  320.         currentTextColor = textColors[i]
  321.       end
  322.     end
  323.   elseif my == 5 then
  324.     for i = 1, #bgColors do
  325.       if mx >= i * 2 - 1 and mx <= i * 2 then
  326.         currentBgColor = bgColors[i]
  327.       end
  328.     end
  329.   end
  330. end
  331.  
  332. -- Startanzeige
  333. drawColorBoxes()
  334. drawCursor()
  335.  
  336. -- Eingabeschleife
  337. while true do
  338.   local event, p1, p2, p3 = os.pullEvent()
  339.  
  340.   if event == "char" then
  341.     local char = p1
  342.     mon.setCursorPos(x, y)
  343.     mon.setTextColor(currentTextColor)
  344.     mon.setBackgroundColor(currentBgColor)
  345.     mon.write(char)
  346.     table.insert(content, {
  347.       x = x, y = y,
  348.       textColor = currentTextColor,
  349.       bgColor = currentBgColor,
  350.       char = char
  351.     })
  352.     x = clamp(x + 1, 1, width)
  353.     drawCursor()
  354.  
  355.   elseif event == "key" then
  356.     local key = p1
  357.     cursorVisible = false
  358.     drawCursor()
  359.  
  360.     if key == keys.enter then
  361.       term.setCursorPos(1, 8)
  362.       term.setTextColor(colors.white)
  363.       term.setBackgroundColor(colors.black)
  364.       term.clearLine()
  365.       write("Speichern? (j/n): ")
  366.       local ans = read()
  367.       if ans == "j" then
  368.         term.clearLine()
  369.         write("Speichername: ")
  370.         local name = read()
  371.         saveScreen(name, content)
  372.       end
  373.       break
  374.  
  375.     elseif key == keys.left then
  376.       x = clamp(x - 1, 1, width)
  377.     elseif key == keys.right then
  378.       x = clamp(x + 1, 1, width)
  379.     elseif key == keys.up then
  380.       y = clamp(y - 1, 1, height)
  381.     elseif key == keys.down then
  382.       y = clamp(y + 1, 1, height)
  383.  
  384.     elseif key == keys.backspace then
  385.       if x > 1 then
  386.         x = x - 1
  387.       elseif y > 1 then
  388.         y = y - 1
  389.         x = width
  390.       end
  391.       for i = #content, 1, -1 do
  392.         local c = content[i]
  393.         if c.x == x and c.y == y then
  394.           table.remove(content, i)
  395.           break
  396.         end
  397.       end
  398.       mon.setCursorPos(x, y)
  399.       mon.setTextColor(currentTextColor)
  400.       mon.setBackgroundColor(currentBgColor)
  401.       mon.write(" ")
  402.  
  403.     elseif key == keys.delete then
  404.       for i = #content, 1, -1 do
  405.         local c = content[i]
  406.         if c.x == x and c.y == y then
  407.           table.remove(content, i)
  408.           break
  409.         end
  410.       end
  411.       mon.setCursorPos(x, y)
  412.       mon.setTextColor(currentTextColor)
  413.       mon.setBackgroundColor(currentBgColor)
  414.       mon.write(" ")
  415.     end
  416.  
  417.     cursorVisible = true
  418.     drawCursor()
  419.  
  420.   elseif event == "mouse_click" then
  421.     local btn, mx, my = p1, p2, p3
  422.     checkColorClick(mx, my)
  423.  
  424.   elseif event == "timer" and p1 == timerID then
  425.     cursorVisible = not cursorVisible
  426.     drawCursor()
  427.     timerID = os.startTimer(0.5)
  428.   end
  429. end
  430.  
  431. cursorVisible = false
  432. drawCursor()
  433. mon.setCursorPos(1, height)
  434. mon.write("Programm beendet.")
  435. end
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442. -- [3] Auto-Start Bildschirm festlegen
  443. local function promptAutoStart()
  444.   clear()
  445.   print("Auto-Start Bildschirm festlegen:\n")
  446.   local screens = listScreens()
  447.   for i, s in ipairs(screens) do
  448.     print(i .. ") " .. s)
  449.   end
  450.   io.write("\nNummer eingeben: ")
  451.   local n = tonumber(read())
  452.   if n and screens[n] then
  453.     local file = fs.open("auto_start", "w")
  454.     file.write(screens[n])
  455.     file.close()
  456.     term.setTextColor(colors.green)
  457.     print("\nGespeichert! Beim nächsten Start wird dieser Bildschirm automatisch angezeigt.")
  458.     term.setTextColor(colors.lightBlue)
  459.   else
  460.     term.setTextColor(colors.red)
  461.     print("Ungültige Eingabe.")
  462.     term.setTextColor(colors.lightBlue)
  463.   end
  464.   waitEnter()
  465. end
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472. -- [4] Gespeicherten Bildschirm löschen
  473. local function deleteScreen()
  474.   clear()
  475.   print("Screen löschen:\n")
  476.   local screens = listScreens()
  477.   for i, s in ipairs(screens) do
  478.     print(i .. ") " .. s)
  479.   end
  480.   io.write("\nNummer eingeben: ")
  481.   local n = tonumber(read())
  482.   if n and screens[n] then
  483.     fs.delete("screens/" .. screens[n])
  484.     term.setTextColor(colors.green)
  485.     print("\nBildschirm gelöscht.")
  486.     term.setTextColor(colors.lightBlue)
  487.   else
  488.     term.setTextColor(colors.red)
  489.     print("Ungültige Eingabe.")
  490.     term.setTextColor(colors.lightBlue)
  491.   end
  492.   waitEnter()
  493. end
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500. -- [5] Hauptmenü
  501. local function main()
  502.   while true do
  503.     clear()
  504.     print([[
  505. Willkommen im Bildschirm - Konfigurator
  506.  
  507. Du kannst ohne Programmierkenntnisse folgendes tun:
  508.  
  509. 1) Gespeicherten Bildschirm anschauen      
  510. 2) Neuen Bildschirm konfigurieren          
  511. 3) Auto-Start für Bildschirm festlegen    
  512. 4) Gespeicherten Bildschirm löschen        
  513. 5) Beenden                                
  514.  
  515. ]])
  516.     io.write("Bitte Zahl eingeben und [Enter] drücken: ")
  517.     local input = read()
  518.  
  519.     if input == "1" then promptScreenStart()         -- [1]
  520.     elseif input == "2" then screenConfig()          -- [2]
  521.     elseif input == "3" then promptAutoStart()       -- [3]
  522.     elseif input == "4" then deleteScreen()          -- [4]
  523.     elseif input == "5" then                         -- [5]
  524.       print("Beende Menü...")
  525.       sleep(1)
  526.       break
  527.     else
  528.       term.setTextColor(colors.red)
  529.       print("Ungültige Eingabe.")
  530.       term.setTextColor(colors.lightBlue)
  531.       waitEnter()
  532.     end
  533.   end
  534. end
  535.  
  536. main()
  537.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement