mirror of
https://github.com/ExeVirus/formspec_editor.git
synced 2025-04-30 16:31:43 -04:00
base lua based form def
This commit is contained in:
parent
228f60dfb0
commit
019e7ad8e2
11 changed files with 238 additions and 91 deletions
|
@ -9,7 +9,7 @@ This mod needs access to an insecure environment in order to work.
|
||||||
|
|
||||||
Add `formspec_edit` to `secure.trusted_mods` in your `minetest.conf` file.
|
Add `formspec_edit` to `secure.trusted_mods` in your `minetest.conf` file.
|
||||||
|
|
||||||
The file *formspec.spec* in your:
|
The file *formspec.lua* in your:
|
||||||
|
|
||||||
```minetest_folder/games/formspec_editor/mods/formspec_edit```
|
```minetest_folder/games/formspec_editor/mods/formspec_edit```
|
||||||
|
|
||||||
|
|
1
mods/formspec_edit/formspec.lua
Normal file
1
mods/formspec_edit/formspec.lua
Normal file
|
@ -0,0 +1 @@
|
||||||
|
return "size[5,10]"
|
|
@ -1,9 +0,0 @@
|
||||||
size[9,8.75]
|
|
||||||
label[0,0;Ender Chest]
|
|
||||||
list[current_player;enderchest;0,0.5;9,3;]
|
|
||||||
mcl_formspec.get_itemslot_bg(0,0.5,9,3)
|
|
||||||
label[0,4.0;Inventory]
|
|
||||||
list[current_player;main;0,4.5;9,3;9]
|
|
||||||
list[current_player;main;0,7.74;9,1;]
|
|
||||||
listring[current_player;enderchest]
|
|
||||||
listring[current_player;main]
|
|
123
mods/formspec_edit/game_specific.lua
Normal file
123
mods/formspec_edit/game_specific.lua
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
formspec_edit.prepend = ""
|
||||||
|
formspec_edit.inventories = {
|
||||||
|
main = {width = 8, size = 32}
|
||||||
|
}
|
||||||
|
|
||||||
|
--Get formspec style
|
||||||
|
local styling = minetest.settings:get("formspec_editor.style")
|
||||||
|
if not styling or styling == "" then
|
||||||
|
styling = "builtin"
|
||||||
|
end
|
||||||
|
|
||||||
|
local prepend = ""
|
||||||
|
if styling == "minetest_game" then
|
||||||
|
------------------------------------------------
|
||||||
|
---------------Minetest Game code---------------
|
||||||
|
------------------------------------------------
|
||||||
|
prepend = [[
|
||||||
|
bgcolor[#080808BB;true]
|
||||||
|
listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]
|
||||||
|
]]
|
||||||
|
local name = player_ref:get_player_name()
|
||||||
|
local info = minetest.get_player_information(name)
|
||||||
|
if info.formspec_version > 1 then
|
||||||
|
prepend = prepend .. "background9[5,5;1,1;mtg_gui_formbg.png;true;10]"
|
||||||
|
else
|
||||||
|
prepend = prepend .. "background[5,5;1,1;mtg_gui_formbg.png;true]"
|
||||||
|
end
|
||||||
|
|
||||||
|
formspec_edit.lua_env.default = {}
|
||||||
|
function formspec_edit.lua_env.default.get_hotbar_bg(x, y)
|
||||||
|
local out = ""
|
||||||
|
for i=0,7,1 do
|
||||||
|
out = out .."image["..x+i..","..y..";1,1;gui_hb_bg.png]"
|
||||||
|
end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
elseif styling == "mineclone2" then
|
||||||
|
------------------------------------------------
|
||||||
|
---------------MineClone2 code------------------
|
||||||
|
------------------------------------------------
|
||||||
|
--Sadly, this code doesn't support inventory slot styling (texture based)
|
||||||
|
prepend = table.concat({
|
||||||
|
"listcolors[#9990;#FFF7;#FFF0;#000;#FFF]",
|
||||||
|
"style_type[image_button;border=false;bgimg=mcl_inventory_button9.png;bgimg_pressed=mcl_inventory_button9_pressed.png;bgimg_middle=2,2]",
|
||||||
|
"style_type[button;border=false;bgimg=mcl_inventory_button9.png;bgimg_pressed=mcl_inventory_button9_pressed.png;bgimg_middle=2,2]",
|
||||||
|
"style_type[field;textcolor=#323232]",
|
||||||
|
"style_type[label;textcolor=#323232]",
|
||||||
|
"style_type[textarea;textcolor=#323232]",
|
||||||
|
"style_type[checkbox;textcolor=#323232]",
|
||||||
|
"bgcolor[#00000000]",
|
||||||
|
"background9[1,1;1,1;mcl_base_textures_background9.png;true;7]",
|
||||||
|
})
|
||||||
|
|
||||||
|
--Add custom inventory function to lua env
|
||||||
|
formspec_edit.lua_env.mcl_formspec = {}
|
||||||
|
function formspec_edit.lua_env.mcl_formspec.get_itemslot_bg(x, y, w, h)
|
||||||
|
local out = ""
|
||||||
|
for i = 0, w - 1, 1 do
|
||||||
|
for j = 0, h - 1, 1 do
|
||||||
|
out = out .."image["..x+i..","..y+j..";1,1;mcl_formspec_itemslot.png]"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
|
--Same as above, but for v4 forms
|
||||||
|
function mcl_formspec.get_itemslot_bg_v4(x, y, w, h)
|
||||||
|
local out = ""
|
||||||
|
for i = 0, w - 1, 1 do
|
||||||
|
for j = 0, h - 1, 1 do
|
||||||
|
out = out .."image["..x+i+(i*0.25)..","..y+j+(j*0.25)..";1,1;mcl_formspec_itemslot.png]"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
|
--Add mcl2 color codes
|
||||||
|
formspec_edit.lua_env.mcl_colors = {
|
||||||
|
BLACK = "#000000",
|
||||||
|
DARK_BLUE = "#0000AA",
|
||||||
|
DARK_GREEN = "#00AA00",
|
||||||
|
DARK_AQUA = "#00AAAA",
|
||||||
|
DARK_RED = "#AA0000",
|
||||||
|
DARK_PURPLE = "#AA00AA",
|
||||||
|
GOLD = "#FFAA00",
|
||||||
|
GRAY = "#AAAAAA",
|
||||||
|
DARK_GRAY = "#555555",
|
||||||
|
BLUE = "#5555FF",
|
||||||
|
GREEN = "#55FF55",
|
||||||
|
AQUA = "#55FFFF",
|
||||||
|
RED = "#FF5555",
|
||||||
|
LIGHT_PURPLE = "#FF55FF",
|
||||||
|
YELLOW = "#FFFF55",
|
||||||
|
WHITE = "#FFFFFF",
|
||||||
|
background = {
|
||||||
|
BLACK = "#000000",
|
||||||
|
DARK_BLUE = "#00002A",
|
||||||
|
DARK_GREEN = "#002A00",
|
||||||
|
DARK_AQUA = "#002A2A",
|
||||||
|
DARK_RED = "#2A0000",
|
||||||
|
DARK_PURPLE = "#2A002A",
|
||||||
|
GOLD = "#2A2A00",
|
||||||
|
GRAY = "#2A2A2A",
|
||||||
|
DARK_GRAY = "#151515",
|
||||||
|
BLUE = "#15153F",
|
||||||
|
GREEN = "#153F15",
|
||||||
|
AQUA = "#153F3F",
|
||||||
|
RED = "#3F1515",
|
||||||
|
LIGHT_PURPLE = "#3F153F",
|
||||||
|
YELLOW = "#3F3F15",
|
||||||
|
WHITE = "#373501",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
--Add mcl2 inventory lists
|
||||||
|
formspec_edit.inventories = {
|
||||||
|
main = {width = 9, size = 36},
|
||||||
|
armor = {size = 5},
|
||||||
|
craft = {width = 3, size = 9},
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
formspec_edit.prepend = prepend
|
|
@ -3,6 +3,8 @@
|
||||||
---------------------------------
|
---------------------------------
|
||||||
-----------By ExeVirus-----------
|
-----------By ExeVirus-----------
|
||||||
|
|
||||||
|
formspec_edit = {}
|
||||||
|
|
||||||
--Fix builtin
|
--Fix builtin
|
||||||
minetest.register_alias("mapgen_stone", "air")
|
minetest.register_alias("mapgen_stone", "air")
|
||||||
minetest.register_alias("mapgen_water_source", "air")
|
minetest.register_alias("mapgen_water_source", "air")
|
||||||
|
@ -19,47 +21,110 @@ end
|
||||||
local io = insecure_env.io
|
local io = insecure_env.io
|
||||||
local update_time = tonumber(minetest.settings:get("formspec_editor.update_time")) or 0.2
|
local update_time = tonumber(minetest.settings:get("formspec_editor.update_time")) or 0.2
|
||||||
|
|
||||||
--Load provided file if present
|
--Get provided modpath if present
|
||||||
local filepath = minetest.settings:get("formspec_editor.file_path")
|
local filepath = minetest.settings:get("formspec_editor.file_path")
|
||||||
if not filepath or filepath == "" then
|
if not filepath or filepath == "" then
|
||||||
filepath = modpath .. "/formspec.spec"
|
filepath = modpath .. "/formspec.lua"
|
||||||
end
|
|
||||||
|
|
||||||
--Get styling presets
|
|
||||||
local styling = minetest.settings:get("formspec_editor.style")
|
|
||||||
if not styling or styling == "" then
|
|
||||||
styling = "builtin"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local error_formspec = [[
|
local error_formspec = [[
|
||||||
formspec_version[4]
|
formspec_version[4]
|
||||||
size[8,2]
|
size[8,2]
|
||||||
label[0.375,0.5;Error:formspec.spec is either ]
|
label[0.375,0.5;Error: formspec.lua is either]
|
||||||
label[0.375,1;non-existent,or empty]
|
label[0.375,1;non-existent,or empty]
|
||||||
]]
|
]]
|
||||||
|
|
||||||
--Crash if not singleplayer
|
--Crash if not singleplayer
|
||||||
--TODO: hide the 'Host server' checkbox in main menu then possible
|
|
||||||
if not minetest.is_singleplayer() then
|
if not minetest.is_singleplayer() then
|
||||||
error("[formspec_editor] This game doesn't work in multiplayer!")
|
error("[formspec_editor] This game doesn't work in multiplayer!")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-----------------------------------
|
||||||
|
--error_handler()
|
||||||
|
-----------------------------------
|
||||||
|
local function error_handler(msg)
|
||||||
|
return string.format([[
|
||||||
|
formspec_version[4]
|
||||||
|
size[8,2]
|
||||||
|
label[0.375,0.5;Error: %s]
|
||||||
|
]], msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
--Define the basic lua env (only contains safe functions)
|
||||||
|
formspec_edit.lua_env = {
|
||||||
|
print = print, --can be useful for debugging
|
||||||
|
dump = dump,
|
||||||
|
dump2 = dump2,
|
||||||
|
table = table,
|
||||||
|
string = string,
|
||||||
|
math = math,
|
||||||
|
vector = vector,
|
||||||
|
F = minetest.formspec_escape,
|
||||||
|
C = minetest.colorize,
|
||||||
|
CE = minetest.get_color_escape_sequence,
|
||||||
|
S = function(str, ...) --oversimplificated version of minetest.translate
|
||||||
|
local arg = {n=select('#', ...), ...}
|
||||||
|
local arg_index = 1
|
||||||
|
local translated = str:gsub("@(.)", function(matched)
|
||||||
|
local c = string.byte(matched)
|
||||||
|
if string.byte("1") <= c and c <= string.byte("9") then
|
||||||
|
local a = c - string.byte("0")
|
||||||
|
if a ~= arg_index then
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
if a > arg.n then
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
arg_index = arg_index + 1
|
||||||
|
return arg[a]
|
||||||
|
elseif matched == "n" then
|
||||||
|
return "\n"
|
||||||
|
else
|
||||||
|
return matched
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
return translated
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
dofile(modpath.."/game_specific.lua")
|
||||||
|
|
||||||
--function definitions
|
--function definitions
|
||||||
|
|
||||||
-----------------------------------
|
---------------------------------
|
||||||
--load_formspec()
|
--check_updates()
|
||||||
-----------------------------------
|
---------------------------------
|
||||||
local function load_formspec()
|
|
||||||
|
local cached_file = ""
|
||||||
|
|
||||||
|
local function check_updates()
|
||||||
|
--attemp to open the file
|
||||||
local file = io.open(filepath, "rb")
|
local file = io.open(filepath, "rb")
|
||||||
if file == nil then
|
if file == nil then
|
||||||
return error_formspec
|
return error_formspec
|
||||||
else
|
else
|
||||||
|
--read file content
|
||||||
local content = file:read("*all")
|
local content = file:read("*all")
|
||||||
file:close()
|
file:close()
|
||||||
|
|
||||||
|
--validate content
|
||||||
if content == nil then
|
if content == nil then
|
||||||
return error_formspec
|
return error_formspec
|
||||||
else
|
else
|
||||||
return content
|
if content == cached_file then
|
||||||
|
return
|
||||||
|
else
|
||||||
|
cached_file = content
|
||||||
|
--load code
|
||||||
|
local func = load(content, "formspec_editor", "bt", formspec_edit.lua_env)
|
||||||
|
local state, msg = pcall(func)
|
||||||
|
if not state then
|
||||||
|
return error_handler(msg)
|
||||||
|
end
|
||||||
|
print(dump(msg))
|
||||||
|
return msg or "size[8,2]"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -68,54 +133,35 @@ end
|
||||||
--update_formspec()
|
--update_formspec()
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
local function update_formspec(player_name)
|
local function update_formspec(player_name)
|
||||||
|
local new = check_updates()
|
||||||
|
if new then
|
||||||
minetest.after(0.1, function(name)
|
minetest.after(0.1, function(name)
|
||||||
minetest.show_formspec(name, "fs", load_formspec())
|
minetest.show_formspec(name, "fs", new)
|
||||||
end, player_name)
|
end, player_name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-----------------------------------
|
|
||||||
--apply_styling()
|
--Registrations
|
||||||
-----------------------------------
|
|
||||||
local function apply_styling(player_ref)
|
|
||||||
local prepend = ""
|
|
||||||
if styling == "minetest_game" then
|
|
||||||
------------------------------------------------
|
|
||||||
---------------Minetest Game code---------------
|
|
||||||
------------------------------------------------
|
|
||||||
prepend = [[
|
|
||||||
bgcolor[#080808BB;true]
|
|
||||||
listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]
|
|
||||||
]]
|
|
||||||
local name = player_ref:get_player_name()
|
|
||||||
local info = minetest.get_player_information(name)
|
|
||||||
if info.formspec_version > 1 then
|
|
||||||
prepend = prepend .. "background9[5,5;1,1;mtg_gui_formbg.png;true;10]"
|
|
||||||
else
|
|
||||||
prepend = prepend .. "background[5,5;1,1;mtg_gui_formbg.png;true]"
|
|
||||||
end
|
|
||||||
elseif styling == "mineclone2" then
|
|
||||||
------------------------------------------------
|
|
||||||
---------------MineClone2 code------------------
|
|
||||||
------------------------------------------------
|
|
||||||
--Sadly, this code doesn't support inventory slot styling (texture based)
|
|
||||||
prepend = "listcolors[#9990;#FFF7;#FFF0;#000;#FFF]"..
|
|
||||||
"style_type[image_button;border=false;bgimg=mcl_inventory_button9.png;bgimg_pressed=mcl_inventory_button9_pressed.png;bgimg_middle=2,2]"..
|
|
||||||
"style_type[button;border=false;bgimg=mcl_inventory_button9.png;bgimg_pressed=mcl_inventory_button9_pressed.png;bgimg_middle=2,2]"..
|
|
||||||
"style_type[field;textcolor=#323232]"..
|
|
||||||
"style_type[label;textcolor=#323232]"..
|
|
||||||
"style_type[textarea;textcolor=#323232]"..
|
|
||||||
"style_type[checkbox;textcolor=#323232]"..
|
|
||||||
"bgcolor[#00000000]"..
|
|
||||||
"background9[1,1;1,1;mcl_base_textures_background9.png;true;7]"
|
|
||||||
end
|
|
||||||
player_ref:set_formspec_prepend(prepend)
|
|
||||||
end
|
|
||||||
|
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
--turn_off_hud()
|
--on_joinplayer()
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
local function turn_off_hud(player_ref)
|
minetest.register_on_joinplayer(function(player,_)
|
||||||
player_ref:hud_set_flags({
|
--Apply formspec prepend
|
||||||
|
player:set_formspec_prepend(formspec_edit.prepend)
|
||||||
|
|
||||||
|
--Create inventories
|
||||||
|
local inv = player:get_inventory()
|
||||||
|
for name,def in pairs(formspec_edit.inventories) do
|
||||||
|
inv:set_size(name, def.size)
|
||||||
|
if def.width then
|
||||||
|
inv:set_width(name, def.width)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--Hide builtin HUD elements
|
||||||
|
player:hud_set_flags({
|
||||||
hotbar = false,
|
hotbar = false,
|
||||||
healthbar = false,
|
healthbar = false,
|
||||||
crosshair = false,
|
crosshair = false,
|
||||||
|
@ -124,41 +170,27 @@ local function turn_off_hud(player_ref)
|
||||||
minimap = false,
|
minimap = false,
|
||||||
minimap_radar = false,
|
minimap_radar = false,
|
||||||
})
|
})
|
||||||
end
|
|
||||||
|
|
||||||
-----------------------------------
|
--Update sky
|
||||||
--set_sky()
|
player:set_sky({
|
||||||
-----------------------------------
|
|
||||||
local function set_sky(player_ref)
|
|
||||||
player_ref:set_sky({
|
|
||||||
base_color = "#AAF",
|
base_color = "#AAF",
|
||||||
type = "plain",
|
type = "plain",
|
||||||
clouds = false,
|
clouds = false,
|
||||||
})
|
})
|
||||||
player_ref:set_stars({visible = false})
|
player:set_stars({visible = false})
|
||||||
player_ref:set_sun({visible = false})
|
player:set_sun({visible = false})
|
||||||
player_ref:set_moon({visible = false})
|
player:set_moon({visible = false})
|
||||||
player_ref:override_day_night_ratio(0)
|
player:override_day_night_ratio(0)
|
||||||
end
|
|
||||||
|
|
||||||
--Registrations
|
|
||||||
|
|
||||||
-----------------------------------
|
|
||||||
--on_joinplayer()
|
|
||||||
-----------------------------------
|
|
||||||
minetest.register_on_joinplayer(function(player_ref,_)
|
|
||||||
apply_styling(player_ref)
|
|
||||||
turn_off_hud(player_ref)
|
|
||||||
set_sky(player_ref)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
--on_player_receive_fields()
|
--on_player_receive_fields()
|
||||||
-----------------------------------
|
-----------------------------------
|
||||||
minetest.register_on_player_receive_fields(function(player_ref, _, fields)
|
minetest.register_on_player_receive_fields(function(player, _, fields)
|
||||||
if fields.quit then
|
if fields.quit then
|
||||||
minetest.request_shutdown()
|
minetest.request_shutdown()
|
||||||
end
|
end
|
||||||
update_formspec(player_ref:get_player_name())
|
update_formspec(player:get_player_name())
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local time = 0
|
local time = 0
|
||||||
|
|
BIN
mods/formspec_edit/textures/mcl2/mcl_formspec_itemslot.png
Normal file
BIN
mods/formspec_edit/textures/mcl2/mcl_formspec_itemslot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 231 B |
BIN
mods/formspec_edit/textures/mtg/gui_furnace_arrow_bg.png
Normal file
BIN
mods/formspec_edit/textures/mtg/gui_furnace_arrow_bg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 324 B |
BIN
mods/formspec_edit/textures/mtg/gui_furnace_arrow_fg.png
Normal file
BIN
mods/formspec_edit/textures/mtg/gui_furnace_arrow_fg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 251 B |
BIN
mods/formspec_edit/textures/mtg/gui_hb_bg.png
Normal file
BIN
mods/formspec_edit/textures/mtg/gui_hb_bg.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 98 B |
BIN
mods/formspec_edit/textures/mtg/gui_hotbar.png
Normal file
BIN
mods/formspec_edit/textures/mtg/gui_hotbar.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 411 B |
BIN
mods/formspec_edit/textures/mtg/gui_hotbar_selected.png
Normal file
BIN
mods/formspec_edit/textures/mtg/gui_hotbar_selected.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 175 B |
Loading…
Add table
Reference in a new issue