Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --урезанная api.lib для codedoor ECS
- local component = require("component")
- local term = require("term")
- local unicode = require("unicode")
- local event = require("event")
- local ecs = {}
- ecs.windowColors = {
- background = 0xeeeeee,
- usualText = 0x444444,
- }
- ecs.colors = {
- green = 0x57A64E,
- red = 0xCC4C4C
- }
- --Цветной текст
- function ecs.colorText(x,y,textColor,text)
- component.gpu.setForeground(textColor)
- component.gpu.set(x,y,text)
- end
- --Цветной текст с жопкой!
- function ecs.colorTextWithBack(x,y,textColor,backColor,text)
- component.gpu.setForeground(textColor)
- component.gpu.setBackground(backColor)
- component.gpu.set(x,y,text)
- end
- --Юникодовский разделитель
- function ecs.separator(x, y, width, back, fore)
- ecs.colorTextWithBack(x, y, fore, back, string.rep("-", width))
- end
- --Юникодовская рамка
- function ecs.border(x, y, width, height, back, fore)
- local stringUp = "┌"..string.rep("─", width - 2).."┐"
- local stringDown = "└"..string.rep("─", width - 2).."┘"
- component.gpu.setForeground(fore)
- component.gpu.setBackground(back)
- component.gpu.set(x, y, stringUp)
- component.gpu.set(x, y + height - 1, stringDown)
- local yPos = 1
- for i = 1, (height - 2) do
- component.gpu.set(x, y + yPos, "│")
- component.gpu.set(x + width - 1, y + yPos, "│")
- yPos = yPos + 1
- end
- end
- --Кнопка в виде текста в рамке
- function ecs.drawFramedButton(x, y, width, height, text, color)
- ecs.border(x, y, width, height, component.gpu.getBackground(), color)
- component.gpu.fill(x + 1, y + 1, width - 2, height - 2, " ")
- x = x + math.floor(width / 2 - unicode.len(text) / 2)
- y = y + math.floor(width / 2 - 1)
- component.gpu.set(x, y, text)
- end
- --Заливка всего экрана указанным цветом
- function ecs.clearScreen(color)
- if color then component.gpu.setBackground(color) end
- term.clear()
- end
- --Очистить экран, установить комфортные цвета и поставить курсок на 1, 1
- function ecs.prepareToExit(color1, color2)
- term.setCursor(1, 1)
- ecs.clearScreen(color1 or 0x333333)
- component.gpu.setForeground(color2 or 0xffffff)
- component.gpu.set(1, 1, "")
- end
- --Корректировка стартовых координат. Core-функция для всех моих программ
- function ecs.correctStartCoords(xStart,yStart,xWindowSize,yWindowSize)
- local xSize,ySize = component.gpu.getResolution()
- if xStart == "auto" then
- xStart = math.floor(xSize/2 - xWindowSize/2)
- end
- if yStart == "auto" then
- yStart = math.ceil(ySize/2 - yWindowSize/2)
- end
- return xStart,yStart
- end
- --Обычный квадрат указанного цвета
- function ecs.square(x,y,width,height,color)
- component.gpu.setBackground(color)
- component.gpu.fill(x,y,width,height," ")
- end
- --Вертикальный скроллбар. Маст-хев!
- function ecs.srollBar(x, y, width, height, countOfAllElements, currentElement, backColor, frontColor)
- local sizeOfScrollBar = math.ceil(1 / countOfAllElements * height)
- local displayBarFrom = math.floor(y + height * ((currentElement - 1) / countOfAllElements))
- ecs.square(x, y, width, height, backColor)
- ecs.square(x, displayBarFrom, width, sizeOfScrollBar, frontColor)
- sizeOfScrollBar, displayBarFrom = nil, nil
- end
- --Отрисовка поля с текстом. Сюда пихать массив вида {"строка1", "строка2", "строка3", ...}
- function ecs.textField(x, y, width, height, lines, displayFrom, background, foreground, scrollbarBackground, scrollbarForeground)
- x, y = ecs.correctStartCoords(x, y, width, height)
- background = background or 0xffffff
- foreground = foreground or ecs.windowColors.usualText
- local sLines = #lines
- local lineLimit = width - 3
- --Парсим строки
- local line = 1
- while lines[line] do
- local sLine = unicode.len(lines[line])
- if sLine > lineLimit then
- local part1, part2 = unicode.sub(lines[line], 1, lineLimit), unicode.sub(lines[line], lineLimit + 1, -1)
- lines[line] = part1
- table.insert(lines, line + 1, part2)
- part1, part2 = nil, nil
- end
- line = line + 1
- sLine = nil
- end
- line = nil
- ecs.square(x, y, width - 1, height, background)
- ecs.srollBar(x + width - 1, y, 1, height, sLines, displayFrom, scrollbarBackground, scrollbarForeground)
- component.gpu.setBackground(background)
- component.gpu.setForeground(foreground)
- local yPos = y
- for i = displayFrom, (displayFrom + height - 1) do
- if lines[i] then
- component.gpu.set(x + 1, yPos, lines[i])
- yPos = yPos + 1
- else
- break
- end
- end
- return sLines
- end
- --Функция отрисовки кнопки указанной ширины
- function ecs.drawButton(x,y,width,height,text,backColor,textColor)
- x,y = ecs.correctStartCoords(x,y,width,height)
- local textPosX = math.floor(x + width / 2 - unicode.len(text) / 2)
- local textPosY = math.floor(y + height / 2)
- ecs.square(x,y,width,height,backColor)
- ecs.colorText(textPosX,textPosY,textColor,text)
- return x, y, (x + width - 1), (y + height - 1)
- end
- --Ограничение длины строки. Маст-хев функция.
- function ecs.stringLimit(mode, text, size, noDots)
- if unicode.len(text) <= size then return text end
- local length = unicode.len(text)
- if mode == "start" then
- if noDots then
- return unicode.sub(text, length - size + 1, -1)
- else
- return "…" .. unicode.sub(text, length - size + 2, -1)
- end
- else
- if noDots then
- return unicode.sub(text, 1, size)
- else
- return unicode.sub(text, 1, size - 1) .. "…"
- end
- end
- end
- --Конвертация из юникода в символ. Вроде норм, а вроде и не норм. Но полезно.
- function ecs.convertCodeToSymbol(code)
- local symbol
- if code ~= 0 and code ~= 13 and code ~= 8 and code ~= 9 and code ~= 200 and code ~= 208 and code ~= 203 and code ~= 205 and not keyboard.isControlDown() then
- symbol = unicode.char(code)
- if keyboard.isShiftPressed then symbol = unicode.upper(symbol) end
- end
- return symbol
- end
- --Функция для ввода текста в мини-поле.
- function ecs.inputText(x, y, limit, cheBiloVvedeno, background, foreground, justDrawNotEvent, maskTextWith)
- limit = limit or 10
- cheBiloVvedeno = cheBiloVvedeno or ""
- background = background or 0xffffff
- foreground = foreground or 0x000000
- component.gpu.setBackground(background)
- component.gpu.setForeground(foreground)
- component.gpu.fill(x, y, limit, 1, " ")
- local text = cheBiloVvedeno
- local function draw()
- term.setCursorBlink(false)
- local dlina = unicode.len(text)
- local xCursor = x + dlina
- if xCursor > (x + limit - 1) then xCursor = (x + limit - 1) end
- if maskTextWith then
- component.gpu.set(x, y, ecs.stringLimit("start", string.rep("?", dlina), limit))
- else
- component.gpu.set(x, y, ecs.stringLimit("start", text, limit))
- end
- term.setCursor(xCursor, y)
- term.setCursorBlink(true)
- end
- draw()
- if justDrawNotEvent then term.setCursorBlink(false); return cheBiloVvedeno end
- while true do
- local e = {event.pull()}
- if e[1] == "key_down" then
- if e[4] == 14 then
- term.setCursorBlink(false)
- text = unicode.sub(text, 1, -2)
- if unicode.len(text) < limit then component.gpu.set(x + unicode.len(text), y, " ") end
- draw()
- elseif e[4] == 28 then
- term.setCursorBlink(false)
- return text
- else
- local symbol = ecs.convertCodeToSymbol(e[3])
- if symbol then
- text = text..symbol
- draw()
- end
- end
- elseif e[1] == "touch" then
- term.setCursorBlink(false)
- return text
- elseif e[1] == "clipboard" then
- if e[3] then
- text = text..e[3]
- draw()
- end
- end
- end
- end
- --Запомнить область пикселей и возвратить ее в виде массива
- function ecs.rememberOldPixels(x, y, x2, y2)
- local newPNGMassiv = { ["backgrounds"] = {} }
- local xSize, ySize = component.gpu.getResolution()
- newPNGMassiv.x, newPNGMassiv.y = x, y
- --Перебираем весь массив стандартного PNG-вида по высоте
- local xCounter, yCounter = 1, 1
- for j = y, y2 do
- xCounter = 1
- for i = x, x2 do
- if (i > xSize or i < 0) or (j > ySize or j < 0) then
- error("Can't remember pixel, because it's located behind the screen: x("..i.."), y("..j..") out of xSize("..xSize.."), ySize("..ySize..")\n")
- end
- local symbol, fore, back = component.gpu.get(i, j)
- newPNGMassiv["backgrounds"][back] = newPNGMassiv["backgrounds"][back] or {}
- newPNGMassiv["backgrounds"][back][fore] = newPNGMassiv["backgrounds"][back][fore] or {}
- table.insert(newPNGMassiv["backgrounds"][back][fore], {xCounter, yCounter, symbol} )
- xCounter = xCounter + 1
- back, fore, symbol = nil, nil, nil
- end
- yCounter = yCounter + 1
- end
- xSize, ySize = nil, nil
- return newPNGMassiv
- end
- --Нарисовать запомненные ранее пиксели из массива
- function ecs.drawOldPixels(massivSudaPihay)
- --Перебираем массив с фонами
- for back, backValue in pairs(massivSudaPihay["backgrounds"]) do
- component.gpu.setBackground(back)
- for fore, foreValue in pairs(massivSudaPihay["backgrounds"][back]) do
- component.gpu.setForeground(fore)
- for pixel = 1, #massivSudaPihay["backgrounds"][back][fore] do
- if massivSudaPihay["backgrounds"][back][fore][pixel][3] ~= transparentSymbol then
- component.gpu.set(massivSudaPihay.x + massivSudaPihay["backgrounds"][back][fore][pixel][1] - 1, massivSudaPihay.y + massivSudaPihay["backgrounds"][back][fore][pixel][2] - 1, massivSudaPihay["backgrounds"][back][fore][pixel][3])
- end
- end
- end
- end
- end
- --КЛИКНУЛИ ЛИ В ЗОНУ
- function ecs.clickedAtArea(x,y,sx,sy,ex,ey)
- if (x >= sx) and (x <= ex) and (y >= sy) and (y <= ey) then return true end
- return false
- end
- --Моя любимая функция ошибки C:
- function ecs.error(...)
- local args = {...}
- local text = ""
- if #args > 1 then
- for i = 1, #args do
- --text = text .. "[" .. i .. "] = " .. tostring(args[i])
- if type(args[i]) == "string" then args[i] = "\"" .. args[i] .. "\"" end
- text = text .. tostring(args[i])
- if i ~= #args then text = text .. ", " end
- end
- else
- text = tostring(args[1])
- end
- ecs.universalWindow("auto", "auto", math.ceil(component.gpu.getResolution() * 0.45), ecs.windowColors.background, true, {"EmptyLine"}, {"CenterText", 0x880000, "Ошибка!"}, {"EmptyLine"}, {"WrappedText", 0x262626, text}, {"EmptyLine"}, {"Button", {0x880000, 0xffffff, "OK!"}})
- end
- --Окошки
- function ecs.universalWindow(x, y, width, background, closeWindowAfter, ...)
- local objects = {...}
- local countOfObjects = #objects
- local pressedButton
- local pressedMultiButton
- --Задаем высотные константы для объектов
- local objectsHeights = {
- ["button"] = 3,
- ["centertext"] = 1,
- ["emptyline"] = 1,
- ["input"] = 3,
- ["slider"] = 3,
- ["select"] = 3,
- ["selector"] = 3,
- ["separator"] = 1,
- ["switch"] = 1,
- ["color"] = 3,
- }
- --Скорректировать ширину, если нужно
- local function correctWidth(newWidthForAnalyse)
- width = math.max(width, newWidthForAnalyse)
- end
- --Корректируем ширину
- for i = 1, countOfObjects do
- local objectType = string.lower(objects[i][1])
- if objectType == "centertext" then
- correctWidth(unicode.len(objects[i][3]) + 2)
- elseif objectType == "slider" then --!!!!!!!!!!!!!!!!!! ВОТ ТУТ НЕ ЗАБУДЬ ФИКСАНУТЬ
- correctWidth(unicode.len(objects[i][7]..tostring(objects[i][5].." ")) + 2)
- elseif objectType == "select" then
- for j = 4, #objects[i] do
- correctWidth(unicode.len(objects[i][j]) + 2)
- end
- --elseif objectType == "selector" then
- --elseif objectType == "separator" then
- elseif objectType == "textfield" then
- correctWidth(5)
- elseif objectType == "wrappedtext" then
- correctWidth(6)
- elseif objectType == "button" then
- --Корректируем ширину
- local widthOfButtons = 0
- local maxButton = 0
- for j = 2, #objects[i] do
- maxButton = math.max(maxButton, unicode.len(objects[i][j][3]) + 2)
- end
- widthOfButtons = maxButton * #objects[i]
- correctWidth(widthOfButtons)
- elseif objectType == "switch" then
- local dlina = unicode.len(objects[i][5]) + 2 + 10 + 4
- correctWidth(dlina)
- elseif objectType == "color" then
- correctWidth(unicode.len(objects[i][2]) + 6)
- end
- end
- --Считаем высоту этой хуйни
- local height = 0
- for i = 1, countOfObjects do
- local objectType = string.lower(objects[i][1])
- if objectType == "select" then
- height = height + (objectsHeights[objectType] * (#objects[i] - 3))
- elseif objectType == "textfield" then
- height = height + objects[i][2]
- elseif objectType == "wrappedtext" then
- --Заранее парсим текст перенесенный
- objects[i].wrapped = string.wrap({objects[i][3]}, width - 4)
- objects[i].height = #objects[i].wrapped
- height = height + objects[i].height
- else
- height = height + objectsHeights[objectType]
- end
- end
- --Коорректируем стартовые координаты
- x, y = ecs.correctStartCoords(x, y, width, height)
- --Запоминаем инфу о том, что было нарисовано, если это необходимо
- local oldPixels, oldBackground, oldForeground
- if closeWindowAfter then
- oldBackground = component.gpu.getBackground()
- oldForeground = component.gpu.getForeground()
- oldPixels = ecs.rememberOldPixels(x, y, x + width - 1, y + height - 1)
- end
- --Считаем все координаты объектов
- objects[1].y = y
- if countOfObjects > 1 then
- for i = 2, countOfObjects do
- local objectType = string.lower(objects[i - 1][1])
- if objectType == "select" then
- objects[i].y = objects[i - 1].y + (objectsHeights[objectType] * (#objects[i - 1] - 3))
- elseif objectType == "textfield" then
- objects[i].y = objects[i - 1].y + objects[i - 1][2]
- elseif objectType == "wrappedtext" then
- objects[i].y = objects[i - 1].y + objects[i - 1].height
- else
- objects[i].y = objects[i - 1].y + objectsHeights[objectType]
- end
- end
- end
- --Объекты для тача
- local obj = {}
- local function newObj(class, name, ...)
- obj[class] = obj[class] or {}
- obj[class][name] = {...}
- end
- --Отображение объекта по номеру
- local function displayObject(number, active)
- local objectType = string.lower(objects[number][1])
- if objectType == "centertext" then
- local xPos = x + math.floor(width / 2 - unicode.len(objects[number][3]) / 2)
- component.gpu.setForeground(objects[number][2])
- component.gpu.setBackground(background)
- component.gpu.set(xPos, objects[number].y, objects[number][3])
- elseif objectType == "input" then
- if active then
- --Рамочка
- ecs.border(x + 1, objects[number].y, width - 2, objectsHeights.input, background, objects[number][3])
- --Тестик
- objects[number][4] = ecs.inputText(x + 3, objects[number].y + 1, width - 6, "", background, objects[number][3], false, objects[number][5])
- else
- --Рамочка
- ecs.border(x + 1, objects[number].y, width - 2, objectsHeights.input, background, objects[number][2])
- --Текстик
- component.gpu.set(x + 3, objects[number].y + 1, ecs.stringLimit("start", objects[number][4], width - 6))
- ecs.inputText(x + 3, objects[number].y + 1, width - 6, objects[number][4], background, objects[number][2], true, objects[number][5])
- end
- newObj("Inputs", number, x + 1, objects[number].y, x + width - 2, objects[number].y + 2)
- elseif objectType == "slider" then
- local widthOfSlider = width - 2
- local xOfSlider = x + 1
- local yOfSlider = objects[number].y + 1
- local countOfSliderThings = objects[number][5] - objects[number][4]
- local showSliderValue= objects[number][7]
- local dolya = widthOfSlider / countOfSliderThings
- local position = math.floor(dolya * objects[number][6])
- --Костыль
- if (xOfSlider + position) > (xOfSlider + widthOfSlider - 1) then position = widthOfSlider - 2 end
- --Две линии
- ecs.separator(xOfSlider, yOfSlider, position, background, objects[number][3])
- ecs.separator(xOfSlider + position, yOfSlider, widthOfSlider - position, background, objects[number][2])
- --Слудир
- ecs.square(xOfSlider + position, yOfSlider, 2, 1, objects[number][3])
- --Текстик под слудиром
- if showSliderValue then
- local text = showSliderValue .. tostring(objects[number][6]) .. (objects[number][8] or "")
- local textPos = (xOfSlider + widthOfSlider / 2 - unicode.len(text) / 2)
- ecs.square(x, yOfSlider + 1, width, 1, background)
- ecs.colorText(textPos, yOfSlider + 1, objects[number][2], text)
- end
- newObj("Sliders", number, xOfSlider, yOfSlider, x + widthOfSlider, yOfSlider, dolya)
- elseif objectType == "select" then
- local usualColor = objects[number][2]
- local selectionColor = objects[number][3]
- objects[number].selectedData = objects[number].selectedData or 1
- local symbol = "?"
- local yPos = objects[number].y
- for i = 4, #objects[number] do
- --Коробка для галочки
- ecs.border(x + 1, yPos, 5, 3, background, usualColor)
- --Текст
- component.gpu.set(x + 7, yPos + 1, objects[number][i])
- --Галочка
- if objects[number].selectedData == (i - 3) then
- ecs.colorText(x + 3, yPos + 1, selectionColor, symbol)
- else
- component.gpu.set(x + 3, yPos + 1, " ")
- end
- obj["Selects"] = obj["Selects"] or {}
- obj["Selects"][number] = obj["Selects"][number] or {}
- obj["Selects"][number][i - 3] = { x + 1, yPos, x + width - 2, yPos + 2 }
- yPos = yPos + objectsHeights.select
- end
- elseif objectType == "selector" then
- local borderColor = objects[number][2]
- local arrowColor = objects[number][3]
- local selectorWidth = width - 2
- objects[number].selectedElement = objects[number].selectedElement or objects[number][4]
- local topLine = "-" .. string.rep("-", selectorWidth - 6) .. "T---¬"
- local midLine = "¦" .. string.rep(" ", selectorWidth - 6) .. "¦ ¦"
- local botLine = "L" .. string.rep("-", selectorWidth - 6) .. "+----"
- local yPos = objects[number].y
- local function bordak(borderColor)
- component.gpu.setBackground(background)
- component.gpu.setForeground(borderColor)
- component.gpu.set(x + 1, objects[number].y, topLine)
- component.gpu.set(x + 1, objects[number].y + 1, midLine)
- component.gpu.set(x + 1, objects[number].y + 2, botLine)
- component.gpu.set(x + 3, objects[number].y + 1, ecs.stringLimit("start", objects[number].selectedElement, width - 6))
- ecs.colorText(x + width - 4, objects[number].y + 1, arrowColor, "Ў")
- end
- bordak(borderColor)
- --Выпадающий список, самый гемор, блядь
- if active then
- local xPos, yPos = x + 1, objects[number].y + 3
- local spisokWidth = width - 2
- local countOfElements = #objects[number] - 3
- local spisokHeight = countOfElements + 1
- local oldPixels = ecs.rememberOldPixels( xPos, yPos, xPos + spisokWidth - 1, yPos + spisokHeight - 1)
- local coords = {}
- bordak(arrowColor)
- --Рамку рисуем поверх фоника
- local topLine = "+"..string.rep("-", spisokWidth - 6).."+---+"
- local midLine = "¦"..string.rep(" ", spisokWidth - 2).."¦"
- local botLine = "L"..string.rep("-", selectorWidth - 2) .. "-"
- ecs.colorTextWithBack(xPos, yPos - 1, arrowColor, background, topLine)
- for i = 1, spisokHeight - 1 do
- component.gpu.set(xPos, yPos + i - 1, midLine)
- end
- component.gpu.set(xPos, yPos + spisokHeight - 1, botLine)
- --Элементы рисуем
- xPos = xPos + 2
- for i = 1, countOfElements do
- ecs.colorText(xPos, yPos, borderColor, ecs.stringLimit("start", objects[number][i + 3], spisokWidth - 4))
- coords[i] = {xPos - 1, yPos, xPos + spisokWidth - 4, yPos}
- yPos = yPos + 1
- end
- --Обработка
- local exit
- while true do
- if exit then break end
- local e = {event.pull()}
- if e[1] == "touch" then
- for i = 1, #coords do
- if ecs.clickedAtArea(e[3], e[4], coords[i][1], coords[i][2], coords[i][3], coords[i][4]) then
- ecs.square(coords[i][1], coords[i][2], spisokWidth - 2, 1, ecs.colors.blue)
- ecs.colorText(coords[i][1] + 1, coords[i][2], 0xffffff, objects[number][i + 3])
- os.sleep(0.3)
- objects[number].selectedElement = objects[number][i + 3]
- exit = true
- break
- end
- end
- end
- end
- ecs.drawOldPixels(oldPixels)
- end
- newObj("Selectors", number, x + 1, objects[number].y, x + width - 2, objects[number].y + 2)
- elseif objectType == "separator" then
- ecs.separator(x, objects[number].y, width, background, objects[number][2])
- elseif objectType == "textfield" then
- newObj("TextFields", number, x + 1, objects[number].y, x + width - 2, objects[number].y + objects[number][2] - 1)
- if not objects[number].strings then objects[number].strings = string.wrap({objects[number][7]}, width - 3) end
- objects[number].displayFrom = objects[number].displayFrom or 1
- ecs.textField(x, objects[number].y, width, objects[number][2], objects[number].strings, objects[number].displayFrom, objects[number][3], objects[number][4], objects[number][5], objects[number][6])
- elseif objectType == "wrappedtext" then
- component.gpu.setBackground(background)
- component.gpu.setForeground(objects[number][2])
- for i = 1, #objects[number].wrapped do
- component.gpu.set(x + 2, objects[number].y + i - 1, objects[number].wrapped[i])
- end
- elseif objectType == "button" then
- obj["MultiButtons"] = obj["MultiButtons"] or {}
- obj["MultiButtons"][number] = {}
- local widthOfButton = math.floor(width / (#objects[number] - 1))
- local xPos, yPos = x, objects[number].y
- for i = 1, #objects[number] do
- if type(objects[number][i]) == "table" then
- local x1, y1, x2, y2 = ecs.drawButton(xPos, yPos, widthOfButton, 3, objects[number][i][3], objects[number][i][1], objects[number][i][2])
- table.insert(obj["MultiButtons"][number], {x1, y1, x2, y2, widthOfButton})
- xPos = x2 + 1
- if i == #objects[number] then
- ecs.square(xPos, yPos, x + width - xPos, 3, objects[number][i][1])
- obj["MultiButtons"][number][i - 1][5] = obj["MultiButtons"][number][i - 1][5] + x + width - xPos
- end
- x1, y1, x2, y2 = nil, nil, nil, nil
- end
- end
- elseif objectType == "switch" then
- local xPos, yPos = x + 2, objects[number].y
- local activeColor, passiveColor, textColor, text, state = objects[number][2], objects[number][3], objects[number][4], objects[number][5], objects[number][6]
- local switchWidth = 8
- ecs.colorTextWithBack(xPos, yPos, textColor, background, text)
- xPos = x + width - switchWidth - 2
- if state then
- ecs.square(xPos, yPos, switchWidth, 1, activeColor)
- ecs.square(xPos + switchWidth - 2, yPos, 2, 1, passiveColor)
- --ecs.colorTextWithBack(xPos + 4, yPos, passiveColor, activeColor, "ON")
- else
- ecs.square(xPos, yPos, switchWidth, 1, passiveColor - 0x444444)
- ecs.square(xPos, yPos, 2, 1, passiveColor)
- --ecs.colorTextWithBack(xPos + 4, yPos, passiveColor, passiveColor - 0x444444, "OFF")
- end
- newObj("Switches", number, xPos, yPos, xPos + switchWidth - 1, yPos)
- elseif objectType == "color" then
- local xPos, yPos = x + 1, objects[number].y
- local blendedColor = require("colorlib").alphaBlend(objects[number][3], 0xFFFFFF, 180)
- local w = width - 2
- ecs.colorTextWithBack(xPos, yPos + 2, blendedColor, background, string.rep("-", w))
- ecs.colorText(xPos, yPos, objects[number][3], string.rep("-", w))
- ecs.square(xPos, yPos + 1, w, 1, objects[number][3])
- ecs.colorText(xPos + 1, yPos + 1, 0xffffff - objects[number][3], objects[number][2])
- newObj("Colors", number, xPos, yPos, x + width - 2, yPos + 2)
- end
- end
- --Отображение всех объектов
- local function displayAllObjects()
- for i = 1, countOfObjects do
- displayObject(i)
- end
- end
- --Подготовить массив возвращаемый
- local function getReturn()
- local massiv = {}
- for i = 1, countOfObjects do
- local type = string.lower(objects[i][1])
- if type == "button" then
- table.insert(massiv, pressedButton)
- elseif type == "input" then
- table.insert(massiv, objects[i][4])
- elseif type == "select" then
- table.insert(massiv, objects[i][objects[i].selectedData + 3])
- elseif type == "selector" then
- table.insert(massiv, objects[i].selectedElement)
- elseif type == "slider" then
- table.insert(massiv, objects[i][6])
- elseif type == "switch" then
- table.insert(massiv, objects[i][6])
- elseif type == "color" then
- table.insert(massiv, objects[i][3])
- else
- table.insert(massiv, nil)
- end
- end
- return massiv
- end
- local function redrawBeforeClose()
- if closeWindowAfter then
- ecs.drawOldPixels(oldPixels)
- component.gpu.setBackground(oldBackground)
- component.gpu.setForeground(oldForeground)
- end
- end
- --Рисуем окно
- ecs.square(x, y, width, height, background)
- displayAllObjects()
- while true do
- local e = {event.pull()}
- if e[1] == "touch" or e[1] == "drag" then
- --Анализируем клик на кнопки
- if obj["MultiButtons"] then
- for key in pairs(obj["MultiButtons"]) do
- for i = 1, #obj["MultiButtons"][key] do
- if ecs.clickedAtArea(e[3], e[4], obj["MultiButtons"][key][i][1], obj["MultiButtons"][key][i][2], obj["MultiButtons"][key][i][3], obj["MultiButtons"][key][i][4]) then
- ecs.drawButton(obj["MultiButtons"][key][i][1], obj["MultiButtons"][key][i][2], obj["MultiButtons"][key][i][5], 3, objects[key][i + 1][3], objects[key][i + 1][2], objects[key][i + 1][1])
- os.sleep(0.3)
- pressedButton = objects[key][i + 1][3]
- redrawBeforeClose()
- return getReturn()
- end
- end
- end
- end
- --А теперь клик на инпуты!
- if obj["Inputs"] then
- for key in pairs(obj["Inputs"]) do
- if ecs.clickedAtArea(e[3], e[4], obj["Inputs"][key][1], obj["Inputs"][key][2], obj["Inputs"][key][3], obj["Inputs"][key][4]) then
- displayObject(key, true)
- displayObject(key)
- break
- end
- end
- end
- --А теперь галочковыбор!
- if obj["Selects"] then
- for key in pairs(obj["Selects"]) do
- for i in pairs(obj["Selects"][key]) do
- if ecs.clickedAtArea(e[3], e[4], obj["Selects"][key][i][1], obj["Selects"][key][i][2], obj["Selects"][key][i][3], obj["Selects"][key][i][4]) then
- objects[key].selectedData = i
- displayObject(key)
- break
- end
- end
- end
- end
- --Хм, а вот и селектор подъехал!
- if obj["Selectors"] then
- for key in pairs(obj["Selectors"]) do
- if ecs.clickedAtArea(e[3], e[4], obj["Selectors"][key][1], obj["Selectors"][key][2], obj["Selectors"][key][3], obj["Selectors"][key][4]) then
- displayObject(key, true)
- displayObject(key)
- break
- end
- end
- end
- --Слайдеры, епта! "Потный матан", все делы
- if obj["Sliders"] then
- for key in pairs(obj["Sliders"]) do
- if ecs.clickedAtArea(e[3], e[4], obj["Sliders"][key][1], obj["Sliders"][key][2], obj["Sliders"][key][3], obj["Sliders"][key][4]) then
- local xOfSlider, dolya = obj["Sliders"][key][1], obj["Sliders"][key][5]
- local currentPixels = e[3] - xOfSlider
- local currentValue = math.floor(currentPixels / dolya)
- --Костыль
- if e[3] == obj["Sliders"][key][3] then currentValue = objects[key][5] end
- objects[key][6] = currentValue or objects[key][6]
- displayObject(key)
- break
- end
- end
- end
- if obj["Switches"] then
- for key in pairs(obj["Switches"]) do
- if ecs.clickedAtArea(e[3], e[4], obj["Switches"][key][1], obj["Switches"][key][2], obj["Switches"][key][3], obj["Switches"][key][4]) then
- objects[key][6] = not objects[key][6]
- displayObject(key)
- break
- end
- end
- end
- if obj["Colors"] then
- for key in pairs(obj["Colors"]) do
- if ecs.clickedAtArea(e[3], e[4], obj["Colors"][key][1], obj["Colors"][key][2], obj["Colors"][key][3], obj["Colors"][key][4]) then
- local oldColor = objects[key][3]
- objects[key][3] = 0xffffff - objects[key][3]
- displayObject(key)
- os.sleep(0.3)
- objects[key][3] = oldColor
- displayObject(key)
- local color = loadfile("lib/palette.lua")().draw("auto", "auto", objects[key][3])
- objects[key][3] = color or oldColor
- displayObject(key)
- break
- end
- end
- end
- elseif e[1] == "scroll" then
- if obj["TextFields"] then
- for key in pairs(obj["TextFields"]) do
- if ecs.clickedAtArea(e[3], e[4], obj["TextFields"][key][1], obj["TextFields"][key][2], obj["TextFields"][key][3], obj["TextFields"][key][4]) then
- if e[5] == 1 then
- if objects[key].displayFrom > 1 then objects[key].displayFrom = objects[key].displayFrom - 1; displayObject(key) end
- else
- if objects[key].displayFrom < #objects[key].strings then objects[key].displayFrom = objects[key].displayFrom + 1; displayObject(key) end
- end
- end
- end
- end
- elseif e[1] == "key_down" then
- if e[4] == 28 then
- redrawBeforeClose()
- return getReturn()
- end
- end
- end
- end
- return ecs
Add Comment
Please, Sign In to add comment