Go to file
bell07 580508fa37 Fix showing up complex images
Some complex texture strings contains [ character that needs to be escaped.
Issue found with [halloween](https://github.com/GreenXenith/halloween) preview texture on player tab page button in smart_inventory. The button was dissapeared, after this change visible again.
2018-08-27 20:15:55 +01:00
docs Update API.md 2018-01-26 16:11:35 +00:00
.gitattributes Initial Push 2013-10-28 14:50:00 +00:00
.gitignore Initial Push 2013-10-28 14:50:00 +00:00
README.md Add support different smartfs versions in different mods 2017-01-02 21:15:02 +00:00
depends.txt Add support for inventory_plus 2014-02-20 20:15:19 +00:00
description.txt Create description.txt 2016-03-08 18:21:39 +00:00
init.lua Add views and containers 2017-08-12 00:01:35 +01:00
mod.conf WTFPL -> CC0 2016-05-18 22:56:47 +01:00
smartfs.lua Fix showing up complex images 2018-08-27 20:15:55 +01:00

README.md

smartfs

This mod provides a 2nd generation way of creating forms - this means that the modder does not need to worry about complex formspec strings

  • Expandable: you can register your own elements to use on the form.
  • Easy event handling: use binding to handle events.
  • New elements: Includes a toggle button

License: WTFPL

#Using Smart Formspec Smartfs provides 2nd generation Minetest forms to replace clunky formspec strings. Each smartfs form is a container filled with GUI elements. A number of default elements are included with smartfs, but modders can also define their own custom elements. This document describes the basic usage of the smartfs API.

##Installation Smartfs can be used as a library or a mod.

To use smartfs as a library, copy the smartfs.lua file to your mod folder and add local smartfs = dofile(minetest.get_modpath(minetest.get_current_modname()).."/smartfs.lua")

to the top of your init.lua. If your mod is splitted to multiple files you can transport the library reference trough your mod namespace yourmod.smartfs = dofile(minetest.get_modpath(minetest.get_current_modname()).."/smartfs.lua")

To use smartfs as a mod, add it to your game's mods folder or to the user mods folder and enable it. You need to set up a dependency for your mod to use it. The library is available in the global "smartfs" table in this case.

Creating and showing forms

A form is a rectangular area of the screen upon which all elements are placed. Use the smartfs.create() function to create a new form. This function takes two arguments and returns a form object.

The first argument is a unique string that identifies the form. The second argument is a function that should take a single argument called state which is used to set form properties like size and background color. State also has constructors for all form elements and can be used with state:element_name. Below is a quick example.

myform = smartfs.create("My Form",function(state)
    --sets the form's size
    -- (width, hieght)
    state:size(5,5)

    --creates a label and places it on the form
    --(x-pos, y-pos, name, text)
    state:label(3,3,"label1", "A label!")
end)

Forms can be shown to the player by using the show(target) function. The target argument is the name of the player that will see the form.

myform:show("singleplayer")

Here is a list of steps the library takes.

  • You create a new form using smartfs.create().
  • The form is registered and stored for later use.
  • You show a form to a player using the myform:show()
  • The state is created and stored.
  • The function in smartfs.create runs and creates the elements.
  • The form is displayed to the player.

Modifying Elements

Elements have functions of the form element:function(args) where you need to have access to the element object.

You can get the element object by assigning a variable to its creation function like so:

local button1 = state:button(0,0, 1,4, "btn1", "A button")
--button1 is now a table representing the button

You can also get the element by using state:get(name). The example below will retrieve a button with the name "btn1":

button1 = state:get("btn1")
--or
state:get("btn1"):onClick(your\_onclick\_function

Both of these methods should be used inside the form creation callback function, the function you pass to smartfs.create, or in event callbacks.

Now that you have located your element you can modify it.

button1:setPos(4,0)

Inventory Support

Smartfs supports adding a button to Inventory+ or Unified Inventory which will open one of your own custom forms. Use the smartfs.add_to_inventory(form, icon, title) function where form is the smartfs form linked to by the button, icon is the button image (only for unified inventory), and title is the button text (only for inventory+).

smartfs.add_to_inventory(form, icon, title)

Dynamic forms

Dynamic forms allow you to make a form without having to register it before the game finished loading.

local state = smartfs.dynamic("smartfs:dyn_form", name)
state:load(minetest.get_modpath("smartfs").."/example.smartfs")
state:get("btn"):click(function(self,state)
	print("Button clicked!")
end)
state:show()

Make sure you call state:show()

Formspec on nodes

SmartFS is able to attach a formspec to a node metadata. The attached form can be opened by any player and all players see the same. If a player enter information and take updates on the form, all users see the updated form instantly. All changes are saved in node meta data and visible to all players. Do not write sensitive to the formspec since the nodemeta is sent to all player clients. To show something sensitive to only a single player info please use form:show method instead of nodemeta. Please note: there is a "reset" implemented if all players leave the node formspec, the initial state is restored.

minetest.register_node("smartfs:demoblock", {
	description = "SmartFS Demo block",
	groups = {cracky = 3},
	tiles = {"demo.png"},
	after_place_node = function(pos, placer, itemstack, pointed_thing)
		state:attach_nodemeta(pos, placer)
	end,
	on_receive_fields = smartfs.nodemeta_on_receive_fields
})