CharlyZM

Untitled

Feb 8th, 2023 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.08 KB | None | 0 0
  1.  
  2. function fieldSymbolInput:new(x, y, lengthField, cursorSymbol, customInitTextOnField, canInputOnlyNumbers)
  3. local obj = {}
  4. obj.onFocusedStatus = false
  5. obj.debugStatus = false
  6. obj.x = x
  7. obj.y = y
  8. obj.lengthField = lengthField
  9. obj.cursorSymbol = cursorSymbol
  10. obj.savedText = ""
  11. obj.xPosC = obj.x + 1
  12. obj.signalHandler = signalHandler
  13. obj.canFocused = false
  14. obj.canInputOnlyNumbers = canInputOnlyNumbers == nil and false or canInputOnlyNumbers
  15.  
  16. obj.backgroundColor = 0xb4b4b4
  17.  
  18. obj.customInitTextOnField = tostring(customInitTextOnField)
  19.  
  20. local privateVariable = {}
  21.  
  22. privateVariable.xPosC = obj.x + 1
  23. privateVariable.xWithLengthField = obj.x + obj.lengthField
  24.  
  25. function obj:changeOnFocusedStatus(status)
  26. self.onFocusedStatus = status
  27. end
  28.  
  29. function obj:dropFocused(signal)
  30. if signal[3] >= 2 + privateVariable.xWithLengthField and signal[3] <= 4 + privateVariable.xWithLengthField then
  31. self.onFocusedStatus = false
  32. end
  33. end
  34.  
  35. function obj:drawCrossButton()
  36. -----сивол X на кнопке стирания-------- 0x343a40
  37. gpuSet(self.x + self.lengthField + 2, self.y, " Г— ", 0x343a40, 0x0d6efd)
  38. -----сивол X на кнопке стирания-------
  39. end
  40. function obj:drawInit()
  41. --------------
  42. gpuFill(self.x, self.y, 2 + self.lengthField, 1, " ", self.backgroundColor)
  43. gpuSet(1 + self.x, self.y, self.customInitTextOnField, self.backgroundColor, 0x212529)
  44. ---------------0x212529
  45. self:drawCrossButton()
  46. end
  47.  
  48. obj:drawInit()
  49.  
  50.  
  51. function obj:customMatch(symbol)
  52. if not self.canInputOnlyNumbers then
  53. local result = symbol:match("^[A-Za-z0-9Рђ-РЇ-Р°-СЏ -_#&]")
  54. return result == nil and (symbol == "]" and symbol or (symbol == "[" and symbol or (symbol == "-" and symbol or nil))) or result
  55. end
  56. if tonumber(self.savedText == "" and 0 or self.savedText) < 100 then
  57. return symbol:match("^[0-9]")
  58. end
  59. return nil
  60. end
  61.  
  62. function obj:blinkCrossButton()
  63. gpuSet((3) + (self.lengthField) + self.x, self.y, "Г—", 0x343a40, 0x00b600)
  64. sleep(0.001)
  65. gpuSet((3) + (self.lengthField) + self.x, self.y, "Г—", 0x343a40, 0x0d6efd)
  66. end
  67.  
  68. function obj:drawCursor()
  69. gpu.setBackground(self.backgroundColor)
  70. gpu.setForeground(0xffc107)
  71. gpu.set(self.xPosC, self.y, self.cursorSymbol)
  72. end
  73.  
  74. function obj:blinkCursor()
  75. gpu.setBackground(self.backgroundColor)
  76. gpu.setForeground(0x00b600)
  77. gpu.set(self.xPosC, self.y, self.cursorSymbol)
  78. sleep(0.1)
  79. gpu.setForeground(0xffc107)
  80. gpu.set(self.xPosC, self.y, self.cursorSymbol)
  81. end
  82.  
  83. function obj:reDrawText(text)
  84. gpuFill(self.x, self.y, 2 + self.lengthField, 1, " ", self.backgroundColor)
  85. gpuSet(self.x + 1, self.y, tostring(text), self.backgroundColor, 0x212529)
  86. end
  87.  
  88. function obj:modifySavedTextAndPreCalcCursor(text)
  89. self.savedText = text
  90. self.xPosC = self.x + unicode.len(self.savedText) + 1
  91. self:reDrawText(self.savedText)
  92. end
  93.  
  94. function obj:reDrawTextWithCursor(text)
  95. gpuFill(self.x, self.y, 2 + self.lengthField, 1, " ", self.backgroundColor)
  96. gpuSet(self.x + 1, self.y, tostring(text), self.backgroundColor, 0x212529)
  97. self:drawCursor()
  98. end
  99.  
  100. function obj:reDrawTextWithCursorAndCross(text)
  101. self:reDrawTextWithCursor(text)
  102. self:drawCrossButton()
  103. end
  104.  
  105. function obj:lenSavedText()
  106. return unicode.len(self.savedText)
  107. end
  108.  
  109. function obj:eventTrap(signal)
  110. if signal[1] == "touch" then
  111. if signal[3] >= 2 + privateVariable.xWithLengthField and signal[3] <= 4 + privateVariable.xWithLengthField then --прожал X, отчистить
  112. obj:blinkCrossButton()
  113. self.xPosC = self.x + 1
  114. self.savedText = ""
  115. self:reDrawTextWithCursor("")
  116. obj:debug(signal)
  117. -- return "erase"
  118. end
  119. if signal[3] <= self.lengthField and signal[3] >= 4 + privateVariable.xWithLengthField or signal[4] ~= self.y then
  120. return "focusedDroped"
  121. end
  122. elseif signal[1] == "key_up" and signal[1] ~= "key_down" then
  123. local char, sChar = signal[3], signal[4]
  124. local TextLen = self:lenSavedText()
  125. local symbol = unicode.char(char)
  126. if signal[3] == 127 or signal[4] == 211 then --прожал delete, отчистить
  127. self:blinkCrossButton()
  128. self.xPosC = self.x + 1
  129. self.savedText = ""
  130. self:reDrawTextWithCursor("")
  131. obj:debug(signal)
  132. -- return "erase"
  133. end
  134. if self:customMatch(symbol) ~= nil and TextLen ~= self.lengthField then
  135. if self.xPosC == 1 + self.x then -- пишу символ в самом конце строки
  136. local afterCur = unicode.sub(self.savedText, 1 + self.x - self.xPosC, TextLen)
  137. self.xPosC = self.xPosC + 1
  138. self.savedText = symbol .. afterCur
  139. self:reDrawTextWithCursor(self.savedText)
  140. return true
  141.  
  142. elseif self.xPosC ~= self.x and 1 + self.xPosC >= 1 + self.x + unicode.len(self.savedText) then -- Проверка, находится ли курсор в начале строки и символ текст
  143. self.xPosC = self.xPosC + 1
  144. self.savedText = self.savedText .. unicode.char(signal[3])
  145. self:reDrawTextWithCursor(self.savedText)
  146. return true
  147.  
  148. elseif self.xPosC >= 1 + self.x and self.xPosC <= 1 + self.x + TextLen then -- пишу символ в центре строки
  149. self.savedText = unicode.sub(self.savedText, 1, self.xPosC - 1 - self.x) .. symbol .. unicode.sub(self.savedText, self.xPosC - self.x, TextLen)
  150. self.xPosC = self.xPosC + 1
  151. self:reDrawTextWithCursor(self.savedText)
  152. return true
  153. end
  154. end
  155. if signal[1] == "key_up" and signal[4] == 28 then
  156. return "focusedDroped"
  157. end
  158. elseif signal[1] ~= "key_up" and signal[1] == "key_down" then
  159. if signal[4] == 205 and self.xPosC <= self:lenSavedText() + self.x and self.xPosC ~= 1 + self.x + self.lengthField then -- 205 стрелка в право
  160. self.xPosC = self.xPosC + 1 -- Стрелка в право, курсор двигается в право.
  161. self:reDrawTextWithCursor(self.savedText)
  162. elseif signal[4] == 203 and self.xPosC >= 2 + self.x then -- 203 стрелка в лево
  163. self.xPosC = self.xPosC - 1 -- Стрелка в лево, курсор двигается в лево.
  164. self:reDrawTextWithCursor(self.savedText)
  165. end
  166. if signal[3] and signal[4] == 14 then --Стирал очка
  167. if self.xPosC >= 1 + self.x then
  168. local TextLen = self:lenSavedText()
  169. if self.xPosC == 1 + self.x + TextLen and TextLen > 0 then
  170. if self.xPosC ~= 1 + self.x then self.xPosC = self.xPosC - 1 end
  171. self.savedText = unicode.sub(self.savedText, 1, TextLen - 1)
  172. self:reDrawTextWithCursor(self.savedText)
  173. if self.savedText == "" then return "erase" end
  174. return true
  175.  
  176. elseif self.xPosC >= 3 + self.x and self.xPosC <= self.x + TextLen then
  177. self.savedText = unicode.sub(self.savedText, 1, self.xPosC - 2 - self.x)..unicode.sub(self.savedText, self.xPosC - self.x, TextLen)
  178. self.xPosC = self.xPosC - 1
  179. self:reDrawTextWithCursor(self.savedText)
  180. if self.savedText == "" then return "erase" end
  181. return true
  182.  
  183. elseif self.xPosC == 2 + self.x then
  184. self.savedText = unicode.sub(self.savedText, 2, TextLen)
  185. self.xPosC = self.xPosC - 1
  186. self:reDrawTextWithCursor(self.savedText)
  187. return true
  188. end
  189. return false
  190. end
  191. end
  192. elseif signal[1] == "clipboard" then
  193. local stringGeted = tostring(signal[3])
  194. local stringToPaste = ""
  195. if unicode.len(stringGeted) > self.lengthField then stringToPaste = unicode.sub(stringGeted, 1, self.lengthField) else stringToPaste = stringGeted end
  196. self.savedText = stringToPaste
  197. self:modifySavedTextAndPreCalcCursor(self.savedText)
  198. end
  199. end
  200.  
  201. function obj:canFocusedToggle(bool)
  202. self.canFocused = bool
  203. end
  204.  
  205. function obj:isFocused(signal)
  206. if signal and signal[1] == "touch" and signal[3] >= self.x and signal[3] <= self.x + self.lengthField and signal[4] == self.y then
  207. return true
  208. end
  209. return false
  210. end
  211.  
  212. function obj:isDropFocused(signal)
  213. if self:eventTrap(signal) == "focusedDroped" then
  214. return true
  215. end
  216. return false
  217. end
  218. function obj:onFocused(_SIGNAL, errorText)
  219. if self:isFocused(_SIGNAL) then
  220. if self.canFocused then
  221. self.backgroundColor = 0xf8f9fa
  222. self:reDrawTextWithCursor(self.savedText)
  223. local blinkCursorCount = 0
  224. while true do
  225. computer.pushSignal("fakeEvent")
  226. blinkCursorCount = blinkCursorCount + 1
  227. if blinkCursorCount == 50 then self:blinkCursor() blinkCursorCount = 0 end
  228. if self:isDropFocused(self:signalHandler()) then
  229. self.backgroundColor = 0xb4b4b4
  230. self:reDrawText(tostring(self.savedText))
  231. return "focusedDroped"
  232. end
  233. end
  234. else
  235. self.backgroundColor = 0xf8f9fa
  236. self:reDrawText(tostring(errorText)) --"НЕ ВЫБРАЛ ГРУППУ!"
  237. computer.beep(600, 0.1)
  238. self.backgroundColor = 0xb4b4b4
  239. self:reDrawText(tostring(self.customInitTextOnField))
  240. return
  241. end
  242. end
  243. end
  244.  
  245.  
  246. function obj:debug(signal)
  247. -- if self.debugStatus then
  248. -- local x = 41
  249. -- gpuFill(abs(x- 1), 1, 50, 8, " ", 0xf8f9fa)
  250. -- gpuSet(x, 2, tostring(signal[3]), color.white, color.yellow)
  251. -- gpuSet(x, 3, tostring(signal[4]), color.white, color.yellow)
  252. -- gpuSet(x, 4, "xPosC: " .. tostring(self.xPosC), color.background, color.yellow)
  253. -- gpuSet(x, 5, "TextLen: " .. tostring(unicode.len(self.savedText)), color.background, color.red)
  254. -- gpuSet(x, 6, "savedText: " .. self.savedText, color.background, color.red)
  255. -- end
  256. end
  257.  
  258. setmetatable(obj, self)
  259. self.__index = self
  260. return obj
  261. end
Advertisement
Add Comment
Please, Sign In to add comment