abc123mewot

Powerplant UI API

Dec 18th, 2023 (edited)
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. function initUI(m)
  2.     local mW, mH = m.getSize()
  3.     local tbl = {
  4.         mon = m,
  5.         monW = mW,
  6.         monH = mH
  7.     }
  8.     function tbl.spacer(y, str)
  9.         local l = string.len(str)
  10.         local x = math.floor(tbl.monW / 2 - l / 2)
  11.         tbl.mon.setCursorPos(1, y)
  12.         for i=1,x do tbl.mon.write(" ") end
  13.         tbl.mon.write(str)
  14.         for i=1,tbl.monW-l do tbl.mon.write(" ") end
  15.         return y + 1
  16.     end
  17.     function tbl.drawVbar(sy, ey)
  18.         local cx, cy = tbl.mon.getCursorPos()
  19.         for i=sy,ey do
  20.             tbl.mon.setCursorPos(cx, i)
  21.             tbl.mon.write(' ')
  22.         end
  23.         tbl.mon.setCursorPos(cx + 1, cy)
  24.     end
  25.     function tbl.writeMid(str, nlen)
  26.         local slen = string.len(str)
  27.         local cx, cy = tbl.mon.getCursorPos()
  28.         if slen >= nlen then error("Invalid length!") end
  29.         local hlen = (nlen - slen) / 2
  30.         tbl.mon.setCursorPos(cx + hlen, cy)
  31.         tbl.mon.write(str)
  32.         tbl.mon.setCursorPos(cx + nlen, cy)
  33.         return str
  34.     end
  35.     function tbl.monWriteFGBG(str)
  36.         local fg, bg = tbl.mon.getTextColor(), tbl.mon.getBackgroundColor()
  37.         local lcol, col, slen = bg, bg, string.len(str)
  38.         for i=1,slen do
  39.             if string.sub(str, i, i) == ' ' then col = bg else col = fg end
  40.             if col ~= lcol then tbl.mon.setBackgroundColor(col) end
  41.             tbl.mon.write(' ')
  42.             lcol = col
  43.         end
  44.         tbl.mon.setTextColor(fg)
  45.         tbl.mon.setBackgroundColor(bg)
  46.     end
  47.     function tbl.writeDigit(d)
  48.         if d < 0 or d > 14 then error("Invalid digit!") end
  49.         local cx, cy = tbl.mon.getCursorPos()
  50.         local pos = d * 4 + 1
  51.         local npos = pos + 4
  52.         --                                                            _0_ _1_ _2_ _3_ _4_ _5_ _6_ _7_ _8_ _9_ _,_ _._ _%_ _%_
  53.                                          tbl.monWriteFGBG(string.sub("###  #  ### ### # # ### ### ### ### ###         ##   # ", pos, npos))
  54.         tbl.mon.setCursorPos(cx, cy + 1) tbl.monWriteFGBG(string.sub("# #  #    #   # # # #   #     # # # # #         ##  #  ", pos, npos))
  55.         tbl.mon.setCursorPos(cx, cy + 2) tbl.monWriteFGBG(string.sub("# #  #  ### ### ### ### ###  #  ### ###            #   ", pos, npos))
  56.         tbl.mon.setCursorPos(cx, cy + 3) tbl.monWriteFGBG(string.sub("# #  #  #     #   #   # # # #   # #   #  #        # ## ", pos, npos))
  57.         tbl.mon.setCursorPos(cx, cy + 4) tbl.monWriteFGBG(string.sub("###  #  ### ###   # ### ### #   ### ###  #   #   #  ## ", pos, npos))
  58.         tbl.mon.setCursorPos(cx + 4, cy)
  59.     end
  60.     function tbl.writeNumber(n, digits, noZero)
  61.         beginning = true
  62.         for i=1,digits do
  63.             local i10 = 10 ^ (digits - i + 1)
  64.             local digit = math.floor((n % i10) / (i10 / 10))
  65.             if noZero and beginning and digit == 0 then
  66.                 local cx, cy = tbl.mon.getCursorPos()
  67.                 tbl.mon.setCursorPos(cx + 4, cy)
  68.             else
  69.                 tbl.writeDigit(digit, i ~= digits)
  70.                 beginning = false
  71.             end
  72.         end
  73.     end
  74.     return tbl
  75. end
  76.  
Advertisement
Add Comment
Please, Sign In to add comment