Skip to content

Commit

Permalink
Add formspec table
Browse files Browse the repository at this point in the history
  • Loading branch information
kahrl authored and ShadowNinja committed Jan 13, 2014
1 parent 2b1eff7 commit 8966c16
Show file tree
Hide file tree
Showing 13 changed files with 1,798 additions and 246 deletions.
2 changes: 1 addition & 1 deletion builtin/gamemgr.lua
Expand Up @@ -31,7 +31,7 @@ end
--------------------------------------------------------------------------------
function gamemgr.handle_games_buttons(fields)
if fields["gamelist"] ~= nil then
local event = explode_textlist_event(fields["gamelist"])
local event = engine.explode_textlist_event(fields["gamelist"])
gamemgr.selected_game = event.index
end

Expand Down
22 changes: 11 additions & 11 deletions builtin/mainmenu.lua
Expand Up @@ -459,8 +459,8 @@ function tabbuilder.handle_multiplayer_buttons(fields)
end

if fields["favourites"] ~= nil then
local event = explode_textlist_event(fields["favourites"])
if event.typ == "DCL" then
local event = engine.explode_textlist_event(fields["favourites"])
if event.type == "DCL" then
if event.index <= #menu.favorites then
gamedata.address = menu.favorites[event.index].address
gamedata.port = menu.favorites[event.index].port
Expand All @@ -484,7 +484,7 @@ function tabbuilder.handle_multiplayer_buttons(fields)
end
end

if event.typ == "CHG" then
if event.type == "CHG" then
if event.index <= #menu.favorites then
local address = menu.favorites[event.index].address
local port = menu.favorites[event.index].port
Expand Down Expand Up @@ -586,12 +586,12 @@ function tabbuilder.handle_server_buttons(fields)
local world_doubleclick = false

if fields["srv_worlds"] ~= nil then
local event = explode_textlist_event(fields["srv_worlds"])
local event = engine.explode_textlist_event(fields["srv_worlds"])

if event.typ == "DCL" then
if event.type == "DCL" then
world_doubleclick = true
end
if event.typ == "CHG" then
if event.type == "CHG" then
engine.setting_set("mainmenu_last_selected_world",
filterlist.get_raw_index(worldlist,engine.get_textlist_index("srv_worlds")))
end
Expand Down Expand Up @@ -737,13 +737,13 @@ function tabbuilder.handle_singleplayer_buttons(fields)
local world_doubleclick = false

if fields["sp_worlds"] ~= nil then
local event = explode_textlist_event(fields["sp_worlds"])
local event = engine.explode_textlist_event(fields["sp_worlds"])

if event.typ == "DCL" then
if event.type == "DCL" then
world_doubleclick = true
end

if event.typ == "CHG" then
if event.type == "CHG" then
engine.setting_set("mainmenu_last_selected_world",
filterlist.get_raw_index(worldlist,engine.get_textlist_index("sp_worlds")))
end
Expand Down Expand Up @@ -813,8 +813,8 @@ end
--------------------------------------------------------------------------------
function tabbuilder.handle_texture_pack_buttons(fields)
if fields["TPs"] ~= nil then
local event = explode_textlist_event(fields["TPs"])
if event.typ == "CHG" or event.typ=="DCL" then
local event = engine.explode_textlist_event(fields["TPs"])
if event.type == "CHG" or event.type == "DCL" then
local index = engine.get_textlist_index("TPs")
engine.setting_set("mainmenu_last_selected_TP",
index)
Expand Down
51 changes: 31 additions & 20 deletions builtin/misc_helpers.lua
Expand Up @@ -115,26 +115,6 @@ function math.hypot(x, y)
return x * math.sqrt(1 + t * t)
end

--------------------------------------------------------------------------------
function explode_textlist_event(text)

local retval = {}
retval.typ = "INV"

local parts = text:split(":")

if #parts == 2 then
retval.typ = parts[1]:trim()
retval.index= tonumber(parts[2]:trim())

if type(retval.index) ~= "number" then
retval.typ = "INV"
end
end

return retval
end

--------------------------------------------------------------------------------
function get_last_folder(text,count)
local parts = text:split(DIR_DELIM)
Expand Down Expand Up @@ -368,6 +348,37 @@ if minetest then
end
end

--------------------------------------------------------------------------------
function tbl.explode_table_event(evt)
if evt ~= nil then
local parts = evt:split(":")
if #parts == 3 then
local t = parts[1]:trim()
local r = tonumber(parts[2]:trim())
local c = tonumber(parts[3]:trim())
if type(r) == "number" and type(c) == "number" and t ~= "INV" then
return {type=t, row=r, column=c}
end
end
end
return {type="INV", row=0, column=0}
end

--------------------------------------------------------------------------------
function tbl.explode_textlist_event(evt)
if evt ~= nil then
local parts = evt:split(":")
if #parts == 2 then
local t = parts[1]:trim()
local r = tonumber(parts[2]:trim())
if type(r) == "number" and t ~= "INV" then
return {type=t, index=r}
end
end
end
return {type="INV", index=0}
end

--------------------------------------------------------------------------------
-- mainmenu only functions
--------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions builtin/modmgr.lua
Expand Up @@ -572,7 +572,7 @@ function modmgr.handle_modmgr_buttons(fields)
}

if fields["modlist"] ~= nil then
local event = explode_textlist_event(fields["modlist"])
local event = engine.explode_textlist_event(fields["modlist"])
modmgr.selected_mod = event.index
end

Expand Down Expand Up @@ -693,10 +693,10 @@ end
--------------------------------------------------------------------------------
function modmgr.handle_configure_world_buttons(fields)
if fields["world_config_modlist"] ~= nil then
local event = explode_textlist_event(fields["world_config_modlist"])
local event = engine.explode_textlist_event(fields["world_config_modlist"])
modmgr.world_config_selected_mod = event.index

if event.typ == "DCL" then
if event.type == "DCL" then
modmgr.world_config_enable_mod(nil)
end
end
Expand Down
62 changes: 62 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -1011,6 +1011,7 @@ textlist[<X>,<Y>;<W>,<H>;<name>;<listelem 1>,<listelem 2>,...,<listelem n>;<sele
^ if you want a listelement to start with # write ##
^ index to be selected within textlist
^ true/false draw transparent background
^ see also minetest.explode_textlist_event (main menu: engine.explode_textlist_event)

tabheader[<X>,<Y>;<name>;<caption 1>,<caption 2>,...,<caption n>;<current_tab>;<transparent>;<draw_border>]
^ show a tabHEADER at specific position (ignores formsize)
Expand Down Expand Up @@ -1043,6 +1044,57 @@ checkbox[<X>,<Y>;<name>;<label>;<selected>]
^ label to be shown left of checkbox
^ selected (optional) true/false

table[<X>,<Y>;<W>,<H>;<name>;<cell 1>,<cell 2>,...,<cell n>;<selected idx>]
^ show scrollable table using options defined by the previous tableoptions[]
^ displays cells as defined by the previous tablecolumns[]
^ x and y position the itemlist relative to the top left of the menu
^ w and h are the size of the itemlist
^ name fieldname sent to server on row select or doubleclick
^ cell 1...n cell contents given in row-major order
^ selected idx: index of row to be selected within table (first row = 1)
^ see also minetest.explode_table_event (main menu: engine.explode_table_event)

tableoptions[<opt 1>;<opt 2>;...]
^ sets options for table[]:
^ color=#RRGGBB
^^ default text color (HEX-Color), defaults to #FFFFFF
^ background=#RRGGBB
^^ table background color (HEX-Color), defaults to #000000
^ border=<true/false>
^^ should the table be drawn with a border? (default true)
^ highlight=#RRGGBB
^^ highlight background color (HEX-Color), defaults to #466432
^ highlight_text=#RRGGBB
^^ highlight text color (HEX-Color), defaults to #FFFFFF
^ opendepth=<value>
^^ all subtrees up to depth < value are open (default value = 0)
^^ only useful when there is a column of type "tree"

tablecolumns[<type 1>,<opt 1a>,<opt 1b>,...;<type 2>,<opt 2a>,<opt 2b>;...]
^ sets columns for table[]:
^ types: text, image, color, indent, tree
^^ text: show cell contents as text
^^ image: cell contents are an image index, use column options to define images
^^ color: cell contents are a HEX-Color and define color of following cell
^^ indent: cell contents are a number and define indentation of following cell
^^ tree: same as indent, but user can open and close subtrees (treeview-like)
^ column options:
^^ align=<value> for "text" and "image": content alignment within cells
^^ available values: left (default), center, right, inline
^^ width=<value> for "text" and "image": minimum width in em (default 0)
^^ for "indent" and "tree": indent width in em (default 1.5)
^^ padding=<value> padding left of the column, in em (default 0.5)
^^ exception: defaults to 0 for indent columns
^^ tooltip=<value> tooltip text (default empty)
^ "image" column options:
^^ 0=<value> sets image for image index 0
^^ 1=<value> sets image for image index 1
^^ 2=<value> sets image for image index 2
^^ and so on; defined indices need not be contiguous
^^ empty or non-numeric cells are treated as 0
^ "color" column options:
^^ span=<value> number of following columns to affect (default infinite)

Note: do NOT use a element name starting with "key_" those names are reserved to
pass key press events to formspec!

Expand Down Expand Up @@ -1346,11 +1398,21 @@ minetest.get_inventory(location) -> InvRef
minetest.create_detached_inventory(name, callbacks) -> InvRef
^ callbacks: See "Detached inventory callbacks"
^ Creates a detached inventory. If it already exists, it is cleared.

Formspec:
minetest.show_formspec(playername, formname, formspec)
^ playername: name of player to show formspec
^ formname: name passed to on_player_receive_fields callbacks
^ should follow "modname:<whatever>" naming convention
^ formspec: formspec to display
minetest.formspec_escape(string) -> string
^ escapes characters [ ] \ , ; that can not be used in formspecs
minetest.explode_table_event(string) -> table
^ returns e.g. {type="CHG", row=1, column=2}
^ type: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)
minetest.explode_textlist_event(string) -> table
^ returns e.g. {type="CHG", index=1}
^ type: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)

Item handling:
minetest.inventorycube(img1, img2, img3)
Expand Down
40 changes: 23 additions & 17 deletions doc/menu_lua_api.txt
Expand Up @@ -89,12 +89,33 @@ engine.sound_play(spec, looped) -> handle
^ looped = bool
engine.sound_stop(handle)

GUI:
Formspec:
engine.update_formspec(formspec)
- engine.set_background(type, texturepath)
engine.get_table_index(tablename) -> index
^ can also handle textlists
engine.formspec_escape(string) -> string
^ escapes characters [ ] \ , ; that can not be used in formspecs
engine.explode_table_event(string) -> table
^ returns e.g. {type="CHG", row=1, column=2}
^ type: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)
engine.explode_textlist_event(string) -> table
^ returns e.g. {type="CHG", index=1}
^ type: "INV" (no row selected), "CHG" (selected) or "DCL" (double-click)

GUI:
engine.set_background(type, texturepath)
^ type: "background", "overlay", "header" or "footer"
engine.set_clouds(<true/false>)
engine.set_topleft_text(text)
engine.show_keys_menu()
engine.file_open_dialog(formname,caption)
^ shows a file open dialog
^ formname is base name of dialog response returned in fields
^ -if dialog was accepted "_accepted"
^^ will be added to fieldname containing the path
^ -if dialog was canceled "_cancelled"
^ will be added to fieldname value is set to formname itself
^ returns nil or selected file/folder

Games:
engine.get_game(index)
Expand Down Expand Up @@ -155,22 +176,7 @@ engine.get_worlds() -> list of worlds (possible in async calls)
engine.create_world(worldname, gameid)
engine.delete_world(index)


UI:
engine.get_textlist_index(textlistname) -> index
engine.show_keys_menu()
engine.file_open_dialog(formname,caption)
^ shows a file open dialog
^ formname is base name of dialog response returned in fields
^ -if dialog was accepted "_accepted"
^^ will be added to fieldname containing the path
^ -if dialog was canceled "_cancelled"
^ will be added to fieldname value is set to formname itself
^ returns nil or selected file/folder

Helpers:
engine.formspec_escape(string) -> string
^ escapes characters [ ] \ , ; that can not be used in formspecs
engine.gettext(string) -> string
^ look up the translation of a string in the gettext message catalog
fgettext(string, ...) -> string
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -374,6 +374,7 @@ set(minetest_SRCS
guiMessageMenu.cpp
guiTextInputMenu.cpp
guiFormSpecMenu.cpp
guiTable.cpp
guiPauseMenu.cpp
guiPasswordChange.cpp
guiVolumeChange.cpp
Expand Down

0 comments on commit 8966c16

Please sign in to comment.