Clean up element constructors, add ele:onCreate() to validate parameters

This commit is contained in:
rubenwardy 2016-11-13 17:14:57 +00:00
parent 013201fe8f
commit 62fb1e604c
2 changed files with 171 additions and 57 deletions

View File

@ -1,2 +1,6 @@
dofile(minetest.get_modpath("smartfs").."/smartfs.lua") dofile(minetest.get_modpath("smartfs").."/smartfs.lua")
-- dofile(minetest.get_modpath("smartfs").."/docs/example.lua") dofile(minetest.get_modpath("smartfs").."/docs/example.lua")
assert(minetest.is_yes(true) == true)
assert(minetest.is_yes(false) == false)

View File

@ -11,6 +11,10 @@ smartfs = {
inv = {} inv = {}
} }
local function boolToStr(v)
return v and "true" or "false"
end
-- the smartfs() function -- the smartfs() function
function smartfs.__call(self, name) function smartfs.__call(self, name)
return smartfs.get(name) return smartfs.get(name)
@ -239,53 +243,12 @@ function smartfs._makeState_(form, newplayer, params, is_inv, nodepos)
if not key then return end if not key then return end
return self.param[key] or default return self.param[key] or default
end, end,
button = function(self,x,y,w,h,name,text,exitf)
if exitf == nil then exitf = false end
return self:element("button",{pos={x=x,y=y},size={w=w,h=h},name=name,value=text,closes=exitf})
end,
label = function(self,x,y,name,text)
return self:element("label",{pos={x=x,y=y},name=name,value=text})
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})
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,
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,
element = function(self,typen,data) element = function(self,typen,data)
local type = smartfs._edef[typen] local type = smartfs._edef[typen]
assert(type, "Element type "..typen.." does not exist!")
assert(not self._ele[data.name], "Element "..data.name.." already exists")
if not type then
error("Element type "..typen.." does not exist!")
end
if self._ele[data.name] then
error("Element "..data.name.." already exists")
end
data.type = typen data.type = typen
local ele = { local ele = {
name = data.name, name = data.name,
root = self, root = self,
@ -301,8 +264,105 @@ function smartfs._makeState_(form, newplayer, params, is_inv, nodepos)
self._ele[data.name] = ele self._ele[data.name] = ele
return self._ele[data.name] if type.onCreate then
type.onCreate(ele)
end end
return self._ele[data.name]
end,
--
-- 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,
label = function(self, x, y, name, text)
return self:element("label", {
pos = {x=x,y=y},
name = name,
value = text
})
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
})
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,
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,
} }
end end
@ -438,6 +498,12 @@ end)
----------------------------------------------------------------- -----------------------------------------------------------------
smartfs.element("button", { 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) build = function(self)
if self.data.img then if self.data.img then
return "image_button[".. return "image_button["..
@ -513,6 +579,12 @@ smartfs.element("button",{
}) })
smartfs.element("toggle", { 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) build = function(self)
return "button[".. return "button["..
self.data.pos.x..","..self.data.pos.y.. self.data.pos.x..","..self.data.pos.y..
@ -562,6 +634,10 @@ smartfs.element("toggle",{
}) })
smartfs.element("label", { 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) build = function(self)
return "label[".. return "label["..
self.data.pos.x..","..self.data.pos.y.. self.data.pos.x..","..self.data.pos.y..
@ -584,6 +660,13 @@ smartfs.element("label",{
}) })
smartfs.element("field", { 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) build = function(self)
if self.data.ml then if self.data.ml then
return "textarea[".. return "textarea["..
@ -648,6 +731,11 @@ smartfs.element("field",{
}) })
smartfs.element("image", { smartfs.element("image", {
onCreate = function(self)
assert(self.data.pos and self.data.pos.x and self.data.pos.y, "image needs valid pos")
assert(self.data.size and self.data.size.w and self.data.size.h, "image needs valid size")
self.data.value = self.data.value or ""
end,
build = function(self) build = function(self)
return "image[".. return "image["..
self.data.pos.x..","..self.data.pos.y.. self.data.pos.x..","..self.data.pos.y..
@ -678,10 +766,16 @@ smartfs.element("image",{
}) })
smartfs.element("checkbox", { smartfs.element("checkbox", {
onCreate = function(self)
assert(self.data.pos and self.data.pos.x and self.data.pos.y, "checkbox needs valid pos")
assert(self.name, "checkbox needs name")
self.data.value = minetest.is_yes(self.data.value)
self.data.label = self.data.label or ""
end,
build = function(self) build = function(self)
if self.data.value == true then if self.data.value then
self.data.value = "true" self.data.value = "true"
elseif self.data.value ~= "true" then else
self.data.value = "false" self.data.value = "false"
end end
return "checkbox[".. return "checkbox["..
@ -690,7 +784,7 @@ smartfs.element("checkbox",{
self.name.. self.name..
";".. ";"..
minetest.formspec_escape(self.data.label).. minetest.formspec_escape(self.data.label)..
";"..self.data.value.."]" ";" .. boolToStr(self.data.value) .."]"
end, end,
submit = function(self, fields, player) submit = function(self, fields, player)
if fields[self.name] then if fields[self.name] then
@ -719,6 +813,14 @@ smartfs.element("checkbox",{
}) })
smartfs.element("list", { smartfs.element("list", {
onCreate = function(self)
assert(self.data.pos and self.data.pos.x and self.data.pos.y, "list needs valid pos")
assert(self.data.size and self.data.size.x and self.data.size.y, "list needs valid pos")
assert(self.name, "list needs name")
self.data.value = minetest.is_yes(self.data.value)
self.data.label = self.data.label or ""
self.data.items = self.data.items or {}
end,
build = function(self) build = function(self)
if not self.data.items then if not self.data.items then
self.data.items = {} self.data.items = {}
@ -815,6 +917,11 @@ smartfs.element("list", {
}) })
smartfs.element("inventory", { smartfs.element("inventory", {
onCreate = function(self)
assert(self.data.pos and self.data.pos.x and self.data.pos.y, "list needs valid pos")
assert(self.data.size and self.data.size.x and self.data.size.y, "list needs valid pos")
assert(self.name, "list needs name")
end,
build = function(self) build = function(self)
return "list[".. return "list["..
(self.data.location or "current_player") .. (self.data.location or "current_player") ..
@ -870,6 +977,9 @@ smartfs.element("inventory",{
}) })
smartfs.element("code", { smartfs.element("code", {
onCreate = function(self)
self.data.code = self.data.code or ""
end,
build = function(self) build = function(self)
if self._build then if self._build then
self:_build() self:_build()