-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit.lua
2916 lines (2562 loc) · 133 KB
/
init.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local data = { -- window size
width = 15,
height = 10,
}
local form_esc = minetest.formspec_escape -- shorten the function
local modstorage = core.get_mod_storage()
local function create_tabs(selected)
return "tabheader[0,0;_option_tabs_;" ..
" LUA EDITOR ,FORMSPEC EDITOR, LUA CONSOLE , FILES , STARTUP , FUNCTIONS , HELP ;"..selected..";;]"
end
local function copy_table(table)
local new = {}
for i, v in pairs(table) do
if type(v) == "table" then
v = copy_table(v)
end
new[i] = v
end
return new
end
----------
-- LOAD AND DEFINE STUFF - global stuff is accissible from the UI
----------
local split = function (str, splitter) -- a function to split a string into a list. "\" before the splitter makes it ignore it (usefull for minetests formspecs)
local result = {""}
for i=1, str:len() do
char = string.sub(str, i, i)
if char == splitter and string.sub(str, i-1, i-1) ~= "\\" then
table.insert(result, "")
else
result[#result] = result[#result]..char
end
end
return result
end
local output = {} -- the output for errors, prints, etc
local saved_file = modstorage:get_string("_lua_saved") -- remember what file is currently being edited
if saved_file == "" then
saved_file = false -- if the file had no save name (it was still saved)
end
local lua_startup = split(modstorage:get_string("_lua_startup"), ",") -- the list of scripts to run at startup
local lua_files = split(modstorage:get_string("_lua_files_list"), ",") -- the list of names of all saved files
local ui_files = split(modstorage:get_string("_UI_files_list"), ",") -- UI files list
local reg_funcs = {formspec_input={}, chatcommands={}, on_connect={}, joinplayer={}, sending_chat_message={}, recieving_chat_message={}}
local selected_files = {0, 0}
minetest.register_on_connect(function() -- some functions don't work after startup. this tries to replace them
minetest.get_mod_storage = function()
return modstorage
end
core.get_mod_storage = function()
return modstorage
end
-- show formspec
end) -- add whatever functions don't work after startup to here (if possible)
----------
-- FUNCTIONS FOR UI
----------
function print(...) -- replace print to output into the UI. (doesn't refresh untill the script has ended)
params = {...}
if #params == 1 then
local str = params[1]
if type(str) ~= "string" then
str = dump(str)
end
table.insert(output, "")
for i=1, str:len() do
char = string.sub(str, i, i)
if char == "\n" then
table.insert(output, "") -- split multiple lines over multiple lines. without this, text with line breaks would not display properly
else
output[#output] = output[#output]..char
end
end
else
for i, v in pairs(params) do
print(v)
end
end
end
function safe(func) -- run a function without crashing the game. All errors are displayed in the UI.
f = function(...) -- This can be used for functions being registered with minetest, like "minetest.register_chat_command()"
status, out = pcall(func, ...)
if status then
return out
else
table.insert(output, "#ff0000Error: "..out)
minetest.debug("Error (func): "..out)
return nil
end
end
return f
end
----------
-- CODE EXECUTION
----------
local function run(code, name) -- run a script
if name == nil then
name = saved_file
end
status, err = pcall(loadstring(code)) -- run
if status then
if saved_file == false then
table.insert(output, "#00ff00finished") -- display that the script ran without errors
else
table.insert(output, "#00ff00"..name..": finished") -- display which script, if it was saved
end
else
if err == "attempt to call a nil value" then
err = "Syntax Error"
end
if saved_file == false then
table.insert(output, "#ff0000Error: "..err) -- display errors
minetest.log("Error (unsaved): "..err)
else
table.insert(output, "#ff0000"..name..": Error: "..err)
minetest.log("Error ("..name.."): "..err)
end
end
end
local function on_startup() -- ran on startup. Runs all scripts registered for startup
for i, v in pairs(lua_startup) do
if v ~= "" then
run(modstorage:get_string("_lua_file_"..v, v), v) -- errors still get displayed in the UI
end
end
end
on_startup()
----------
-- FILE READING AND SAVING
----------
local function load_lua() -- returns the contents of the file currently being edited
if saved_file == false then
return modstorage:get_string("_lua_temp") -- unsaved files are remembered (get saved on UI reloads - when clicking on buttons)
else
return modstorage:get_string("_lua_file_"..saved_file)
end
end
local function save_lua(code) -- save a file
if saved_file == false then
modstorage:set_string("_lua_temp", code)
else
modstorage:set_string("_lua_file_"..saved_file, code)
end
end
----------
-- FORM DEFINITIONS
----------
local function startup_form() -- the formspec for adding or removing files for startup
local startup_str = ""
for i, v in pairs(lua_startup) do
if i ~= 1 then startup_str = startup_str.."," end
startup_str = startup_str .. form_esc(v)
end
local files_str = ""
for i, v in pairs(lua_files) do
if i ~= 1 then files_str = files_str.."," end
files_str = files_str .. form_esc(v)
end
local form = ""..
"size["..data.width..","..data.height.."]" ..
"label[0,0.1;Startup Items:]"..
"label["..data.width/2 ..",0.1;File List:]"..
"textlist[0,0.5;"..data.width/2-0.1 ..","..data.height-1 ..";starts;"..startup_str.."]"..
"textlist["..data.width/2 ..",0.5;"..data.width/2-0.1 ..","..data.height-1 ..";chooser;"..files_str.."]"..
"label[0," .. data.height-0.3 .. ";double click items to add or remove from startup]"..
"" .. create_tabs(5)
return form
end
local function lua_editor() -- the main formspec for editing
local output_str = "" -- convert the output to a string
for i, v in pairs(output) do
if output_str:len() > 0 then output_str = output_str .. "," end
output_str = output_str .. form_esc(v)
end
local code = form_esc(load_lua())
-- create the form
local form = ""..
"size["..data.width..","..data.height.."]" ..
"textarea[0.3,0.1;"..data.width ..","..data.height-3 ..";editor;Lua editor;"..code.."]"..
"button[0," .. data.height-3.5 .. ";1,0;run;RUN]"..
"button[1," .. data.height-3.5 .. ";1,0;clear;CLEAR]"..
"button[2," .. data.height-3.5 .. ";1,0;save;SAVE]"..
"textlist[0,"..data.height-3 ..";"..data.width-0.2 ..","..data.height-7 ..";output;"..output_str..";".. #output .."]"..
"" .. create_tabs(1)
return form
end
local function file_viewer() -- created with the formspec editor!
local lua_files_item_str = ""
for i, item in pairs(lua_files) do
if i ~= 1 then lua_files_item_str = lua_files_item_str.."," end
lua_files_item_str = lua_files_item_str .. form_esc(item)
end
local ui_select_item_str = ""
for i, item in pairs(ui_files) do
if i ~= 1 then ui_select_item_str = ui_select_item_str.."," end
ui_select_item_str = ui_select_item_str .. form_esc(item)
end
local form = "" ..
"size["..data.width..","..data.height.."]" ..
"textlist[-0.2,0.2;"..data.width/2.02- -0.2 ..","..data.height- 1 ..";lua_select;"..lua_files_item_str.."]" ..
"label[-0.2,-0.2;LUA FILES]" ..
"field[0.1,"..data.height- 0.2 ..";3,1;new_lua;NEW;]" ..
"field_close_on_enter[new_lua;false]" ..
"button[2.6,"..data.height- 0.5 ..";0.5,1;add_lua;+]" ..
"textlist["..data.width/1.97 ..",0.2;"..data.width- 0-(data.width/1.97) ..","..data.height- 1 ..";ui_select;"..ui_select_item_str.."]" ..
"label["..data.width/1.96 ..",-0.2;FORMSPEC FILES]" ..
"field["..data.width- 2.8 ..","..data.height- 0.2 ..";3,1;new_ui;NEW;]" ..
"field_close_on_enter[new_ui;false]" ..
"button["..data.width- 0.3 ..","..data.height- 0.5 ..";0.5,1;add_ui;+]" ..
"label["..data.width/2.4 ..","..data.height- 0.8 ..";Double click a file to open it]" ..
"button[3.1,"..data.height- 0.5 ..";1.1,1;del_lua;DELETE]" ..
"button["..data.width- 4.2 ..","..data.height- 0.5 ..";1.1,1;del_ui;DELETE]" ..
"" .. create_tabs(4)
return form
end
----------
-- FUNCTIONALITY
----------
minetest.register_on_formspec_input(function(formname, fields)
-- EDITING PAGE
----------
if formname == "lua:editor" then
if fields.run then --[RUN] button
save_lua(fields.editor)
run(fields.editor)
minetest.show_formspec("lua:editor", lua_editor())
elseif fields.save then --[SAVE] button
if saved_file == false then
modstorage:set_string("_lua_temp", fields.editor)
else
modstorage:set_string("_lua_file_"..saved_file, fields.editor)
end
elseif fields.clear then --[CLEAR] button
output = {}
save_lua(fields.editor)
minetest.show_formspec("lua:editor", lua_editor())
end
-- STARTUP EDITOR
----------
elseif formname == "lua:startup" then -- double click a file to remove it from the list
if fields.starts then
local select = {["type"] = string.sub(fields.starts, 1, 3), ["row"] = tonumber(string.sub(fields.starts, 5, 5))}
if select.type == "DCL" then
table.remove(lua_startup, select.row)
local startup_str = ""
for i, v in pairs(lua_startup) do
if v ~= "" then
startup_str = startup_str..v..","
end
end
modstorage:set_string("_lua_startup", startup_str)
minetest.show_formspec("lua:startup", startup_form())
end
elseif fields.chooser then -- double click a file to add it to the list
local select = {["type"] = string.sub(fields.chooser, 1, 3), ["row"] = tonumber(string.sub(fields.chooser, 5, 5))}
if select.type == "DCL" then
table.insert(lua_startup, lua_files[select.row])
local startup_str = ""
for i, v in pairs(lua_startup) do
if v ~= "" then
startup_str = startup_str..v..","
end
end
modstorage:set_string("_lua_startup", startup_str)
minetest.show_formspec("lua:startup", startup_form())
end
end
end
end)
---------- ----------
-- PASTE FORMSPEC EDITOR HERE --
---------- ----------
---------- ----------
-- FORMSPEC EDITOR START --
---------- ----------
local widg_list = {"Button", "DropDown", "CheckBox", "Slider", "Tabs", "TextList", "Table", "Field", "TextArea", "InvList", "Label", "Image", "Box", "Tooltip", "Container"} -- all widget options
local widgets = nil -- stores all widget data for the current file
local selected_widget = 1 -- the widget/tab currently being edited
local new_widg_tab = false -- so the new widget tab can be displayed without moving the selection
local main_ui_form -- make this function global to the rest of the program
local current_ui_file = modstorage:get_string("_GUI_editor_selected_file") -- file name of last edited file
if current_ui_file == "" then -- for first ever load
current_ui_file = "new"
modstorage:set_string("_GUI_editor_selected_file", current_ui_file)
modstorage:set_string("_GUI_editor_file_"..current_ui_file, dump({{type="Display", name="", width=5, height=5, width_param=false, height_param=false, left=0.5, top=0.5,
position=false, background=false, colour="#000000aa", fullscreen=false, colour_tab=false,
col={col=false, bg_normal="#f0fa", bg_hover="#f0fa", set_border=false, border="#f0fa", set_tool=false, tool_bg="#f0fa", tool_font="#f0fa"}}}))
end
local function reload_ui() -- update the display, and save the file
modstorage:set_string("_GUI_editor_file_"..current_ui_file, dump(widgets))
minetest.show_formspec("ui_editor:main", main_ui_form())
end
local function load_UI(name) -- open/create a ui file
current_ui_file = name
modstorage:set_string("_GUI_editor_selected_file", current_ui_file)
_, widgets = pcall(loadstring("return "..modstorage:get_string("_GUI_editor_file_"..current_ui_file)))
if widgets == nil then
widgets = {{type="Display", name="", width=5, height=5, width_param=false, height_param=false, left=0.5, top=0.5,
position=false, background=false, colour="#000000aa", fullscreen=false, colour_tab=false,
col={col=false, bg_normal="#f0fa", bg_hover="#f0fa", set_border=false, border="#f0fa", set_tool=false, tool_bg="#f0fa", tool_font="#f0fa"}}}
end
end
load_UI(current_ui_file)
--widgets = {{type="Display", name="", width=5, height=5, width_param=false, height_param=false, left=0.5, top=0.5, position=false, background=false, colour="#000000aa", fullscreen=false, colour_tab=false, col={col=false, bg_normal="#f0fa", bg_hover="#f0fa", set_border=false, border="#f0fa", set_tool=false, tool_bg="#f0fa", tool_font="#f0fa"}} }
----------
-- UI DISPLAY
----------
-- generates the preview of the UI being edited
local function generate_ui()
local width = data.width-5 -- the size that is needed for the final formspec size, so large formspecs can be previewed
local height = data.height
-- data for calculating positions
local left = {0.1}
local top = {0.1}
local fwidth = {1}
local fheight = {1}
local form = ""
local boxes = "" -- because I can't add to form in get_rect() function
local depth = 1 -- container depth
-- calculates the positions of widgets, and creates the position syntax from a widget def
local function get_rect(widget, real, full)
local wleft = 0 -- widget top (etc)
if widget.left_type == "R-" then -- right is value from right side
wleft = left[depth]+fwidth[depth]-widget.left
elseif widget.left_type == "W/" then -- right is width/value from left side
wleft = left[depth]+fwidth[depth]/widget.left
else -- right is value from left side
wleft = left[depth]+widget.left
end
if full then -- container only takes whole numbers as positions.
wleft = math.floor(wleft-left[depth])+left[depth]
end
local wtop = 0
if widget.top_type == "B-" then -- value from bottom
wtop = top[depth]+fheight[depth]-widget.top
elseif widget.top_type == "H/" then -- height/value from top
wtop = top[depth]+(fheight[depth]/widget.top)
else -- value from top
wtop = top[depth]+widget.top
end
if full then
wtop = math.floor(wtop-top[depth])+top[depth]
end
if widget.right == nil then -- for widgets with no size option
return wleft..","..wtop..";"
else
local wright = 0
if widget.right_type == "R-" then
wright = left[depth]+fwidth[depth]-widget.right-wleft
elseif widget.right_type == "W/" then
wright = left[depth]+fwidth[depth]/widget.right-wleft
elseif widget.right_type == "R" then -- relative to left
wright = widget.right
else
wright = left[depth]+widget.right-wleft
end
local wbottom = 0
if widget.bottom_type == "B-" then
wbottom = top[depth]+fheight[depth]-widget.bottom-wtop
elseif widget.bottom_type == "H/" then
wbottom = top[depth]+fheight[depth]/widget.bottom-wtop
elseif widget.bottom_type == "R" then
wbottom = widget.bottom
else
wbottom = top[depth]+widget.bottom-wtop
end
if wleft+wright > width then -- stops widgets covering the controlls
boxes = boxes.."box["..width ..","..wtop ..";"..wleft+wright-width..","..wbottom..";#ff0000]" -- shows where it would go
wright = width-wleft
end
if real then
return {left=wleft, top=wtop, width=wright, height=wbottom} -- table uses the calculated values for other things
end
return wleft..","..wtop..";"..wright..","..wbottom..";"
end
end
-- iterate all widgets
for i, v in pairs(widgets) do
if v.type == "Display" then -- defines the size
if v.width < data.width-5.2 then
left = {math.floor(((data.width-5)/2 - v.width/2)*10)/10} -- place the form in the center
else
width = math.floor((v.width+0.2)*10)/10 -- resize for large forms
end
if v.height < data.height-0.2 then -- ^
top = {data.height/2 - v.height/2}
else
height = v.height+0.2
end
fwidth = {v.width}
fheight = {v.height}
form = form ..
-- "box["..left[1]..","..top[1]..";"..v.width..","..v.height..";#000000]" -- this adds it to the form
"box["..left[1]-0.28 ..","..top[1]-0.3 ..";"..v.width+0.38 ..",".. 0.32 ..";#000000]" ..
"box["..left[1]-0.28 ..","..top[1]+v.height..";"..v.width+0.38 ..",".. 0.4 ..";#000000]" ..
"box["..left[1]-0.28 ..","..top[1]..";".. 0.3 ..","..v.height..";#000000]" ..
"box["..left[1]+v.width..","..top[1]..";".. 0.1 ..","..v.height..";#000000]"
if v.background then
form = form .. "bgcolor["..v.colour..";"..tostring(v.fullscreen).."]"
end
if v.col.col then
form = form.."listcolors["..v.col.bg_normal..";"..v.col.bg_hover
if v.col.set_border then
form = form..";"..v.col.border
if v.col.set_tool then
form = form..";"..v.col.tool_bg..";"..v.col.tool_font
end
end
form = form.."]"
end
elseif v.type == "Button" then
if v.image then -- image option
if v.item and not v.exit then
form = form .. "item_image_button["..get_rect(v)..form_esc(v.texture)..";"..i.."_none;"..form_esc(v.label).."]"
else
form = form .. "image_button["..get_rect(v)..form_esc(v.texture)..";"..i.."_none;"..form_esc(v.label).."]"
end
else
form = form .. "button["..get_rect(v)..i.."_none;"..form_esc(v.label).."]"
end
elseif v.type == "Field" then
if v.password then -- password option
form = form .. "pwdfield["..get_rect(v)..i.."_none;"..form_esc(v.label).."]"
else
form = form .. "field["..get_rect(v)..i.."_none;"..form_esc(v.label)..";"..form_esc(v.default).."]"
end
form = form .. "field_close_on_enter["..i.."_none;false]"
elseif v.type == "TextArea" then
form = form .. "textarea["..get_rect(v)..i.."_none;"..form_esc(v.label)..";"..form_esc(v.default).."]"
elseif v.type == "Label" then
if v.vertical then -- vertical option
form = form .. "vertlabel["..get_rect(v)..form_esc(v.label).."]"
else
form = form .. "label["..get_rect(v)..form_esc(v.label).."]"
end
elseif v.type == "TextList" then
local item_str = "" -- convert the list to a sting
for i, item in pairs(v.items) do
item_str = item_str .. form_esc(item)..","
end
if v.transparent then -- transparent option
form = form .. "textlist["..get_rect(v)..i.."_none;"..item_str..";1;True]"
else
form = form .. "textlist["..get_rect(v)..i.."_none;"..item_str.."]"
end
elseif v.type == "DropDown" then
local item_str = "" -- convert the list to a string
for i, item in pairs(v.items) do
item_str = item_str .. form_esc(item)..","
end
form = form .. "dropdown["..get_rect(v)..i.."_none;"..item_str..";"..v.select_id.."]"
elseif v.type == "CheckBox" then
form = form .. "checkbox["..get_rect(v)..i.."_none;"..v.label..";"..tostring(v.checked).."]"
elseif v.type == "Box" then -- a coloured square
form = form .. "box["..get_rect(v)..form_esc(v.colour).."]"
elseif v.type == "Image" then
if v.item then
form = form .. "item_image["..get_rect(v)..form_esc(v.image).."]"
elseif v.background then
if v.fill then
form = form .. "background["..left[1]-0.18 ..","..top[1]-0.22 ..";"..fwidth[1]+0.37 ..","..fheight[1]+0.7 ..";"..form_esc(v.image).."]"
else
form = form .. "background["..get_rect(v)..form_esc(v.image).."]"
end
else
form = form .. "image["..get_rect(v)..form_esc(v.image).."]"
end
elseif v.type == "Slider" then
orientation = "horizontal"
if v.vertical then
orientation = "vertical"
end
form = form .. "scrollbar["..get_rect(v)..orientation..";"..i.."_none;"..v.value.."]"
elseif v.type == "InvList" then
local extras = {["player:"]=1, ["nodemeta:"]=1, ["detached:"]=1} -- locations that need extra info (v.data)
if extras[v.location] then
form = form .. "list["..v.location..form_esc(v.data)..";"..form_esc(v.name)..";"..get_rect(v)..v.start.."]"
if v.ring then -- items can be shift clicked between ring items
form = form .. "listring["..v.location..form_esc(v.data)..";"..form_esc(v.name).."]"
end
else
form = form .. "list["..v.location..";"..form_esc(v.name)..";"..get_rect(v)..v.start.."]"
if v.ring then
form = form .. "listring["..v.location..";"..form_esc(v.name).."]"
end
end
elseif v.type == "Table" then
local cell_str = ""
local column_str = ""
local most_items = 0 -- the amount of rows needed = the most items in a column
for i, c in pairs(v.columns) do
if #c.items > most_items then -- get max length
most_items = #c.items
end
if i > 1 then
column_str = column_str..";"
end
column_str = column_str .. c.type -- add column types
if c.type == "image" then -- add list of available images
for n, t in pairs(c.images) do
column_str = column_str..","..n .."="..form_esc(t)
end
elseif c.type == "color" and c.distance ~= "infinite" then -- add distance that coloures affect
column_str = column_str..",span="..c.distance
end
end
for i=1, most_items do -- create a list from all column's lists
for n, c in pairs(v.columns) do -- create a row from column's items
if n > 1 or i > 1 then
cell_str = cell_str..","
end
local item = c.items[i]
if item == nil then -- blank item if this column doesn't exend as far
item = ""
end
cell_str = cell_str..form_esc(item)
end
end
if column_str:len() > 0 then
form = form .. "tablecolumns["..column_str.."]"
end
form = form .. "table["..get_rect(v)..i.."_none;"..cell_str..";-1]"
elseif v.type == "Tooltip" then
for n, w in pairs(widgets) do
if w.name == v.name and w.type ~= "Tooltip" then
if v.colours then
form = form .. "tooltip["..n.."_none;"..v.text..";"..v.bg..";"..v.fg.."]"
else
form = form .. "tooltip["..n.."_none;"..v.text.."]"
end
end
end
elseif v.type == "Container - Start" then
local rect = get_rect(v, true, true)
left[depth+1] = rect.left -- register the new area
top[depth+1] = rect.top
fwidth[depth+1] = rect.width
fheight[depth+1] = rect.height
depth = depth+1
elseif v.type == "Container - End" then
depth = depth-1 -- go back to the parent area
elseif v.type == "Tabs" then
local capt_str = ""
for i, capt in pairs(v.captions) do
if i > 1 then capt_str = capt_str.."," end
capt_str = capt_str .. form_esc(capt)
end
form = form .. "tabheader["..get_rect(v)..i.."_none;"..capt_str..";"..v.tab..";"..tostring(v.transparent)..";"..tostring(v.border).."]"
end
end
return form..boxes, width+5, height
end
----------
-- Compiling
----------
-- generates a function to create the UI, with parameters
local function generate_function()
local form_esc = function(str) -- escape symbols need to be escaped
return string.gsub(minetest.formspec_escape(str), "\\", "\\\\") -- which have to be escaped...
end
local parameters = {} -- these store info which will be put together at the end
local before_str = ""
local display = {}
local form = ""
local table_items = false
local function name(v) -- converts the name into something that can be used in parameters
n = v.name
if v.type == "InvList" then -- the name of inv lists is used differently
n = v.location.."_"..v.name
end
local new = ""
chars = "abcdefghijklmnopqrstuvwxyz"
chars = chars..string.upper(chars)
for i=1, #n do
local c = n:sub(i,i)
if string.find(chars, c, 1, true) or (string.find("1234567890", c, 1, true) and i ~= 1) then -- numbers only allowed after first char
new = new..c
else
new = new.."_"
end
end
return new
end
local width = {widgets[1].width}
if widgets[1].width_param then -- if size defined from parameters
width = {"width"}
end
local height = {widgets[1].height}
if widgets[1].height_param then
height = {"height"}
end
local dep = 1 -- depth into containers
-- returns a string containing the position and size of a widget, or (hopefully) the most efficient calculation
local function get_rect(widget, real, l, t)
local fwidth = width[dep]
local fheight = height[dep]
local wleft = "0"
if type(fwidth) == "string" or l then -- if the area width (window or continer) will be changed with a parameter
local l_ = l -- if the left of the widget comes from a parameter
if l_ == nil then
l_ = widget.left
end
if widget.left_type == "R-" then -- different position types
wleft = fwidth..'- '..l_
elseif widget.left_type == "W/" then
wleft = fwidth..'/'..l_
else
wleft = l_
end
if type(wleft) == "string" and not real then
wleft = '"..'..wleft..' .."'
end
else
if widget.left_type == "R-" then -- calculation made now if nothing comes from a parameter
wleft = fwidth-widget.left
elseif widget.left_type == "W/" then
wleft = fwidth/widget.left
else
wleft = widget.left
end
end
local wtop = "0" --top
if type(fheight) == "string" or t then
local t_ = t
if t_ == nil then
t_ = widget.top
end
if widget.top_type == "B-" then
wtop = fheight..'- '..t_
elseif widget.left_type == "H/" then
wtop = fheight..'/'..t_
else
wtop = t_
end
if type(wtop) == "string" and not real then
wtop = '"..'..wtop..' .."'
end
else
if widget.top_type == "B-" then
wtop = fheight-widget.top
elseif widget.left_type == "H/" then
wtop = fheight/widget.top
else
wtop = widget.top
end
end
if widget.right == nil then -- for widgets with no size option
return wleft..","..wtop
else
local wright = 0
if type(fwidth) == "string" then -- if the width is changed by a parameter
local l_ = l
if l_ == nil then
l_ = widget.left
end
-- goes through all right types, and for eacg, goes through all left types to get the best calculation.
if widget.right_type == "R-" then -- (I know there is a better way of doing this)
if widget.left_type == "R-" then
wright = fwidth..'- '..widget.right..'-('..fwidth..'- '..l_..')'
elseif widget.left_type == "W/" then
wright = fwidth..'- '..widget.right..'-('..fwidth..'/'..l_..')'
elseif type(l_) == "string" then
wright = fwidth..'- '..widget.right.."- "..l_
else
wright = fwidth..'- '..widget.right+l_
end
elseif widget.right_type == "W/" then
if widget.left_type == "R-" then
wright = fwidth..'/'..widget.right..'-('..fwidth..'- '..l_..')'
elseif widget.left_type == "W/" then
wright = fwidth..'/'..widget.right..'-('..fwidth..'/'..l_..')'
else
wright = fwidth..'/'..widget.right.."- "..l_
end
elseif widget.right_type == "R" then
wright = widget.right
else
if widget.left_type == "R-" then
wright = widget.right..'-('..fwidth..'- '..l_..')'
elseif widget.left_type == "W/" then
wright = widget.right..'-('..fwidth..'/'..l_..')'
elseif type(l) == "string" then
wright = widget.right.."- "..l_
else
wright = widget.right-l_
end
end
if type(wright) == "string" and not real then
wright = '"..'..wright..' .."'
end
elseif l then -- if there is a parameter for the left, but not the width
if widget.right_type == "R-" then
if widget.left_type == "R-" then
wright = fwidth-widget.right..'-('..fwidth..'- '..l..')'
elseif widget.left_type == "W/" then
wright = fwidth-widget.right..'-('..fwidth..'/'..l..')'
else
wright = fwidth-widget.right.."- "..l
end
elseif widget.right_type == "W/" then
if widget.left_type == "R-" then
wright = fwidth..'/'..widget.right..'-('..fwidth..'- '..l..')'
elseif widget.left_type == "W/" then
wright = fwidth..'/'..widget.right..'-('..fwidth..'/'..l..')'
else
wright = fwidth..'/'..widget.right.."- "..l
end
elseif widget.right_type == "R" then
wright = widget.right
else
if widget.left_type == "R-" then
wright = widget.right..'-('..fwidth..'- '..l..')'
elseif widget.left_type == "W/" then
wright = widget.right..'-('..fwidth..'/'..l..')'
else
wright = widget.right.."- "..l
end
end
if type(wright) == "string" and not real then
wright = '"..'..wright..' .."'
end
else -- if all values are known now
if widget.right_type == "R-" then
wright = fwidth-widget.right-wleft
elseif widget.right_type == "W/" then
wright = fwidth/widget.right-wleft
elseif widget.right_type == "R" then
wright = widget.right
else
wright = widget.right-wleft
end
end
local wbottom = 0 -- similar for bottom
if type(fheight) == "string" then
local t_ = t
if t_ == nil then -- if widget's top comes from a parameter (container)
t_ = widget.top
end
if widget.bottom_type == "B-" then
if widget.top_type == "B-" then
wbottom = fheight..'- '..widget.bottom..'-('..fheight..'- '..t_..')'
elseif widget.left_type == "W/" then
wbottom = fheight..'- '..widget.bottom..'-('..fheight..'/'..t_..')'
elseif type(t_) == "string" then
wbottom = fheight..'- '..widget.bottom.."- "..t_
else
wbottom = fheight..'- '..widget.bottom+t_
end
elseif widget.bottom_type == "H/" then
if widget.top_type == "B-" then
wbottom = fheight..'/'..widget.bottom..'-('..fheight..'- '..t_..')'
elseif widget.left_type == "W/" then
wbottom = fheight..'/'..widget.bottom..'-('..fheight..'/'..t_..')'
else
wbottom = fheight..'/'..widget.bottom.."- "..t_
end
elseif widget.bottom_type == "R" then
wbottom = widget.bottom
else
if widget.top_type == "B-" then
wbottom = widget.bottom..'-('..fheight..'- '..t_..')'
elseif widget.left_type == "W/" then
wbottom = widget.bottom..'-('..fheight..'/'..t_..')'
elseif type(t_) == "string" then
wbottom = widget.bottom.."- "..t_
else
wbottom = widget.bottom-t_
end
end
if type(wbottom) == "string" and not real then
wbottom = '"..'..wbottom..' .."'
end
elseif t then
if widget.bottom_type == "B-" then
if widget.top_type == "B-" then
wbottom = fheight-widget.bottom-fheight..'- '..t
elseif widget.left_type == "W/" then
wbottom = fheight-widget.bottom-fheight..'/'..t
else
wbottom = fheight-widget.bottom.."+"..t
end
elseif widget.bottom_type == "H/" then
if widget.top_type == "B-" then
wbottom = fheight/widget.bottom-fheight..'- '..t
elseif widget.left_type == "W/" then
wbottom = fheight/widget.bottom-fheight..'/'..t
else
wbottom = fheight/widget.bottom.."- "..t
end
elseif widget.bottom_type == "R" then
wbottom = widget.bottom
else
if widget.top_type == "B-" then
wbottom = widget.bottom-fheight..'- '..t
elseif widget.left_type == "W/" then
wbottom = widget.bottom-fheight..'/'..t
elseif type(t) == "string" then
wbottom = widget.bottom.."- "..t
else
wbottom = widget.bottom-t
end
end
if type(wbottom) == "string" and not real then
wbottom = '"..'..wbottom..' .."'
end
else
if widget.bottom_type == "B-" then
wbottom = fheight-widget.bottom-wtop
elseif widget.bottom_type == "H/" then
wbottom = fheight/widget.bottom-wtop
elseif widget.bottom_type == "R" then
wbottom = widget.bottom
else
wbottom = widget.bottom-wtop
end
end
if real then
return {left=wleft, top=wtop, width=wright, height=wbottom} -- container needs the values seperate
end
return wleft..","..wtop..";"..wright..","..wbottom
end
end
local w, h = 0, 0
-- go through all the widgets, and add their code
for i, v in pairs(widgets) do
if v.type == "Display" then
local w, h
if v.width_param then -- things like this are for parameters
table.insert(parameters, "width")
w = '"..width.."'
else
w = tostring(v.width)
end
if v.height_param then
table.insert(parameters, "height")
h = '"..height.."'
else
h = tostring(v.height)
end
table.insert(display, '"size['..w..','..h..']"')
if v.position then -- for non-default position
table.insert(display, '"position['..v.left..','..v.top..']"')
end
if v.background then
table.insert(display, '"bgcolor['..v.colour..';'..tostring(v.fullscreen)..']"')
end
if v.col.col then
local cols = '"listcolors['..v.col.bg_normal..";"..v.col.bg_hover
if v.col.set_border then
cols = cols..";"..v.col.border
if v.col.set_tool then
cols = cols..";"..v.col.tool_bg..";"..v.col.tool_font
end
end
cols = cols..']"'
table.insert(display, cols)
end
elseif v.type == "Button" then
if v.image then
local tex = ""
if v.image_param then -- image texture param
table.insert(parameters, name(v).."_image")