Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local unicode = require("unicode")
- local ecs = require("ecs")
- local event = require("event")
- local computer = require("computer")
- local fs = require("filesystem")
- local com = require('component')
- --local interface = com.me_interface
- local gpu = com.gpu
- local choice,run = false,true
- local drawFrom = 0
- local ind = {1,2,3,4,5,6,7,8,9,10}
- local items,pos_str = {},{}
- local patch_items = "/home/items.lua"
- if not fs.exists(patch_items) then
- local f = io.open(patch_items,'w')
- f:write("{".."\n")
- f:write(" shop = {".."\n")
- --local data = interface.getItemsInNetwork()
- for item = 1,#data do
- if data[item] then
- f:write(' { text = "'..data[item].label..'", price = "0", label = "'..data[item].label..'" },'..'\n')
- end
- end
- f:write(" }".."\n")
- f:write("}")
- f:close()
- os.execute("edit "..patch_items)
- os.exit()
- end
- local f, err = io.open(patch_items, "r")
- if not f then
- error(err, 2)
- end
- local text = f:read('*a')
- f:close()
- local chunk, err = load("return " .. text, "=items.lua", "t")
- if not chunk then
- error(err, 2)
- else
- items = chunk()
- end
- table.sort(items.shop, function(a,b) if a.text then return a.text < b.text end end)
- for i = 1,#items.shop do
- --items.shop[i].available = "0"
- items.shop[i].available = tostring(math.random(0,10))
- end
- local ind = {}
- for i = 1,#items.shop do
- if items.shop[i].available ~= "0" then
- table.insert(ind,i)
- end
- end
- local function square(x,y,width,height,color)
- if color and gpu.getBackground() ~= color then
- gpu.setBackground(color)
- end
- gpu.fill(x,y,width,height," ")
- end
- --Вертикальный скроллбар. Маст-хев!
- local function srollBar(x,y,width,height,countOfAllElements,currentElement,backcolors,frontcolors)
- local sizeOfScrollBar = math.ceil(1/countOfAllElements*height)
- local displayBarFrom = math.floor(y+height*((currentElement)/countOfAllElements))
- square(x,y,width,height,backcolors)
- square(x,displayBarFrom,width,sizeOfScrollBar,frontcolors)
- end
- local function drawlist()
- pos_str = {}
- local yPos = 4
- for i = 1,11 do
- square(1,yPos,77,1,0x000000)
- local i = drawFrom + i
- if items.shop[ind[i]] then
- gpu.setForeground(0xFFFFFF)
- table.insert(pos_str,{yPos,ind[i]})
- gpu.set(4,yPos,items.shop[ind[i]].text)
- gpu.set(54,yPos,items.shop[ind[i]].price)
- if tonumber(items.shop[ind[i]].available) > 0 then
- gpu.set(64,yPos,items.shop[ind[i]].available)
- else
- gpu.set(64,yPos,"-")
- end
- end
- yPos = yPos + 2
- end
- end
- local function scroll(n)
- if n == 1 or n == "+" then
- drawFrom = drawFrom - 11
- else
- drawFrom = drawFrom + 11
- end
- if drawFrom >= #ind - 11 then
- drawFrom = #ind - 11
- end
- if drawFrom <= 0 then
- drawFrom = 0
- end
- drawlist()
- end
- gpu.setResolution(80,25)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- os.execute("cls")
- drawlist()
- while run do
- srollBar(10,5,2,16,9,drawFrom,0xFFFF99,0xFF9900)
- local e = {event.pull(1)}
- if e[1] == "scroll" then
- scroll(e[5])
- end
- if e[1] == "key_down" then
- if e[4] == 29 then
- run = false
- elseif e[4] == 200 then
- scroll("+")
- elseif e[4] == 208 then
- scroll("-")
- end
- elseif e[1] == "scroll" then
- scroll(e[5])
- elseif e[1] == "touch" then
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.set(1,1,e[3].." "..e[4].." ")
- choice = false
- for i = 1,#pos_str do
- if e[3] <= 77 and e[4] == pos_str[i][1] then
- choice = pos_str[i][2]
- break
- end
- end
- if choice then
- drawlist()
- gpu.set(10,1,"choice = "..choice.." ")
- square(1,e[4],77,1,0xDEDE6C)
- gpu.setForeground(0x3366CC)
- gpu.set(4,e[4],items.shop[choice].text)
- gpu.set(54,e[4],items.shop[choice].price)
- if tonumber(items.shop[choice].available) > 0 then
- gpu.set(64,e[4],items.shop[choice].available)
- else
- gpu.set(64,e[4],"-")
- end
- end
- end
- end
- gpu.setResolution(80,25)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- os.execute("cls")
Add Comment
Please, Sign In to add comment