View difference between Paste ID: SqktAzd2 and fH56Pdch
SHOW: | | - or go back to the newest paste.
1
local unicode = require("unicode")
2
local ecs = require("ecs")
3
local event = require("event")
4
local computer = require("computer")
5
local fs = require("filesystem")
6
local com = require('component')
7
--local interface = com.me_interface
8
local gpu = com.gpu
9
local choice,run = false,true
10
local drawFrom = 0
11
local ind = {1,2,3,4,5,6,7,8,9,10}
12
local items,pos_str = {},{}
13
local patch_items = "/home/items.lua"
14
15
if not fs.exists(patch_items) then
16
  local f = io.open(patch_items,'w')
17
  f:write("{".."\n")
18
  f:write("  shop = {".."\n")
19
  --local data = interface.getItemsInNetwork()
20
  for item = 1,#data do
21
    if data[item] then
22
      f:write('    { text = "'..data[item].label..'", price = "0", label = "'..data[item].label..'" },'..'\n')
23
    end
24
  end
25
  f:write("  }".."\n")
26
  f:write("}")
27
  f:close()
28
  os.execute("edit "..patch_items)
29
  os.exit()
30
end
31
32
local f, err = io.open(patch_items, "r")
33
if not f then
34
  error(err, 2)
35
end
36
local text = f:read('*a')
37
f:close() 
38
local chunk, err = load("return " .. text, "=items.lua", "t")
39
if not chunk then 
40
  error(err, 2)
41
else
42
  items = chunk()
43
end
44
table.sort(items.shop, function(a,b) if a.text then return a.text < b.text end end)
45
for i = 1,#items.shop do
46
  --items.shop[i].available = "0"
47
  items.shop[i].available = tostring(math.random(0,10))
48
end
49
50
local ind = {}
51
for i = 1,#items.shop do
52
  if items.shop[i].available ~= "0" then
53
    table.insert(ind,i)
54
  end
55
end
56
57
local function square(x,y,width,height,color)
58
  if color and gpu.getBackground() ~= color then
59
    gpu.setBackground(color)
60
  end
61
  gpu.fill(x,y,width,height," ")
62
end
63
64
65
66
67
68
69
70
71
--Вертикальный скроллбар. Маст-хев!
72
local function srollBar(x,y,width,height,countOfAllElements,currentElement,backcolors,frontcolors)
73
  local sizeOfScrollBar = math.ceil(1/countOfAllElements*height)
74
  local displayBarFrom = math.floor(y+height*((currentElement)/countOfAllElements))
75
  square(x,y,width,height,backcolors)
76
  square(x,displayBarFrom,width,sizeOfScrollBar,frontcolors)
77
end
78
79
80
81
82
83
local function drawlist()
84
  pos_str = {}
85
  local yPos = 4
86
  for i = 1,11 do
87
    square(1,yPos,77,1,0x000000)
88
    local i = drawFrom + i
89
    if items.shop[ind[i]] then
90
      gpu.setForeground(0xFFFFFF)
91
      table.insert(pos_str,{yPos,ind[i]})
92
      gpu.set(4,yPos,items.shop[ind[i]].text)
93
      gpu.set(54,yPos,items.shop[ind[i]].price)
94
      if tonumber(items.shop[ind[i]].available) > 0 then
95
        gpu.set(64,yPos,items.shop[ind[i]].available)
96
      else
97
        gpu.set(64,yPos,"-")
98
      end
99
    end
100
    yPos = yPos + 2
101
  end
102
end
103
104
local function scroll(n)
105
  if n == 1 or n == "+" then
106
    drawFrom = drawFrom - 11
107
  else
108
    drawFrom = drawFrom + 11
109
  end
110
  if drawFrom >= #ind - 11 then
111
    drawFrom = #ind - 11
112
  end
113
  if drawFrom <= 0 then
114
    drawFrom = 0
115
  end
116
  drawlist()
117
end
118
119
gpu.setResolution(80,25)
120
gpu.setBackground(0x000000)
121
gpu.setForeground(0xFFFFFF)
122
os.execute("cls")
123
drawlist()
124
125
while run do
126
  
127
 srollBar(10,5,2,16,9,drawFrom,0xFFFF99,0xFF9900)
128
  local e = {event.pull(1)}
129
  if e[1] == "scroll" then
130
    scroll(e[5])
131
  end
132
  if e[1] == "key_down" then
133
    if e[4] == 29 then
134
      run = false
135
    elseif e[4] == 200 then
136
      scroll("+")
137
    elseif e[4] == 208 then
138
      scroll("-")
139
    end
140
  elseif e[1] == "scroll" then
141
    scroll(e[5])
142
  elseif e[1] == "touch" then
143
    gpu.setBackground(0x000000)
144
    gpu.setForeground(0xFFFFFF)
145
    gpu.set(1,1,e[3].."  "..e[4].." ")
146
    choice = false
147
    for i = 1,#pos_str do
148
      if e[3] <= 77 and e[4] == pos_str[i][1] then
149
        choice = pos_str[i][2]
150
        break
151
      end
152
    end
153
    if choice then
154
      drawlist()
155
      gpu.set(10,1,"choice = "..choice.."  ")
156
      square(1,e[4],77,1,0xDEDE6C)
157
      gpu.setForeground(0x3366CC)
158
      gpu.set(4,e[4],items.shop[choice].text)
159
      gpu.set(54,e[4],items.shop[choice].price)
160
      if tonumber(items.shop[choice].available) > 0 then
161
        gpu.set(64,e[4],items.shop[choice].available)
162
      else
163
        gpu.set(64,e[4],"-")
164
      end
165
    end
166
  end
167
end
168
169
gpu.setResolution(80,25)
170
gpu.setBackground(0x000000)
171
gpu.setForeground(0xFFFFFF)
172
os.execute("cls")