Advertisement
Piorjade

fsEAPI (fsExpose graphics API)

Dec 29th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.41 KB | None | 0 0
  1. --[[
  2.          _         _            _     _      _              _          _            _            _
  3.         /\ \      / /\         /\ \ /_/\    /\ \           /\ \       /\ \         / /\         /\ \
  4.        /  \ \    / /  \       /  \ \\ \ \   \ \_\         /  \ \     /  \ \       / /  \       /  \ \
  5.       / /\ \ \  / / /\ \__   / /\ \ \\ \ \__/ / /        / /\ \ \   / /\ \ \     / / /\ \__   / /\ \ \
  6.      / / /\ \_\/ / /\ \___\ / / /\ \_\\ \__ \/_/        / / /\ \_\ / / /\ \ \   / / /\ \___\ / / /\ \_\
  7.     / /_/_ \/_/\ \ \ \/___// /_/_ \/_/ \/_/\__/\       / / /_/ / // / /  \ \_\  \ \ \ \/___// /_/_ \/_/
  8.    / /____/\    \ \ \     / /____/\     _/\/__\ \     / / /__\/ // / /   / / /   \ \ \     / /____/\
  9.   / /\____\/_    \ \ \   / /\____\/    / _/_/\ \ \   / / /_____// / /   / / /_    \ \ \   / /\____\/
  10.  / / /     /_/\__/ / /  / / /______   / / /   \ \ \ / / /      / / /___/ / //_/\__/ / /  / / /______
  11. / / /      \ \/___/ /  / / /_______\ / / /    /_/ // / /      / / /____\/ / \ \/___/ /  / / /_______\
  12. \/_/        \_____\/   \/__________/ \/_/     \_\/ \/_/       \/_________/   \_____\/   \/__________/
  13.  
  14. fsExpose graphics API
  15.  
  16. ~made by Piorjade
  17.  
  18. to be expanded
  19. ]]
  20.  
  21. --[[
  22.     example items table
  23. local items = {
  24.     ['mybutton'] = {
  25.         type = "button",
  26.         text = "next",
  27.         length = 4,
  28.         x = 2,
  29.         y = 2,
  30.         bg = colors.gray,
  31.         fg = colors.blue,
  32.     }
  33. }
  34. ]]
  35. local oldTerm = term.current()
  36. local maxX, maxY = term.getSize()
  37. local mainWindow = window.create(oldTerm, 1, 1, maxX, maxY)
  38. term.redirect(mainWindow)
  39. function addButton(items, name, text, length, x, y)
  40.     -- check for errors in arguments
  41.     if type(items) ~= "table" then return false, "table expected" end
  42.     if type(name) ~= "string" or #name < 1 then return false, "no name given" end
  43.     if type(text) ~= "string" then return false, "no text given" end
  44.     if type(length) ~= "number" or length < 1 then return false, "no given length" end
  45.     if type(x) ~= "number" or x < 1 then return false, "no x coordinate given" end
  46.     if type(y) ~= "number" or y < 1 then return false, "no y coordinate given" end
  47.     --errorcheck end
  48.     --insert into table; NOTE: this will delete items with the same name
  49.     items[name] = {}
  50.     items[name]['type'] = "button"
  51.     items[name]['text'] = text
  52.     items[name]['length'] = length
  53.     items[name]['bg'] = colors.lightGray
  54.     items[name]['fg'] = colors.blue
  55.     items[name]['x'] = x
  56.     items[name]['y'] = y
  57.     return true
  58. end
  59.  
  60. function addToggle(items, name, x, y)
  61.     -- check for errors in arguments
  62.     if type(items) ~= "table" then return false, "table expected" end
  63.     if type(name) ~= "string" or #name < 1 then return false, "no name given" end
  64.     if type(x) ~= "number" or x < 1 then return false, "no x coordinate given" end
  65.     if type(y) ~= "number" or y < 1 then return false, "no y coordinate given" end
  66.     --errorcheck end
  67.     items[name] = {}
  68.     items[name]['type'] = "toggle"
  69.     items[name]['toggled'] = false
  70.     items[name]['x'] = x
  71.     items[name]['y'] = y
  72.     return true
  73. end
  74.  
  75. function addContextMenu(scrollBox, tableChilds)
  76.     -- check for errors in arguments
  77.     if type(scrollBox) ~= "table" then return false, "table expected" end
  78.     if type(tableChilds) ~= "table" or #tableChilds < 1 then return false, "no table of childs" end
  79.     --errorcheck end
  80.     --insert the contextmenu in the scrollBox, NOTE: an already context menu will be deleted
  81.     scrollBox['context'] = {}
  82.     scrollBox.context['list'] = {}
  83.     scrollBox['context']['window'] = window.create(oldTerm, 1, 1, 20, #tableChilds, false)
  84.     term.redirect(scrollBox.context.window)
  85.     for k, v in ipairs(tableChilds) do
  86.         term.setBackgroundColor(v.bg)
  87.         term.setTextColor(v.fg)
  88.         term.clearLine()
  89.         term.write(v.text)
  90.         local oldx, oldy = term.getCursorPos()
  91.         term.setCursorPos(1, oldy+1)
  92.         table.insert(scrollBox.context.list, v)
  93.     end
  94.     term.redirect(mainWindow)
  95.     return true
  96. end
  97.  
  98. function addScrollBox(items, name, lengthx, lengthy, x, y)
  99.     -- check for errors in arguments
  100.     if type(items) ~= "table" then return false, "table expected" end
  101.     if type(name) ~= "string" or #name < 1 then return false, "no name given" end
  102.     if type(lengthx) ~= "number" or lengthx < 1 then return false, "no given lengthX" end
  103.     if type(lengthy) ~= "number" or lengthy < 1 then return false, "no given lengthY" end
  104.     if type(x) ~= "number" or x < 1 then return false, "no x coordinate given" end
  105.     if type(y) ~= "number" or y < 1 then return false, "no y coordinate given" end
  106.     --errorcheck end
  107.     --insert into table; NOTE: this will delete items with the same name
  108.     items[name] = {}
  109.     items[name]['type'] = "scrollBox"
  110.     items[name]['lengthx'] = lengthx
  111.     items[name]['lengthy'] = lengthy
  112.     items[name]['bg'] = colors.lightGray
  113.     items[name]['fg'] = colors.lime
  114.     items[name]['x'] = x
  115.     items[name]['y'] = y
  116.     --how many childs are above, outside the box
  117.     items[name]['missing'] = 0
  118.     --how many childs are below, outside the box
  119.     items[name]['left'] = 0
  120.     --table of strings, these will act like buttons, which stay selected
  121.     items[name]['childs'] = {}
  122.     --EXAMPLE:
  123.     --[[
  124.     items[name]['childs'] = {
  125.         {
  126.             text = "myFile",
  127.             selected = false,
  128.  
  129.         },
  130.         {
  131.             text = "mySecondFile",
  132.             selected = true,
  133.         }
  134.     }
  135.     ]]
  136.     return true
  137. end
  138.  
  139. function addChild(scrollBox, str)
  140.     --add a child into a scrollbox
  141.     --errorcheck
  142.     if type(scrollBox) ~= "table" then return false, "table expected" end
  143.     if type(str) ~= "string" then return false, "string expected" end
  144.     --errorcheck end
  145.     --insert into table; NOTE: child with the same name will be removed
  146.     local tab = {}
  147.     tab['selected'] = false
  148.     tab['text'] = str
  149.     tab['fg'] = colors.lime
  150.     tab['bg'] = colors.lightGray
  151.     table.insert(scrollBox.childs, tab)
  152.     if #scrollBox.childs > scrollBox.lengthy then
  153.         scrollBox.left = scrollBox.left+1
  154.     end
  155.     return true
  156. end
  157.  
  158. function clearChilds(scrollBox)
  159.     --remove all childs from the scrollbox
  160.     --errorcheck
  161.     if type(scrollBox) ~= "table" then return false, "table expected" end
  162.     --errorcheck end
  163.     scrollBox.left = 0
  164.     scrollBox.missing = 0
  165.     scrollBox.childs = {}
  166.     return true
  167. end
  168.  
  169. function removeChild(scrollBox, key)
  170.     --remove a child from the scrollbox
  171.     --errorcheck
  172.     if type(scrollBox) ~= "table" then return false, "table expected" end
  173.     if type(key) ~= "number" then return false, "string expected" end
  174.     --errorcheck end
  175.     scrollBox.left = scrollBox.left - 1
  176.     if scrollBox.left < 0 then
  177.         scrollBox.left = 0
  178.         scrollBox.missing = scrollBox.missing - 1
  179.         if scrollBox.missing < 0 then scrollBox.missing = 0 end
  180.     end
  181.     table.remove(scrollBox.childs, key)
  182.     return true
  183. end
  184.  
  185. function addTextBox(items, name, text, length, x, y)
  186.     -- check for errors in arguments
  187.     if type(items) ~= "table" then return false, "table expected" end
  188.     if type(name) ~= "string" or #name < 1 then return false, "no name given" end
  189.     if type(text) ~= "string" then return false, "no text given" end
  190.     if type(length) ~= "number" or length < 1 then return false, "no given length" end
  191.     if type(x) ~= "number" or x < 1 then return false, "no x coordinate given" end
  192.     if type(y) ~= "number" or y < 1 then return false, "no y coordinate given" end
  193.     --errorcheck end
  194.     --insert into table; NOTE: this will delete items with the same name
  195.     items[name] = {}
  196.     items[name]['type'] = "textBox"
  197.     items[name]['text'] = text
  198.     items[name]['length'] = length
  199.     items[name]['bg'] = colors.lightGray
  200.     items[name]['fg'] = colors.lime
  201.     items[name]['x'] = x
  202.     items[name]['y'] = y
  203.     return true
  204. end
  205.  
  206. function addLabel(items, name, text, length, x, y)
  207.     -- check for errors in arguments
  208.     if type(items) ~= "table" then return false, "table expected" end
  209.     if type(name) ~= "string" or #name < 1 then return false, "no name given" end
  210.     if type(text) ~= "string" then return false, "no text given" end
  211.     if type(length) ~= "number" or length < 1 then return false, "no given length" end
  212.     if type(x) ~= "number" or x < 1 then return false, "no x coordinate given" end
  213.     if type(y) ~= "number" or y < 1 then return false, "no y coordinate given" end
  214.     --errorcheck end
  215.     --insert into table; NOTE: this will delete items with the same name
  216.     items[name] = {}
  217.     items[name]['type'] = "label"
  218.     items[name]['text'] = text
  219.     items[name]['length'] = length
  220.     items[name]['bg'] = colors.black
  221.     items[name]['fg'] = colors.white
  222.     items[name]['x'] = x
  223.     items[name]['y'] = y
  224.     return true
  225. end
  226.  
  227. function updateAll(items, bg)
  228.     --check for errors
  229.     if type(items) ~= "table" then return false, "table expected" end
  230.     --errorcheck end
  231.  
  232.     --clear the screen with the given background color and draw every item of the given item table
  233.     term.setCursorPos(1,1)
  234.     term.setBackgroundColor(bg)
  235.     term.clear()
  236.     for k, v in pairs(items) do
  237.         update(v)
  238.     end
  239. end
  240.  
  241. local function readN(preset, maxL)
  242.     --read function, no new line
  243.     term.setCursorBlink(true)
  244.     local str = preset
  245.     term.write(str)
  246.     repeat
  247.         local ev, key = os.pullEvent()
  248.         if ev == "char" and #str < maxL then
  249.             str = str..key
  250.             term.write(key)
  251.         elseif ev == "key" and key == keys.backspace and #str >= 1 then
  252.             str = string.reverse(str)
  253.             str = string.sub(str, 2)
  254.             str = string.reverse(str)
  255.             local cX, cY = term.getCursorPos()
  256.             term.setCursorPos(cX-1, cY)
  257.             term.write(" ")
  258.             term.setCursorPos(cX-1, cY)
  259.         end
  260.     until ev == "key" and key == keys.enter
  261.     term.setCursorBlink(false)
  262.     return str
  263. end
  264.  
  265. function update(v)
  266.     --redraw a specific item, giving the data (table) of that item
  267.     if v.type ~= "scrollBox" and v.type ~= "toggle" then
  268.         term.setCursorPos(v.x, v.y)
  269.         term.setBackgroundColor(v.bg)
  270.         term.setTextColor(v.fg)
  271.         local c = 1
  272.         repeat
  273.             if c <= v.length then
  274.                 term.write(" ")
  275.             end
  276.             c = c+1
  277.         until c > v.length
  278.         term.setCursorPos(v.x, v.y)
  279.         term.write(v.text)
  280.     elseif v.type == "toggle" then
  281.         term.setCursorPos(v.x, v.y)
  282.         if v.toggled then
  283.             term.setBackgroundColor(colors.green)
  284.             term.write(" ")
  285.             term.setBackgroundColor(colors.white)
  286.             term.write(" ")
  287.         else
  288.             term.setBackgroundColor(colors.white)
  289.             term.write(" ")
  290.             term.setBackgroundColor(colors.red)
  291.             term.write(" ")
  292.         end
  293.     else
  294.         local cy = 1
  295.         term.setCursorPos(v.x, v.y)
  296.         term.setBackgroundColor(v.bg)
  297.         term.setTextColor(v.fg)
  298.         repeat
  299.             if cy <= v.lengthy then
  300.                 local c = 1
  301.                 term.setCursorPos(v.x, v.y-1+cy)
  302.                 repeat
  303.                     if c <= v.lengthx then
  304.                         term.write(" ")
  305.                     end
  306.                     c = c+1
  307.                 until c > v.lengthx
  308.             end
  309.             cy = cy+1
  310.         until cy > v.lengthy
  311.         term.setCursorPos(v.x, v.y)
  312.         cy = 1
  313.         for each, child in ipairs(v.childs) do
  314.             if cy+v.missing == each and cy <= v.lengthy then
  315.                 local oldx, oldy = term.getCursorPos()
  316.                 term.setBackgroundColor(child.bg)
  317.                 term.setTextColor(child.fg)
  318.                 if child.selected then
  319.                     term.setBackgroundColor(colors.lightBlue)
  320.                     term.setTextColor(colors.white)
  321.                 end
  322.                 local c = 1
  323.                 repeat
  324.                     if c <= v.lengthx then
  325.                     term.write(" ")
  326.                 end
  327.                 c = c+1
  328.                 until c > v.lengthx
  329.                 term.setCursorPos(oldx, oldy)
  330.                 term.write(child.text)
  331.                 term.setBackgroundColor(v.bg)
  332.                 term.setTextColor(v.fg)
  333.                 term.setCursorPos(oldx, oldy+1)
  334.                 cy = cy+1
  335.             end
  336.         end
  337.     end
  338. end
  339.  
  340.  
  341.  
  342. function run(items)
  343.     --catch events for that specific item-table
  344.     if type(items) == "table" then
  345.         while true do
  346.             local evt, button, x, y = os.pullEvent()
  347.             for k, v in pairs(items) do
  348.                 if evt == "mouse_click" and button == 1 and v.type == "button" and x >= v.x and x < v.x+v.length and y == v.y then
  349.                     local oldbg = v.bg
  350.                     local oldfg = v.fg
  351.                     v.bg = colors.blue
  352.                     v.fg = colors.white
  353.                     update(v)
  354.                     sleep(0.125)
  355.                     v.bg = oldbg
  356.                     v.fg = oldfg
  357.                     update(v)
  358.                     os.queueEvent("button_press", "button", k, nil)
  359.                 elseif evt == "mouse_click" and button == 1 and v.type == "toggle" and x >= v.x and x < v.x+2 and y == v.y then
  360.                     if v.toggled then
  361.                         v.toggled = false
  362.                         update(v)
  363.                     else
  364.                         v.toggled = true
  365.                         update(v)
  366.                     end
  367.                     os.queueEvent("toggle", "toggle", k, nil)
  368.                 elseif evt == "mouse_click" and button == 1 and v.type == "textBox" and x >= v.x and x < v.x+v.length and y == v.y then
  369.                     v.bg = colors.gray
  370.                     v.fg = colors.lime
  371.                     update(v)
  372.                     term.setCursorPos(v.x, v.y)
  373.                     term.setBackgroundColor(v.bg)
  374.                     term.setTextColor(v.fg)
  375.                     local e = readN(v.text, v.length)
  376.                     v.text = e
  377.                     v.bg = colors.lightGray
  378.                     v.fg = colors.lime
  379.                     update(v)
  380.                     os.queueEvent("textBox_input", "textBox", k, nil)
  381.                 elseif evt == "mouse_click" and button == 1 and v.type == "scrollBox" and x >= v.x and x < v.x+v.lengthx and y >= v.y and y < v.y+v.lengthy then
  382.                     local newy = y-v.y+1+v.missing
  383.                     if v.childs[newy] ~= nil then
  384.                         for each, child in ipairs(v.childs) do
  385.                             if child.selected then
  386.                                 child.selected = false
  387.                             end
  388.                         end
  389.                         v.childs[newy].selected = true
  390.                         update(v)
  391.                         os.queueEvent("scrollBox_select", "scrollBox", k, newy)
  392.                     end
  393.                 elseif evt == "mouse_click" and button == 2 and v.context ~= nil and v.type == "scrollBox" and x >= v.x and x < v.x+v.lengthx and y >= v.y and y < v.y+v.lengthy then
  394.                     local newy = y-v.y+1+v.missing
  395.                     if v.childs[newy] ~= nil then
  396.                         for each, child in ipairs(v.childs) do
  397.                             if child.selected then
  398.                                 child.selected = false
  399.                             end
  400.                         end
  401.                         v.childs[newy].selected = true
  402.                         update(v)
  403.                     else
  404.                         for each, child in ipairs(v.childs) do
  405.                             if child.selected then
  406.                                 newy = each
  407.                                 break
  408.                             end
  409.                         end
  410.                     end
  411.                     local xPosEnd = x+20
  412.                     local yPosEnd = y+#v.context.list
  413.                     local newX = x
  414.                     local newY = y
  415.                     if xPosEnd > maxX then
  416.                         local c = xPosEnd - maxX
  417.                         x = x-c
  418.                     end
  419.                     if yPosEnd > maxY then
  420.                         local c = yPosEnd - maxY
  421.                         y = y-c
  422.                     end
  423.                     v.context.window.reposition(x, y)
  424.                     v.context.window.setVisible(true)
  425.                     term.redirect(v.context.window)
  426.                     repeat
  427.                         local ev, button, nx, ny = os.pullEvent("mouse_click")
  428.                         local width, height = term.getSize()
  429.                         if ev == "mouse_click" and button == 1 and nx >= x and nx < x+20 and ny >= y and ny < y+height then
  430.                             ny = ny-y+1
  431.                             term.setCursorPos(1, ny)
  432.                             term.setBackgroundColor(colors.blue)
  433.                             term.setTextColor(colors.white)
  434.                             term.clearLine()
  435.                             term.write(v.context.list[ny].text)
  436.                             sleep(0.125)
  437.                             term.setCursorPos(1, ny)
  438.                             term.setBackgroundColor(v.context.list[ny].bg)
  439.                             term.setTextColor(v.context.list[ny].fg)
  440.                             term.clearLine()
  441.                             term.write(v.context.list[ny].text)
  442.                             v.context.window.setVisible(false)
  443.                             mainWindow.redraw()
  444.                             term.redirect(mainWindow)
  445.                             os.queueEvent("context_select", k, newy, v.context.list[ny].text)
  446.                         elseif ev == "mouse_click" then
  447.                             v.context.window.setVisible(false)
  448.                             mainWindow.redraw()
  449.                             term.redirect(mainWindow)
  450.                         end
  451.                     until ev == "mouse_click"
  452.                     update(v)
  453.                 elseif evt == "mouse_scroll" and button == 1 and v.type == "scrollBox" and x >= v.x and x < v.x+v.lengthx and y >= v.y and y < v.y+v.lengthy and v.left > 0 then
  454.                     v.missing = v.missing + 1
  455.                     v.left = v.left - 1
  456.                     update(v)
  457.                     os.queueEvent("scrollBox_scroll", "scrollbox", k, 1)
  458.                 elseif evt == "mouse_scroll" and button == -1 and v.type == "scrollBox" and x >= v.x and x < v.x+v.lengthx and y >= v.y and y < v.y+v.lengthy and v.missing > 0 then
  459.                     v.missing = v.missing - 1
  460.                     v.left = v.left + 1
  461.                     update(v)
  462.                     os.queueEvent("scrollBox_scroll", "scrollbox", k, -1)
  463.                 end
  464.             end
  465.         end
  466.     elseif type(items) ~= "table" then
  467.         return false, "table expected"
  468.     else
  469.         return false, "already running"
  470.     end
  471. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement