techage/icta_controller/action.lua

108 lines
2.4 KiB
Lua
Raw Normal View History

2020-02-29 21:20:54 +03:00
--[[
TechAge
=======
Copyright (C) 2019-2020 Joachim Stolberg
2020-10-19 20:09:17 +03:00
AGPL v3
2020-02-29 21:20:54 +03:00
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
2020-02-29 21:20:54 +03:00
ICTA Controller - Action Registration
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local logic = techage.logic
-- tables with all data from action registrations
local kvRegisteredActn = {}
-- list of keys for actions
local aActnTypes = {}
-- list of titles for actions
local aActnTitles = {}
--
-- API function for action registrations
--
function techage.icta_register_action(key, tData)
table.insert(aActnTypes, key)
table.insert(aActnTitles, tData.title)
tData.__idx__ = #aActnTypes
if kvRegisteredActn[key] ~= nil then
print("[Techage] Action registration error "..key)
return
end
kvRegisteredActn[key] = tData
for _,item in ipairs(tData.formspec) do
if item.type == "textlist" then
item.tChoices = string.split(item.choices, ",")
item.num_choices = #item.tChoices
end
end
end
-- return formspec string
function techage.actn_formspec(row, kvSelect)
return techage.submenu_generate_formspec(
row, "actn", "Action type", aActnTypes, aActnTitles, kvRegisteredActn, kvSelect)
2022-01-03 23:40:31 +03:00
end
2020-02-29 21:20:54 +03:00
-- evaluate the row action input
-- and return new data
function techage.actn_eval_input(kvSelect, fields)
kvSelect = techage.submenu_eval_input(kvRegisteredActn, aActnTypes, aActnTitles, kvSelect, fields)
return kvSelect
end
2022-01-03 23:40:31 +03:00
-- return the Lua code
2020-02-29 21:20:54 +03:00
function techage.code_action(kvSelect, environ)
if kvSelect and kvRegisteredActn[kvSelect.choice] then
2020-03-01 14:32:11 +03:00
if techage.submenu_verify(environ.owner, kvRegisteredActn, kvSelect) then
2020-02-29 21:20:54 +03:00
return kvRegisteredActn[kvSelect.choice].code(kvSelect, environ)
end
end
return nil
end
techage.icta_register_action("default", {
title = "",
formspec = {},
code = function(data, environ) return false, false end,
button = function(data, environ) return "..." end,
})
techage.icta_register_action("print", {
title = "print to output window",
formspec = {
{
2022-01-03 23:40:31 +03:00
type = "ascii",
2020-02-29 21:20:54 +03:00
name = "text",
2022-01-03 23:40:31 +03:00
label = "Output the following text",
2020-02-29 21:20:54 +03:00
default = "",
},
{
2022-01-03 23:40:31 +03:00
type = "label",
name = "lbl",
label = "Use a '*' character as reference to any\ncondition state",
2020-02-29 21:20:54 +03:00
},
},
2022-01-03 23:40:31 +03:00
button = function(data, environ)
2020-02-29 21:20:54 +03:00
return 'print("'..data.text:sub(1,12)..'")'
end,
code = function(data, environ)
return function(env, output, idx)
local text = string.gsub(data.text, "*", tostring(env.result[idx]))
output(env.pos, text)
end
2020-02-29 21:20:54 +03:00
end,
})