Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <GluePath>LAPIPrograms/LDraw
- --Сторонние библиотеки
- os.loadAPI("LayerAPI/layerobj")
- os.loadAPI("LayerAPI/layermanager")
- os.loadAPI("LayerAPI/FIO")
- os.loadAPI("LayerAPI/LAPIShell")
- os.loadAPI("LayerAPI/PullEventAdvanced")
- os.loadAPI("LayerAPI/BottomCtrlMenu")
- local args = {...} --Список аргументов
- local visGUI = true --Отображать ли GUI?
- local isMonitor = false --Монитор или терминал?
- local running = true
- --Если первый аргумент существует, то отметить флаг инициализации программы в режиме монитора
- if (args[1] == true) then
- isMonitor = true
- error("Monitor not supported!")
- end
- --Техническое--
- local tx, ty = term.getSize() --Разрешение устройства, РЅР° которое осуществляется вывод графической информации(Р“Р?)
- local OutputLayer = layerobj.tLayer:Init(tx, ty, "Output") --Буферный слой, РЅР° который выводится РІСЃСЏ Р“Р? СЃ РґСЂСѓРіРёС… слоев;
- --это нужно для того, чтобы процесс отрисовки не отображался на устройстве,
- --а сразу выводилась цельная картинка.
- local BGLayer = layerobj.tLayer:Init(tx, ty, "Background") --Слой заднего фона.
- local HostLayer = layerobj.tLayer:Init(tx, ty, "Host" ) --Слой на котором осуществляется рисование кистями.
- local GUILayer = layerobj.tLayer:Init(tx, ty, "GUI" ) --Слой графического интерфейса.
- --Регистрация слоев, в данный момент устанавливается их z позиция(очередность отрисовки).
- layermanager.RegisterLayer(BGLayer)
- layermanager.RegisterLayer(HostLayer)
- layermanager.RegisterLayer(GUILayer)
- --Функции меню--
- local function Exit()
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(tx/2-4, ty/2)
- print("Goodbye.")
- sleep(1)
- running = false
- end
- local function NewFile()
- print("Does not work!")
- sleep(1)
- end
- local function OpenFile()
- write("Enter file name: ")
- local _file = read()
- if (_file and type(_file) == "string" and fs.exists(_file)) then
- FIO.ReadFromFile(_file, HostLayer)
- else
- print("File not found.")
- sleep(1)
- end
- end
- local function SaveFile()
- write("Enter file name: ")
- local _file = read()
- if (_file ~= nil and _file ~= "" and type(_file) == "string" and not fs.exists(_file)) then
- FIO.SaveToFile(_file, HostLayer)
- elseif (not fs.exists(_file)) then
- print("This file already exists. Overwrite? y/n")
- local _, key = os.pullEvent("char")
- if (key == "y") then
- FIO.SaveToFile(_file, HostLayer)
- end
- else
- print("Error.")
- sleep(1)
- end
- end
- local function ClearHost()
- HostLayer:ClearLayer()
- end
- --Главное меню данной программы, которое открывается\закрывается по нажатию right ctrl снизу экрана.
- BottomCtrlMenu.AddItem("New" , NewFile )
- BottomCtrlMenu.AddItem("Open" , OpenFile )
- BottomCtrlMenu.AddItem("Save" , SaveFile )
- BottomCtrlMenu.AddItem("Clear", ClearHost)
- BottomCtrlMenu.AddItem("Exit" , Exit )
- --Параметры кистей--
- --Цвета бэкграцнда
- local LBC = colors.white --Цвет бэкграунда символа на ЛКМ.
- local RBC = colors.black --Цвет бэкграунда символа на ПКМ.
- local LBTC = colors.black --Цвет символа на ЛКМ.
- local RBTC = colors.white --Цвет символа на ПКМ.
- local char = " " --Заполняющий символ.
- --Р?нициализация--
- term.clear() --Очистка экрана.
- term.setCursorPos(tx/2-5, ty/2) --Установка курсора в позицию в девом верхнем углу экрана.
- write("Loading...")
- FIO.ReadFromFile("LAPIPrograms/LDrawBackground.lyr", BGLayer) --Чтение из файла информации для слоя заднего фона.
- FIO.ReadFromFile("LAPIPrograms/LDrawGUI.lyr", GUILayer) --Чтение из файла информации для слоя графического интерфеса.
- GUILayer:SetPixel(1, 1, char, LBTC, LBC) --Вывод информации о текущем состоянии кисти на ЛКМ.
- GUILayer:SetPixel(2, 1, char, RBTC, RBC) --Вывод информации о текущем состоянии кисти на ПКМ.
- BottomCtrlMenu.Draw(GUILayer) --Вывод меню на слой GUI.
- --sleep(1)
- layermanager.DrawLayers(OutputLayer, {[GUILayer] = not visGUI}) --Вывод всех слоев на буферный слой с пропуском GUI при установке флага true.
- layermanager.DrawLayerOnDevice(OutputLayer, term) --Вывод буферного слоя на устройство.
- --Секция для term--
- --Отрисовка GUI если флаг отрисовки GUI равен true.
- local function TermGUI()
- if (not visGUI) then return end
- GUILayer:SetPixel(1, 1, char, LBTC, LBC)
- GUILayer:SetPixel(2, 1, char, RBTC, RBC)
- end
- --Обработка событий GUI если флаг отрисовки GUI равен true.
- local function TermInteractive(event, button, x, y)
- if not visGUI then return false end
- if (event == "mouse_double_click") then --При двойном клике выбирается цвет символа.
- if (y == 1 and (x > 4 and x < 21) ) then --Цвет выбирается только если клик был совершен на первой строке от 4 символа до 21.
- if (button == 1) then --Если нажата ЛКМ, то выбрать цвета для кисти ЛКМ.
- LBTC = 2^(x-5)
- elseif (button == 2) then --Если нажата ПКМ, то выбрать цвета для кисти ПКМ.
- RBTC = 2^(x-5)
- end
- return true
- end
- elseif (event == "mouse_click") then --Р?наче РїСЂРё одиночном клике выбрать цвет фона символа.
- if (y == 1 and (x > 4 and x < 21) ) then
- if (button == 1) then
- LBC = 2^(x-5)
- elseif (button == 2) then
- RBC = 2^(x-5)
- end
- return true
- end
- end
- if (x == 7 and y == 2) then --Установить ластик на ЛКМ или ПКМ при клике на 7 символ во второй строке.
- if (button == 1) then LBC = 0 elseif (button == 2) then RBC = 0 end
- return true
- end
- return false
- end
- --Секция для монитора--
- local function MonitorGUI()
- end
- local function MonitorInteractive(x, y)
- end
- local function MonitorMainMenu()
- end
- --Главный цикл программы
- local Main = function()
- while running do
- local event, button, x, y = os.pullEvent()
- if event == "redraw" then
- layermanager.DrawLayers(OutputLayer, {[GUILayer] = not visGUI}) --Баг: Бесполезная перерисовка.
- layermanager.DrawLayerOnDevice(OutputLayer, term)
- end
- if (event == "mouse_double_click" or event == "mouse_click" or event == "mouse_drag") and x and y then
- local _lr = layermanager.LayerClick(event, x, y)
- local _c, _tx, _bc = HostLayer:GetPixel(x, y)
- if TermInteractive(event, button, x, y) then TermGUI() os.queueEvent("redraw") end
- if (_lr and _lr ~= GUILayer or not visGUI) then
- if button == 1 then
- HostLayer:SetPixel(x, y, char, LBTC, LBC)
- LAPIShell.Fast_DrawPixel(x, y, char, LBTC, LBC, BGLayer)
- elseif button == 2 then
- HostLayer:SetPixel(x, y, char, RBTC, RBC)
- LAPIShell.Fast_DrawPixel(x, y, char, RBTC, RBC, BGLayer)
- end
- end
- end
- if (event == "char") then
- char = button
- TermGUI()
- end
- if (event == "key") then
- if (button == 15) then
- visGUI = not visGUI
- elseif (button == 205) then
- BottomCtrlMenu.ShiftSelector(1)
- BottomCtrlMenu.Draw(GUILayer)
- elseif button == 203 then
- BottomCtrlMenu.ShiftSelector(-1)
- BottomCtrlMenu.Draw(GUILayer)
- elseif (visGUI and button == 28) then
- BottomCtrlMenu.CallItem()
- end
- os.queueEvent("redraw")
- end
- end
- end
- Main() --Запуск программы
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- --LAPIPrograms/LDraw
- <GluePath>LAPIPrograms/LDrawBackground.lyr
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- +70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70+70
- eof
- <GluePath>LAPIPrograms/LDrawGUI.lyr
- 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
- 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
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
- 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
- eof
- <GluePath>LayerAPI/BottomCtrlMenu
- local buttons = {}
- local selector = 1
- local xt, ty = term.getSize()
- function Draw(layer)
- local _str = ""
- for i = 1, #buttons, 1 do
- if (i == selector) then
- _str = _str .. '[' .. buttons[i]["name"] .. ']' .. ' '
- else
- _str = _str .. buttons[i]["name"] .. ' '
- end
- end
- layer:SetPixel(1, ty, _str, colors.white, colors.gray)
- end
- function AddItem(name, func)
- if (type(name) == "string" and type(func) == "function") then
- table.insert(buttons, {})
- buttons[#buttons]["name"] = name
- buttons[#buttons]["func"] = func
- buttons[#buttons]["active"] = true
- end
- end
- function CallItem()
- buttons[selector]["func"]()
- end
- function SetActive(name, value)
- if (buttons[name] and type(value) == "boolean") then buttons[name]["active"] = value end
- end
- function ShiftSelector(step)
- local _counter = selector - 1
- _counter = (_counter + step) % #buttons
- selector = _counter + 1
- end
- <GluePath>LayerAPI/FIO
- ----------------------------
- -- Programm: FIO --
- -- Author: JaggerDer --
- -- Created: --.--.14 --
- -- Modifed: 11.01.15 --
- ----------------------------
- local tDecToPaint =
- {
- ["0"] = "x",
- ["1"] = "0",
- ["2"] = "1",
- ["4"] = "2",
- ["8"] = "3",
- ["16"] = "4",
- ["32"] = "5",
- ["64"] = "6",
- ["128"] = "7",
- ["256"] = "8",
- ["512"] = "9",
- ["1024"] = "a",
- ["2048"] = "b",
- ["4096"] = "c",
- ["8192"] = "d",
- ["16384"] = "e",
- ["32768"] = "f"
- }
- local tPaintToDec =
- {
- ["x"] = 0,
- ["0"] = 1,
- ["1"] = 2,
- ["2"] = 4,
- ["3"] = 8,
- ["4"] = 16,
- ["5"] = 32,
- ["6"] = 64,
- ["7"] = 128,
- ["8"] = 256,
- ["9"] = 512,
- ["a"] = 1024,
- ["b"] = 2048,
- ["c"] = 4096,
- ["d"] = 8192,
- ["e"] = 16384,
- ["f"] = 32768
- }
- local ParseData = function(asData)
- local _sChar = string.sub(asData, 1, 1)
- local _nCharColor = string.sub(asData, 2, 2)
- local _nColor = string.sub(asData, 3, 3)
- return _sChar, tPaintToDec[_nCharColor], tPaintToDec[_nColor]
- end
- SaveToFile = function(asFilename, aloLayer)
- _fFile = fs.open(asFilename, "w")
- local _, _nWidth, _nHeight = aloLayer:GetParameters()
- local _sXLine = ""
- for i = 1, _nHeight, 1 do
- for j = 1, _nWidth, 1 do
- local _sText, _nTextColor, _nColor = aloLayer:GetPixel(j, i)
- _sXLine = _sXLine .. _sText .. tDecToPaint[_nTextColor .. ""] .. tDecToPaint[_nColor .. ""]
- end
- _fFile.writeLine(_sXLine)
- _sXLine = ""
- end
- _fFile.writeLine("eof")
- _fFile.close()
- end
- ReadFromFile = function(asFilename, aloLayer)
- local _sData = nil
- local _sChar = nil
- local _nCharColor = nil
- local _nColor = nil
- local _, _nWidth, _nHeight = aloLayer:GetParameters()
- local _nX = 1
- local _nY = 1
- if (fs.exists(asFilename)) then
- _fFile = fs.open(asFilename, "r")
- while true do
- _sData = _fFile.readLine()
- if (_sData == "eof") then break end
- for inf in string.gmatch(_sData, "...") do
- _sChar, _nCharColor, _nColor = ParseData(inf)
- aloLayer:SetPixel(_nX, _nY, _sChar, _nCharColor, _nColor)
- if (_nX < _nWidth) then _nX = _nX + 1 else _nX = 1 _nY = _nY + 1 end
- end
- end
- _fFile.close()
- end
- end
- <GluePath>LayerAPI/LAPIShell
- ----------------------------
- -- Programm: LAPIShell --
- -- Author: JaggerDer --
- -- Created: --.--.14 --
- -- Modifed: 11.01.15 --
- ----------------------------
- Fast_DrawPixel = function(x, y, char, textcolor, color, bglayer)
- local _char, _textcolor, _color = char, textcolor, color
- if color == 0 then
- _char, _textcolor, _color = bglayer:GetPixel(x, y)
- end
- term.setCursorPos(x, y)
- term.setTextColor(_textcolor)
- term.setBackgroundColor(_color)
- write(_char)
- end
- DrawLine = function(anX1, anY1, anX2, anY2, asChar, anCharColor, anColor, aloLayer)
- local _nx1 = anX1
- local _ny1 = anY1
- local _nx2 = anX2
- local _ny2 = anY2
- local _nSteep = math.abs(anY2 - anY1) > math.abs(anX2 - anX1)
- if (_nSteep) then
- _nx1 = anY1
- _ny1 = anX1
- _nx2 = anY2
- _ny2 = anX2
- end
- if (_nx1 > _nx2) then
- _nx1 = anX2
- _nx2 = anX1
- _ny1 = anY2
- _ny2 = anY1
- end
- local _ndx = _nx2 - _nx1
- local _ndy = math.abs(_ny2 - _ny1)
- local _nerr = _ndx / 2
- local _nystep = -1
- if (_ny1 < _ny2) then _nystep = 1 end
- local _ny = _ny1
- local _nx = _nx1
- while (_nx < _nx2) do
- aloLayer:SetPixel(_nx, _ny, asChar, anCharColor, anColor)
- _nerr = _nerr - _ndy
- if (_nerr < 0) then
- _ny = _ny + _nystep
- _nerr = _nerr + _ndx
- end
- _nx = _nx + 1
- end
- end
- DrawBox = function(anX1, anY1, anX2, anY2, asChar, anCharColor, anColor, aloLayer)
- for y = anY1, anY2, 1 do
- for x = anX1, anX2, 1 do
- aloLayer:SetPixel(x, y, asChar, anCharColor, anColor)
- end
- end
- end
- DrawCircle = function(anX, anY, anR, asText, anTextColor, anColor, anLayerName)
- end
- <GluePath>LayerAPI/layermanager
- ----------------------------
- -- Programm: LayerManager --
- -- Author: JaggerDer --
- -- Created: --.--.14 --
- -- Modifed: 11.01.15 --
- ----------------------------
- --Зпрегестрированные слои
- local tLayers = {}
- --Зарагестрировать слой
- RegisterLayer = function(aloLayer)
- if (type(aloLayer) ~= "table" or not aloLayer:GetParameters()) then
- error("Error: expected LayerObj.")
- end
- table.insert(tLayers, aloLayer)
- return #tLayers
- end
- --Удалить слой
- UnregisterLayer = function(anID)
- if (type(anID) ~= "number") then
- error("Error: expected number, got " .. type(anID) .. ".")
- elseif (anID > #tLayers or anID < 1) then
- error("ID out of range: [layers] > [id] > 0")
- end
- table.remove(tLayers, anID)
- end
- --Отобразить все слои на экране
- DrawLayers = function(Output, skip)
- for i, v in pairs(tLayers) do
- if (not (skip and skip[v]) ) then
- DrawLayerOnOutput(v, Output)
- end
- end
- end
- DrawLayerOnOutput = function(aloLayer, Output)
- local _, _nWidth, _nHeight, _nXs, _nYs = aloLayer:GetParameters()
- for y = 1, _nHeight, 1 do
- for x = 1, _nWidth, 1 do
- local _sText, _nTextColor, _nColor = aloLayer:GetPixel(x, y)
- if (_nColor > 0) then
- Output:SetPixel(x + _nXs, y + _nYs, _sText, _nTextColor, _nColor)
- end
- end
- end
- end
- --Отобразить конкретный слой на экране
- DrawLayerOnDevice = function(aloLayer, aDevice)
- local _, _nWidth, _nHeight, _nXs, _nYs = aloLayer:GetParameters()
- for y = 1, _nHeight, 1 do
- for x = 1, _nWidth, 1 do
- local _sText, _nTextColor, _nColor = aloLayer:GetPixel(x, y)
- if (_nColor > 0) then
- aDevice.setCursorPos(x + _nXs, y + _nYs)
- aDevice.setTextColor(_nTextColor)
- aDevice.setBackgroundColor(_nColor)
- aDevice.write(_sText)
- end
- end
- end
- end
- --Сдвиг слоя по z координате
- ShiftLayer = function(anID, asDir, anStep)
- if (type(anID) ~= "number" or type(asDir) ~= "string" or type(anStep) ~= "number") then
- error("Usage: <number> <string> <number>")
- 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
- error("Arguments out of range: [layers] > [ID] > 0 ; shift direction only left or right; [steps]+[id] > [layers] and [steps]-[id] > 0")
- end
- _loBf = tLayers[anID]
- if (asDir == "left") then
- tLayers[anID] = tLayers[anID-anStep]
- tLayers[anID-anStep] = _loBf
- elseif (asDir == "right") then
- tLayers[anID] = tLayers[anID+anStep]
- tLayers[anID+anStep] = _loBf
- end
- end
- --...
- LayerClick = function(event, anX, anY)
- if (event ~= "mouse_click" and event ~= "mouse_drag" and event ~= "mouse_double_click") then return nil end
- for i = #tLayers, 1, -1 do
- local _sName, _, _, _nXs, _nYs = tLayers[i]:GetParameters()
- local _sText, _nTextColor, _nColor = tLayers[i]:GetPixel(anX + _nXs, anY + _nYs)
- if (_nColor > 0) then
- return tLayers[i], _sName, _sText, _nTextColor, _nColor
- end
- end
- return nil
- end
- --Дебаг
- Info = function()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, 1)
- term.clear()
- for i, v in pairs(tLayers) do
- print("ID: ", i, ". Name: ", v:GetParameters(), ".")
- end
- end
- <GluePath>LayerAPI/layerobj
- ------------------------
- -- Object: tLayer --
- -- Author: JaggerDer --
- -- Created: --.--.14 --
- -- Modifed: 11.01.15 --
- ------------------------
- tLayer = {}
- function tLayer:Init(anWidth, anHeight, asName, abVis, abLck)
- if (type(anWidth) ~= "number" or type(anHeight) ~= "number") then
- error("Error: expected number, got " .. type(anWidth) or type(anHeight) .. ".")
- elseif (type(asName) ~= "string") then
- error("Error: expected string, got " .. type(asName) .. ".")
- end
- tLayer = {}
- tLayer.tTextSpace = {}
- tLayer.tTextColorSpace = {}
- tLayer.tColorSpace = {}
- tLayer.sName = asName or "Default"
- tLayer.nWidth = anWidth or 1
- tLayer.nHeight = anHeight or 1
- tLayer.nShiftX = 0
- tLayer.nShiftY = 0
- for y = 1, tLayer.nHeight, 1 do
- tLayer.tColorSpace[y] = {}
- tLayer.tTextSpace[y] = {}
- tLayer.tTextColorSpace[y] = {}
- for x = 1, tLayer.nWidth, 1 do
- tLayer.tColorSpace[y][x] = 0
- tLayer.tTextSpace[y][x] = " "
- tLayer.tTextColorSpace[y][x] = 0
- end
- end
- setmetatable(tLayer, self)
- self.__index = self
- return tLayer
- end
- function tLayer:Remove()
- self.sName = nil
- self.tTextSpace = nil
- self.tTextColorSpace = nil
- self.tColorSpace = nil
- return true
- end
- function tLayer:GetParameters()
- return self.sName, self.nWidth, self.nHeight, self.nShiftX, self.nShiftY
- end
- function tLayer:Shift(anShiftX, anShiftY)
- self.nShiftX = anShiftX
- self.nShiftY = anShiftY
- end
- function tLayer:SetPixel(anX, anY, asText, anTextColor, anColor)
- if (string.len(asText) > 1) then
- for i = 1, string.len(asText), 1 do
- self.tTextSpace[anY][anX+i-1] = string.char(string.byte(asText, i))
- self.tTextColorSpace[anY][anX+i-1] = anTextColor
- self.tColorSpace[anY][anX+i-1] = anColor
- end
- else
- self.tTextSpace[anY][anX] = asText
- self.tTextColorSpace[anY][anX] = anTextColor
- self.tColorSpace[anY][anX] = anColor
- end
- end
- function tLayer:GetPixel(anX, anY)
- return self.tTextSpace[anY][anX], self.tTextColorSpace[anY][anX], self.tColorSpace[anY][anX]
- end
- function tLayer:ClearLayer()
- for y = 1, tLayer.nHeight, 1 do
- self.tColorSpace[y] = {}
- self.tTextSpace[y] = {}
- self.tTextColorSpace[y] = {}
- for x = 1, tLayer.nWidth, 1 do
- self.tColorSpace[y][x] = 0
- self.tTextSpace[y][x] = " "
- self.tTextColorSpace[y][x] = 0
- end
- end
- return true
- end
- <GluePath>LayerAPI/PullEventAdvanced
- local _dblclk = nil
- local _eventData = nil
- function os.pullEvent( _sFilter )
- local eventData = { os.pullEventRaw( _sFilter ) }
- if eventData[1] == "terminate" then
- error( "Terminated", 0 )
- elseif eventData[1] == "mouse_click" then
- if (_dblclk) then
- eventData = _eventData
- eventData[1] = "mouse_double_click"
- _eventData = nil
- _dblclk = nil
- else
- _dblclk = os.startTimer(0.16)
- _eventData = eventData
- return
- end
- elseif eventData[1] == "timer" then
- if eventData[2] == _dblclk then
- eventData = _eventData
- _eventData = nil
- _dblclk = nil
- end
- end
- return unpack( eventData )
- end
- Keys = function() --Баг: мышь не оиличается от цифр. Не передаются координаты клика.
- local Events = {}
- local EventsLifeTime = nil
- while true do
- local event, keycode = os.pullEvent()
- if ( (event == "timer" and keycode == EventsLifeTime) or ((event == "key" or event == "mouse_click") and keycode == Events[#Events]) ) then
- Events = {}
- elseif (event == "key" or event == "mouse_click") then
- table.insert(Events, keycode)
- EventsLifeTime = os.startTimer(1)
- if (#Events > 1) then
- os.queueEvent("keys", Events[1], Events[2])
- Events = {}
- end
- end
- end
- end
- <eof>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement