Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ****************************************************** --
- -- ****************************************************** --
- -- ** ENERGY BIG SCREEN DISPLAY ** --
- -- ** by cedmeu ** --
- -- ** Based on program by krakaen ** --
- -- ** cf. : ** --
- -- ** https://www.youtube.com/watch?v=SbbT7ncyS2M ** --
- -- ****************************************************** --
- -- ****************************************************** --
- function checkVersion()
- local monitorsCheck = {peripheral.find("monitor")}
- end
- function checkErrors()
- if pcall(checkVersion) then
- else
- error("The version of ComputerCraft is too old to use this program, sorry", 0)
- end
- local monitorsCheck = {peripheral.find("monitor")}
- if monitorsCheck[1] == nil then
- error("The Monitor is not being detected, make sure the connections(modem) are activated", 0)
- end
- end
- local args = { ... }
- local button = {}
- local filleds = {}
- local boxes = {}
- local lines = {}
- local texts = {}
- local refresh = true
- local infosSize = {}
- local controlsSize = {}
- local numbersSize = {}
- local currentRfTotal = 1
- checkErrors() -- verify that everything is okay to start the program
- local monitors = {peripheral.find("monitor")}
- local mon = monitors[1]
- local cells = {peripheral.find("tile_thermalexpansion_cell_basic_name")}
- local genSolar = {peripheral.find("extrautils_generatorsolar")}
- -- use the black thingy sponge to clear the chalkboard
- term.redirect(mon)
- mon.clear()
- mon.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- function clearTable()
- button = {}
- end
- -- ****************************************************** --
- -- ****************************************************** --
- -- some functions for drawing boxes, buttons... --
- -- ****************************************************** --
- -- ****************************************************** --
- -- All the things that make my buttons work
- function setButton(name, title, func, xmin, ymin, xmax, ymax, elem, elem2, color)
- button[name] = {}
- button[name]["title"] = title
- button[name]["func"] = func
- button[name]["active"] = false
- button[name]["xmin"] = xmin
- button[name]["ymin"] = ymin
- button[name]["xmax"] = xmax
- button[name]["ymax"] = ymax
- button[name]["color"] = color
- button[name]["elem"] = elem
- button[name]["elem2"] = elem2
- end
- -- stuff and things for buttons
- function fill(text, color, bData)
- mon.setBackgroundColor(color)
- mon.setTextColor(colors.white)
- local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
- local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(bData["title"])) /2) +1
- for j = bData["ymin"], bData["ymax"] do
- mon.setCursorPos(bData["xmin"], j)
- if j == yspot then
- for k = 0, bData["xmax"] - bData["xmin"] - string.len(bData["title"]) +1 do
- if k == xspot then
- mon.write(bData["title"])
- else
- mon.write(" ")
- end
- end
- else
- for i = bData["xmin"], bData["xmax"] do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- end
- -- stuff and things for buttons
- function screen()
- local currColor
- for name,data in pairs(button) do
- local on = data["active"]
- currColor = data["color"]
- fill(name, currColor, data)
- end
- end
- -- stuff and things for buttons
- function flash(name)
- screen()
- end
- -- magical handler for clicky clicks
- function checkxy(x, y)
- for name, data in pairs(button) do
- if y>=data["ymin"] and y <= data["ymax"] then
- if x>=data["xmin"] and x<= data["xmax"] then
- data["func"](data["elem"], data["elem2"])
- flash(data['name'])
- return true
- --data["active"] = not data["active"]
- --print(name)
- end
- end
- end
- return false
- end
- -- Don't question my code, it works on my machine...
- function label(w, h, text)
- mon.setCursorPos(w, h)
- mon.write(text)
- end
- -- Draw function : put's all the beautiful magic in the screen
- function draw()
- for key,value in pairs(filleds) do
- local counter = 1
- for i=value[2],value[4],1 do
- paintutils.drawLine(value[1] , value[2]+counter, value[3], value[2]+counter, value[5])
- counter = counter + 1
- end
- end
- for key,value in pairs(boxes) do
- paintutils.drawLine(value[1] , value[2], value[1], value[4], value[5])
- paintutils.drawLine(value[1] , value[2], value[3], value[2], value[5])
- paintutils.drawLine(value[1] , value[4], value[3], value[4], value[5])
- paintutils.drawLine(value[3] , value[2], value[3], value[4], value[5])
- end
- for key,value in pairs(lines) do
- paintutils.drawLine(value[1] , value[2], value[3], value[4], value[5])
- end
- for key,value in pairs(texts) do
- mon.setCursorPos(value[1], value[2])
- mon.setTextColor(value[4])
- mon.setBackgroundColor(value[5])
- mon.write(value[3])
- end
- screen()
- resetDraw()
- end
- -- Resets the elements to draw to only draw the neccessity
- function resetDraw()
- filleds = {}
- boxes = {}
- lines = {}
- texts = {}
- end
- -- Handles all the clicks for the buttons
- function clickEvent()
- local myEvent={os.pullEvent("monitor_touch")}
- checkxy(myEvent[3], myEvent[4])
- end
- -- ****************************************************** --
- -- ****************************************************** --
- -- Creates the frame and the basic of the visual --
- -- Also adds the variables informations for placement of --
- -- stuff and things --
- -- ****************************************************** --
- -- ****************************************************** --
- function addDrawBoxesEnergy()
- local w, h = mon.getSize()
- local margin = math.floor((w/100)*2)
- infosSize['startX'] = margin + 1
- infosSize['startY'] = margin + 1
- --infosSize['endX'] = (((w-(margin*2))/3)*2)-margin
- infosSize['endX'] = w - (margin + 1)
- --infosSize['endY'] = h - margin
- infosSize['endY'] = h - ((h*1/3)- (margin + 1))
- infosSize['height'] = infosSize['endY']-infosSize['startY']-(margin*2)-2
- infosSize['width'] = infosSize['endX']-infosSize['startX']-(margin*2)-2
- infosSize['inX'] = infosSize['startX'] + margin +1
- infosSize['inY'] = infosSize['startY'] + margin +1
- --infosSize['sectionHeight'] = math.floor(infosSize['height']/3)
- infosSize['sectionHeight'] = 5
- -- Boxe INFOS
- table.insert(boxes, {infosSize['startX'] , infosSize['startY'], infosSize['endX'], infosSize['endY'], colors.gray})
- local name = "ENERGY"
- table.insert(lines, {infosSize['startX'] + margin , infosSize['startY'], infosSize['startX'] + (margin*2) + #name+1, infosSize['startY'], colors.black})
- table.insert(texts, {infosSize['startX'] + (margin*2), infosSize['startY'], name, colors.white, colors.black})
- -- Les 3 jauges
- local names = {}
- names[1] = 'TOTAL ENERGY STORED'
- names[2] = 'ENERGY FROM BASIC CELLS'
- names[3] = 'ENERGY FROM SOLAR GENERATOR'
- for i=0,2,1 do
- table.insert(texts, {infosSize['inX'] + margin, infosSize['inY'] + (infosSize['sectionHeight']*i) +i, names[i+1], colors.white, colors.black})
- table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 2 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
- end
- -- Numbers
- numbersSize['startX'] = infosSize['endX'] + margin + 1
- numbersSize['startY'] = infosSize['startY'] + margin + 1 +35
- numbersSize['endX'] = w-margin
- numbersSize['endY'] = h-margin
- numbersSize['height'] = numbersSize['endY']-numbersSize['startY']-(margin)-1
- numbersSize['width'] = numbersSize['endX']-numbersSize['startX']-(margin*2)-2
- numbersSize['inX'] = numbersSize['startX'] + margin +1
- numbersSize['inY'] = numbersSize['startY'] + margin
- --table.insert(boxes, {numbersSize['startX'] , numbersSize['startY'], numbersSize['endX'], numbersSize['endY'], colors.gray})
- name = "NUMBERS"
- --table.insert(lines, {numbersSize['startX'] + margin , numbersSize['startY'], numbersSize['startX'] + (margin*2) + #name+1, numbersSize['startY'], colors.black})
- --table.insert(texts, {numbersSize['startX'] + (margin*2), numbersSize['startY'], name, colors.white, colors.black})
- --draw()
- refresh = true
- while refresh do
- parallel.waitForAny(refreshEnergy,clickEvent)
- end
- end
- -- Makes and Handles the draw function for less lag in the visual
- function refreshEnergy()
- local rfTotal = 0
- local rfTotalMax = 0
- local rfTotalCells = 0
- local rfTotalMaxCells = 0
- local rfTotalGenSolar = 0
- local rfTotalMaxGenSolar = 0
- local j=0
- for j = 1, #cells do
- rfTotalCells = rfTotalCells + cells[j].getEnergyStored()
- rfTotalMaxCells = rfTotalMaxCells + cells[j].getMaxEnergyStored()
- end
- for j = 1, #genSolar do
- rfTotalGenSolar = rfTotalGenSolar + genSolar[j].getEnergyStored()
- rfTotalMaxGenSolar = rfTotalMaxGenSolar + genSolar[j].getMaxEnergyStored()
- end
- rfTotal = rfTotal + rfTotalCells + rfTotalGenSolar
- rfTotalMax = rfTotalMax + rfTotalMaxCells + rfTotalMaxGenSolar
- local i = 0
- local infotoAdd = 'TOTAL ENERGY STORED : '
- table.insert(lines, {numbersSize['inX'] , numbersSize['inY'],numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'], colors.black})
- --table.insert(texts, {numbersSize['inX'], numbersSize['inY'], infotoAdd .. rfTotal .. " RF", colors.white, colors.black})
- table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
- width = math.floor((infosSize['width'] / rfTotalMax)*rfTotal)
- table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
- i = 1
- infotoAdd = 'ENERGY FROM BASIC CELLS : '
- if currentRfTotal ~= rfTotalCells then
- currentRfTotal = rfTotalCells
- table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
- width = math.floor((infosSize['width'] / rfTotalMaxCells)*rfTotalCells)
- table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
- table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +2 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
- --table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 2 , infotoAdd .. rfTotalCells .. " RF", colors.white, colors.black})
- end
- i = 2
- infotoAdd = 'ENERGY FROM GENERATOR SOLAR : '
- if currentRfTotal ~= rfTotalGenSolar then
- currentRfTotal = rfTotalGenSolar
- table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + infosSize['width'], infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.lightGray})
- width = math.floor((infosSize['width'] / rfTotalMaxGenSolar)*rfTotalGenSolar)
- table.insert(filleds, {infosSize['inX'] , infosSize['inY'] + 1 + (infosSize['sectionHeight']*i) +i, infosSize['inX'] + width, infosSize['inY'] + (infosSize['sectionHeight']*(i+1))-2 +i, colors.green})
- table.insert(lines, {numbersSize['inX'] , numbersSize['inY'] +3 ,numbersSize['inX'] + numbersSize['width'] , numbersSize['inY'] +2, colors.black})
- --table.insert(texts, {numbersSize['inX'], numbersSize['inY']+ 3 , infotoAdd .. rfTotalGenSolar .. " RF", colors.white, colors.black})
- end
- mon.setTextColor(colors.white)
- draw()
- sleep(10)
- end
- -- ****************************************************** --
- -- ****************************************************** --
- -- few others utilities functions --
- -- ****************************************************** --
- -- ****************************************************** --
- ---============================================================
- -- add comma to separate thousands
- -- From Lua-users.org/wiki/FormattingNumbers
- --
- --
- function comma_value(amount)
- local formatted = amount
- while true do
- formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
- if (k==0) then
- break
- end
- end
- return formatted
- end
- ---============================================================
- -- rounds a number to the nearest decimal places
- -- From Lua-users.org/wiki/FormattingNumbers
- --
- --
- function round(val, decimal)
- if (decimal) then
- return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
- else
- return math.floor(val+0.5)
- end
- end
- --===================================================================
- -- given a numeric value formats output with comma to separate thousands
- -- and rounded to given decimal places
- -- From Lua-users.org/wiki/FormattingNumbers
- --
- function format_num(amount, decimal, prefix, neg_prefix)
- local str_amount, formatted, famount, remain
- decimal = decimal or 2 -- default 2 decimal places
- neg_prefix = neg_prefix or "-" -- default negative sign
- famount = math.abs(round(amount,decimal))
- famount = math.floor(famount)
- remain = round(math.abs(amount) - famount, decimal)
- -- comma to separate the thousands
- formatted = comma_value(famount)
- -- attach the decimal portion
- if (decimal > 0) then
- remain = string.sub(tostring(remain),3)
- formatted = formatted .. "." .. remain ..
- string.rep("0", decimal - string.len(remain))
- end
- -- attach prefix string e.g '$'
- formatted = (prefix or "") .. formatted
- -- if value is negative then format accordingly
- if (amount<0) then
- if (neg_prefix=="()") then
- formatted = "("..formatted ..")"
- else
- formatted = neg_prefix .. formatted
- end
- end
- return formatted
- end
- -- ****************************************************** --
- -- ****************************************************** --
- -- aaaand here is the "main"... --
- -- ****************************************************** --
- -- ****************************************************** --
- -- Clear and make the pixel smaller because we are not blind
- mon.setBackgroundColor(colors.black)
- mon.clear()
- mon.setTextScale(0.5)
- -- Add's the visual and starts the Loop
- addDrawBoxesEnergy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement