-
Notifications
You must be signed in to change notification settings - Fork 19
/
Image_processing.lua
576 lines (507 loc) · 16.1 KB
/
Image_processing.lua
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
local dragged
math.randomseed(os.clock())
imageColors = L{'red','blue','green','dark-blue','grey','light-green','orange','pink','purple','yellow'}
--[[ui scaling shrinks display
x_res = 1920
y_res = 1080
ui_x_res = 1476
ui_y_res = 830
settings = ui scale 1.3
ui_x_res = x_res / ui scale
]]--
-- image / icon Block Class
ImageBlock = {
x = 0,
y = 0,
width = 228,
height = 42,
alpha = 255,
red = 255,
green = 255,
blue = 255,
visible = true,
draggable = false,
fit = true,
New = function(order, image_type, image_color, text1, text2)
local o = {}
for k, v in pairs(ImageBlock) do
o[k] = v
end
local order_exists = false
if table.length(sections) > 0 then
if sections[image_type] and image_type == 'background' then
return error('can only have 1 background!')
elseif image_type == 'background' then
order = 0
end
if sections[image_type] and image_type == 'logo' then
return error('can only have 1 logo!')
elseif image_type == 'logo' then
order = 1
end
if image_type == 'block' and table.length(sections.block) > 0 then
for k, v in pairs(sections.block) do
if sections[image_type][k] and sections[image_type][k].order == order then
order_exists = true
end
end
if order_exists == false then
o.order = order
else
return error('order magnitude ' .. order .. ' is already assigned!')
end
elseif image_type == 'block' and table.length(sections.block) == 0 then
o.order = order
end
end
if image_type == 'background' then
o.width = 228
o.height = 42
o.image_path = ''
o.dragable = true
o.alpha = 255
o.red = 0
o.green = 0
o.blue = 0
o.type = image_type
o.color = image_color
o.name = 'block_' .. order
o.x = settings.display.pos.x
o.y = settings.display.pos.y
elseif image_type == 'logo' then
o.width = 228
o.height = 42
o.type = image_type
o.color = image_color
o.image_path = windower.addon_path..'textures/'..settings.image_folder_name..'/'.. image_type ..'.png'
o.name = 'block_' .. order
o.x = sections.background:position_x()
o.y = sections.background:position_y()
check_positions()
elseif image_type == 'block' then
if imageColors:contains(image_color) then
o.width = 57
o.height = 42
o.type = image_type
o.color = image_color
o.image_path = windower.addon_path..'textures/'..settings.image_folder_name..'/'..image_color ..'.png'
o.name = 'block_' .. order
o.x, o.y = get_position(o)
o.text = {}
o.text[1] = {name = 'text1'..o.name, text = text1, offset_x = 3 , offset_y = 3}
windower.text.create(o.text[1].name)
windower.text.set_text(o.text[1].name, o.text[1].text)
--windower.text.set_color(o.text[1].name, 255, 0, 0, 0)
windower.text.set_color(o.text[1].name, 255, 150,150,235)
windower.text.set_font_size(o.text[1].name, 9)
windower.text.set_visibility(o.text[1].name, true)
windower.text.set_font(o.text[1].name,'Tahoma') --'Verdana'
windower.text.set_stroke_width(o.text[1].name, 0)
windower.text.set_stroke_color(o.text[1].name, 255, 0, 0, 0)
--windower.text.set_stroke_color(o.text[1].name, 255, 247,243,195)
windower.text.set_location(o.text[1].name, o.text[1].offset_x + o.x, o.text[1].offset_y + o.y)
windower.text.set_bold(o.text[1].name, true)
--print(windower.text.get_extents(o.text[1].name))
o.text[2] = {name = 'text2'..o.name, text = text2, offset_x = 3 , offset_y = 20}
windower.text.create(o.text[2].name)
windower.text.set_text(o.text[2].name, o.text[2].text)
windower.text.set_bold(o.text[2].name, false)
windower.text.set_color(o.text[2].name, 255, 255, 255, 255)
windower.text.set_font_size(o.text[2].name, 12)
windower.text.set_visibility(o.text[2].name, true)
windower.text.set_font(o.text[2].name,'Tahoma') --'Verdana'
windower.text.set_stroke_width(o.text[2].name, 0)
windower.text.set_stroke_color(o.text[2].name, 255, 0, 0, 0)
windower.text.set_location(o.text[2].name, o.text[2].offset_x + o.x, o.text[2].offset_y + o.y)
else
return error('Wrong color type')
end
else
return error('Wrong image type!')
end
windower.prim.create(o.name)
windower.prim.set_color(o.name, o.alpha, o.red, o.green, o.blue)
windower.prim.set_texture(o.name, o.image_path)
windower.prim.set_fit_to_texture(o.name, o.fit)
windower.prim.set_size(o.name, o.width, o.height)
windower.prim.set_visibility(o.name, o.visible)
windower.prim.set_position(o.name, o.x, o.y)
return o
end,
-- Makes the primitive visible
show = function(self)
windower.prim.set_visibility(self.name, true)
self.visible = true
end,
-- Makes the primitive invisible
hide = function(self)
windower.prim.set_visibility(self.name, false)
self.visible = false
end,
-- Returns whether or not the image object is visible
visible = function(self, visible)
if visible == nil then
return self.visible
end
windower.prim.set_visibility(self.name, visible)
end,
position = function(self, new_x, new_y)
if new_x == nil then
return self.x, self.y
end
self.x = new_x
self.y = new_y
windower.prim.set_position(self.name, new_x, new_y)
if self.type == 'block' then
windower.text.set_location(self.text[1].name, new_x + self.text[1].offset_x , new_y + self.text[1].offset_y)
windower.text.set_location(self.text[2].name, new_x + self.text[2].offset_x , new_y + self.text[2].offset_y)
end
end,
position_x = function(self, new_x)
if new_x == nil then
return self.x
end
self:position(new_x, self.y)
if self.type == 'block' then
windower.text.set_location(self.text[1].name, new_x + self.text[1].offset_x , self.y + self.text[1].offset_y)
windower.text.set_location(self.text[2].name, new_x + self.text[2].offset_x , self.y + self.text[2].offset_y)
end
end,
position_y = function(self, new_y)
if new_y == nil then
return self.y
end
self:position(self.x, new_y)
if self.type == 'block' then
windower.text.set_location(self.text[1].name, self.x + self.text[1].offset_x , new_y + self.text[1].offset_y)
windower.text.set_location(self.text[2].name, self.x + self.text[2].offset_x , new_y + self.text[2].offset_y)
end
end,
image_size = function(self, width, height)
if width == nil then
return self.width, self.height
end
windower.prim.set_size(self.name, width, height)
self.width = width
self.height = height
end,
image_width = function(self, width)
if width == nil then
return self.width
end
self:size(width, self.height)
end,
image_height = function(self, height)
if height == nil then
return self.height
end
self:size(self.width, height)
end,
get_extents = function(self)
local ext_x = self.x + self.width
local ext_y = self.y + self.height
return ext_x, ext_y
end,
hover = function(self, mouse_x, mouse_y)
if not self:visible() then
return false
end
local x_bool = false
local y_bool = false
local pos_x, pos_y = self:position()
local off_x, off_y = self:get_extents()
if mouse_x >= pos_x and mouse_x <= (pos_x + self.width) then
x_bool = true
--log('x hit ' .. tostring(x_bool))
end
if mouse_y > pos_y and mouse_y < (pos_y + self.height) then
y_bool = true
--log('y hit ' .. tostring(y_bool))
end
if x_bool == true and y_bool == true then
return true
else
return false
end
end,
draggable = function(self, drag)
if drag == nil then
return self.draggable
end
self.draggable = drag
end,
image_color = function(self, red, green, blue)
if red == nil then
return self.red, self.green, self.blue
end
windower.prim.set_color(self.name, self.alpha, red, green, blue)
self.red = red
self.green = green
self.blue = blue
end,
image_alpha = function(self, alpha)
if alpha == nil then
return self.alpha
end
windower.prim.set_color(self.name, alpha, self.red, self.green, self.blue)
self.alpha = alpha
end,
pos_diff = function (self, t)
local self_x, self_y = self:position()
local bg_x, bg_y = t:position()
return self_x - bg_x , self_y - bg_y
end,
delete = function(self)
windower.prim.delete(self.name)
for k, v in pairs(sections) do
if k ~= 'block' then
if v.name == self.name then
sections[k] = nil
check_positions()
end
else
for i, j in pairs(v) do
if j.name == self.name then
--notice('Delete '..k..' of color '..j.color.. ' '..j.name)
windower.text.delete(self.text[1].name)
windower.text.delete(self.text[2].name)
sections[k][i] = nil
check_positions()
end
end
end
end
end,
}
--ImageBlock.mt.__index = ImageBlock
-- ImageBlock.mt.__newindex = function(self,k,v)
-- error('cannot change '.. k.. ' to ' .. v.. ' in table ' ..tostring(self))
-- end
-- Handle drag and drop
windower.register_event('mouse', function(type, x, y, delta, blocked)
if blocked then
return
end
-- Mouse drag
if type == 0 then
if dragged then
update_all(x - dragged.x, y - dragged.y, dragged.image )
centre_all_text()
dragged.image:position(x - dragged.x, y - dragged.y)
return true
end
-- Mouse left click
elseif type == 1 then
if sections.background.draggable and sections.background:hover(x, y) then
local pos_x, pos_y =sections.background:position()
dragged = {image = sections.background, x = x - pos_x, y = y - pos_y}
return true
end
-- Mouse left release
elseif type == 2 then
if dragged then
dragged = nil
settings.display.pos.x = sections.background:position_x()
settings.display.pos.y = sections.background:position_y()
settings:save('all')
return true
end
end
return false
end)
function update_all(x, y, background)
local difx, dify = 0, 0
if table.length(sections) > 0 then
for k, v in pairs(sections) do
if k ~= 'background' then
if k == 'logo' then
difx, dify = sections.logo:pos_diff(background)
sections.logo:position(difx + x, dify + y)
elseif k == 'block' then
for i, j in pairs(v) do
difx, dify = sections.block[i]:pos_diff(background)
sections.block[i]:position(difx + x, dify + y)
end
end
end
end
end
end
function check_positions()
local x, y = 0, 0
local max_columns = 4
local min_rows = 1
local block_count = table.length(sections.block) + 1
local grid = {}
local block_width = 57
local block_height = 42
local added = {}
--notice('----------------------------------------------------------------------')
local highest_order = 0
local order_exists = {}
for k, v in pairs(sections.block) do
if sections.block[k].order > highest_order then
highest_order = sections.block[k].order
end
end
for i = 1, highest_order do
if sections.block[i] then
order_exists[i] = true
else
order_exists[i] = false
end
end
if block_count > 0 then min_rows = 2 end
--print( (min_rows + math.floor((block_count - 1) / 4) ))
for i = 1, (min_rows + math.floor((block_count - 1) / 4) ) do
grid[i] = {}
for j = 1, 4 do
grid[i][j] = {}
grid[i][j]['empty'] = true
grid[i][j]['image'] = {}
if i == 1 and j < 5 and settings.player.show_logo == true then
grid[i][j]['empty'] = false
grid[i][j]['image'] = sections.logo
end
if grid[i][j]['empty'] == true then
for k, v in ipairs(order_exists) do
if not added[k] and v then
grid[i][j]['empty'] = false
grid[i][j]['image'] = sections.block[k]
sections.block[k]:position_x(((j - 1) * block_width) + sections.background:position_x())
sections.block[k]:position_y(((i - 1) * block_height) + sections.background:position_y())
added[k] = true
break
end
end
-- for k, v in pairs(sections.block) do
-- if not added[k] then
-- --log('order = '..v.order)
-- grid[i][j]['empty'] = false
-- grid[i][j]['image'] = sections.block[k]
-- sections.block[k]:position_x(((j - 1) * block_width) + sections.background:position_x())
-- sections.block[k]:position_y(((i - 1) * block_height) + sections.background:position_y())
-- added[k] = true
-- break
-- end
-- end
end
--notice(i, j, grid[i][j]['empty'] )
end
end
for i = 1, (min_rows + math.floor((block_count - 1) / 4) ) do
for j = 1, 4 do
if grid[i][j]['empty'] == false then
--notice(i, j, grid[i][j]['empty'], grid[i][j]['image'].type, grid[i][j]['image'].color)
sections.background:image_size(228, (i * block_height))
else
--notice(i, j, grid[i][j]['empty'] )
end
end
end
end
function get_position(this_image)
local x, y = 0, 0
local max_columns = 4
local min_rows = 1
local block_count = table.length(sections.block) + 1
local grid = {}
local block_width = 57
local block_height = 42
local order_start = 2
--notice('----------------------------------------------------------------------')
local added = {}
local highest_order = 0
local order_exists = {}
-- print(this_image.order)
for k, v in pairs(sections.block) do
if sections.block[k].order > highest_order then
highest_order = sections.block[k].order
end
end
if this_image.order > highest_order then highest_order = this_image.order end
for i = 1, highest_order do
if sections.block[i] then
order_exists[i] = true
else
order_exists[i] = false
end
if (this_image.order - 1) == i then
order_exists[i] = true
end
end
--table.vprint(order_exists)
--table.rekey(order_exists)
if block_count > 0 then min_rows = 2 end
--print( (min_rows + math.floor((block_count - 1) / 4) ))
for i = 1, (min_rows + math.floor((block_count - 1) / 4) ) do
grid[i] = {}
for j = 1, 4 do
grid[i][j] = {}
grid[i][j]['empty'] = true
grid[i][j]['image'] = {}
if i == 1 and j < 5 and settings.player.show_logo == true then
grid[i][j]['empty'] = false
grid[i][j]['image'] = sections.logo
end
if grid[i][j]['empty'] == true then
for k, v in ipairs(order_exists) do
if not added[k] and v and (this_image.order - 1) ~= k then
grid[i][j]['empty'] = false
grid[i][j]['image'] = sections.block[k]
sections.block[k]:position_x(((j - 1) * block_width) + sections.background:position_x())
sections.block[k]:position_y(((i - 1) * block_height) + sections.background:position_y())
added[k] = true
break
end
if not added[k] and (this_image.order - 1) == k then
added[k] = true
break
end
end
end
--notice(i, j, grid[i][j]['empty'] )
end
end
-- for i = 1, (min_rows + math.floor((block_count - 1) / 4) ) do
-- for j = 1, 4 do
-- if grid[i][j]['empty'] == false then
-- notice(i, j, grid[i][j]['empty'], grid[i][j]['image'].type, grid[i][j]['image'].color)
-- else
-- notice(i, j, grid[i][j]['empty'] )
-- end
-- end
-- end
local added_last = false
if this_image then
for i = 1, (min_rows + math.floor((block_count - 1) / 4) ) do
for j = 1, 4 do
if grid[i][j]['empty'] == true and added_last == false then
added_last = true
grid[i][j]['empty'] = false
x = ((j - 1) * block_width) + sections.background:position_x()
y = ((i - 1) * block_height) + sections.background:position_y()
--notice(i, j, grid[i][j]['empty'], this_image.type, this_image.color)
end
sections.background:image_size(228, (i * block_height))
--notice(i, j, grid[i][j]['empty'])
end
end
return x, y
end
end
function centre_all_text()
--print(windower.text.get_extents(sections.block[1].text[1].name))
for k, v in pairs(sections.block) do
local width, height = windower.text.get_extents(sections.block[k].text[1].name)
local width2, height2 = windower.text.get_extents(sections.block[k].text[2].name)
--x offset = 3
local centre_of_block = 57 / 2
local centre_of_text = width / 2
local centre_of_text2 = width2 / 2
new_centre = centre_of_block - centre_of_text
new_centre2 = centre_of_block - centre_of_text2
windower.text.set_location(sections.block[k].text[1].name, new_centre + sections.block[k].x , sections.block[k].text[1].offset_y + sections.block[k].y)
windower.text.set_location(sections.block[k].text[2].name, new_centre2 + sections.block[k].x , sections.block[k].text[2].offset_y + sections.block[k].y)
end
end