Skip to content

Commit 009e39e

Browse files
v-robrubenwardy
andauthored
FormSpec: Add list spacing, slot size, and noclip (#10083)
* Add list spacing, slot size, and noclip * Simplify StyleSpec * Add test cases Co-authored-by: rubenwardy <rw@rubenwardy.com>
1 parent 4c76239 commit 009e39e

File tree

4 files changed

+78
-22
lines changed

4 files changed

+78
-22
lines changed

doc/lua_api.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2226,7 +2226,8 @@ Elements
22262226
* Show an inventory list if it has been sent to the client. Nothing will
22272227
be shown if the inventory list is of size 0.
22282228
* **Note**: With the new coordinate system, the spacing between inventory
2229-
slots is one-fourth the size of an inventory slot.
2229+
slots is one-fourth the size of an inventory slot by default. Also see
2230+
[Styling Formspecs] for changing the size of slots and spacing.
22302231

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

@@ -2809,6 +2810,7 @@ Some types may inherit styles from parent types.
28092810
* image_button
28102811
* item_image_button
28112812
* label
2813+
* list
28122814
* model
28132815
* pwdfield, inherits from field
28142816
* scrollbar
@@ -2896,6 +2898,10 @@ Some types may inherit styles from parent types.
28962898
* font - Sets font type. See button `font` property for more information.
28972899
* font_size - Sets font size. See button `font_size` property for more information.
28982900
* noclip - boolean, set to true to allow the element to exceed formspec bounds.
2901+
* list
2902+
* noclip - boolean, set to true to allow the element to exceed formspec bounds.
2903+
* size - 2d vector, sets the size of inventory slots in coordinates.
2904+
* spacing - 2d vector, sets the space between inventory slots in coordinates.
28992905
* image_button (additional properties)
29002906
* fgimg - standard image. Defaults to none.
29012907
* fgimg_hovered - image when hovered. Defaults to fgimg when not provided.

games/devtest/mods/testformspec/formspec.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ local tabheaders_fs = [[
3333
tabheader[8,6;10,1.5;tabs_size2;Height=1.5;1;false;false]
3434
]]
3535

36+
local inv_style_fs = [[
37+
style_type[list;noclip=true]
38+
list[current_player;main;-1.125,-1.125;2,2]
39+
style_type[list;spacing=.25,.125;size=.75,.875]
40+
list[current_player;main;3,.5;3,3]
41+
style_type[list;spacing=0;size=1]
42+
list[current_player;main;.5,4;8,4]
43+
]]
44+
3645
local hypertext_basic = [[
3746
<bigger>Normal test</bigger>
3847
This is a normal text.
@@ -310,6 +319,10 @@ local pages = {
310319
"size[12,13]real_coordinates[true]" ..
311320
"container[0.5,1.5]" .. tabheaders_fs .. "container_end[]",
312321

322+
-- Inv
323+
"size[12,13]real_coordinates[true]" ..
324+
"container[0.5,1.5]" .. inv_style_fs .. "container_end[]",
325+
313326
-- Animation
314327
[[
315328
formspec_version[3]
@@ -341,7 +354,7 @@ Number]
341354
local function show_test_formspec(pname, page_id)
342355
page_id = page_id or 2
343356

344-
local fs = pages[page_id] .. "tabheader[0,0;8,0.65;maintabs;Real Coord,Styles,Noclip,Hypertext,Tabs,Anim,ScrollC;" .. page_id .. ";false;false]"
357+
local fs = pages[page_id] .. "tabheader[0,0;8,0.65;maintabs;Real Coord,Styles,Noclip,Hypertext,Tabs,Invs,Anim,ScrollC;" .. page_id .. ";false;false]"
345358

346359
minetest.show_formspec(pname, "testformspec:formspec", fs)
347360
end

src/gui/StyleSpec.h

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class StyleSpec
5555
BORDERCOLORS,
5656
BORDERWIDTHS,
5757
SOUND,
58+
SPACING,
59+
SIZE,
5860
NUM_PROPERTIES,
5961
NONE
6062
};
@@ -119,6 +121,10 @@ class StyleSpec
119121
return BORDERWIDTHS;
120122
} else if (name == "sound") {
121123
return SOUND;
124+
} else if (name == "spacing") {
125+
return SPACING;
126+
} else if (name == "size") {
127+
return SIZE;
122128
} else {
123129
return NONE;
124130
}
@@ -259,27 +265,40 @@ class StyleSpec
259265
return rect;
260266
}
261267

262-
irr::core::vector2d<s32> getVector2i(Property prop, irr::core::vector2d<s32> def) const
268+
v2f32 getVector2f(Property prop, v2f32 def) const
263269
{
264270
const auto &val = properties[prop];
265271
if (val.empty())
266272
return def;
267273

268-
irr::core::vector2d<s32> vec;
269-
if (!parseVector2i(val, &vec))
274+
v2f32 vec;
275+
if (!parseVector2f(val, &vec))
270276
return def;
271277

272278
return vec;
273279
}
274280

275-
irr::core::vector2d<s32> getVector2i(Property prop) const
281+
v2s32 getVector2i(Property prop, v2s32 def) const
282+
{
283+
const auto &val = properties[prop];
284+
if (val.empty())
285+
return def;
286+
287+
v2f32 vec;
288+
if (!parseVector2f(val, &vec))
289+
return def;
290+
291+
return v2s32(vec.X, vec.Y);
292+
}
293+
294+
v2s32 getVector2i(Property prop) const
276295
{
277296
const auto &val = properties[prop];
278297
FATAL_ERROR_IF(val.empty(), "Unexpected missing property");
279298

280-
irr::core::vector2d<s32> vec;
281-
parseVector2i(val, &vec);
282-
return vec;
299+
v2f32 vec;
300+
parseVector2f(val, &vec);
301+
return v2s32(vec.X, vec.Y);
283302
}
284303

285304
gui::IGUIFont *getFont() const
@@ -432,22 +451,20 @@ class StyleSpec
432451
return true;
433452
}
434453

435-
bool parseVector2i(const std::string &value, irr::core::vector2d<s32> *parsed_vec) const
454+
bool parseVector2f(const std::string &value, v2f32 *parsed_vec) const
436455
{
437-
irr::core::vector2d<s32> vec;
456+
v2f32 vec;
438457
std::vector<std::string> v_vector = split(value, ',');
439458

440459
if (v_vector.size() == 1) {
441-
s32 x = stoi(v_vector[0]);
460+
f32 x = stof(v_vector[0]);
442461
vec.X = x;
443462
vec.Y = x;
444463
} else if (v_vector.size() == 2) {
445-
s32 x = stoi(v_vector[0]);
446-
s32 y = stoi(v_vector[1]);
447-
vec.X = x;
448-
vec.Y = y;
464+
vec.X = stof(v_vector[0]);
465+
vec.Y = stof(v_vector[1]);
449466
} else {
450-
warningstream << "Invalid vector2d string format: \"" << value
467+
warningstream << "Invalid 2d vector string format: \"" << value
451468
<< "\"" << std::endl;
452469
return false;
453470
}

src/gui/guiFormSpecMenu.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,20 +497,40 @@ void GUIFormSpecMenu::parseList(parserData *data, const std::string &element)
497497
3
498498
);
499499

500-
v2f32 slot_spacing = data->real_coordinates ?
501-
v2f32(imgsize.X * 1.25f, imgsize.Y * 1.25f) : spacing;
500+
auto style = getDefaultStyleForElement("list", spec.fname);
502501

503-
v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos)
504-
: getElementBasePos(&v_pos);
502+
v2f32 slot_scale = style.getVector2f(StyleSpec::SIZE, v2f32(0, 0));
503+
v2s32 slot_size(
504+
slot_scale.X <= 0 ? imgsize.X : slot_scale.X * imgsize.X,
505+
slot_scale.Y <= 0 ? imgsize.Y : slot_scale.Y * imgsize.Y
506+
);
507+
508+
v2f32 slot_spacing = style.getVector2f(StyleSpec::SPACING, v2f32(-1, -1));
509+
if (data->real_coordinates) {
510+
slot_spacing.X = slot_spacing.X < 0 ? imgsize.X * 1.25f :
511+
slot_spacing.X * imgsize.X + imgsize.X;
512+
slot_spacing.Y = slot_spacing.Y < 0 ? imgsize.Y * 1.25f :
513+
slot_spacing.Y * imgsize.Y + imgsize.Y;
514+
} else {
515+
slot_spacing.X = slot_spacing.X < 0 ? spacing.X :
516+
slot_spacing.X * spacing.X;
517+
slot_spacing.Y = slot_spacing.Y < 0 ? spacing.Y :
518+
slot_spacing.Y * spacing.Y;
519+
}
520+
521+
v2s32 pos = data->real_coordinates ? getRealCoordinateBasePos(v_pos) :
522+
getElementBasePos(&v_pos);
505523

506524
core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
507525
pos.X + (geom.X - 1) * slot_spacing.X + imgsize.X,
508526
pos.Y + (geom.Y - 1) * slot_spacing.Y + imgsize.Y);
509527

510528
GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent,
511-
spec.fid, rect, m_invmgr, loc, listname, geom, start_i, imgsize,
529+
spec.fid, rect, m_invmgr, loc, listname, geom, start_i, slot_size,
512530
slot_spacing, this, data->inventorylist_options, m_font);
513531

532+
e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
533+
514534
m_inventorylists.push_back(e);
515535
m_fields.push_back(spec);
516536
return;

0 commit comments

Comments
 (0)