Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Загрузка библиотек
- local term = require("term")
- local gpu = require("component").gpu
- local component = require("component")
- local event = require("event")
- --Локальные функции всех классов
- local function copyTable(orig) --Копирование таблиц
- local orig_type = type(orig)
- local copy
- if orig_type == 'table' then
- copy = { }
- for orig_key, orig_value in next, orig, nil do
- copy[copyTable(orig_key)] = copyTable(orig_value)
- end
- setmetatable(copy, copyTable(getmetatable(orig)))
- else
- copy = orig
- end
- return copy
- end
- local function isConnect(address, kind) --Проверка подключения по адресу
- local result = false
- for value in component.list(kind) do
- if address == value then
- result = true
- break
- end
- end
- return result
- end
- local function findComp(kind) --Находит любой компонент по типу
- local result = false
- for value in component.list(kind) do
- result = value
- break
- end
- return result
- end
- local function toLines(str, w) --Переносит текст на следующую строку, если не хватает места
- local lines = { }
- for i=1, math.ceil(#str/w) do
- lines[i] = string.sub(str, (i-1)*w+1, i*w)
- end
- return lines
- end
- local function rusType(var) --Руссифицированная функция type
- var = type(var)
- if var == "number" then return "число"
- elseif var == "table" then return "таблица"
- elseif var == "function" then return "функция"
- elseif var == "string" then return "строка"
- elseif var == "boolean" then return "логическое значение"
- elseif var == "nil" then return "ничего"
- else return var end
- end
- local function button_error(list) --Детектирует ошибки в кнопке
- if type(list) ~= "table" then
- error("Задан неверный параметр.")
- end
- list.Draw = nil
- list.Set = nil
- list.Detect = nil
- list.Screen = list.Screen or gpu.getScreen()
- list.Text = tostring(list.Text or "")
- list.TextColor = list.TextColor or 0xE6E6FA
- list.BackgroundColor = list.BackgroundColor or 0x085eff
- list.ActiveColor = list.ActiveColor or 0x13c000
- list.InactiveColor = list.InactiveColor or 0x696969
- list.Time = list.Time or 0.3
- list.Style = list.Style or "round"
- if list.Visible == nil then list.Visible = true end
- --Ошибки
- if type(list.Screen) ~= "string" then
- error("Адрес монитора должен быть строкой!", 2)
- end
- if not isConnect(list.Screen, "screen") then
- error("Не найдено \"screen\" по адресу: \""..list.Screen.."\".", 2)
- end
- if type(list.xPos) ~= "number" then
- local var = rusType(list.xPos)
- list.xPos = tonumber(list.xPos)
- if not list.xPos then
- error("Параметр xPos должен быть числом, получено "..var..".", 2)
- end
- end
- if type(list.yPos) ~= "number" then
- local var = rusType(list.yPos)
- list.yPos = tonumber(list.yPos)
- if not list.yPos then
- error("Параметр yPos должен быть числом, получено "..var..".", 2)
- end
- end
- if type(list.Width) ~= "number" then
- local var = rusType(list.Width)
- list.Width = tonumber(list.Width)
- if not list.Width then
- error("Параметр Width должен быть числом, получено "..var..".", 2)
- end
- end
- if type(list.Height) ~= "number" then
- local var = rusType(list.Height)
- list.Height = tonumber(list.Height)
- if not list.Height then
- error("Параметр Height должен быть числом, получено "..var..".", 2)
- end
- end
- if type(list.TextColor) ~= "number" then
- local var = rusType(list.TextColor)
- list.TextColor = tonumber(list.TextColor)
- if not list.TextColor then
- error("Цвет текста должен быть числом, получено "..var..".", 2)
- end
- end
- if type(list.BackgroundColor) ~= "number" then
- local var = rusType(list.BackgroundColor)
- list.BackgroundColor = tonumber(list.BackgroundColor)
- if not list.BackgroundColor then
- error("Цвет кнопки должен быть числом, получено "..var..".", 2)
- end
- end
- if type(list.ActiveColor) ~= "number" then
- local var = rusType(list.ActiveColor)
- list.ActiveColor = tonumber(list.ActiveColor)
- if not list.ActiveColor then
- error("Цвет активной кнопки должен быть числом, получено "..var..".", 2)
- end
- end
- if type(list.InactiveColor) ~= "number" then
- local var = rusType(list.InactiveColor)
- list.InactiveColor = tonumber(InactiveColor)
- if not list.InactiveColor then
- error("Цвет неактивной кнопки должен быть числом, получено "..var..".", 2)
- end
- end
- if type(list.Visible) ~= "boolean" then
- error("Параметр видимости должен быть логическим значением, получено "..rusType(list.Visible)..".", 2)
- end
- if type(list.Time) ~= "number" then
- local var = rusType(list.Time)
- list.Time = tonumber(list.Time)
- if not list.Time then
- error("Время нажатия должно быть числом, получено "..var..".", 2)
- end
- end
- if list.Style ~= "round" and list.Style ~= "square" then
- error("Стиль кнопки может быть \"round\" или \"square\", получено \""..list.Style.."\".", 2)
- end
- end
- local function object_set(object, list) --Меняет свойства любого объекта
- for key, value in pairs(list) do
- object[key] = value
- end
- if object.Object == "button" then
- button_error(object)
- end
- if object.Visible then
- object.Draw()
- end
- end
- local function object_detect(object, list) --Детектирует взаимодействия со всем
- if list.Action ~= "touch" and list.Action ~= "drag" and list.Action ~= "drop" and list.Action ~= "scroll" then
- error("Получено незарегистрированное действие, \""..list.Action.."\".", 2)
- end
- list.Button = list.Button or 2
- if not list.x then
- _, list.Screen, list.x, list.y, list.CurrentButton = event.pull(list.Action)
- end
- list.Screen = list.Screen or object.Screen
- if list.x >= object.xPos
- and list.x <= object.xPos+object.Width-1
- and list.y >= object.yPos
- and list.y <= object.yPos+object.Height-1
- and (list.Button == list.CurrentButton or list.Button == 2)
- and list.Screen == object.Screen then
- return true, list.CurrentButton
- else
- return false
- end
- end
- --[[
- Классы
- ]]
- local Object = {}
- Object.New = { } --Таблица с классами
- do --Класс кнопки
- local function button_draw(object, state) --Рисует кнопку
- local screen = gpu.getScreen()
- local RES1, RES2 = gpu.getResolution()
- local BG = gpu.getBackground()
- local FG = gpu.getForeground()
- if object.Screen ~= screen then gpu.bind(object.Screen) end
- local backColor
- if state == "active" then
- backColor = object.ActiveColor
- elseif state == "inactive" then
- backColor = object.InactiveColor
- else
- backColor = object.BackgroundColor
- end
- gpu.setBackground(backColor)
- gpu.fill(object.xPos, object.yPos, object.Width, object.Height, " ")
- if object.Style == "round" then
- gpu.setBackground(BG)
- gpu.setForeground(backColor)
- gpu.set(object.xPos, object.yPos, "▄")
- gpu.set(object.xPos+object.Width-1, object.yPos, "▄")
- gpu.set(object.xPos, object.yPos+object.Height-1, "▀")
- gpu.set(object.xPos+object.Width-1, object.yPos+object.Height-1, "▀")
- end
- do
- gpu.setBackground(backColor)
- gpu.setForeground(object.TextColor)
- local text = toLines(object.Text, object.Width)
- for i=1, #text do
- gpu.set(math.floor(object.Width/2+object.xPos-#text[i]/2), math.ceil(object.yPos+object.Height/2-#text/2+(i-1)), text[i])
- end
- end
- if object.Screen ~= screen then
- gpu.setResolution(RES1, RES2)
- gpu.bind(screen)
- end
- gpu.setForeground(FG)
- gpu.setBackground(BG)
- end
- local function button_detect(object, list) --Детектирование действий с кнопкой
- local bool, n = object_detect(object, list)
- if object.Visible and bool then
- object.Draw("active")
- os.sleep(object.Time)
- object.Draw()
- end
- print(bool, n)
- return bool, n
- end
- function Object.New.Button(list) --Класс кнопки
- local object = copyTable(list)
- button_error(object)
- object.Object = "button"
- list = nil
- local Me = {
- ["Draw"] = function(state) --Рисует кнопку
- button_draw(object, state)
- end;
- ["Set"] = function(list) --Меняет значения кнопки
- object_set(object, list)
- end;
- ["Detect"] = function(list) --Детектирует взаимодействия с кнопкой
- local bool, n = button_detect(object, list)
- return bool, n
- end;
- }
- setmetatable(Me, {__index = object})
- setmetatable(object, {__index = Me})
- --Cоздание объекта
- if object.Visible then Me.Draw() end
- return Me
- end
- end
- return Object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement