Skip to content

Commit 5e7004e

Browse files
v-robrubenwardy
authored andcommittedJun 27, 2019
Add compatible, consistent coordinate system to FormSpecs. (#8524)
1 parent 91d244c commit 5e7004e

File tree

3 files changed

+541
-153
lines changed

3 files changed

+541
-153
lines changed
 

Diff for: ‎doc/lua_api.txt

+123-9
Original file line numberDiff line numberDiff line change
@@ -1875,9 +1875,15 @@ is used when the server receives user input. You must not use the name
18751875
Spaces and newlines can be inserted between the blocks, as is used in the
18761876
examples.
18771877

1878-
Position and size units are inventory slots, `X` and `Y` position the formspec
1879-
element relative to the top left of the menu or container. `W` and `H` are its
1880-
width and height values.
1878+
Position and size units are inventory slots unless the new coordinate system
1879+
is enabled. `X` and `Y` position the formspec element relative to the top left
1880+
of the menu or container. `W` and `H` are its width and height values.
1881+
1882+
If the new system is enabled, all elements have unified coordinates for all
1883+
elements with no padding or spacing in between. This is highly recommended
1884+
for new forms. See `real_coordinates[<bool>]` and `Migrating to Real
1885+
Coordinates`.
1886+
18811887
Inventories with a `player:<name>` inventory location are only sent to the
18821888
player named `<name>`.
18831889

@@ -1951,6 +1957,16 @@ Elements
19511957
* Must be used after the `size`, `position`, and `anchor` elements (if present).
19521958
* Disables player:set_formspec_prepend() from applying to this formspec.
19531959

1960+
### `real_coordinates[<bool>]`
1961+
1962+
* When set to true, all following formspec elements will use the new coordinate system.
1963+
* If used immediately after `size`, `position`, `anchor`, and `no_prepend` elements
1964+
(if present), the form size will use the new coordinate system.
1965+
* **Note**: Formspec prepends are not affected by the coordinates in the main form.
1966+
They must enable it explicitly.
1967+
* For information on converting forms to the new coordinate system, see `Migrating
1968+
to Real Coordinates`.
1969+
19541970
### `container[<X>,<Y>]`
19551971

19561972
* Start of a container block, moves all physical elements in the container by
@@ -1968,11 +1984,15 @@ Elements
19681984

19691985
* Show an inventory list if it has been sent to the client. Nothing will
19701986
be shown if the inventory list is of size 0.
1987+
* **Note**: With the new coordinate system, the spacing between inventory
1988+
slots is one-fourth the size of an inventory slot.
19711989

19721990
### `list[<inventory location>;<list name>;<X>,<Y>;<W>,<H>;<starting item index>]`
19731991

19741992
* Show an inventory list if it has been sent to the client. Nothing will
19751993
be shown if the inventory list is of size 0.
1994+
* **Note**: With the new coordinate system, the spacing between inventory
1995+
slots is one-fourth the size of an inventory slot.
19761996

19771997
### `listring[<inventory location>;<list name>]`
19781998

@@ -2064,7 +2084,8 @@ Elements
20642084
* Textual password style field; will be sent to server when a button is clicked
20652085
* When enter is pressed in field, fields.key_enter_field will be sent with the
20662086
name of this field.
2067-
* Fields are a set height, but will be vertically centred on `H`
2087+
* With the old coordinate system, fields are a set height, but will be vertically
2088+
centred on `H`. With the new coordinate system, `H` will modify the height.
20682089
* `name` is the name of the field as returned in fields to `on_receive_fields`
20692090
* `label`, if not blank, will be text printed on the top left above the field
20702091
* See `field_close_on_enter` to stop enter closing the formspec
@@ -2074,7 +2095,8 @@ Elements
20742095
* Textual field; will be sent to server when a button is clicked
20752096
* When enter is pressed in field, `fields.key_enter_field` will be sent with
20762097
the name of this field.
2077-
* Fields are a set height, but will be vertically centred on `H`
2098+
* With the old coordinate system, fields are a set height, but will be vertically
2099+
centred on `H`. With the new coordinate system, `H` will modify the height.
20782100
* `name` is the name of the field as returned in fields to `on_receive_fields`
20792101
* `label`, if not blank, will be text printed on the top left above the field
20802102
* `default` is the default value of the field
@@ -2111,23 +2133,34 @@ Elements
21112133

21122134
* The label formspec element displays the text set in `label`
21132135
at the specified position.
2136+
* **Note**: If the new coordinate system is enabled, labels are
2137+
positioned from the center of the text, not the top.
21142138
* The text is displayed directly without automatic line breaking,
2115-
so label should not be used for big text chunks.
2139+
so label should not be used for big text chunks. Newlines can be
2140+
used to make labels multiline.
2141+
* **Note**: With the new coordinate system, newlines are spaced with
2142+
half a coordinate. With the old system, newlines are spaced 2/5 of
2143+
an inventory slot.
21162144

21172145
### `vertlabel[<X>,<Y>;<label>]`
21182146

21192147
* Textual label drawn vertically
21202148
* `label` is the text on the label
2149+
* **Note**: If the new coordinate system is enabled, vertlabels are
2150+
positioned from the center of the text, not the left.
21212151

21222152
### `button[<X>,<Y>;<W>,<H>;<name>;<label>]`
21232153

21242154
* Clickable button. When clicked, fields will be sent.
2125-
* Fixed button height. It will be vertically centred on `H`
2155+
* With the old coordinate system, buttons are a set height, but will be vertically
2156+
centred on `H`. With the new coordinate system, `H` will modify the height.
21262157
* `label` is the text on the button
21272158

21282159
### `image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>]`
21292160

21302161
* `texture name` is the filename of an image
2162+
* **Note**: Height is supported on both the old and new coordinate systems
2163+
for image_buttons.
21312164

21322165
### `image_button[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>;<noclip>;<drawborder>;<pressed texture name>]`
21332166

@@ -2146,10 +2179,12 @@ Elements
21462179
### `button_exit[<X>,<Y>;<W>,<H>;<name>;<label>]`
21472180

21482181
* When clicked, fields will be sent and the form will quit.
2182+
* Same as `button` in all other respects.
21492183

21502184
### `image_button_exit[<X>,<Y>;<W>,<H>;<texture name>;<name>;<label>]`
21512185

21522186
* When clicked, fields will be sent and the form will quit.
2187+
* Same as `image_button` in all other respects.
21532188

21542189
### `textlist[<X>,<Y>;<W>,<H>;<name>;<listelem 1>,<listelem 2>,...,<listelem n>]`
21552190

@@ -2175,6 +2210,34 @@ Elements
21752210
### `tabheader[<X>,<Y>;<name>;<caption 1>,<caption 2>,...,<caption n>;<current_tab>;<transparent>;<draw_border>]`
21762211

21772212
* Show a tab**header** at specific position (ignores formsize)
2213+
* `X` and `Y`: position of the tabheader
2214+
* *Note*: Width and height are automatically chosen with this syntax
2215+
* `name` fieldname data is transferred to Lua
2216+
* `caption 1`...: name shown on top of tab
2217+
* `current_tab`: index of selected tab 1...
2218+
* `transparent` (optional): show transparent
2219+
* `draw_border` (optional): draw border
2220+
2221+
### `tabheader[<X>,<Y>;<H>;<name>;<caption 1>,<caption 2>,...,<caption n>;<current_tab>;<transparent>;<draw_border>]`
2222+
2223+
* Show a tab**header** at specific position (ignores formsize)
2224+
* **Important note**: This syntax for tabheaders can only be used with the
2225+
new coordinate system.
2226+
* `X` and `Y`: position of the tabheader
2227+
* `H`: height of the tabheader. Width is automatically determined with this syntax.
2228+
* `name` fieldname data is transferred to Lua
2229+
* `caption 1`...: name shown on top of tab
2230+
* `current_tab`: index of selected tab 1...
2231+
* `transparent` (optional): show transparent
2232+
* `draw_border` (optional): draw border
2233+
2234+
### `tabheader[<X>,<Y>;<W>,<H>;<name>;<caption 1>,<caption 2>,...,<caption n>;<current_tab>;<transparent>;<draw_border>]`
2235+
2236+
* Show a tab**header** at specific position (ignores formsize)
2237+
* **Important note**: This syntax for tabheaders can only be used with the
2238+
new coordinate system.
2239+
* `X` and `Y`: position of the tabheader
2240+
* `W` and `H`: width and height of the tabheader
21782241
* `name` fieldname data is transferred to Lua
21792242
* `caption 1`...: name shown on top of tab
21802243
* `current_tab`: index of selected tab 1...
@@ -2193,8 +2256,22 @@ Elements
21932256
* **Important note**: There are two different operation modes:
21942257
1. handle directly on change (only changed dropdown is submitted)
21952258
2. read the value on pressing a button (all dropdown values are available)
2196-
* `x` and `y` position of dropdown
2197-
* Width of dropdown
2259+
* `X` and `Y`: position of the dropdown
2260+
* `W`: width of the dropdown. Height is automatically chosen with this syntax.
2261+
* Fieldname data is transferred to Lua
2262+
* Items to be shown in dropdown
2263+
* Index of currently selected dropdown item
2264+
2265+
### `dropdown[<X>,<Y>;<W>,<H>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>]`
2266+
2267+
* Show a dropdown field
2268+
* **Important note**: This syntax for dropdowns can only be used with the
2269+
new coordinate system.
2270+
* **Important note**: There are two different operation modes:
2271+
1. handle directly on change (only changed dropdown is submitted)
2272+
2. read the value on pressing a button (all dropdown values are available)
2273+
* `X` and `Y`: position of the dropdown
2274+
* `W` and `H`: width and height of the dropdown
21982275
* Fieldname data is transferred to Lua
21992276
* Items to be shown in dropdown
22002277
* Index of currently selected dropdown item
@@ -2205,6 +2282,8 @@ Elements
22052282
* `name` fieldname data is transferred to Lua
22062283
* `label` to be shown left of checkbox
22072284
* `selected` (optional): `true`/`false`
2285+
* **Note**: If the new coordinate system is enabled, checkboxes are
2286+
positioned from the center of the checkbox, not the top.
22082287

22092288
### `scrollbar[<X>,<Y>;<W>,<H>;<orientation>;<name>;<value>]`
22102289

@@ -2281,6 +2360,41 @@ Elements
22812360
**Note**: do _not_ use a element name starting with `key_`; those names are
22822361
reserved to pass key press events to formspec!
22832362

2363+
Migrating to Real Coordinates
2364+
-----------------------------
2365+
2366+
In the old system, positions included padding and spacing. Padding is a gap between
2367+
the formspec window edges and content, and spacing is the gaps between items. For
2368+
example, two `1x1` elements at `0,0` and `1,1` would have a spacing of `5/4` between them,
2369+
and a padding of `3/8` from the formspec edge. It may be easiest to recreate old layouts
2370+
in the new coordinate system from scratch.
2371+
2372+
To recreate an old layout with padding, you'll need to pass the positions and sizes
2373+
through the following formula to re-introduce padding:
2374+
2375+
```
2376+
pos = (oldpos + 1)*spacing + padding
2377+
where
2378+
padding = 3/8
2379+
spacing = 5/4
2380+
```
2381+
2382+
You'll need to change the `size[]` tag like this:
2383+
2384+
```
2385+
size = (oldsize-1)*spacing + padding*2 + 1
2386+
```
2387+
2388+
A few elements had random offsets in the old system. Here is a table which shows these
2389+
offsets when migrating:
2390+
2391+
| Element | Position | Size | Notes
2392+
|---------|------------|---------|-------
2393+
| box | +0.3, +0.1 | 0, -0.4 |
2394+
| button | | | Buttons now support height, so set h = 2 * 15/13 * 0.35, and reposition if h ~= 15/13 * 0.35 before
2395+
| list | | | Spacing is now 0.25 for both directions, meaning lists will be taller in height
2396+
| label | 0, +0.3 | | The first line of text is now positioned centered exactly at the position specified
2397+
22842398

22852399

22862400

0 commit comments

Comments
 (0)
Please sign in to comment.