I have been very busy with RL but just got some time to write up a list element. It is mostly functional but I'm sure it could use some improving. I just expanded on the example code I wrote earlier to develop/test it. I have an error i don't understand when running close() on the form in the double click function. The form closes but another form pops up with the text "formspec closing not yet created."
As always you can keep it or discard it at your discretion. This code has not been tested on a server yet.
Code: Select all
dofile(minetest.get_modpath(minetest.get_current_modname()).."/smartfs.lua")
local form = smartfs.create("smartfs_test:test", function(state)
state:size(8,8)
--set variable using creation assignment
local button1 = state:button(0,0, 2,1, "btn1","A button")
--set variable using state:get
local button2 = state:button(3,0, 3,1, "btn2","Another button")
--lists to hold values
local lst1 = mymod_constructors.list(state, 0,2, 3,4, "list1")
local lst2 = mymod_constructors.list(state, 4,2, 3,4, "list2")
lst1:onClick(function(self, state, index)
print("click")
print(index)
end)
lst1:onDoubleClick(function(self, state, index)
print("Dclick")
print(index)
state:close()
end)
--button one on clicked function
state:get("btn1"):onClick(function(self, state)
lst1:addItem("A label!")
end)
--button two on clicked function
button2:onClick(function(self, state)
lst2:addItem("A label!")
end)
end)
minetest.register_on_joinplayer(function(player)
form:show(player:get_player_name())
end)
mymod_constructors ={
list = function(state, x,y,w,h,name)
return state:element("list", {pos={x=x,y=y}, size={w=w,h=h}, name=name, items={}})
end
}
smartfs.element("list",{
build = function(self)
local listformspec = "textlist["..
self.data.pos.x..","..self.data.pos.y..
";"..
self.data.size.w..","..self.data.size.h..
";"..
self.name..
";"
--loop through the list's items and add them to the formspec
for i,value in ipairs(self.data.items) do
listformspec = listformspec..value..","
end
listformspec = string.sub(listformspec, 0, -2) --removes extra ,
--close out the list items section
listformspec = listformspec..";"
--TODO support selected idx and transparency
--close formspec definition and return formspec
listformspec = listformspec.."]"
return listformspec
end,
submit = function(self,fields)
if fields[self.name] then
local _type = string.sub(fields[self.data.name],1,3)
local index = string.sub(fields[self.data.name],5)
if _type == "CHG" and self._click then
self:_click(self.root, index)
elseif _type == "DCL" and self._doubleClick then
self:_doubleClick(self.root, index)
end
end
end,
onClick = function(self, func)
self._click = func
end,
onDoubleClick = function(self, func)
self._doubleClick = func
end,
setPosition = function(self,x,y)
self.data.pos = {x=x,y=y}
end,
getPosition = function(self,x,y)
return self.data.pos
end,
setSize = function(self,w,h)
self.data.size = {w=w,h=h}
end,
getSize = function(self,x,y)
return self.data.size
end,
--adds an item to the end of the list
--TODO allow an optional pos argument to be passed
addItem = function(self, item)
table.insert(self.data.items, item)
end,
--Deletes the last item of the list
--TODO allow an optional pos argument to be passed
removeItem = function(self)
table.remove(self.data.items)
end,
--removes the last item and returns it
popItem = function(self)
local item = self.data.items[#self.data.items]
table.remove(self.data.items)
return item
end
})