MarkFergus

/lib/f

Jul 15th, 2019 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.80 KB | None | 0 0
  1. --[[ Mark_functions v.1.86 (12/08/2020 20:51)
  2. call os.loadAPI("f")
  3. docs available soon
  4. --]]
  5. function progression_bar(m,x,y,length,curVal,bg_color,bar_color)
  6.   drawLine(m,x,y,length,bg_color)
  7.   percent_bar = curVal / 100 * length
  8.   drawLine(m,x,y,percent_bar,bar_color)
  9. end
  10. function drawLine(m,x,y,length,color)
  11.   if length < 0 then
  12.   length = 0
  13.   end
  14.   m.setBackgroundColor(colors[color])
  15.   m.setCursorPos(x,y)
  16.   m.write(string.rep(" ",length))
  17. end
  18. function drawBox(m,x,y,x2,y2,color)
  19.   for a=0,y2-y do
  20.     drawLine(m,x,y+a,x2-x,color)
  21.   end
  22.   return x,y,x2,y2
  23. end
  24. function centerText(m,y,text,color,bg_color)
  25.   w, h = m.getSize()
  26.   m.setCursorPos((math.floor(w/2) - (math.floor(#text/2))) + 1,y)
  27.   if bg_color ~= nil then m.setBackgroundColor(colors[bg_color]) end
  28.   m.setTextColor(colors[color])
  29.   m.write(text)
  30. end
  31. function centerTextRight(m,y,text,color,bg_color)
  32.   w, h = m.getSize()
  33.   m.setCursorPos(w - (math.floor(#text)),y)
  34.   if bg_color ~= nil then m.setBackgroundColor(colors[bg_color]) end
  35.   m.setTextColor(colors[color])
  36.   m.write(text)
  37. end
  38. function print(m,text,color)
  39.   m.setTextColor(colors[color])
  40.   m.write(text.."\n")
  41.   m.setTextColor(colors.white)
  42.   x,y = m.getCursorPos()
  43.   m.setCursorPos(1,y+1)
  44. end
  45. function cprint(m,x,y,text,color,bg_color)
  46.   m.setCursorPos(x,y)
  47.   m.setTextColor(colors[color])
  48.   if bg_color ~= nil then m.setBackgroundColor(colors[bg_color]) end
  49.   m.write(text)
  50.   m.setBackgroundColor(colors.black)
  51.   m.setTextColor(colors.white)
  52. end
  53. function format_int(number)
  54.   if number == nil then number = 0 end
  55.   local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
  56.   int = int:reverse():gsub("(%d%d%d)", "%1,")
  57.   return minus .. int:reverse():gsub("^,", "") .. fraction
  58. end
  59. function itrv(x,y,z)
  60.   if x == nil or y == nil or z == nil then
  61.     return false
  62.   else
  63.     return (x >= y and x <= z)
  64.   end
  65. end
  66. function box(x,y,x1,y1,x2,y2)
  67.   return (itrv(x,x1,x2) and itrv(y,y1,y2))
  68. end
  69. function vn(val)
  70.   return (val ~= nil)
  71. end
  72. --[[
  73.     val : a string, table, number or function
  74.    
  75.     The function returns true if the value isn't 'nil' or false if it is.
  76. --]]
  77. function cv(arg,cv1,param,cv2)
  78.   if param == nil then error("No parameter found : usage cv(arg,val1,param,val2)") end
  79.   if param == "or" then return (arg == cv1 or arg == cv2) end
  80.   if param == "and" then return (arg == cv1 and arg == cv2)  end
  81. end
  82. --[[
  83.     arg : the value you want to compare
  84.     cv1 : value1
  85.     cv2 : value2
  86.     param : < or / and >
  87.    
  88.     parameters :
  89.         - 'or' will compare if (arg == cv1 or arg == cv2)
  90.         - 'and' will compare if (arg == cv1 or arg == cv2)
  91. --]]
  92. function check(list,name)
  93.     local check = false
  94.     for i,v in pairs(list) do
  95.         if type(v) == "string" then
  96.             if string.find(name,v) ~= nil then check = true end
  97.             local array = i
  98.         end
  99.     end
  100.     return check, array
  101. end
  102. function addWin(m,x1,y1,x2,y2)
  103.     if m == nil then
  104.         error("No object found")
  105.     end
  106.     local b = window.create(m,x1,y1,x2,y2)
  107.     b.isWindow = true
  108.     b.parent = m
  109.     b.name = name
  110.     function b.update()
  111.         b.pos = {b.getPosition()}
  112.         b.size = {b.getSize()}
  113.         local dX = b.size[1]+b.pos[1]
  114.         local dY = b.size[2]+b.pos[2]
  115.         b.l = {b.pos[1],b.pos[2],dX,dY}
  116.     end
  117.     function b.isClicked(x,y)
  118.         if b.parent.isWindow == true then
  119.             ok, x, y = b.parent.isClicked(x,y)
  120.             x=x+1 y=y+1
  121.         end
  122.         ok = f.box(x,y,b.l[1],b.l[2],b.l[3],b.l[4])
  123.         if ok then tX = x-b.pos[1] tY = y-b.pos[2] end
  124.         return ok, tX, tY
  125.     end
  126.     function b.drag(x,y,args)
  127.         if b.isClicked(x,y) then
  128.             local w,h = b.parent.getSize()
  129.             local prototype = { __index = {
  130.             bX = 1, bY = 1, bW = w, bH = h,
  131.             } }
  132.             local a = args or {}
  133.             setmetatable(a, prototype)
  134.             repeat
  135.                 e = {os.pullEvent()}
  136.                 local nX,nY = e[3]-x, e[4]-y
  137.                 local sX,sY = b.pos[1]+nX, b.pos[2]+nY
  138.                 if vn(a["redraw"]) then
  139.                     for i,v in pairs(a["redraw"]) do
  140.                         v[1].apply(v[2])
  141.                     end
  142.                 end
  143.                 if a.borders == true then
  144.                     if sX < a.bX then sX = a.bX end
  145.                     if sY < a.bY then sY = a.bY end
  146.                     if sX+b.size[1] > a.bW then sX = a.bW-b.size[1]+1 end
  147.                     if sY+b.size[2] > a.bH then sY = a.bH-b.size[2]+1 end
  148.                 end
  149.                 b.reposition(sX,sY)
  150.             until e[1] == "mouse_up"
  151.             b.update()
  152.             return nX or nY
  153.         else
  154.             return false
  155.         end
  156.     end
  157.     function b.getName() return b.name end
  158.     function b.apply(id)
  159.         local this_color = b[id].bg_color
  160.         b.setBackgroundColor(colors[this_color])
  161.         b.clear()
  162.         if f.vn(b[id].printText) then b[id].printText() end
  163.     end
  164.     b.update()
  165.     return b
  166. end
  167. --[[
  168.     m : parent (window object, term.native(), monitor ...)
  169.     x1, y1 : relative position on the parent (screen)
  170.     x2, y2 : do not confuse with coordinates. Those are length and width of the object
  171.     name : bonus label to the table (not mandatory)
  172.    
  173.     the function returns an object which you can use whenever you want
  174.    
  175.     Object usages :
  176.         - getName() : returns the object label
  177.         - isClicked(x,y) : returns if the mouse_click/monitor_touch position is hovering the button
  178.         - drag(x,y, [args] ) : if the window is clicked, the object will get dragged until the function detects a 'mouse_up' event.accuracy
  179.             + args is a table. See exemple : { redraw = { {window2, "reset"},{} }, borders = true }
  180.             + redraw parameters will automaticly redraw those windows each time the object changes its position.
  181.             + borders will set a border within the object cannot get through. (only boolean true)
  182.         - apply(name) : apply a preset font
  183.        
  184.     Font usages :
  185.         - exemple : window = f.addWin(term.native(),1,1,20,5)
  186.         -           window.reset = {bg_color = "back", printText = function() print("This is a button") end}
  187.         -           window.apply("reset")
  188.         Now, the object background will be filled in black with the printed text 'This is a button'
  189. --]]
  190. --print("WINDOWS = ",os.loadAPI("/lib/WindowAPI"))
  191. --addSwitch = WindowAPI.addSwitch
  192. --addSelector = WindowAPI.addSelector
Add Comment
Please, Sign In to add comment