quenti77

Editor for OpenComputers

Oct 28th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.52 KB | None | 0 0
  1. --[[
  2. Filename: open.lua
  3. Author: quenti77
  4.  
  5. Description:
  6.  
  7. --]]
  8.  
  9. -- Require
  10. local event = require "event"
  11. local component = require "component"
  12. local unicode = require "unicode"
  13. local shell = require "shell"
  14. local fs = require "filesystem"
  15.  
  16. -- Configuration
  17. local config = {
  18.     defaultColor = {
  19.         back = 0x263238,
  20.         front = 0xC3CEE3
  21.     },
  22.  
  23.     menuColor = {
  24.         bar = {
  25.             back = 0x304047,
  26.             front = 0xC3CEE3
  27.         },
  28.         quitButton = {
  29.             back = 0xC41F1A,
  30.             front = 0xC3CEE3
  31.         },
  32.         select = {
  33.             back = 0xC7C7C7,
  34.             front = 0x383838
  35.         },
  36.         sub = {
  37.             back = 0xA1A1A1,
  38.             front = 0x252525
  39.         }
  40.     },
  41.  
  42.     statusColor = {
  43.         bar = {
  44.             back = 0x304047,
  45.             front = 0xC3CEE3,
  46.             normal = 0x3A8C4C,
  47.             updated = 0xF08C65
  48.         }
  49.     },
  50.  
  51.     editorColor = {
  52.         lineNumber = {
  53.             back = 0x34444c,
  54.             front = 0xC3CEE3
  55.         },
  56.         content = {
  57.             back = 0x263238,
  58.             front = 0xC3CEE3
  59.         }
  60.     },
  61.  
  62.     modalColor = {
  63.         background = {
  64.             back = 0x485F6A,
  65.             front = 0xC3CEE3
  66.         },
  67.  
  68.         titleBar = {
  69.             back = 0x34444c,
  70.             front = 0xC3CEE3
  71.         },
  72.  
  73.         textbox = {
  74.             back = 0xF0F3F5,
  75.             front = 0x383838
  76.         },
  77.  
  78.         button = {
  79.             success = {
  80.                 back = 0x00802B,
  81.                 front = 0xC3CEE3
  82.             },
  83.             cancel = {
  84.                 back = 0xB30000,
  85.                 front = 0xC3CEE3
  86.             }
  87.         }
  88.     },
  89.  
  90.     screen = {
  91.         width = 154,
  92.         height = 50
  93.     },
  94.  
  95.     screenRefresh = 0.05
  96. }
  97.  
  98. -- Menu
  99. local menu = {
  100.     file = {
  101.         x = 1,
  102.         textX = 3,
  103.         width = 8,
  104.         name = "File",
  105.         items = {
  106.             x = 1,
  107.             y = 2,
  108.             width = 11,
  109.             height = 7,
  110.             items = {{
  111.                 x = 3,
  112.                 y = 2,
  113.                 name = "New"
  114.             }, {
  115.                 x = 3,
  116.                 y = 3,
  117.                 name = "Open"
  118.             }, {
  119.                 x = 1,
  120.                 y = 4,
  121.                 name = "-----------"
  122.             }, {
  123.                 x = 3,
  124.                 y = 5,
  125.                 name = "Save"
  126.  
  127.             }, {
  128.                 x = 3,
  129.                 y = 6,
  130.                 name = "Save As"
  131.             }, {
  132.                 x = 1,
  133.                 y = 7,
  134.                 name = "-----------"
  135.             }, {
  136.                 x = 3,
  137.                 y = 8,
  138.                 name = "Quit"
  139.             }}
  140.         }
  141.     }
  142. }
  143.  
  144. -- Status
  145. local status = {
  146.     menuOpen = {
  147.         file = false
  148.     },
  149.  
  150.     filename = nil,
  151.     updated = false,
  152.     blink = false
  153. }
  154.  
  155. -- File in editor
  156. local files = {
  157.     cursor = {
  158.         line = 1,
  159.         column = 1
  160.     },
  161.     content = nil,
  162.     view = {
  163.         top = 1,
  164.         left = 1
  165.     }
  166. }
  167.  
  168. -- Modal
  169. local modal = {
  170.     open = false,
  171.     value = "",
  172.     action =  nil,
  173.     title = ""
  174. }
  175.  
  176. -- Other variable
  177. local running = true
  178. local gpu = component.gpu
  179. local blinkConter = 0
  180.  
  181. -- Basic function
  182. function checkPos(x, y, xmin, ymin, xmax, ymax)
  183.     return ((x >= xmin and x <= xmax) and (y >= ymin and y <= ymax))
  184. end
  185.  
  186. -- Display function
  187. function clearScreen()
  188.     gpu.setBackground(config.defaultColor.back)
  189.     gpu.setForeground(config.defaultColor.front)
  190.     gpu.fill(1, 1, config.screen.width, config.screen.height, " ")
  191. end
  192.  
  193. function menuFile()
  194.     if (status.menuOpen.file) then
  195.         gpu.setBackground(config.menuColor.select.back)
  196.         gpu.setForeground(config.menuColor.select.front)
  197.         gpu.fill(menu.file.x, 1, menu.file.width, 1, " ")
  198.         gpu.set(menu.file.textX, 1, menu.file.name)
  199.  
  200.         gpu.setBackground(config.menuColor.sub.back)
  201.         gpu.setForeground(config.menuColor.sub.front)
  202.         gpu.fill(menu.file.items.x, menu.file.items.y, menu.file.items.width, menu.file.items.height, " ")
  203.  
  204.         for i = 1, #menu.file.items.items do
  205.             local item = menu.file.items.items[i]
  206.             gpu.set(item.x, item.y, item.name)
  207.         end
  208.     else
  209.         gpu.setBackground(config.menuColor.bar.back)
  210.         gpu.setForeground(config.menuColor.bar.front)
  211.         gpu.fill(menu.file.x, 1, menu.file.width, 1, " ")
  212.         gpu.set(menu.file.textX, 1, menu.file.name)
  213.     end
  214. end
  215.  
  216. function menuScreen()
  217.     -- Bar
  218.     gpu.setBackground(config.menuColor.bar.back)
  219.     gpu.setForeground(config.menuColor.bar.front)
  220.     gpu.fill(1, 1, config.screen.width, 1, " ")
  221.  
  222.     -- X button for close app
  223.     gpu.setBackground(config.menuColor.quitButton.back)
  224.     gpu.setForeground(config.menuColor.quitButton.front)
  225.     gpu.fill(config.screen.width - 2, 1, 3, 1, " ")
  226.     gpu.set(config.screen.width - 1, 1, "×")
  227. end
  228.  
  229. function statusScreen()
  230.     -- Bar
  231.     gpu.setBackground(config.statusColor.bar.back)
  232.     gpu.setForeground(config.statusColor.bar.front)
  233.     gpu.fill(1, config.screen.height, config.screen.width, 1, " ")
  234.  
  235.     local textRight = string.format("L: %u ¦ C: %u", files.cursor.line, files.cursor.column)
  236.     local length = unicode.len(textRight)
  237.     gpu.set(config.screen.width - 1 - length, config.screen.height, textRight)
  238.  
  239.     local textLeft = "Aucun fichier"
  240.     if (status.filename ~= nil) then
  241.         if (unicode.len(status.filename) > 50) then
  242.             textLeft = unicode.sub(status.filename, -50)
  243.         else
  244.             textLeft = status.filename
  245.         end
  246.  
  247.         gpu.setForeground(config.statusColor.bar.normal)
  248.         if (status.updated) then
  249.             gpu.setForeground(config.statusColor.bar.updated)
  250.         end
  251.     end
  252.     gpu.set(3, config.screen.height, textLeft)
  253. end
  254.  
  255. function editorScreen()
  256.     local content = files.content
  257.     local contentSize = #content
  258.     local top = files.view.top
  259.     local bottom = top + config.screen.height - 3
  260.  
  261.     if (bottom > contentSize) then
  262.         bottom = contentSize
  263.     end
  264.  
  265.     local lengthBottom = unicode.len(string.format("%u", bottom))
  266.     local gutterWidth = lengthBottom + 2
  267.     local width = config.screen.width - gutterWidth - 2
  268.  
  269.     gpu.setBackground(config.editorColor.lineNumber.back)
  270.     gpu.setForeground(config.editorColor.lineNumber.front)
  271.     gpu.fill(1, 2, gutterWidth, config.screen.height - 2, " ")
  272.  
  273.     for line = top, bottom do
  274.         gpu.set(2 + lengthBottom - (unicode.len(string.format("%u", line))), line - top + 2, string.format("%u", line))
  275.     end
  276.  
  277.     gpu.setBackground(config.editorColor.content.back)
  278.     gpu.setForeground(config.editorColor.content.front)
  279.     for line = top, bottom do
  280.         gpu.set(gutterWidth + 2, line - top + 2, unicode.sub(content[line], files.view.left, files.view.left + width))
  281.     end
  282.  
  283.     -- ColorCursor
  284.     if (status.blink) then
  285.         local t = files.cursor.line - top + 2
  286.         local l = gutterWidth + 1 + files.cursor.column
  287.         gpu.setBackground(config.editorColor.content.front)
  288.         gpu.setForeground(config.editorColor.content.back)
  289.         gpu.set(l, t, unicode.sub(files.content[files.cursor.line], files.cursor.column, files.cursor.column))
  290.     end
  291. end
  292.  
  293. function modalScreen()
  294.     local x = 15
  295.     local y = config.screen.height / 2 - 2
  296.     local w = config.screen.width - 30
  297.     local h = 6
  298.     local middleW = config.screen.width / 2
  299.  
  300.     -- Modal
  301.     gpu.setBackground(config.modalColor.background.back)
  302.     gpu.setForeground(config.modalColor.background.front)
  303.     gpu.fill(x, y, w, h, " ")
  304.  
  305.     -- Titlebar
  306.     gpu.setBackground(config.modalColor.titleBar.back)
  307.     gpu.setForeground(config.modalColor.titleBar.front)
  308.     gpu.fill(x, y, w, 1, " ")
  309.     gpu.set(x + 2, y, modal.title)
  310.  
  311.     gpu.setBackground(config.menuColor.quitButton.back)
  312.     gpu.setForeground(config.menuColor.quitButton.front)
  313.     gpu.fill(x + w - 3, y, 3, 1, " ")
  314.     gpu.set(x + w - 2, y, "×")
  315.  
  316.     -- Textbox
  317.     local contentTextbox = modal.value
  318.     if (status.blink) then
  319.         contentTextbox = contentTextbox.."|"
  320.     end
  321.  
  322.     gpu.setBackground(config.modalColor.textbox.back)
  323.     gpu.setForeground(config.modalColor.textbox.front)
  324.     gpu.fill(x + 2, y + 2, w - 4, 1, " ")
  325.     gpu.set(x + 2, y + 2, contentTextbox)
  326.  
  327.     -- Buttons
  328.     gpu.setBackground(config.modalColor.button.success.back)
  329.     gpu.setForeground(config.modalColor.button.success.front)
  330.     gpu.fill(middleW - 10, y + 4, 10, 1, " ")
  331.     gpu.set(middleW - 8, y + 4, "  Ok  ")
  332.  
  333.     gpu.setBackground(config.modalColor.button.cancel.back)
  334.     gpu.setForeground(config.modalColor.button.cancel.front)
  335.     gpu.fill(middleW + 2, y + 4, 10, 1, " ")
  336.     gpu.set(middleW + 4, y + 4, "Cancel")
  337. end
  338.  
  339. function updateScreen()
  340.     clearScreen()
  341.     menuScreen()
  342.     statusScreen()
  343.  
  344.     if (files.content ~= nil) then
  345.         editorScreen()
  346.     end
  347.  
  348.     blinkConter = blinkConter + 1
  349.     if (blinkConter >= 10) then
  350.         blinkConter = 0
  351.         status.blink = not status.blink
  352.     end
  353.  
  354.     menuFile()
  355.  
  356.     if (modal.open) then
  357.         modalScreen()
  358.     end
  359. end
  360.  
  361. function clearContent()
  362.     if (files.content ~= nil) then
  363.         for k, v in pairs(files.content) do
  364.             files.content[k] = nil
  365.         end
  366.         files.content = nil
  367.     end
  368. end
  369.  
  370. function saveFile(filename)
  371.     local f = io.open(filename, "w")
  372.  
  373.     if (f ~= nil) then
  374.         for k, v in pairs(files.content) do
  375.             local l = files.content[k]
  376.  
  377.             f:write(unicode.sub(l, 1, unicode.len(l) - 1).."\n")
  378.         end
  379.         f:close()
  380.     end
  381. end
  382.  
  383. function openFile(filename)
  384.     if (unicode.sub(filename, 1, 1) ~= "/") then
  385.         filename = "/"..filename
  386.     end
  387.  
  388.     if (fs.exists(filename) and not fs.isDirectory(filename)) then
  389.         local f = io.open(filename, "r")
  390.  
  391.         if (f ~= nil) then
  392.             local l = 1
  393.  
  394.             clearContent()
  395.             files.content = {}
  396.  
  397.             for line in f:lines() do
  398.                 files.content[l] = line.." "
  399.                 l = l + 1
  400.             end
  401.  
  402.             f:close()
  403.  
  404.             if (#files.content == 0) then
  405.                 files.content = {" "}
  406.             end
  407.  
  408.             files.cursor.line = 1
  409.             files.cursor.column = 1
  410.             files.view.top = 1
  411.             files.view.left = 1
  412.  
  413.             status.filename = filename
  414.         end
  415.     end
  416. end
  417.  
  418. function menuFileClick(item)
  419.     local filename = ""
  420.     if (status.filename ~= nil) then
  421.         filename = status.filename
  422.     end
  423.  
  424.     if (item.name == "New") then
  425.         modal.open = true
  426.         modal.title = "Nouveau fichier ..."
  427.         modal.action = "new"
  428.         modal.value = shell.getWorkingDirectory()
  429.         status.menuOpen.file = false
  430.     elseif (item.name == "Open") then
  431.         modal.open = true
  432.         modal.title = "Ouvrir un fichier ..."
  433.         modal.action = "open"
  434.         modal.value = shell.getWorkingDirectory()
  435.         status.menuOpen.file = false
  436.     elseif (item.name == "Save") then
  437.         if (filename == "") then
  438.             item.name = "Save As"
  439.             menuFileClick(item)
  440.         else
  441.             saveFile(filename)
  442.             status.updated = false
  443.         end
  444.  
  445.         status.menuOpen.file = false
  446.     elseif (item.name == "Save As") then
  447.         modal.open = true
  448.         modal.title = "Enregistrer sous ..."
  449.         modal.action = "saveAs"
  450.  
  451.         if (filename == "") then
  452.             modal.value = shell.getWorkingDirectory()
  453.         else
  454.             modal.value = filename
  455.         end
  456.  
  457.         status.menuOpen.file = false
  458.     elseif (item.name == "Quit") then
  459.         running = false
  460.     end
  461. end
  462.  
  463. -- Handler event
  464. function unknownEvent()
  465.     --print("Unknown event called")
  466. end
  467.  
  468. local handler = setmetatable({}, { __index = function() return unknownEvent end })
  469.  
  470. function modalTouch()
  471.     local filename = modal.value
  472.  
  473.     if (modal.action == "new") then
  474.         clearContent()
  475.         files.content = {" "}
  476.     elseif (modal.action == "open") then
  477.         openFile(filename)
  478.     elseif (modal.action == "saveAs") then
  479.         saveFile(filename)
  480.     end
  481.  
  482.     status.filename = filename
  483.     status.updated = false
  484. end
  485.  
  486. function handler.touch(address, x, y, button, playerName)
  487.     if (checkPos(x, y, config.screen.width - 2, 1, config.screen.width, 1)) then
  488.         running = false
  489.         return true
  490.     end
  491.  
  492.     if (not modal.open) then
  493.         if (not status.menuOpen.file and checkPos(x, y, menu.file.x, 1, menu.file.width, 1)) then
  494.             status.menuOpen.file = true
  495.             return true
  496.         end
  497.         if (status.menuOpen.file) then
  498.             local itemClicked
  499.  
  500.             for i = 1, #menu.file.items.items do
  501.                 local item = menu.file.items.items[i]
  502.  
  503.                 if (checkPos(x, y, menu.file.items.x, item.y, menu.file.items.width, item.y)) then
  504.                     itemClicked = item
  505.                 end
  506.             end
  507.  
  508.             if (itemClicked ~= nil) then
  509.                 menuFileClick(itemClicked)
  510.             else
  511.                 status.menuOpen.file = false
  512.             end
  513.  
  514.             return true
  515.         end
  516.  
  517.         local content = files.content
  518.         if (content == nil) then
  519.             return true
  520.         end
  521.  
  522.         local contentSize = #content
  523.         local top = files.view.top
  524.         local bottom = top + config.screen.height - 3
  525.  
  526.         if (bottom > contentSize) then
  527.             bottom = contentSize
  528.         end
  529.  
  530.         local lengthBottom = unicode.len(string.format("%u", bottom))
  531.         local gutterWidth = lengthBottom + 2
  532.         local width = config.screen.width - gutterWidth - 2
  533.  
  534.         if (checkPos(x, y, gutterWidth + 2, 2, config.screen.width, config.screen.height - 2)) then
  535.             local line = top + (y - 2)
  536.             local column = x - gutterWidth - files.view.left
  537.  
  538.             if (line < 1) then
  539.                 line = 1
  540.             end
  541.             if (line > #files.content) then
  542.                 line = #files.content
  543.             end
  544.  
  545.             local columnMax = unicode.len(files.content[line])
  546.             if (column < 1) then
  547.                 column = 1
  548.             end
  549.             if (column > columnMax) then
  550.                 column = columnMax
  551.             end
  552.  
  553.             files.cursor.line = line
  554.             files.cursor.column = column
  555.  
  556.             blinkConter = 0
  557.             status.blink = true
  558.         end
  559.     else
  560.         local xStart = 15
  561.         local yStart = config.screen.height / 2 - 2
  562.         local w = config.screen.width - 30
  563.         local h = 5
  564.         local middleW = config.screen.width / 2
  565.  
  566.         -- Cancel button
  567.         if (checkPos(x, y, middleW + 2, yStart + 4, middleW + 11, yStart + 5)) then
  568.             modal.open = false
  569.         end
  570.  
  571.         -- Ok button
  572.         if (checkPos(x, y, middleW - 10, yStart + 4, middleW - 1, yStart + 5)) then
  573.             modalTouch()
  574.             modal.open = false
  575.         end
  576.     end
  577.  
  578.     return false
  579. end
  580.  
  581. function handler.scroll(address, x, y, direction, playerName)
  582.     if (not modal.open) then
  583.         local newTop = files.view.top + (direction * -1)
  584.         local maxTop = #files.content - 5
  585.  
  586.         if (newTop < 1) then
  587.             newTop = 1
  588.         elseif (newTop > maxTop) then
  589.             newTop = maxTop
  590.         end
  591.  
  592.         files.view.top = newTop
  593.     end
  594. end
  595.  
  596. function keyEditor(char, code)
  597.     local line = files.cursor.line
  598.     local column = files.cursor.column
  599.     local direction = 0
  600.     local updated = false
  601.  
  602.     if (files.content == nil) then
  603.         return true
  604.     end
  605.  
  606.     if (code == 0xC8) then
  607.         -- up
  608.         line = line - 1
  609.     elseif (code == 0xCB) then
  610.         -- left
  611.         column = column - 1
  612.     elseif (code == 0xD0) then
  613.         -- down
  614.         line = line + 1
  615.     elseif (code == 0xCD) then
  616.         -- right
  617.         column = column + 1
  618.     elseif (code == 0x2A or code == 0x36) then
  619.         -- shift
  620.     elseif (code == 0x1D or code == 0x9D) then
  621.         -- ctrl
  622.     elseif (code == 0x38 or code == 0xB8) then
  623.         -- alt
  624.     elseif (code == 0xC7) then
  625.         -- home
  626.         column = 1
  627.     elseif (code == 0x0F) then
  628.         -- tab
  629.         for i = 1, 2 do
  630.             keyEditor(string.byte(" "), 0x39)
  631.         end
  632.  
  633.         column = column + 2
  634.         updated = true
  635.     elseif (code == 0xCF) then
  636.         -- end
  637.         column = unicode.len(files.content[line])
  638.     elseif (code == 0x1C) then
  639.         -- enter
  640.         local contentLine = files.content[line]
  641.         local saveLine = ""
  642.  
  643.         if (column > 1) then
  644.             saveLine = unicode.sub(contentLine, 1, column - 1)
  645.         end
  646.  
  647.         local newLine = unicode.sub(contentLine, column, unicode.len(contentLine))
  648.  
  649.         local spaces = ""
  650.         local posChar = 1
  651.         local charTest = unicode.sub(saveLine, posChar, posChar)
  652.  
  653.         while (charTest == " ") do
  654.             spaces = spaces.." "
  655.             posChar = posChar + 1
  656.             charTest = unicode.sub(saveLine, posChar, posChar)
  657.         end
  658.  
  659.         local countSpace = unicode.len(spaces)
  660.  
  661.         column = countSpace + 1
  662.         files.content[line] = saveLine.." "
  663.  
  664.         line = line + 1
  665.         table.insert(files.content, line, spaces..newLine)
  666.         updated = true
  667.     elseif (code == 0x0E) then
  668.         -- backspace
  669.         local lineContent = files.content[line]
  670.         if (column == 1) then
  671.             if (line == 1) then
  672.                 return true
  673.             end
  674.             table.remove(files.content, line)
  675.             line = line - 1
  676.             column = unicode.len(files.content[line])
  677.  
  678.             lineContent = unicode.sub(files.content[line], 1, unicode.len(files.content[line]) - 1)..lineContent
  679.         else
  680.             lineContent = unicode.sub(lineContent, 1, files.cursor.column - 2)..unicode.sub(lineContent, files.cursor.column, unicode.len(lineContent))
  681.             column = column - 1
  682.         end
  683.  
  684.         files.content[line] = lineContent
  685.         updated = true
  686.     elseif (code == 0xD3) then
  687.         -- del
  688.         local lineContent = files.content[line]
  689.         local sizeLineContent = unicode.len(lineContent)
  690.        
  691.         if (column == sizeLineContent) then
  692.             local lineDownContent = files.content[line + 1]
  693.            
  694.             if (lineDownContent ~= nil) then
  695.                 lineContent = unicode.sub(lineContent, 1, sizeLineContent - 1)
  696.                 files.content[line] = lineContent..lineDownContent
  697.                 table.remove(files.content, line + 1)
  698.             end
  699.         else
  700.             local before = ""
  701.             if (column > 1) then
  702.                 before = unicode.sub(lineContent, 1, column - 1)
  703.             end
  704.            
  705.             local after = unicode.sub(lineContent, column + 1, sizeLineContent)
  706.             files.content[line] = before..""..after
  707.         end
  708.        
  709.         update = true
  710.     else
  711.         local lineContent = files.content[files.cursor.line]
  712.         lineContent = unicode.sub(lineContent, 1, files.cursor.column - 1)..unicode.char(char)..unicode.sub(lineContent, files.cursor.column, unicode.len(lineContent))
  713.         files.content[files.cursor.line] = lineContent
  714.  
  715.         column = column + 1
  716.         updated = true
  717.     end
  718.  
  719.     if (line < 1) then
  720.         line = 1
  721.     end
  722.     if (line > #files.content) then
  723.         line = #files.content
  724.     end
  725.  
  726.     local columnMax = unicode.len(files.content[line])
  727.     if (column < 1) then
  728.         column = 1
  729.     end
  730.     if (column > columnMax) then
  731.         column = columnMax
  732.     end
  733.  
  734.     if (files.view.top > line) then
  735.         direction = files.view.top - line
  736.     end
  737.     if ((files.view.top + config.screen.height - 3) < line) then
  738.         direction = (files.view.top + config.screen.height - 3) - line
  739.     end
  740.  
  741.     if (direction ~= 0) then
  742.         handler.scroll(address, 0, 0, direction, playerName)
  743.     end
  744.  
  745.     files.cursor.line = line
  746.     files.cursor.column = column
  747.  
  748.     if (updated) then
  749.         status.updated = true
  750.     end
  751. end
  752.  
  753. function keyModal(char, code)
  754.     if (code == 0xC8) then
  755.         -- up
  756.     elseif (code == 0xCB) then
  757.         -- left
  758.     elseif (code == 0xD0) then
  759.         -- down
  760.     elseif (code == 0xCD) then
  761.         -- right
  762.     elseif (code == 0x2A or code == 0x36) then
  763.         -- shift
  764.     elseif (code == 0x1D or code == 0x9D) then
  765.         -- ctrl
  766.     elseif (code == 0x38 or code == 0xB8) then
  767.         -- alt
  768.     elseif (code == 0xC7) then
  769.         -- home
  770.     elseif (code == 0xCF) then
  771.         -- end
  772.     elseif (code == 0x1C) then
  773.         -- enter
  774.         modalTouch()
  775.         modal.open = false
  776.     elseif (code == 0x0E) then
  777.         -- backspace
  778.         modal.value = unicode.sub(modal.value, 1, unicode.len(modal.value) - 1)
  779.     elseif (code == 0xD3) then
  780.         -- del
  781.     else
  782.         modal.value = modal.value..unicode.char(char)
  783.     end
  784. end
  785.  
  786. function handler.key_down(address, char, code, playerName)
  787.     if (modal.open) then
  788.         keyModal(char, code)
  789.     else
  790.         keyEditor(char, code)
  791.     end
  792. end
  793.  
  794. function handleEvent(eventId, ...)
  795.     if (eventId) then
  796.         handler[eventId](...)
  797.         --updateScreen()
  798.     end
  799. end
  800.  
  801. -- Start app
  802. local args = { ... }
  803.  
  804. if (#args == 1) then
  805.     openFile(shell.getWorkingDirectory()..args[1])
  806. end
  807.  
  808. gpu.setResolution(config.screen.width, config.screen.height)
  809.  
  810. local timerId = event.timer(config.screenRefresh, updateScreen, math.huge)
  811. while running do
  812.     handleEvent(event.pull())
  813. end
  814.  
  815. -- Stop app
  816. event.cancel(timerId)
  817. clearScreen()
  818. shell.execute("clear")
Advertisement
Add Comment
Please, Sign In to add comment