Advertisement
melzneni

monitor_mUnit

Apr 11th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. local args = { ... }
  2.  
  3. os.loadAPI("vislib")
  4.  
  5. local m = peripheral.find("monitor")
  6. m.setTextScale(0.5)
  7. local monX, monY = m.getSize()
  8.  
  9. local sx = math.floor(0.047 * monX + 0.8)
  10. local sy = math.floor(0.07 * monY + 0.8)
  11.  
  12. local scrW = 16 * sx - 4
  13. local scrH = 16 * sy - 4
  14.  
  15. local scrResX = (scrH / monY) / (scrW / monX)
  16. local f = monY / scrH
  17. local scrResX = scrResX * f
  18. local scrResY = f
  19.  
  20. function round(num)
  21. return math.floor(num + 0.4)
  22. end
  23.  
  24. function drawLine(x1, y1, x2, y2, color)
  25. m.setBackgroundColor(color)
  26. x1 = round(x1 * scrResX)
  27. x2 = round(x2 * scrResX)
  28. y1 = round(y1 * scrResY)
  29. y2 = round(y2 * scrResY)
  30.  
  31. if math.abs(x2 - x1) > math.abs(y2 - y1) then
  32. for x = math.min(x1, x2), math.max(x1, x2) do
  33. if x > 0 and x <= monX then
  34. local y = round(y1 + 1.0 * (x - x1) * (y2 - y1) / (x2 - x1))
  35. if y > 0 and y <= monY then
  36. m.setCursorPos(x, y)
  37. m.write(" ")
  38. end
  39. end
  40. end
  41. else
  42. for y = math.min(y1, y2), math.max(y1, y2) do
  43. local x = x1 + 1.0 * (y - y1) * (x2 - x1) / (y2 - y1)
  44. m.setCursorPos(x, y)
  45. m.write(" ")
  46. end
  47. end
  48. end
  49.  
  50. function drawRect(x, y, w, h, c)
  51. m.setBackgroundColor(c)
  52. local x1 = round((x + w) * scrResX)
  53. x = round(x * scrResX)
  54. local y1 = round((y + h) * scrResY)
  55. y = round(y * scrResY)
  56.  
  57. if 0 < x1 and x <= monX and 0 < y1 and y <= monY then
  58. if x < 0 then x = 0 end
  59. if y < 0 then y = 0 end
  60. if x1 > monX then x1 = monX end
  61. if y1 > monY then y1 = monY end
  62. local txt = ""
  63. for x2 = x, x1 do
  64. txt = txt .. " "
  65. end
  66. for y2 = y, y1 do
  67. m.setCursorPos(x, y2)
  68. m.write(txt)
  69. end
  70. end
  71. end
  72.  
  73. function getSize()
  74. local x, y = m.getSize()
  75. return x / scrResX, y / scrResY
  76. end
  77.  
  78. function clear()
  79. m.setBackgroundColor(colors.black)
  80. m.clear()
  81. end
  82.  
  83. if #args < 3 then error("3 arguments expected") end
  84.  
  85. local screenName = args[1]
  86. local screenX = 0 --tonumber(args[2])
  87. local screenY = 0 --tonumber(args[3])
  88. local screenCX = tonumber(args[2])
  89. local screenCY = tonumber(args[3])
  90.  
  91. local screenW, screenH = getSize()
  92.  
  93. print("screen[" .. screenName .. "]: ", screenX, ", ", screenY, ", ", screenW, ", ", screenH)
  94.  
  95. rednet.open("top")
  96.  
  97. function main()
  98. local id, msg = vislib.receiveRednet()
  99. local tag, pts = vislib.getMsgData(msg)
  100. if tag == "@mon" and pts[1] == screenName then
  101. if pts[2] == "clear" then
  102. clear()
  103. elseif pts[2] == "line" then
  104. drawLine(tonumber(pts[3]) - screenX, tonumber(pts[4]) - screenY,
  105. tonumber(pts[5]) - screenX, tonumber(pts[6]) - screenY,
  106. tonumber(pts[7]))
  107. elseif pts[2] == "rect" then
  108. drawRect(tonumber(pts[3]) - screenX, tonumber(pts[4]) - screenY,
  109. tonumber(pts[5]), tonumber(pts[6]),
  110. tonumber(pts[7]))
  111. elseif pts[2] == "reboot" then
  112. os.reboot()
  113. elseif pts[2] == "sendData" then
  114. vislib.sendRednet(id, "@monman:" .. screenName .. ",monData," .. os.getComputerLabel() .. "," .. screenCX .. "," .. screenCY .. "," .. screenW .. "," .. screenH)
  115. elseif pts[2] == "setPos" then
  116. print("setPos:"..screenX..","..screenY)
  117. screenX = tonumber(pts[3])
  118. screenY = tonumber(pts[4])
  119. else print("unknown message: ", msg)
  120. end
  121. end
  122. end
  123.  
  124. while true do
  125. local status, msg = pcall(main)
  126. if not status then
  127. if msg=="Terminated" then return end
  128. print("err: " .. msg)
  129. end
  130. end
  131.  
  132. --pastebin run 0YB9PsQV startup={files={vislib=<pb:GYvMFCY5>,mon=<pb:aTJkv2dh>},cmds={{'mon',<input:'monitorName'>,<input:'x'>,<input:'y'>}}} label=<input:'Label'> reboot=true
  133. --cp disk/pastebin pastebin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement