-
Notifications
You must be signed in to change notification settings - Fork 4
/
E621Viewer.txt
371 lines (332 loc) · 10.8 KB
/
E621Viewer.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
--@name E621Viewer
--@author Sparky
--@shared
-- Just connect a starfall screen and wire a wire_keyboard to the chip.
-- Then type in e621 search tags and press enter.
-- Press use on an image to zoom in or on the next/prev button to switch pages.
function net.writeLinks(links)
if #links>0 then
local linkData = fastlz.compress(table.concat(links,"\0"))
net.writeUInt(#linkData,32)
net.writeData(linkData,#linkData)
else
net.writeUInt(0,32)
end
end
function net.readLinks()
local linkSize = net.readUInt(32)
if linkSize>0 then
local linkData = fastlz.decompress(net.readData(linkSize))
if linkData then
return string.split(linkData, "\0")
else
return {}
end
else
return {}
end
end
if CLIENT then
local browser = class("browser")
local mybrowser
local image = class("image")
function browser:initialize()
self.pagesloaded = 0
self.page = 1
self.noresults = false
self.links = {}
self.images = {}
self.sortedimages = {}
for i=1, 16 do
self.images[i] = image:new(i, ((i-1)%4)*128, math.floor((i-1)/4)*128)
self.sortedimages[i] = self.images[i]
end
self.search = ""
end
function browser:used(pl,x,y)
if y>=494 then
if x>400 then
if player()==pl then
net.start("nextpage") net.send()
end
elseif x<112 then
if player()==pl then
net.start("prevpage") net.send()
end
end
return
end
x = math.floor(x/128)
y = math.floor(y/128)
local index = y*4+x+1
for i, image in ipairs(self.images) do
if i==index then
image:toggleZoom()
else
image:zoomOut()
end
end
end
function browser:startsearch()
self.pagesloaded = 0
self.page = 1
self.noresults = false
self.links = {}
self.searching = self.search
self.search = ""
if player()==owner() then
mybrowser:loadPage()
end
end
function browser:loadPage()
if self.loading or self.noresults or self.page*16 < #self.links-32 then return end
self.loading = true
http.get("https://e621.net/posts.json?limit=64&tags="..http.urlEncode(self.searching).."&page="..(self.pagesloaded + 1), function(body)
local data = json.decode(body)
if type(data.posts)=="table" and #data.posts > 0 then
self.pagesloaded = self.pagesloaded + 1
local links = {}
for k, v in pairs(data.posts) do
if v.file and v.file.url then
local ext = string.getExtensionFromFilename( v.file.url )
if not (ext=="swf" or ext=="webm") then
links[#links + 1] = v.file.url
end
end
end
net.start("gotlinks")
net.writeLinks(links)
net.send()
else
self.noresults = true
end
self.loading = false
end,
function()
self.loading = false
timer.simple(1,function() self:loadPage() end)
end)
end
function browser:setPage(page)
self.page = page
local linkstart = (self.page-1)*16
for k, v in pairs(self.images) do
v:loadImage(self.links[linkstart + k])
end
if player()==owner() then
mybrowser:loadPage()
end
end
function browser:gotLinks(links)
local linkcount = #self.links
table.add(self.links, links)
if linkcount < self.page*16 then
self:setPage(self.page)
end
if player()==owner() then
mybrowser:loadPage()
end
end
function browser:draw()
render.setColor(Color(255,255,255,255))
table.sort(self.sortedimages, function(a,b) return a.interp < b.interp end)
for _, v in ipairs(self.sortedimages) do
v:draw()
end
render.setColor(Color(0,0,0,255))
render.drawRect(0, 494, 512, 18)
if #self.search>0 then
render.drawRect(0, 0, 512, 18)
end
render.setColor(Color(255,255,255,255))
render.drawText(256, 495, "Press use to zoom in/out.", 1)
if self.page > 1 then
render.drawText(50, 495, "<-Prev", 1)
end
if self.page*16 < #self.links then
render.drawText(462, 495, "Next->", 1)
end
render.drawText(20, 1, self.search)
end
image.loadingMaterial = render.createMaterial("expression 2/cog")
function image:initialize(i,x,y)
self.i = i
self.x = x
self.y = y
self.zoomreverse = true
self.zoomtime=0
self.interp = 0
self.imagematerial = material.create("UnlitGeneric")
self.material = image.loadingMaterial
end
local function forwardTime(image)
local x = math.min((timer.curtime()-image.zoomtime)/0.5,1)
local y = 1-math.sqrt(x)
local x2 = y^2
image.zoomtime = timer.curtime()-x2*0.5
end
local function reverseTime(image)
local x = math.min((timer.curtime()-image.zoomtime)/0.5,1)
local y = math.sqrt(x)
local x2 = (1-y)^2
image.zoomtime = timer.curtime()-x2*0.5
end
function image:toggleZoom()
self.zoom = true
self.zoomreverse = not self.zoomreverse
if self.zoomreverse then
reverseTime(self)
else
forwardTime(self)
end
end
function image:zoomOut()
if self.zoom and not self.zoomreverse then
self.zoomreverse = true
reverseTime(self)
end
end
function image:draw()
local x, y, w, h
if self.zoom and self.material ~= self.loadingMaterial then
self.interp = math.min((timer.curtime()-self.zoomtime)/0.5,1)
if self.zoomreverse then
self.interp = 1-math.sqrt(self.interp)
if self.interp==0 then self.zoom = false end
else
self.interp=math.sqrt(self.interp)
end
x, y, w, h = self.x-self.x*self.interp, self.y-self.y*self.interp, 128+384*self.interp, 128+384*self.interp
else
x, y, w, h = self.x, self.y, 128, 128
end
render.setMaterial(self.material)
if self.material == image.loadingMaterial then
local mat = Matrix()
local sin = math.sin(timer.curtime()*5+self.i)*10
mat:translate(Vector(x+64, y+64, 0))
mat:rotate(Angle(0,math.sin(timer.curtime()*5+self.i)*10,0))
mat:translate(Vector(-64, -64, 0))
render.pushMatrix(mat)
render.drawTexturedRect(0, 0, w, h)
render.popMatrix()
else
render.drawTexturedRect(x, y, w, h)
end
end
function image:loadImage(link)
self.material = image.loadingMaterial
self.interp = 0
self.zoom = false
if not link then return end
if self.loading then
self.loadnext = link
else
self.loading = true
self.imagematerial:setTextureURL("$basetexture", link, function(tx,url,w,h,layout)
if not tx then return end
if w>h then
h = h*1024/w
w = 1024
else
w = w*1024/h
h = 1024
end
pcall(layout, 512 - w/2, 512 - h/2, w, h)
end,
function()
self.material = self.imagematerial
self.loading = false
if self.loadnext then
self:loadImage(self.loadnext)
self.loadnext = nil
end
end)
end
end
mybrowser = browser:new()
net.receive("gotlinks", function()
mybrowser:gotLinks(net.readLinks())
end)
net.receive("page", function()
mybrowser:setPage(net.readUInt(16))
end)
net.receive("search", function()
local enter = net.readBool()
mybrowser.search = net.readString()
if enter then
mybrowser:startsearch()
end
end)
net.receive("init", function()
local page = net.readUInt(16)
mybrowser.search = net.readString(search)
if linkData and #linkData>0 then
mybrowser.links = net.readLinks()
mybrowser:setPage(page)
end
end)
net.start("init") net.send()
hook.add("starfallused","",function(ply, screen)
local ok, x, y = pcall(render.cursorPos, ply, screen)
if not (ok and x and y) then return end
mybrowser:used(ply,x,y)
end)
hook.add("render","screen",function() mybrowser:draw() end)
else
local links, page, search, searchstr
local function reset()
links = {}
page = 1
search = ""
end
reset()
net.receive("gotlinks", function()
local newlinks = net.readLinks()
table.add(links, newlinks)
net.start("gotlinks")
net.writeLinks(newlinks)
net.send()
end)
net.receive("nextpage", function()
if #links > page*16 then
page = page + 1
net.start("page")
net.writeUInt(page, 16)
net.send()
end
end)
net.receive("prevpage", function()
if page > 1 then
page = page - 1
net.start("page")
net.writeUInt(page, 16)
net.send()
end
end)
net.receive("init",function(len,pl)
net.start("init")
net.writeUInt(page, 16)
net.writeString(search)
net.writeLinks(links)
net.send(pl)
end)
wire.adjustInputs({"Key"},{"normal"})
hook.add("input","",function(input,key)
if input=="Key" then
if key==127 then
search = string.sub(search, 1, #search-1)
net.start("search") net.writeBool(false) net.writeString(search) net.send()
elseif key==10 then
net.start("search") net.writeBool(true) net.writeString(search) net.send()
reset()
else
local str = string.char(key)
if string.find(str, "[%a _<>:%+%-/]") then
search = search .. str
net.start("search") net.writeBool(false) net.writeString(search) net.send()
end
end
end
end)
end