Advertisement
Pirnogion

TextWriter

Feb 7th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. --ПЕРЕМЕННЫЕ--
  2. local CurrentMonitor, Monitors
  3. local CursorPosX, CursorPosY = 1, 1
  4. local BGColor, TextColor, Scale = colors.black, colors.white, 3
  5.  
  6. --ВСПОМОГАТЕЛЬНЫЕ ФУНКЦИИ--
  7. function PointInRect(x, y, sx, sy, ex, ey)
  8.     if (x >= sx) and (x <= ex) and (y >= sy) and (y <= ey) then return true end
  9.    
  10.     return false
  11. end
  12.  
  13. --ПРОГРАММА--
  14.  
  15. --Получить относительные координаты точки по абсолютным координатам
  16. function GetRelativeCursorPos(ax, ay)
  17.     local MonitorID, PosX, PosY
  18.    
  19.     for j = 1, Monitors["monitorsCountInHeight"], 1 do
  20.         for i = 1, Monitors["monitorsCountInWidth"], 1 do
  21.             if PointInRect(ax, ay, Monitors[j][i]["xStart"], Monitors[j][i]["yStart"], Monitors[j][i]["xEnd"], Monitors[j][i]["yEnd"]) then
  22.                 PosX = ax - Monitors[j][i]["xStart"] + 1
  23.                 PosY = ay - Monitors[j][i]["yStart"] + 1
  24.                 MonitorID = Monitors[j][i]["id"]
  25.                 return MonitorID, PosX, PosY, i, j
  26.             end
  27.         end
  28.     end
  29.    
  30.     return nil
  31. end
  32.  
  33. --Получить абсолютные координаты точки по относительным координатам
  34. function GetAbsoluteCursorPos(MonitorID, rx, ry)
  35.     local PosX, PosY
  36.  
  37.     for j = 1, Monitors["monitorsCountInHeight"], 1 do
  38.         for i = 1, Monitors["monitorsCountInWidth"], 1 do
  39.             if Monitors[j][i]["id"] == MonitorID then
  40.                 PosX = Monitors[j][i]["xStart"] + rx - 1
  41.                 PosY = Monitors[j][i]["yStart"] + ry - 1
  42.                 return PosX, PosY, i, j
  43.             end
  44.         end
  45.     end
  46.    
  47.     return nil
  48. end
  49.  
  50. --Инициализация API
  51. function Init(ConfigFilePath)
  52.     local _cfg = ConfigFilePath or "System/ClusterCalibration.cfg"
  53.     if fs.exists(_cfg) then error("Configuration file not found.") end
  54.    
  55.     file = fs.open(_cfg, "r")
  56.     local _data = file.readAll(monitors)
  57.     file.close()
  58.    
  59.     Monitors = textutils.unserialise(_data)
  60.     SetMonitorParameters(nil, nil, Monitors["monitorsScale"])
  61.     SetCursorPos(CursorPosX, CursorPosY)
  62. end
  63.  
  64. --Установка абсолютной точки
  65. function SetCursorPos(ax, ay)
  66.     CursorPosX, CursorPosY = ax, ay
  67.    
  68.     local _m, _rx, _ry = GetRelativeCursorPos(CursorPosX, CursorPosY)
  69.     CurrentMonitor = peripheral.wrap(_m)
  70.     CurrentMonitor.setCursorPos(_rx, _ry)
  71. end
  72.  
  73. --Вывод текста
  74. function WriteText(...)
  75.     local str = ""
  76.  
  77.     for i = 1, #arg, 1 do
  78.         str = str .. tostring(arg[i])
  79.     end
  80.  
  81.     for s in str:gmatch(".") do
  82.         if CursorPosY > Monitors["totalMonitorSizeY"] then
  83.             break
  84.         elseif s == '\n' then
  85.             CursorPosX = 0
  86.             CursorPosY = CursorPosY + 1
  87.             SetCursorPos(CursorPosX, CursorPosY)
  88.         elseif CursorPosX + 1 > Monitors["totalMonitorSizeX"] then
  89.             CurrentMonitor.write(s)
  90.             CursorPosX = 1
  91.             CursorPosY = CursorPosY + 1
  92.             SetCursorPos(CursorPosX, CursorPosY)
  93.         else
  94.             CurrentMonitor.write(s)
  95.             CursorPosX = CursorPosX + 1
  96.             SetCursorPos(CursorPosX, CursorPosY)
  97.         end
  98.     end
  99. end
  100.  
  101. --Установка цветов\размера символов
  102. function SetMonitorParameters(backgroundcolor, textcolor, scale)
  103.     BGColor = backgroundcolor or BGColor
  104.     TextColor = textcolor or TextColor
  105.     Scale = scale or Scale
  106.    
  107.     for j = 1, Monitors["monitorsCountInHeight"], 1 do
  108.         for i = 1, Monitors["monitorsCountInWidth"], 1 do
  109.             local _m = peripheral.wrap(Monitors[j][i]["id"])
  110.             _m.setBackgroundColor(BGColor)
  111.             _m.setTextColor(TextColor)
  112.             _m.setTextScale(Scale)
  113.         end
  114.     end
  115. end
  116.  
  117. --Очистка
  118. function ClearAll()
  119.     for j = 1, Monitors["monitorsCountInHeight"], 1 do
  120.         for i = 1, Monitors["monitorsCountInWidth"], 1 do
  121.             local _m = peripheral.wrap(Monitors[j][i]["id"])
  122.             _m.setBackgroundColor(BGColor)
  123.             _m.setTextColor(TextColor)
  124.             _m.clear()
  125.         end
  126.     end
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement