forked from minetest-mods/townchest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartfs.lua
1394 lines (1334 loc) · 37.5 KB
/
smartfs.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
---------------------------
-- SmartFS: Smart Formspecs
-- License: CC0 or WTFPL
-- by Rubenwardy
---------------------------
local smartfs = {
_fdef = {},
_edef = {},
_ldef = {},
opened = {},
inv = {}
}
local function boolToStr(v)
return v and "true" or "false"
end
-- the smartfs() function
function smartfs.__call(self, name)
return smartfs.get(name)
end
function smartfs.get(name)
return smartfs._fdef[name]
end
------------------------------------------------------
-- Smartfs Interface - Creates a new form and adds elements to it by running the function. Use before Minetest loads. (like minetest.register_node)
------------------------------------------------------
-- Register forms and elements
function smartfs.create(name, onload)
assert(not smartfs._fdef[name],
"SmartFS - (Error) Form "..name.." already exists!")
assert(not smartfs.loaded or smartfs._loaded_override,
"SmartFS - (Error) Forms should be declared while the game loads.")
smartfs._fdef[name] = {
form_setup_callback = onload,
name = name,
show = smartfs._show_,
attach_to_node = smartfs._attach_to_node_
}
return smartfs._fdef[name]
end
------------------------------------------------------
-- Smartfs Interface - Creates a new element type
------------------------------------------------------
function smartfs.element(name, data)
assert(not smartfs._edef[name],
"SmartFS - (Error) Element type "..name.." already exists!")
assert(data.onCreate, "element requires onCreate method")
smartfs._edef[name] = data
return smartfs._edef[name]
end
------------------------------------------------------
-- Smartfs Interface - Creates a dynamic form. Returns state
------------------------------------------------------
function smartfs.dynamic(name,player)
if not smartfs._dynamic_warned then
smartfs._dynamic_warned = true
minetest.log("warning", "SmartFS - (Warning) On the fly forms are being used. May cause bad things to happen")
end
local statelocation = smartfs._ldef.player._make_state_location_(player)
local state = smartfs._makeState_({name=name}, nil, statelocation, player)
smartfs.opened[player] = state
return state
end
------------------------------------------------------
-- Smartfs Interface - Returns the name of an installed and supported inventory mod that will be used above, or nil
------------------------------------------------------
function smartfs.inventory_mod()
if unified_inventory then
return "unified_inventory"
elseif inventory_plus then
return "inventory_plus"
else
return nil
end
end
------------------------------------------------------
-- Smartfs Interface - Adds a form to an installed advanced inventory. Returns true on success.
------------------------------------------------------
function smartfs.add_to_inventory(form, icon, title)
local ldef
local invmod = smartfs.inventory_mod()
if invmod then
ldef = smartfs._ldef[invmod]
else
return false
end
return ldef.add_to_inventory(form, icon, title)
end
------------------------------------------------------
-- Smartfs Interface - Set the form as players inventory
------------------------------------------------------
function smartfs.set_player_inventory(form)
smartfs._ldef.inventory.set_inventory(form)
end
------------------------------------------------------
-- Smartfs Interface - Allows you to use smartfs.create after the game loads. Not recommended!
------------------------------------------------------
function smartfs.override_load_checks()
smartfs._loaded_override = true
end
------------------------------------------------------
-- Smartfs formspec locations
------------------------------------------------------
-- Unified inventory plugin
smartfs._ldef.unified_inventory = {
add_to_inventory = function(form, icon, title)
unified_inventory.register_button(form.name, {
type = "image",
image = icon,
})
unified_inventory.register_page(form.name, {
get_formspec = function(player, formspec)
local name = player:get_player_name()
local statelocation = smartfs._ldef.unified_inventory._make_state_location_(name)
local state = smartfs._makeState_(form, nil, statelocation, name)
if form.form_setup_callback(state) ~= false then
smartfs.inv[name] = state
return {formspec = state:_buildFormspec_(false)}
else
return nil
end
end
})
end,
_make_state_location_ = function(player)
return {
type = "inventory",
player = player,
_show_ = function(state)
unified_inventory.set_inventory_formspec(minetest.get_player_by_name(state.location.player), state.def.name)
end
}
end
}
-- Inventory plus plugin
smartfs._ldef.inventory_plus = {
add_to_inventory = function(form, icon, title)
minetest.register_on_joinplayer(function(player)
inventory_plus.register_button(player, form.name, title)
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "" and fields[form.name] then
local name = player:get_player_name()
local statelocation = smartfs._ldef.inventory_plus._make_state_location_(name)
local state = smartfs._makeState_(form, nil, statelocation, name)
if form.form_setup_callback(state) ~= false then
smartfs.inv[name] = state
state:show()
end
end
end)
end,
_make_state_location_ = function(player)
return {
type = "inventory",
player = player,
_show_ = function(state)
inventory_plus.set_inventory_formspec(minetest.get_player_by_name(state.location.player), state:_buildFormspec_(true))
end
}
end
}
-- Show to player
smartfs._ldef.player = {
_make_state_location_ = function(player)
return {
type = "player",
player = player,
_show_ = function(state)
minetest.show_formspec(state.location.player, state.def.name, state:_buildFormspec_(true))
end
}
end
}
-- Standalone inventory
smartfs._ldef.inventory = {
set_inventory = function(form)
if sfinv and sfinv.enabled then
sfinv.enabled = nil
end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
local statelocation = smartfs._ldef.inventory._make_state_location_(name)
local state = smartfs._makeState_(form, nil, statelocation, name)
if form.form_setup_callback(state) ~= false then
smartfs.inv[name] = state
-- can be back to direct call if the issue is solved https://github.com/minetest/minetest_game/issues/1546
-- state:show()
minetest.after(1, state.show, state)
end
end)
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
smartfs.inv[name].obsolete = true
smartfs.inv[name] = nil
end)
end,
_make_state_location_ = function(name)
return {
type = "inventory",
player = name,
_show_ = function(state)
local player = minetest.get_player_by_name(state.location.player)
player:set_inventory_formspec(state:_buildFormspec_(true))
end
}
end
}
-- Node metadata
smartfs._ldef.nodemeta = {
_make_state_location_ = function(nodepos)
return {
type = "nodemeta",
pos = nodepos,
_show_ = function(state)
local meta = minetest.get_meta(state.location.pos)
meta:set_string("formspec", state:_buildFormspec_(true))
meta:set_string("smartfs_name", state.def.name)
end,
}
end
}
-- Sub-container (internally used)
smartfs._ldef.container = {
_make_state_location_ = function(element)
local self = {
type = "container",
containerElement = element,
parentState = element.root
}
if self.parentState.location.type == "container" then
self.rootState = self.parentState.location.rootState
else
self.rootState = self.parentState
end
return self
end
}
------------------------------------------------------
-- Minetest Interface - on_receive_fields callback can be used in minetest.register_node for nodemeta forms
------------------------------------------------------
function smartfs.nodemeta_on_receive_fields(nodepos, formname, fields, sender, params)
local meta = minetest.get_meta(nodepos)
local nodeform = meta:get_string("smartfs_name")
if not nodeform or nodeform == "" then
print("SmartFS - (Warning) smartfs.nodemeta_on_receive_fields for node without smarfs data")
return false
end
-- get the currentsmartfs state
local opened_id = minetest.pos_to_string(nodepos)
local state
local form = smartfs.get(nodeform)
if not smartfs.opened[opened_id] or -- If opened first time
smartfs.opened[opened_id].def.name ~= nodeform or -- Or form is changed
smartfs.opened[opened_id].obsolete then
local statelocation = smartfs._ldef.nodemeta._make_state_location_(nodepos)
state = smartfs._makeState_(form, params, statelocation)
if smartfs.opened[opened_id] then
smartfs.opened[opened_id].obsolete = true
end
smartfs.opened[opened_id] = state
form.form_setup_callback(state)
else
state = smartfs.opened[opened_id]
end
-- Set current sender check for multiple users on node
local name
if sender then
name = sender:get_player_name()
state.players:connect(name)
end
-- take the input
state:_sfs_on_receive_fields_(name, fields)
-- Reset form if all players disconnected
if sender and not state.players:get_first() and not state.obsolete then
local statelocation = smartfs._ldef.nodemeta._make_state_location_(nodepos)
local resetstate = smartfs._makeState_(form, params, statelocation)
if form.form_setup_callback(resetstate) ~= false then
resetstate:show()
end
smartfs.opened[opened_id] = nil
end
end
------------------------------------------------------
-- Minetest Interface - on_player_receive_fields callback in case of inventory or player
------------------------------------------------------
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if smartfs.opened[name] and smartfs.opened[name].location.type == "player" then
if smartfs.opened[name].def.name == formname then
local state = smartfs.opened[name]
state:_sfs_on_receive_fields_(name, fields)
-- disconnect player if form closed
if not state.players:get_first() then
smartfs.opened[name].obsolete = true
smartfs.opened[name] = nil
end
end
elseif smartfs.inv[name] and smartfs.inv[name].location.type == "inventory" then
local state = smartfs.inv[name]
state:_sfs_on_receive_fields_(name, fields)
end
return false
end)
------------------------------------------------------
-- Minetest Interface - Notify loading of smartfs is done
------------------------------------------------------
minetest.after(0, function()
smartfs.loaded = true
end)
------------------------------------------------------
-- Form Interface [linked to form:show()] - Shows the form to a player
------------------------------------------------------
function smartfs._show_(form, name, params)
assert(form)
assert(type(name) == "string", "smartfs: name needs to be a string")
assert(minetest.get_player_by_name(name), "player does not exist")
local statelocation = smartfs._ldef.player._make_state_location_(name)
local state = smartfs._makeState_(form, params, statelocation, name)
if form.form_setup_callback(state) ~= false then
if smartfs.opened[name] then -- set maybe previous form to obsolete
smartfs.opened[name].obsolete = true
end
smartfs.opened[name] = state
state:show()
end
return state
end
------------------------------------------------------
-- Form Interface [linked to form:attach_to_node()] - Attach a formspec to a node meta
------------------------------------------------------
function smartfs._attach_to_node_(form, nodepos, params)
assert(form)
assert(nodepos and nodepos.x)
local statelocation = smartfs._ldef.nodemeta._make_state_location_(nodepos)
local state = smartfs._makeState_(form, params, statelocation)
if form.form_setup_callback(state) ~= false then
local opened_id = minetest.pos_to_string(nodepos)
if smartfs.opened[opened_id] then -- set maybe previous form to obsolete
smartfs.opened[opened_id].obsolete = true
end
state:show()
end
return state
end
------------------------------------------------------
-- Smartfs Framework - create a form object (state)
------------------------------------------------------
function smartfs._makeState_(form, params, location, newplayer)
------------------------------------------------------
-- State - -- Object to manage players
------------------------------------------------------
local function _make_players_(newplayer)
local self = {
_list = {}
}
function self.connect(self, player)
self._list[player] = true
end
function self.disconnect(self, player)
self._list[player] = nil
end
function self.get_first(self)
return next(self._list)
end
if newplayer then
self:connect(newplayer)
end
return self
end
local compat_is_inv
if location.type == "inventory" then
compat_is_inv = true
else
compat_is_inv = false
end
------------------------------------------------------
-- State - create returning state object
------------------------------------------------------
return {
_ele = {},
def = form,
players = _make_players_(newplayer),
location = location,
is_inv = compat_is_inv, -- obsolete / compatibility
player = newplayer, -- obsolete / compatibility
param = params or {},
get = function(self,name)
return self._ele[name]
end,
close = function(self)
self.closed = true
end,
getSize = function(self)
return self._size
end,
size = function(self,w,h)
self._size = {w=w,h=h}
end,
setSize = function(self,w,h)
self._size = {w=w,h=h}
end,
getNamespace = function(self)
local ref = self
local namespace = ""
while ref.location.type == "container" do
namespace = ref.location.containerElement.name.."#"..namespace
ref = ref.location.parentState -- step near to the root
end
return namespace
end,
_buildFormspec_ = function(self,size)
local res = ""
if self._size and size then
res = "size["..self._size.w..","..self._size.h.."]"
end
for key,val in pairs(self._ele) do
if val:getVisible() then
res = res .. val:getBackgroundString() .. val:build()
end
end
return res
end,
show = location._show_,
_get_element_recursive_ = function(self, field)
local topfield
for z in field:gmatch("[^#]+") do
topfield = z
break
end
local element = self._ele[topfield]
if element and field == topfield then
return element
elseif element then
if element._getSubElement_ then
local rel_field = string.sub(field, string.len(topfield)+2)
return element:_getSubElement_(rel_field)
else
return element
end
else
return nil
end
end,
-- process onInput hook for the state
_sfs_process_oninput_ = function(self, fields, player)
if self._onInput then
self:_onInput(fields, player)
end
-- recursive all onInput hooks on visible containers
for elename, eledef in pairs(self._ele) do
if eledef.getContainerState and eledef:getVisible() then
eledef:getContainerState():_sfs_process_oninput_(fields, player)
end
end
end,
-- Receive fields and actions from formspec
_sfs_on_receive_fields_ = function(self, player, fields)
-- fields assignment
for field, value in pairs(fields) do
local element = self:_get_element_recursive_(field)
if element then
element:setValue(value)
end
end
-- process onInput hooks
self:_sfs_process_oninput_(fields, player)
-- do actions
for field, value in pairs(fields) do
local element = self:_get_element_recursive_(field)
if element and element.submit then
element:submit(value, player)
end
end
-- handle key_enter
if fields.key_enter and fields.key_enter_field then
local element = self:_get_element_recursive_(fields.key_enter_field)
if element and element.submit_key_enter then
element:submit_key_enter(fields[fields.key_enter_field], player)
end
end
-- handle quit/exit
if not fields.quit and not self.closed and not self.obsolete then
self:show()
else
self.players:disconnect(player)
if not fields.quit and self.closed and not self.obsolete then
--closed by application (without fields.quit). currently not supported, see: https://github.com/minetest/minetest/pull/4675
minetest.show_formspec(player,"","size[5,1]label[0,0;Formspec closing not yet created!]")
end
end
return true
end,
onInput = function(self, func)
self._onInput = func -- (fields, player)
end,
load = function(self,file)
local file = io.open(file, "r")
if file then
local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then
if table.size then
self._size = table.size
end
for key,val in pairs(table.ele) do
self:element(val.type,val)
end
return true
end
end
return false
end,
save = function(self,file)
local res = {ele={}}
if self._size then
res.size = self._size
end
for key,val in pairs(self._ele) do
res.ele[key] = val.data
end
local file = io.open(file, "w")
if file then
file:write(minetest.serialize(res))
file:close()
return true
end
return false
end,
setparam = function(self,key,value)
if not key then return end
self.param[key] = value
return true
end,
getparam = function(self,key,default)
if not key then return end
return self.param[key] or default
end,
element = function(self,typen,data)
local type = smartfs._edef[typen]
assert(type, "Element type "..typen.." does not exist!")
assert(not self._ele[data.name], "Element "..data.name.." already exists")
data.type = typen
local ele = {
name = data.name,
root = self,
data = data,
remove = function(self)
self.root._ele[self.name] = nil
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
end,
getPosition = function(self)
return self.data.pos
end,
setSize = function(self,w,h)
self.data.size = {w=w,h=h}
end,
getSize = function(self)
return self.data.size
end,
setVisible = function(self, visible)
if visible == nil then
self.data.visible = true
else
self.data.visible = visible
end
end,
getVisible = function(self)
return self.data.visible
end,
getAbsName = function(self)
return self.root:getNamespace()..self.name
end,
setBackground = function(self, image)
self.data.background = image
end,
getBackground = function(self)
return self.data.background
end,
getBackgroundString = function(self)
if self.data.background then
local size = self:getSize()
if size then
return "background["..
self.data.pos.x..","..self.data.pos.y..";"..
size.w..","..size.h..";"..
self.data.background.."]"
else
return ""
end
else
return ""
end
end,
setValue = function(self, value)
self.data.value = value
end,
}
ele.data.visible = true --visible by default
for key, val in pairs(type) do
ele[key] = val
end
self._ele[data.name] = ele
type.onCreate(ele)
return self._ele[data.name]
end,
------------------------------------------------------
-- State - Element Constructors
------------------------------------------------------
button = function(self, x, y, w, h, name, text, exitf)
return self:element("button", {
pos = {x=x,y=y},
size = {w=w,h=h},
name = name,
value = text,
closes = exitf or false
})
end,
image_button = function(self, x, y, w, h, name, text, image, exitf)
return self:element("button", {
pos = {x=x,y=y},
size = {w=w,h=h},
name = name,
value = text,
image = image,
closes = exitf or false
})
end,
item_image_button = function(self, x, y, w, h, name, text, item, exitf)
return self:element("button", {
pos = {x=x,y=y},
size = {w=w,h=h},
name = name,
value = text,
item = item,
closes = exitf or false
})
end,
label = function(self, x, y, name, text)
return self:element("label", {
pos = {x=x,y=y},
name = name,
value = text,
vertical = false
})
end,
vertlabel = function(self, x, y, name, text)
return self:element("label", {
pos = {x=x,y=y},
name = name,
value = text,
vertical = true
})
end,
toggle = function(self, x, y, w, h, name, list)
return self:element("toggle", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name,
id = 1,
list = list
})
end,
field = function(self, x, y, w, h, name, label)
return self:element("field", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name,
value = "",
label = label
})
end,
pwdfield = function(self, x, y, w, h, name, label)
local res = self:element("field", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name,
value = "",
label = label
})
res:isPassword(true)
return res
end,
textarea = function(self, x, y, w, h, name, label)
local res = self:element("field", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name,
value = "",
label = label
})
res:isMultiline(true)
return res
end,
image = function(self, x, y, w, h, name, img)
return self:element("image", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name,
value = img,
imgtype = "image"
})
end,
background = function(self, x, y, w, h, name, img)
return self:element("image", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name,
background = img,
imgtype = "background"
})
end,
item_image = function(self, x, y, w, h, name, img)
return self:element("image", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name,
value = img,
imgtype = "item"
})
end,
checkbox = function(self, x, y, name, label, selected)
return self:element("checkbox", {
pos = {x=x, y=y},
name = name,
value = selected,
label = label
})
end,
listbox = function(self, x, y, w, h, name, selected, transparent)
return self:element("list", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name,
selected = selected,
transparent = transparent
})
end,
dropdown = function(self, x, y, w, h, name, selected)
return self:element("dropdown", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name,
selected = selected
})
end,
inventory = function(self, x, y, w, h, name)
return self:element("inventory", {
pos = {x=x, y=y},
size = {w=w, h=h},
name = name
})
end,
container = function(self, x, y, name, relative)
return self:element("container", {
pos = {x=x, y=y},
name = name,
relative = false
})
end,
view = function(self, x, y, name, relative)
return self:element("container", {
pos = {x=x, y=y},
name = name,
relative = true
})
end,
}
end
-----------------------------------------------------------------
------------------------- ELEMENTS ----------------------------
-----------------------------------------------------------------
smartfs.element("button", {
onCreate = function(self)
assert(self.data.pos and self.data.pos.x and self.data.pos.y, "button needs valid pos")
assert(self.data.size and self.data.size.w and self.data.size.h, "button needs valid size")
assert(self.name, "button needs name")
assert(self.data.value, "button needs label")
end,
build = function(self)
local specstring
if self.data.image then
if self.data.closes then
specstring = "image_button_exit["
else
specstring = "image_button["
end
elseif self.data.item then
if self.data.closes then
specstring = "item_image_button_exit["
else
specstring = "item_image_button["
end
else
if self.data.closes then
specstring = "button_exit["
else
specstring = "button["
end
end
specstring = specstring ..
self.data.pos.x..","..self.data.pos.y..";"..
self.data.size.w..","..self.data.size.h..";"
if self.data.image then
specstring = specstring..self.data.image..";"
elseif self.data.item then
specstring = specstring..self.data.item..";"
end
specstring = specstring..self:getAbsName()..";"..
minetest.formspec_escape(self.data.value).."]"
if self.data.tooltip then
specstring = specstring.."tooltip["..self:getAbsName()..";"..self.data.tooltip.."]"
end
return specstring
end,
submit = function(self, field, player)
if self._click then
self:_click(self.root, player)
end
end,
onClick = function(self,func)
self._click = func
end,
click = function(self,func)
self._click = func
end,
setText = function(self,text)
self:setValue(text)
end,
getText = function(self)
return self.data.value
end,
setImage = function(self,image)
self.data.image = image
self.data.item = nil
end,
getImage = function(self)
return self.data.image
end,
setItem = function(self,item)
self.data.item = item
self.data.image = nil
end,
getItem = function(self)
return self.data.item
end,
setTooltip = function(self,text)
self.data.tooltip = minetest.formspec_escape(text)
end,
getTooltip = function(self)
return self.data.tooltip
end,
setClose = function(self,bool)
self.data.closes = bool
end,
getClose = function(self)
return self.data.closes or false
end
})
smartfs.element("toggle", {
onCreate = function(self)
assert(self.data.pos and self.data.pos.x and self.data.pos.y, "toggle needs valid pos")
assert(self.data.size and self.data.size.w and self.data.size.h, "toggle needs valid size")
assert(self.name, "toggle needs name")
assert(self.data.list, "toggle needs data")
end,
build = function(self)
return "button["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self:getAbsName()..
";"..
minetest.formspec_escape(self.data.list[self.data.id])..
"]"
end,
submit = function(self, field, player)
self.data.id = self.data.id + 1
if self.data.id > #self.data.list then
self.data.id = 1
end
if self._tog then
self:_tog(self.root, player)
end
end,
onToggle = function(self,func)
self._tog = func
end,
setId = function(self,id)
self.data.id = id
end,
getId = function(self)
return self.data.id
end,
getText = function(self)
return self.data.list[self.data.id]
end
})
smartfs.element("label", {
onCreate = function(self)
assert(self.data.pos and self.data.pos.x and self.data.pos.y, "label needs valid pos")
assert(self.data.value, "label needs text")
end,
build = function(self)
local specstring
if self.data.vertical then
specstring = "vertlabel["
else
specstring = "label["
end
return specstring..
self.data.pos.x..","..self.data.pos.y..
";"..
minetest.formspec_escape(self.data.value)..
"]"
end,
setText = function(self,text)
self:setValue(text)
end,
getText = function(self)
return self.data.value
end
})
smartfs.element("field", {
onCreate = function(self)
assert(self.data.pos and self.data.pos.x and self.data.pos.y, "field needs valid pos")
assert(self.data.size and self.data.size.w and self.data.size.h, "field needs valid size")
assert(self.name, "field needs name")
self.data.value = self.data.value or ""
self.data.label = self.data.label or ""
end,
build = function(self)
if self.data.ml then
return "textarea["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self:getAbsName()..
";"..
minetest.formspec_escape(self.data.label)..
";"..
minetest.formspec_escape(self.data.value)..
"]"
elseif self.data.pwd then
return "pwdfield["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..