Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function fieldSymbolInput:new(x, y, lengthField, cursorSymbol, customInitTextOnField, canInputOnlyNumbers)
- local obj = {}
- obj.onFocusedStatus = false
- obj.debugStatus = false
- obj.x = x
- obj.y = y
- obj.lengthField = lengthField
- obj.cursorSymbol = cursorSymbol
- obj.savedText = ""
- obj.xPosC = obj.x + 1
- obj.signalHandler = signalHandler
- obj.canFocused = false
- obj.canInputOnlyNumbers = canInputOnlyNumbers == nil and false or canInputOnlyNumbers
- obj.backgroundColor = 0xb4b4b4
- obj.customInitTextOnField = tostring(customInitTextOnField)
- local privateVariable = {}
- privateVariable.xPosC = obj.x + 1
- privateVariable.xWithLengthField = obj.x + obj.lengthField
- function obj:changeOnFocusedStatus(status)
- self.onFocusedStatus = status
- end
- function obj:dropFocused(signal)
- if signal[3] >= 2 + privateVariable.xWithLengthField and signal[3] <= 4 + privateVariable.xWithLengthField then
- self.onFocusedStatus = false
- end
- end
- function obj:drawCrossButton()
- -----сивол X на кнопке стирания-------- 0x343a40
- gpuSet(self.x + self.lengthField + 2, self.y, " Г— ", 0x343a40, 0x0d6efd)
- -----сивол X на кнопке стирания-------
- end
- function obj:drawInit()
- --------------
- gpuFill(self.x, self.y, 2 + self.lengthField, 1, " ", self.backgroundColor)
- gpuSet(1 + self.x, self.y, self.customInitTextOnField, self.backgroundColor, 0x212529)
- ---------------0x212529
- self:drawCrossButton()
- end
- obj:drawInit()
- function obj:customMatch(symbol)
- if not self.canInputOnlyNumbers then
- local result = symbol:match("^[A-Za-z0-9Рђ-РЇ-Р°-СЏ -_#&]")
- return result == nil and (symbol == "]" and symbol or (symbol == "[" and symbol or (symbol == "-" and symbol or nil))) or result
- end
- if tonumber(self.savedText == "" and 0 or self.savedText) < 100 then
- return symbol:match("^[0-9]")
- end
- return nil
- end
- function obj:blinkCrossButton()
- gpuSet((3) + (self.lengthField) + self.x, self.y, "Г—", 0x343a40, 0x00b600)
- sleep(0.001)
- gpuSet((3) + (self.lengthField) + self.x, self.y, "Г—", 0x343a40, 0x0d6efd)
- end
- function obj:drawCursor()
- gpu.setBackground(self.backgroundColor)
- gpu.setForeground(0xffc107)
- gpu.set(self.xPosC, self.y, self.cursorSymbol)
- end
- function obj:blinkCursor()
- gpu.setBackground(self.backgroundColor)
- gpu.setForeground(0x00b600)
- gpu.set(self.xPosC, self.y, self.cursorSymbol)
- sleep(0.1)
- gpu.setForeground(0xffc107)
- gpu.set(self.xPosC, self.y, self.cursorSymbol)
- end
- function obj:reDrawText(text)
- gpuFill(self.x, self.y, 2 + self.lengthField, 1, " ", self.backgroundColor)
- gpuSet(self.x + 1, self.y, tostring(text), self.backgroundColor, 0x212529)
- end
- function obj:modifySavedTextAndPreCalcCursor(text)
- self.savedText = text
- self.xPosC = self.x + unicode.len(self.savedText) + 1
- self:reDrawText(self.savedText)
- end
- function obj:reDrawTextWithCursor(text)
- gpuFill(self.x, self.y, 2 + self.lengthField, 1, " ", self.backgroundColor)
- gpuSet(self.x + 1, self.y, tostring(text), self.backgroundColor, 0x212529)
- self:drawCursor()
- end
- function obj:reDrawTextWithCursorAndCross(text)
- self:reDrawTextWithCursor(text)
- self:drawCrossButton()
- end
- function obj:lenSavedText()
- return unicode.len(self.savedText)
- end
- function obj:eventTrap(signal)
- if signal[1] == "touch" then
- if signal[3] >= 2 + privateVariable.xWithLengthField and signal[3] <= 4 + privateVariable.xWithLengthField then --прожал X, отчистить
- obj:blinkCrossButton()
- self.xPosC = self.x + 1
- self.savedText = ""
- self:reDrawTextWithCursor("")
- obj:debug(signal)
- -- return "erase"
- end
- if signal[3] <= self.lengthField and signal[3] >= 4 + privateVariable.xWithLengthField or signal[4] ~= self.y then
- return "focusedDroped"
- end
- elseif signal[1] == "key_up" and signal[1] ~= "key_down" then
- local char, sChar = signal[3], signal[4]
- local TextLen = self:lenSavedText()
- local symbol = unicode.char(char)
- if signal[3] == 127 or signal[4] == 211 then --прожал delete, отчистить
- self:blinkCrossButton()
- self.xPosC = self.x + 1
- self.savedText = ""
- self:reDrawTextWithCursor("")
- obj:debug(signal)
- -- return "erase"
- end
- if self:customMatch(symbol) ~= nil and TextLen ~= self.lengthField then
- if self.xPosC == 1 + self.x then -- пишу символ в самом конце строки
- local afterCur = unicode.sub(self.savedText, 1 + self.x - self.xPosC, TextLen)
- self.xPosC = self.xPosC + 1
- self.savedText = symbol .. afterCur
- self:reDrawTextWithCursor(self.savedText)
- return true
- elseif self.xPosC ~= self.x and 1 + self.xPosC >= 1 + self.x + unicode.len(self.savedText) then -- Проверка, находится ли курсор в начале строки и символ текст
- self.xPosC = self.xPosC + 1
- self.savedText = self.savedText .. unicode.char(signal[3])
- self:reDrawTextWithCursor(self.savedText)
- return true
- elseif self.xPosC >= 1 + self.x and self.xPosC <= 1 + self.x + TextLen then -- пишу символ в центре строки
- self.savedText = unicode.sub(self.savedText, 1, self.xPosC - 1 - self.x) .. symbol .. unicode.sub(self.savedText, self.xPosC - self.x, TextLen)
- self.xPosC = self.xPosC + 1
- self:reDrawTextWithCursor(self.savedText)
- return true
- end
- end
- if signal[1] == "key_up" and signal[4] == 28 then
- return "focusedDroped"
- end
- elseif signal[1] ~= "key_up" and signal[1] == "key_down" then
- if signal[4] == 205 and self.xPosC <= self:lenSavedText() + self.x and self.xPosC ~= 1 + self.x + self.lengthField then -- 205 стрелка в право
- self.xPosC = self.xPosC + 1 -- Стрелка в право, курсор двигается в право.
- self:reDrawTextWithCursor(self.savedText)
- elseif signal[4] == 203 and self.xPosC >= 2 + self.x then -- 203 стрелка в лево
- self.xPosC = self.xPosC - 1 -- Стрелка в лево, курсор двигается в лево.
- self:reDrawTextWithCursor(self.savedText)
- end
- if signal[3] and signal[4] == 14 then --Стирал очка
- if self.xPosC >= 1 + self.x then
- local TextLen = self:lenSavedText()
- if self.xPosC == 1 + self.x + TextLen and TextLen > 0 then
- if self.xPosC ~= 1 + self.x then self.xPosC = self.xPosC - 1 end
- self.savedText = unicode.sub(self.savedText, 1, TextLen - 1)
- self:reDrawTextWithCursor(self.savedText)
- if self.savedText == "" then return "erase" end
- return true
- elseif self.xPosC >= 3 + self.x and self.xPosC <= self.x + TextLen then
- self.savedText = unicode.sub(self.savedText, 1, self.xPosC - 2 - self.x)..unicode.sub(self.savedText, self.xPosC - self.x, TextLen)
- self.xPosC = self.xPosC - 1
- self:reDrawTextWithCursor(self.savedText)
- if self.savedText == "" then return "erase" end
- return true
- elseif self.xPosC == 2 + self.x then
- self.savedText = unicode.sub(self.savedText, 2, TextLen)
- self.xPosC = self.xPosC - 1
- self:reDrawTextWithCursor(self.savedText)
- return true
- end
- return false
- end
- end
- elseif signal[1] == "clipboard" then
- local stringGeted = tostring(signal[3])
- local stringToPaste = ""
- if unicode.len(stringGeted) > self.lengthField then stringToPaste = unicode.sub(stringGeted, 1, self.lengthField) else stringToPaste = stringGeted end
- self.savedText = stringToPaste
- self:modifySavedTextAndPreCalcCursor(self.savedText)
- end
- end
- function obj:canFocusedToggle(bool)
- self.canFocused = bool
- end
- function obj:isFocused(signal)
- if signal and signal[1] == "touch" and signal[3] >= self.x and signal[3] <= self.x + self.lengthField and signal[4] == self.y then
- return true
- end
- return false
- end
- function obj:isDropFocused(signal)
- if self:eventTrap(signal) == "focusedDroped" then
- return true
- end
- return false
- end
- function obj:onFocused(_SIGNAL, errorText)
- if self:isFocused(_SIGNAL) then
- if self.canFocused then
- self.backgroundColor = 0xf8f9fa
- self:reDrawTextWithCursor(self.savedText)
- local blinkCursorCount = 0
- while true do
- computer.pushSignal("fakeEvent")
- blinkCursorCount = blinkCursorCount + 1
- if blinkCursorCount == 50 then self:blinkCursor() blinkCursorCount = 0 end
- if self:isDropFocused(self:signalHandler()) then
- self.backgroundColor = 0xb4b4b4
- self:reDrawText(tostring(self.savedText))
- return "focusedDroped"
- end
- end
- else
- self.backgroundColor = 0xf8f9fa
- self:reDrawText(tostring(errorText)) --"НЕ ВЫБРАЛ ГРУППУ!"
- computer.beep(600, 0.1)
- self.backgroundColor = 0xb4b4b4
- self:reDrawText(tostring(self.customInitTextOnField))
- return
- end
- end
- end
- function obj:debug(signal)
- -- if self.debugStatus then
- -- local x = 41
- -- gpuFill(abs(x- 1), 1, 50, 8, " ", 0xf8f9fa)
- -- gpuSet(x, 2, tostring(signal[3]), color.white, color.yellow)
- -- gpuSet(x, 3, tostring(signal[4]), color.white, color.yellow)
- -- gpuSet(x, 4, "xPosC: " .. tostring(self.xPosC), color.background, color.yellow)
- -- gpuSet(x, 5, "TextLen: " .. tostring(unicode.len(self.savedText)), color.background, color.red)
- -- gpuSet(x, 6, "savedText: " .. self.savedText, color.background, color.red)
- -- end
- end
- setmetatable(obj, self)
- self.__index = self
- return obj
- end
Advertisement
Add Comment
Please, Sign In to add comment