Advertisement
Pirnogion

LAPIPack

Jan 13th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.90 KB | None | 0 0
  1. <GluePath>LAPIPrograms/LDraw
  2. --Сторонние библиотеки
  3. os.loadAPI("LayerAPI/layerobj")
  4. os.loadAPI("LayerAPI/layermanager")
  5. os.loadAPI("LayerAPI/FIO")
  6. os.loadAPI("LayerAPI/LAPIShell")
  7. os.loadAPI("LayerAPI/PullEventAdvanced")
  8. os.loadAPI("LayerAPI/BottomCtrlMenu")
  9.  
  10. local args      = {...} --РЎРїРёСЃРѕРє аргументов
  11. local visGUI    = true  --Отображать ли GUI?
  12. local isMonitor = false --Монитор или терминал?
  13. local running = true
  14.  
  15. --Если первый аргумент существует, то отметить флаг инициализации программы в режиме монитора
  16. if (args[1] == true) then
  17.     isMonitor = true
  18.     error("Monitor not supported!")
  19. end
  20.  
  21. --Техническое--
  22. local tx, ty = term.getSize() --Разрешение устройства, РЅР° которое осуществляется вывод графической информации(Р“Р?)
  23.  
  24. local OutputLayer = layerobj.tLayer:Init(tx, ty, "Output") --Буферный слой, РЅР° который выводится РІСЃСЏ Р“Р? СЃ РґСЂСѓРіРёС… слоев;
  25.                                                      --это нужно для того, чтобы процесс отрисовки РЅРµ отображался РЅР° устройстве,
  26.                                                      --Р° сразу выводилась цельная картинка.
  27.  
  28. local BGLayer   = layerobj.tLayer:Init(tx, ty, "Background") --Слой заднего фона.
  29. local HostLayer = layerobj.tLayer:Init(tx, ty, "Host"      ) --Слой РЅР° котором осуществляется рисование кистями.
  30. local GUILayer  = layerobj.tLayer:Init(tx, ty, "GUI"       ) --Слой графического интерфейса.
  31.  
  32. --Регистрация слоев, в данный момент устанавливается их z позиция(очередность отрисовки).
  33. layermanager.RegisterLayer(BGLayer)
  34. layermanager.RegisterLayer(HostLayer)
  35. layermanager.RegisterLayer(GUILayer)
  36.  
  37. --Функции меню--
  38. local function Exit()
  39.     term.setBackgroundColor(colors.blue)
  40.     term.setTextColor(colors.white)
  41.     term.clear()
  42.     term.setCursorPos(tx/2-4, ty/2)
  43.    
  44.     print("Goodbye.")
  45.     sleep(1)
  46.    
  47.     running = false
  48. end
  49.  
  50. local function NewFile()
  51.     print("Does not work!")
  52.     sleep(1)
  53. end
  54.  
  55. local function OpenFile()
  56.     write("Enter file name: ")
  57.     local _file = read()
  58.    
  59.     if (_file and type(_file) == "string" and fs.exists(_file)) then
  60.         FIO.ReadFromFile(_file, HostLayer)
  61.     else
  62.         print("File not found.")
  63.         sleep(1)
  64.     end
  65. end
  66.  
  67. local function SaveFile()
  68.     write("Enter file name: ")
  69.     local _file = read()
  70.    
  71.     if (_file ~= nil and _file ~= "" and type(_file) == "string" and not fs.exists(_file)) then
  72.         FIO.SaveToFile(_file, HostLayer)
  73.     elseif (not fs.exists(_file)) then
  74.         print("This file already exists. Overwrite? y/n")
  75.         local _, key = os.pullEvent("char")
  76.         if (key == "y") then
  77.             FIO.SaveToFile(_file, HostLayer)
  78.         end
  79.     else
  80.         print("Error.")
  81.         sleep(1)
  82.     end
  83. end
  84.  
  85. local function ClearHost()
  86.     HostLayer:ClearLayer()
  87. end
  88.  
  89. --Главное меню данной программы, которое открывается\закрывается по нажатию right ctrl снизу экрана.
  90. BottomCtrlMenu.AddItem("New"  , NewFile  )
  91. BottomCtrlMenu.AddItem("Open" , OpenFile )
  92. BottomCtrlMenu.AddItem("Save" , SaveFile )
  93. BottomCtrlMenu.AddItem("Clear", ClearHost)
  94. BottomCtrlMenu.AddItem("Exit" , Exit     )
  95.  
  96. --Параметры кистей--
  97. --Цвета бэкграцнда
  98. local LBC  = colors.white --Цвет бэкграунда символа РЅР° ЛКМ.
  99. local RBC  = colors.black --Цвет бэкграунда символа РЅР° РџРљРњ.
  100. local LBTC = colors.black --Цвет символа на ЛКМ.
  101. local RBTC = colors.white --Цвет символа на ПКМ.
  102. local char = " "          --Заполняющий СЃРёРјРІРѕР».
  103.  
  104. --Р?нициализация--
  105. term.clear()            --Очистка экрана.
  106. term.setCursorPos(tx/2-5, ty/2) --Установка курсора в позицию в девом верхнем углу экрана.
  107.  
  108. write("Loading...")
  109.  
  110. FIO.ReadFromFile("LAPIPrograms/LDrawBackground.lyr", BGLayer) --Чтение из файла информации для слоя заднего фона.
  111. FIO.ReadFromFile("LAPIPrograms/LDrawGUI.lyr", GUILayer)       --Чтение РёР· файла информации для слоя графического интерфеса.
  112.  
  113. GUILayer:SetPixel(1, 1, char, LBTC, LBC) --Вывод информации о текущем состоянии кисти на ЛКМ.
  114. GUILayer:SetPixel(2, 1, char, RBTC, RBC) --Вывод информации о текущем состоянии кисти на ПКМ.
  115. BottomCtrlMenu.Draw(GUILayer)            --Вывод меню РЅР° слой GUI.
  116. --sleep(1)
  117.  
  118. layermanager.DrawLayers(OutputLayer, {[GUILayer] = not visGUI}) --Вывод всех слоев на буферный слой с пропуском GUI при установке флага true.
  119. layermanager.DrawLayerOnDevice(OutputLayer, term)               --Вывод буферного слоя РЅР° устройство.
  120.  
  121. --Секция для term--
  122. --Отрисовка GUI если флаг отрисовки GUI равен true.
  123. local function TermGUI()
  124.     if (not visGUI) then return end
  125.     GUILayer:SetPixel(1, 1, char, LBTC, LBC)
  126.     GUILayer:SetPixel(2, 1, char, RBTC, RBC)
  127. end
  128.  
  129. --Обработка событий GUI если флаг отрисовки GUI равен true.
  130. local function TermInteractive(event, button, x, y)
  131.     if not visGUI then return false end
  132.    
  133.     if (event == "mouse_double_click") then      --РџСЂРё РґРІРѕР№РЅРѕРј клике выбирается цвет символа.
  134.         if (y == 1 and (x > 4 and x < 21) ) then --Цвет выбирается только если клик был совершен РЅР° первой строке РѕС‚ 4 символа РґРѕ 21.
  135.             if (button == 1) then                --Если нажата ЛКМ, то выбрать цвета для кисти ЛКМ.
  136.                 LBTC = 2^(x-5)
  137.             elseif (button == 2) then            --Если нажата РџРљРњ, то выбрать цвета для кисти РџРљРњ.
  138.                 RBTC = 2^(x-5)
  139.             end
  140.            
  141.             return true
  142.         end
  143.     elseif (event == "mouse_click") then         --Р?наче РїСЂРё одиночном клике выбрать цвет фона символа.
  144.         if (y == 1 and (x > 4 and x < 21) ) then
  145.             if (button == 1) then
  146.                 LBC = 2^(x-5)
  147.             elseif (button == 2) then
  148.                 RBC = 2^(x-5)
  149.             end
  150.            
  151.             return true
  152.         end
  153.     end
  154.    
  155.     if (x == 7 and y == 2) then                  --Установить ластик РЅР° ЛКМ или РџРљРњ РїСЂРё клике РЅР° 7 СЃРёРјРІРѕР» РІРѕ второй строке.
  156.         if (button == 1) then LBC = 0 elseif (button == 2) then RBC = 0 end
  157.        
  158.         return true
  159.     end
  160.    
  161.     return false
  162. end
  163.  
  164. --Секция для монитора--
  165. local function MonitorGUI()
  166.  
  167. end
  168.  
  169. local function MonitorInteractive(x, y)
  170.    
  171. end
  172.  
  173. local function MonitorMainMenu()
  174.    
  175. end
  176.  
  177. --Главный цикл программы
  178. local Main = function()
  179.     while running do
  180.         local event, button, x, y = os.pullEvent()
  181.        
  182.         if event == "redraw" then
  183.             layermanager.DrawLayers(OutputLayer, {[GUILayer] = not visGUI})  --Баг: Бесполезная перерисовка.
  184.             layermanager.DrawLayerOnDevice(OutputLayer, term)
  185.         end
  186.        
  187.         if (event == "mouse_double_click" or event == "mouse_click" or event == "mouse_drag") and x and y then
  188.             local _lr  = layermanager.LayerClick(event, x, y)
  189.             local _c, _tx, _bc = HostLayer:GetPixel(x, y)
  190.             if TermInteractive(event, button, x, y) then TermGUI() os.queueEvent("redraw") end
  191.            
  192.             if (_lr and _lr ~= GUILayer or not visGUI) then
  193.                 if button == 1 then
  194.                     HostLayer:SetPixel(x, y, char, LBTC, LBC)
  195.                     LAPIShell.Fast_DrawPixel(x, y, char, LBTC, LBC, BGLayer)
  196.                 elseif button == 2 then
  197.                     HostLayer:SetPixel(x, y, char, RBTC, RBC)
  198.                     LAPIShell.Fast_DrawPixel(x, y, char, RBTC, RBC, BGLayer)
  199.                 end
  200.             end
  201.         end
  202.        
  203.         if (event == "char") then
  204.             char = button
  205.             TermGUI()
  206.         end
  207.        
  208.         if (event == "key") then
  209.             if (button == 15) then
  210.                 visGUI = not visGUI
  211.             elseif (button == 205) then
  212.                 BottomCtrlMenu.ShiftSelector(1)
  213.                 BottomCtrlMenu.Draw(GUILayer)
  214.             elseif button == 203 then
  215.                 BottomCtrlMenu.ShiftSelector(-1)
  216.                 BottomCtrlMenu.Draw(GUILayer)
  217.             elseif (visGUI and button == 28) then
  218.                 BottomCtrlMenu.CallItem()
  219.             end
  220.    
  221.             os.queueEvent("redraw")
  222.         end
  223.        
  224.        
  225.     end
  226. end
  227.  
  228. Main() --Запуск программы
  229.  
  230. term.setBackgroundColor(colors.black)
  231. term.setTextColor(colors.white)
  232. term.clear()
  233. term.setCursorPos(1, 1)
  234.  
  235. --LAPIPrograms/LDraw
  236. <GluePath>LAPIPrograms/LDrawBackground.lyr
  237. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  238. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  239. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  240. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  241. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  242. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  243. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  244. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  245. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  246. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  247. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  248. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  249. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  250. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  251. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  252. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  253. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  254. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  255. +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
  256. eof
  257. <GluePath>LAPIPrograms/LDrawGUI.lyr
  258.  07 07 07 07 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07
  259. tf7of7of7lf7sf7 07Xf0 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07
  260.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  261.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  262.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  263.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  264.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  265.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  266.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  267.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  268.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  269.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  270.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  271.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  272.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  273.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  274.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  275.  xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
  276.  07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07 07
  277. eof
  278. <GluePath>LayerAPI/BottomCtrlMenu
  279. local buttons = {}
  280. local selector = 1
  281.  
  282. local xt, ty = term.getSize()
  283.  
  284. function Draw(layer)
  285.     local _str = ""
  286.    
  287.     for i = 1, #buttons, 1 do
  288.         if (i == selector) then
  289.             _str = _str .. '[' .. buttons[i]["name"] .. ']' .. ' '
  290.         else
  291.             _str = _str .. buttons[i]["name"] .. ' '
  292.         end
  293.     end
  294.     layer:SetPixel(1, ty, _str, colors.white, colors.gray)
  295. end
  296.  
  297. function AddItem(name, func)
  298.     if (type(name) == "string" and type(func) == "function") then
  299.         table.insert(buttons, {})
  300.         buttons[#buttons]["name"] = name
  301.         buttons[#buttons]["func"] = func
  302.         buttons[#buttons]["active"] = true
  303.     end
  304. end
  305.  
  306. function CallItem()
  307.     buttons[selector]["func"]()
  308. end
  309.  
  310. function SetActive(name, value)
  311.     if (buttons[name] and type(value) == "boolean") then buttons[name]["active"] = value end
  312. end
  313.  
  314. function ShiftSelector(step)
  315.     local _counter = selector - 1
  316.     _counter = (_counter + step) % #buttons
  317.     selector = _counter + 1
  318. end
  319. <GluePath>LayerAPI/FIO
  320. ----------------------------
  321. -- Programm: FIO          --
  322. -- Author:  JaggerDer     --
  323. -- Created: --.--.14      --
  324. -- Modifed: 11.01.15      --
  325. ----------------------------
  326.  
  327. local tDecToPaint =
  328. {
  329.  ["0"]     = "x",
  330.  ["1"]     = "0",
  331.  ["2"]     = "1",
  332.  ["4"]     = "2",
  333.  ["8"]     = "3",
  334.  ["16"]    = "4",
  335.  ["32"]    = "5",
  336.  ["64"]    = "6",
  337.  ["128"]   = "7",
  338.  ["256"]   = "8",
  339.  ["512"]   = "9",
  340.  ["1024"]  = "a",
  341.  ["2048"]  = "b",
  342.  ["4096"]  = "c",
  343.  ["8192"]  = "d",
  344.  ["16384"] = "e",
  345.  ["32768"] = "f"
  346. }
  347.  
  348. local tPaintToDec =
  349. {
  350.  ["x"] =     0,
  351.  ["0"] =     1,
  352.  ["1"] =     2,
  353.  ["2"] =     4,
  354.  ["3"] =     8,
  355.  ["4"] =    16,
  356.  ["5"] =    32,
  357.  ["6"] =    64,
  358.  ["7"] =   128,
  359.  ["8"] =   256,
  360.  ["9"] =   512,
  361.  ["a"] =  1024,
  362.  ["b"] =  2048,
  363.  ["c"] =  4096,
  364.  ["d"] =  8192,
  365.  ["e"] = 16384,
  366.  ["f"] = 32768
  367. }
  368.  
  369. local ParseData = function(asData)
  370.  local _sChar = string.sub(asData, 1, 1)
  371.  local _nCharColor = string.sub(asData, 2, 2)
  372.  local _nColor = string.sub(asData, 3, 3)
  373.  
  374.  return _sChar, tPaintToDec[_nCharColor], tPaintToDec[_nColor]
  375. end
  376.  
  377. SaveToFile = function(asFilename, aloLayer)
  378.  _fFile = fs.open(asFilename, "w")
  379.  
  380.  local _, _nWidth, _nHeight = aloLayer:GetParameters()
  381.  local _sXLine = ""
  382.  
  383.  for i = 1, _nHeight, 1 do
  384.   for j = 1, _nWidth, 1 do
  385.     local _sText, _nTextColor, _nColor = aloLayer:GetPixel(j, i)
  386.    _sXLine = _sXLine .. _sText .. tDecToPaint[_nTextColor .. ""] .. tDecToPaint[_nColor .. ""]
  387.   end
  388.   _fFile.writeLine(_sXLine)
  389.   _sXLine = ""
  390.  end
  391.  
  392.  _fFile.writeLine("eof")
  393.  _fFile.close()
  394. end
  395.  
  396. ReadFromFile = function(asFilename, aloLayer)
  397.  local _sData = nil
  398.  local _sChar = nil
  399.  local _nCharColor = nil
  400.  local _nColor = nil
  401.  local _, _nWidth, _nHeight = aloLayer:GetParameters()
  402.  local _nX = 1
  403.  local _nY = 1
  404.  
  405.  if (fs.exists(asFilename)) then
  406.   _fFile = fs.open(asFilename, "r")
  407.  
  408.   while true do
  409.    _sData = _fFile.readLine()
  410.    if (_sData == "eof") then break end
  411.  
  412.    for inf in string.gmatch(_sData, "...") do
  413.     _sChar, _nCharColor, _nColor = ParseData(inf)
  414.    
  415.     aloLayer:SetPixel(_nX, _nY, _sChar, _nCharColor, _nColor)
  416.     if (_nX < _nWidth) then _nX = _nX + 1 else _nX = 1 _nY = _nY + 1 end
  417.    end
  418.   end
  419.  
  420.  _fFile.close()
  421.  end
  422. end
  423. <GluePath>LayerAPI/LAPIShell
  424. ----------------------------
  425. -- Programm: LAPIShell    --
  426. -- Author:  JaggerDer     --
  427. -- Created: --.--.14      --
  428. -- Modifed: 11.01.15      --
  429. ----------------------------
  430.  
  431. Fast_DrawPixel = function(x, y, char, textcolor, color, bglayer)
  432.     local _char, _textcolor, _color = char, textcolor, color
  433.     if color == 0 then
  434.         _char, _textcolor, _color = bglayer:GetPixel(x, y)
  435.     end
  436.  
  437.     term.setCursorPos(x, y)
  438.     term.setTextColor(_textcolor)
  439.     term.setBackgroundColor(_color)
  440.     write(_char)
  441. end
  442.  
  443. DrawLine = function(anX1, anY1, anX2, anY2, asChar, anCharColor, anColor, aloLayer)
  444.  local _nx1 = anX1
  445.  local _ny1 = anY1
  446.  local _nx2 = anX2
  447.  local _ny2 = anY2
  448.  
  449.  local _nSteep = math.abs(anY2 - anY1) > math.abs(anX2 - anX1)
  450.  if (_nSteep) then
  451.   _nx1 = anY1
  452.   _ny1 = anX1
  453.   _nx2 = anY2
  454.   _ny2 = anX2
  455.  end
  456.  if (_nx1 > _nx2) then
  457.   _nx1 = anX2
  458.   _nx2 = anX1
  459.   _ny1 = anY2
  460.   _ny2 = anY1
  461.  end
  462.  
  463.  local _ndx = _nx2 - _nx1
  464.  local _ndy = math.abs(_ny2 - _ny1)
  465.  local _nerr = _ndx / 2
  466.  local _nystep = -1
  467.  if (_ny1 < _ny2) then _nystep = 1 end
  468.  
  469.  local _ny = _ny1
  470.  local _nx = _nx1
  471.  
  472.  while (_nx < _nx2) do
  473.   aloLayer:SetPixel(_nx, _ny, asChar, anCharColor, anColor)
  474.  
  475.   _nerr = _nerr - _ndy
  476.   if (_nerr < 0) then
  477.    _ny = _ny + _nystep
  478.    _nerr = _nerr + _ndx
  479.   end
  480.  
  481.   _nx = _nx + 1
  482.  end
  483. end
  484.  
  485. DrawBox = function(anX1, anY1, anX2, anY2, asChar, anCharColor, anColor, aloLayer)
  486.  for y = anY1, anY2, 1 do
  487.   for x = anX1, anX2, 1 do
  488.    aloLayer:SetPixel(x, y, asChar, anCharColor, anColor)
  489.   end
  490.  end
  491. end
  492.  
  493. DrawCircle = function(anX, anY, anR, asText, anTextColor, anColor, anLayerName)
  494.  
  495. end
  496. <GluePath>LayerAPI/layermanager
  497. ----------------------------
  498. -- Programm: LayerManager --
  499. -- Author:  JaggerDer     --
  500. -- Created: --.--.14      --
  501. -- Modifed: 11.01.15      --
  502. ----------------------------
  503.  
  504. --Зпрегестрированные слои
  505. local tLayers = {}
  506.  
  507. --Зарагестрировать слой
  508. RegisterLayer = function(aloLayer)
  509.  if (type(aloLayer) ~= "table" or not aloLayer:GetParameters()) then
  510.   error("Error: expected LayerObj.")
  511.  end
  512.  
  513.  table.insert(tLayers, aloLayer)
  514.  return #tLayers
  515. end
  516.  
  517. --Удалить слой
  518. UnregisterLayer = function(anID)
  519.  if (type(anID) ~= "number") then
  520.   error("Error: expected number, got " .. type(anID) .. ".")
  521.  elseif (anID > #tLayers or anID < 1) then
  522.   error("ID out of range: [layers] > [id] > 0")
  523.  end
  524.  
  525.  table.remove(tLayers, anID)
  526. end
  527.  
  528. --Отобразить все слои на экране
  529. DrawLayers = function(Output, skip)
  530.  for i, v in pairs(tLayers) do
  531.   if (not (skip and skip[v]) ) then
  532.    DrawLayerOnOutput(v, Output)
  533.   end
  534.  end
  535. end
  536.  
  537. DrawLayerOnOutput = function(aloLayer, Output)
  538.     local _, _nWidth, _nHeight, _nXs, _nYs = aloLayer:GetParameters()
  539.    
  540.     for y = 1, _nHeight, 1 do
  541.         for x = 1, _nWidth, 1 do
  542.             local _sText, _nTextColor, _nColor = aloLayer:GetPixel(x, y)
  543.             if (_nColor > 0) then
  544.                 Output:SetPixel(x + _nXs, y + _nYs, _sText, _nTextColor, _nColor)
  545.             end
  546.         end
  547.     end
  548. end
  549.  
  550. --Отобразить конкретный слой на экране
  551. DrawLayerOnDevice = function(aloLayer, aDevice)
  552.  local _, _nWidth, _nHeight, _nXs, _nYs = aloLayer:GetParameters()
  553.  
  554.  for y = 1, _nHeight, 1 do
  555.   for x = 1, _nWidth, 1 do
  556.    local _sText, _nTextColor, _nColor = aloLayer:GetPixel(x, y)
  557.    if (_nColor > 0) then
  558.     aDevice.setCursorPos(x + _nXs, y + _nYs)
  559.     aDevice.setTextColor(_nTextColor)
  560.     aDevice.setBackgroundColor(_nColor)
  561.     aDevice.write(_sText)
  562.    end
  563.   end
  564.  end
  565.  
  566. end
  567.  
  568. --Сдвиг слоя по z координате
  569. ShiftLayer = function(anID, asDir, anStep)
  570.  if (type(anID) ~= "number" or type(asDir) ~= "string" or type(anStep) ~= "number") then
  571.   error("Usage: <number> <string> <number>")
  572.  elseif ( (anID > #tLayers or anID < 1) or (asDir ~= "left" and asDir ~= "right") or ( (asDir == "right" and anID+anStep > #tLayers) or ( asDir == "left" and anID-anStep < 1) ) ) then
  573.   error("Arguments out of range: [layers] > [ID] > 0 ; shift direction only left or right; [steps]+[id] > [layers] and [steps]-[id] > 0")
  574.  end
  575.  
  576.  _loBf = tLayers[anID]
  577.  
  578.  if (asDir == "left") then
  579.   tLayers[anID] = tLayers[anID-anStep]
  580.   tLayers[anID-anStep] = _loBf
  581.  elseif (asDir == "right") then
  582.   tLayers[anID] = tLayers[anID+anStep]
  583.   tLayers[anID+anStep] = _loBf
  584.  end
  585. end
  586.  
  587. --...
  588. LayerClick = function(event, anX, anY)
  589.  if (event ~= "mouse_click" and event ~= "mouse_drag" and event ~= "mouse_double_click") then return nil end
  590.  
  591.  for i = #tLayers, 1, -1 do
  592.   local _sName, _, _, _nXs, _nYs = tLayers[i]:GetParameters()
  593.   local _sText, _nTextColor, _nColor = tLayers[i]:GetPixel(anX + _nXs, anY + _nYs)
  594.   if (_nColor > 0) then
  595.     return tLayers[i], _sName, _sText, _nTextColor, _nColor
  596.   end
  597.  end
  598.  
  599.  return nil
  600. end
  601.  
  602. --Дебаг
  603. Info = function()
  604.  term.setTextColor(colors.white)
  605.  term.setBackgroundColor(colors.black)
  606.  term.setCursorPos(1, 1)
  607.  term.clear()
  608.  
  609.  for i, v in pairs(tLayers) do
  610.   print("ID: ", i, ". Name: ", v:GetParameters(), ".")
  611.  end
  612. end
  613. <GluePath>LayerAPI/layerobj
  614. ------------------------
  615. -- Object: tLayer     --
  616. -- Author:  JaggerDer --
  617. -- Created: --.--.14  --
  618. -- Modifed: 11.01.15  --
  619. ------------------------
  620.  
  621. tLayer = {}
  622.  
  623. function tLayer:Init(anWidth, anHeight, asName, abVis, abLck)
  624.  if (type(anWidth) ~= "number" or type(anHeight) ~= "number") then
  625.   error("Error: expected number, got " .. type(anWidth) or type(anHeight) .. ".")
  626.  elseif (type(asName) ~= "string") then
  627.   error("Error: expected string, got " .. type(asName) .. ".")
  628.  end
  629.  
  630.  tLayer = {}
  631.  
  632.  tLayer.tTextSpace = {}
  633.  tLayer.tTextColorSpace = {}
  634.  tLayer.tColorSpace = {}
  635.  
  636.  tLayer.sName      = asName   or "Default"
  637.  tLayer.nWidth     = anWidth  or 1
  638.  tLayer.nHeight    = anHeight or 1
  639.  tLayer.nShiftX    = 0
  640.  tLayer.nShiftY    = 0
  641.  
  642.  for y = 1, tLayer.nHeight, 1 do
  643.    tLayer.tColorSpace[y] = {}
  644.    tLayer.tTextSpace[y] = {}
  645.    tLayer.tTextColorSpace[y] = {}
  646.    for x = 1, tLayer.nWidth, 1 do
  647.     tLayer.tColorSpace[y][x] = 0
  648.     tLayer.tTextSpace[y][x] = " "
  649.     tLayer.tTextColorSpace[y][x] = 0
  650.    end
  651.  end
  652.  
  653.  setmetatable(tLayer, self)
  654.  self.__index = self
  655.  
  656.  return tLayer
  657. end
  658.  
  659. function tLayer:Remove()
  660.  self.sName = nil
  661.  self.tTextSpace = nil
  662.  self.tTextColorSpace = nil
  663.  self.tColorSpace = nil
  664.  
  665.  return true
  666. end
  667.  
  668. function tLayer:GetParameters()
  669.  return self.sName, self.nWidth, self.nHeight, self.nShiftX, self.nShiftY
  670. end
  671.  
  672. function tLayer:Shift(anShiftX, anShiftY)
  673.  self.nShiftX = anShiftX
  674.  self.nShiftY = anShiftY
  675. end
  676.  
  677. function tLayer:SetPixel(anX, anY, asText, anTextColor, anColor)
  678.  if (string.len(asText) > 1) then
  679.   for i = 1, string.len(asText), 1 do
  680.    self.tTextSpace[anY][anX+i-1] = string.char(string.byte(asText, i))
  681.    self.tTextColorSpace[anY][anX+i-1] = anTextColor
  682.    self.tColorSpace[anY][anX+i-1] = anColor
  683.   end
  684.  else
  685.   self.tTextSpace[anY][anX] = asText
  686.   self.tTextColorSpace[anY][anX] = anTextColor
  687.   self.tColorSpace[anY][anX] = anColor
  688.  end
  689. end
  690.  
  691. function tLayer:GetPixel(anX, anY)
  692.  return self.tTextSpace[anY][anX], self.tTextColorSpace[anY][anX], self.tColorSpace[anY][anX]
  693. end
  694.  
  695. function tLayer:ClearLayer()
  696. for y = 1, tLayer.nHeight, 1 do
  697.     self.tColorSpace[y] = {}
  698.     self.tTextSpace[y] = {}
  699.     self.tTextColorSpace[y] = {}
  700.     for x = 1, tLayer.nWidth, 1 do
  701.         self.tColorSpace[y][x] = 0
  702.         self.tTextSpace[y][x] = " "
  703.         self.tTextColorSpace[y][x] = 0
  704.     end
  705. end
  706.  
  707.  return true
  708. end
  709. <GluePath>LayerAPI/PullEventAdvanced
  710. local _dblclk = nil
  711. local _eventData = nil
  712.  
  713. function os.pullEvent( _sFilter )
  714.     local eventData = { os.pullEventRaw( _sFilter ) }
  715.    
  716.     if eventData[1] == "terminate" then
  717.         error( "Terminated", 0 )
  718.     elseif eventData[1] == "mouse_click" then
  719.         if (_dblclk) then
  720.             eventData = _eventData
  721.             eventData[1] = "mouse_double_click"
  722.            
  723.             _eventData = nil
  724.             _dblclk = nil
  725.         else
  726.             _dblclk = os.startTimer(0.16)
  727.             _eventData = eventData
  728.             return
  729.         end
  730.     elseif eventData[1] == "timer" then
  731.         if eventData[2] == _dblclk then
  732.             eventData = _eventData
  733.            
  734.             _eventData = nil
  735.             _dblclk = nil
  736.         end
  737.     end
  738.  
  739.     return unpack( eventData )
  740. end
  741.  
  742. Keys = function() --Баг: мышь не оиличается от цифр. Не передаются координаты клика.
  743.     local Events = {}
  744.     local EventsLifeTime = nil
  745.    
  746.     while true do
  747.         local event, keycode = os.pullEvent()
  748.        
  749.         if ( (event == "timer" and keycode == EventsLifeTime) or ((event == "key" or event == "mouse_click") and keycode == Events[#Events]) ) then
  750.             Events = {}
  751.         elseif (event == "key" or event == "mouse_click") then
  752.             table.insert(Events, keycode)
  753.             EventsLifeTime = os.startTimer(1)
  754.             if (#Events > 1) then
  755.                 os.queueEvent("keys", Events[1], Events[2])
  756.                 Events = {}
  757.             end
  758.         end
  759.     end
  760. end
  761. <eof>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement