Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------Start Settings Section------------------
- ---Note neither w nor h are directly related to Physical screen size
- ---they just control in how many "pixels" the physical space is divided
- --- I recommend leaving it default and test then change and see what fits your screen
- ---Also the bigger the numbers the smaller the text
- w = 85 --Screen Width
- h = 31 -- Screen Height
- minStackSize = 500 --- This is the stack size used to Calculate Capacity
- headerRow = 1 --- Row where the Header row is drawn
- startRow = 3 --- Row in which itemList starts
- startCol = 1 --- Col in which ItemList starts
- refreshTime = 60 --- Seconds between refresh of Screen also used to measure gain loss
- mediumThreshold = 50 ---when stock higher then this percentage bar color changes to yellow by default
- highThreshold = 90 ---when stock higher then this percentage bar color changes to green by default
- --- Here you can change the Colors of some elements
- --- Backcolors is for Background FrontColors for Foreground
- --- Colors that are used in the setColor Function need to exist in both tables
- BackColors = {default={0,0,0,0},rowEven={255,0,0,0.1},rowOdd = {0,0,0,1},header={255,0,0,0.1},
- lowStockBar={255,0,0,1},mediumStockBar={50,50,0,1},highStockBar={0,255,0,1},nextButton={0,255,0,0.3},
- prevButton = {0,255,0,0.3}}
- FrontColors = {default={255,255,255,1},rowEven={255,255,255,1},rowOdd = {255,255,255,1},header={255,255,255,1},nextButton={0,0,0,1},
- prevButton = {0,0,0,1}}
- --- In this table you can change the Column attributes
- --- width is the Columnwidth be aware that these are cut off if text is longer then width
- --- header is the Text in the Header Column
- --- offset allows you to move header text left or right
- --- Note: Changing Header text does not change the content for that you need to adjust the code in the drawRows function
- Columns = {}
- Columns[0] = {width = 29,header="Item",offset = 0}
- Columns[1] = {width = 10,header="Current",offset = 0}
- Columns[2] = {width = 10,header="Max",offset = 0}
- Columns[3] = {width = 10,header=" %",offset = 0}
- Columns[4] = {width = 10,header="Change",offset = -3}
- Columns[5] = {width = 12,header="Visual",offset = 0}
- buttonText_NextPage = " Next Page "
- buttonText_PrevPage = " Prev Page "
- --- Only change code outside of the settings area if you know what you are doing ;)
- -------------------End Settings Section------------------
- -------------------Start Helper Functions------------------
- function setBackgroundColor(colorName)
- gpu:setBackground(BackColors[colorName][1],BackColors[colorName][2],BackColors[colorName][3],BackColors[colorName][4])
- end
- function setForegroundColor(colorName)
- gpu:setForeground(FrontColors[colorName][1],FrontColors[colorName][2],FrontColors[colorName][3],FrontColors[colorName][4])
- end
- function setColor(colorName)
- setBackgroundColor(colorName)
- setForegroundColor(colorName)
- end
- function setDefaultColor()
- setColor("default")
- end
- function setRowColor(rowNum)
- if rowNum % 2 == 0 then
- setColor("rowEven")
- else
- setColor("rowOdd")
- end
- end
- function clearScreen()
- setDefaultColor()
- gpu:fill(0,0,w,h," ")
- gpu:flush()
- end
- function pairsByKeys (t, f)
- local a = {}
- for n in pairs(t) do table.insert(a, n) end
- table.sort(a, f)
- local i = 0
- local iter = function ()
- i = i + 1
- if a[i] == nil then return nil
- else return a[i], t[a[i]]
- end
- end
- return iter
- end
- function fillString(str,len)
- local trimmedText = string.sub(str,0,len)
- return trimmedText .. string.rep(" ",len-string.len(trimmedText))
- end
- function format_int(number)
- local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
- --- reverse the int-string and append a comma to all blocks of 3 digits
- int = int:reverse():gsub("(%d%d%d)", "%1'")
- --- reverse the int-string back remove an optional comma and put the
- --- optional minus and fractional part back
- return minus .. int:reverse():gsub("^'", "") .. fraction
- end
- function getItemsPerPage()
- return h-2-startRow
- end
- function setMaxPages()
- maxItemsPerPage = getItemsPerPage()
- totalItems = 0
- for k,v in pairs(items) do
- totalItems = totalItems + 1
- end
- maxPage = totalItems/maxItemsPerPage
- end
- ----------------------End Helper Functions------------------
- ----------------------Start Functions for Display Stuff ------------------
- function drawHeader(row)
- local colStart = startCol
- setColor("header")
- for i = 0,#Columns,1 do
- local col = Columns[i]
- gpu:setText(colStart+col.offset,headerRow,fillString(col.header,col.width))
- colStart = colStart + col.width
- end
- gpu:flush()
- end
- function drawButtons()
- setColor("prevButton")
- gpu:setText(0,h-1,buttonText_PrevPage)
- setColor("nextButton")
- gpu:setText(w-14,h-1,buttonText_NextPage)
- setDefaultColor()
- end
- function drawBar(x,y,capacityPercent)
- setDefaultColor()
- gpu:setText(x,y,"|")
- if capacityPercent > 0 and capacityPercent <= 50 then
- setBackgroundColor("lowStockBar")
- elseif capacityPercent > 50 and capacityPercent <= 90 then
- setBackgroundColor("mediumStockBar")
- else
- setBackgroundColor("highStockBar")
- end
- BarLen =math.floor(capacityPercent/10)
- if BarLen < 1 then BarLen = 1 end
- gpu:setText(x+1,y,fillString("",BarLen))
- setDefaultColor()
- gpu:setText(x+11,y,"|")
- end
- function getItemChange(itemName,itemInfo)
- if itemsLast[itemName] ~= nil then
- change = itemInfo.count - itemsLast[itemName].count
- else
- change = "-"
- end
- return change
- end
- function drawRows()
- local currentRow = 0
- local columnValues = {}
- local currentIndex = 1
- local itemsPerPage = getItemsPerPage()
- local maxIndex = itemsPerPage*page
- local minIndex = maxIndex - itemsPerPage +1
- currentRow = currentRow+startRow
- for itemName,itemInfo in pairsByKeys(items) do
- if currentIndex >= minIndex and currentIndex <= maxIndex then
- local capacityPercent = math.floor(itemInfo.count/itemInfo.capacity*100)
- columnValues[0] = itemName
- columnValues[1] = format_int(itemInfo.count)
- columnValues[2] = format_int(itemInfo.capacity)
- columnValues[3] = capacityPercent
- columnValues[4] = getItemChange(itemName,itemInfo)
- columnValues[5] = "Graph"
- local colStart = startCol
- for i = 0,#Columns,1 do
- setRowColor(currentRow)
- local col = Columns[i]
- if columnValues[i] == "Graph" then
- drawBar(colStart+col.offset,currentRow,capacityPercent)
- else
- gpu:setText(colStart+col.offset,currentRow,fillString(columnValues[i],col.width))
- end
- colStart = colStart + col.width
- end
- currentRow = currentRow + 1
- end
- currentIndex = currentIndex +1
- end
- itemsLast = items
- end
- function DrawTable()
- clearScreen()
- drawHeader()
- drawRows()
- drawButtons()
- gpu:flush()
- end
- ----------------------Start Functions for Grabbing Container Info ------------------
- function getContainerInfo(container)
- inv = container:getInventories()[1]
- inventorySize = inv.size
- inv:sort()
- maxInventory = minStackSize*inventorySize
- containerInfo = {name = "nothing", count = 0,capacity=inventorySize}
- for i=0,inventorySize-1,1 do
- stack = inv:getStack(i)
- if stack.item.type then
- itemName = stack.item.type.name
- itemCount = inv.itemCount
- containerInfo = {name=itemName,count = itemCount,capacity = maxInventory}
- return containerInfo
- end
- end
- return containerInfo
- end
- function addToItems(containerInfo)
- if items[containerInfo.name] == nil then
- items[containerInfo.name] = containerInfo
- else
- items[containerInfo.name].count = items[containerInfo.name].count + containerInfo.count
- items[containerInfo.name].capacity = items[containerInfo.name].capacity + containerInfo.capacity
- end
- end
- ----------------------End Functions for Grabbing Container Info ------------------
- --- Grabing Components and initializing Global Variables
- Containers = component.proxy(component.findComponent(findClass("FGBuildableStorage")))
- gpu = computer.getPCIDevices(findClass("GPUT1"))[1]
- comp = component.findComponent(findClass("Screen"))[1]
- screen = component.proxy(comp)
- gpu:bindScreen(screen)
- gpu:setSize(w,h)
- event.listen(gpu)
- pages = {}
- items = {}
- itemsLast = {}
- page = 1
- maxPage = 1
- lastRefresh = computer.millis()
- firstStart = true
- ---Main Loop
- ---
- while true do
- if computer.millis()-lastRefresh >= refreshTime*1000 or firstStart then
- firstStart = false
- Containers = component.proxy(component.findComponent(findClass("FGBuildableStorage")))
- items = {}
- for _,v in pairs(Containers) do
- CurrentInfo = getContainerInfo(v)
- if CurrentInfo.name ~= "nothing" then
- addToItems(CurrentInfo)
- end
- end
- setMaxPages()
- DrawTable()
- itemsLast = items
- lastRefresh = computer.millis()
- end
- signal, sender, x, y = event.pull(refreshTime)
- if signal == "OnMouseDown" then
- if x>=0 and x<=string.len(buttonText_PrevPage) and y==h-1 and page > 1 then
- page=page-1
- DrawTable()
- end
- if x>=w-string.len(buttonText_NextPage) and x<=w and y==h-1 and page < maxPage then
- page=page+1
- DrawTable()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement