Skip to content

Commit ff95981

Browse files
committed
Replace image string by item image definition
1 parent e4c89c6 commit ff95981

File tree

11 files changed

+263
-232
lines changed

11 files changed

+263
-232
lines changed

doc/lua_api.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,10 +2705,10 @@ Some of the values in the key-value store are handled specially:
27052705
See also: `get_description` in [`ItemStack`](#itemstack)
27062706
* `short_description`: Set the item stack's short description.
27072707
See also: `get_short_description` in [`ItemStack`](#itemstack)
2708-
* `inventory_image`: Override inventory_image
2709-
* `inventory_overlay`: Override inventory_overlay
2710-
* `wield_image`: Override wield_image
2711-
* `wield_overlay`: Override wield_overlay
2708+
* `inventory_image`: Override inventory_image.name
2709+
* `inventory_overlay`: Override inventory_overlay.name
2710+
* `wield_image`: Override wield_image.name
2711+
* `wield_overlay`: Override wield_overlay.name
27122712
* `wield_scale`: Override wield_scale, use vector.to_string
27132713
* `color`: A `ColorString`, which sets the stack's color.
27142714
* `palette_index`: If the item has a palette, this is used to get the
@@ -5849,8 +5849,8 @@ Utilities
58495849
particlespawner_exclude_player = true,
58505850
-- core.generate_decorations() supports `use_mapgen_biomes` parameter (5.14.0)
58515851
generate_decorations_biomes = true,
5852-
-- Item definitions make use of the `inventory_image_animation`, `inventory_overlay_animation`,
5853-
-- `wield_overlay_animation` and `wield_image_animation` field (5.16.0)
5852+
-- Item definition fields `inventory_image`, `inventory_overlay`, `wield_image`
5853+
-- and `wield_overlay` accept a table containing animation definitions. (5.16.0)
58545854
item_image_animation = true,
58555855
}
58565856
```
@@ -9855,6 +9855,13 @@ Tile animation definition
98559855
}
98569856
```
98579857

9858+
Item image definition
9859+
---------------------
9860+
9861+
* `"image.png"`
9862+
* `{name="image.png", animation={Tile Animation definition}}`
9863+
* Basically a tile definition but for items
9864+
98589865
Item definition
98599866
---------------
98609867

@@ -9881,32 +9888,20 @@ Used by `core.register_node`, `core.register_craftitem`, and
98819888
-- {bendy = 2, snappy = 1},
98829889
-- {hard = 1, metal = 1, spikes = 1}
98839890

9884-
inventory_image = "",
9885-
-- Texture shown in the inventory GUI
9891+
inventory_image = <Item image definition>,
9892+
-- Image shown in the inventory GUI
98869893
-- Defaults to a 3D rendering of the node if left empty.
98879894

9888-
inventory_overlay = "",
9889-
-- An overlay texture which is not affected by colorization
9895+
inventory_overlay = <Item image definition>,
9896+
-- An overlay image which is not affected by colorization
98909897

9891-
inventory_image_animation = {Tile Animation definition},
9892-
-- Optional, animates the `inventory_image` if specified
9893-
9894-
inventory_overlay_animation = {Tile Animation definition},
9895-
-- Optional, animates the `inventory_overlay` if specified
9896-
9897-
wield_image = "",
9898-
-- Texture shown when item is held in hand
9898+
wield_image = <Item image definition>,
9899+
-- Image shown when item is held in hand
98999900
-- Defaults to a 3D rendering of the node if left empty.
99009901

9901-
wield_overlay = "",
9902+
wield_overlay = <Item image definition>,
99029903
-- Like inventory_overlay but only used in the same situation as wield_image
99039904

9904-
wield_image_animation = {Tile Animation definition}
9905-
-- Optional, animates the `wield_image` if specified
9906-
9907-
wield_overlay_animation = {Tile Animation definition}
9908-
-- Optional, animates `wield_overlay` if specified
9909-
99109905
wield_scale = {x = 1, y = 1, z = 1},
99119906
-- Scale for the item when held in hand
99129907

games/devtest/mods/testitems/init.lua

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -171,72 +171,58 @@ core.register_on_leaveplayer(function(player, timed_out)
171171
vmanip_for_trees[player:get_player_name()] = nil
172172
end)
173173

174-
core.register_craftitem("testitems:inventory_image_animation", {
175-
description = S("Animated Test Item").."\n"..
176-
S("Image animate from A to D in 4s cycle"),
177-
inventory_image= "testnodes_anim.png^[invert:rgb",
178-
inventory_image_animation = {
174+
-- Animation test items
175+
176+
local animated_image = {
177+
name = "testnodes_anim.png^[invert:rgb",
178+
animation = {
179179
type = "vertical_frames",
180180
aspect_w = 16,
181181
aspect_h = 16,
182182
length = 4.0,
183-
},
183+
}
184+
}
185+
186+
local animated_overlay = {
187+
name = "testitems_animation_overlay.png",
188+
animation = {
189+
type = "sheet_2d",
190+
frames_w = 1,
191+
frames_h = 4,
192+
frame_length = 1.0,
193+
}
194+
}
195+
196+
core.register_craftitem("testitems:inventory_image_animation", {
197+
description = S("Animated Test Item").."\n"..
198+
S("Image animate from A to D in 4s cycle"),
199+
inventory_image = animated_image
184200
})
185201

186202
core.register_craftitem("testitems:inventory_image_animation_overlay", {
187203
description = S("Animated Test Item With Overlay").."\n"..
188204
S("Should be colored red and have green stripes that move").."\n"..
189205
S("Image animate from A to D in 4s cycle"),
190-
inventory_image= "testnodes_anim.png^[invert:rgb",
191-
inventory_overlay="testitems_animation_overlay.png",
192-
inventory_image_animation = {
193-
type = "vertical_frames",
194-
aspect_w = 16,
195-
aspect_h = 16,
196-
length = 4.0,
197-
},
198-
inventory_overlay_animation = {
199-
type = "vertical_frames",
200-
aspect_w = 16,
201-
aspect_h = 16,
202-
length = 4.0,
203-
},
206+
inventory_image = animated_image,
207+
inventory_overlay = animated_overlay,
204208
color = "#ff0000",
205209
})
206210

207211
core.register_craftitem("testitems:wield_image_animation", {
208212
description = S("Wield Animated Test Item").."\n"..
209213
S("Looks like the Animated Test Item, "..
210214
"but only animated for the wield mesh."),
211-
inventory_image= "testnodes_anim.png^[invert:rgb^[verticalframe:4:0",
212-
wield_image= "testnodes_anim.png^[invert:rgb",
213-
wield_image_animation = {
214-
type = "sheet_2d",
215-
frames_w = 1,
216-
frames_h = 4,
217-
frame_length = 1.0,
218-
},
215+
inventory_image = "testnodes_anim.png^[invert:rgb^[verticalframe:4:0",
216+
wield_image = animated_image,
219217
})
220218

221219
core.register_craftitem("testitems:wield_image_animation_overlay", {
222220
description = S("Wield Animated Test Item With Overlay").."\n"..
223221
S("Looks like the animated Test Item With Overlay, "..
224222
"but only animated for the wield mesh."),
225-
inventory_image= "testnodes_anim.png^[invert:rgb^[verticalframe:4:0",
226-
inventory_overlay= "testitems_animation_overlay.png^[verticalframe:4:0",
227-
wield_image= "testnodes_anim.png^[invert:rgb",
228-
wield_overlay="testitems_animation_overlay.png",
229-
wield_image_animation = {
230-
type = "sheet_2d",
231-
frames_w = 1,
232-
frames_h = 4,
233-
frame_length = 1.0,
234-
},
235-
wield_overlay_animation = {
236-
type = "sheet_2d",
237-
frames_w = 1,
238-
frames_h = 4,
239-
frame_length = 1.0,
240-
},
223+
inventory_image = "testnodes_anim.png^[invert:rgb^[verticalframe:4:0",
224+
inventory_overlay = "testitems_animation_overlay.png^[verticalframe:4:0",
225+
wield_image = animated_image,
226+
wield_overlay = animated_overlay,
241227
color = "#ff0000",
242228
})

src/client/item_visuals_manager.cpp

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ ItemVisualsManager::ItemVisuals *ItemVisualsManager::createItemVisuals( const It
2424
IItemDefManager *idef = client->idef();
2525

2626
const ItemDefinition &def = item.getDefinition(idef);
27-
std::string inventory_image = item.getInventoryImage(idef);
28-
std::string inventory_overlay = item.getInventoryOverlay(idef);
29-
const TileAnimationParams &inventory_image_animation = item.getInventoryImageAnimation(idef);
30-
const TileAnimationParams &inventory_overlay_animation = item.getInventoryOverlayAnimation(idef);
27+
ItemImageDef inventory_image = item.getInventoryImage(idef);
28+
ItemImageDef inventory_overlay = item.getInventoryOverlay(idef);
3129

30+
// Key only consists of item name + image name,
31+
// because animation currently cannot be overridden by meta
3232
std::ostringstream os(def.name);
33-
if (!inventory_image.empty())
34-
os << "/" << inventory_image;
35-
if (!inventory_overlay.empty())
36-
os << ":" << inventory_overlay;
33+
if (!inventory_image.name.empty())
34+
os << "/" << inventory_image.name;
35+
if (!inventory_overlay.name.empty())
36+
os << ":" << inventory_overlay.name;
3737
std::string cache_key = os.str();
3838

3939

@@ -51,32 +51,37 @@ ItemVisualsManager::ItemVisuals *ItemVisualsManager::createItemVisuals( const It
5151
auto iv = std::make_unique<ItemVisuals>();
5252

5353
auto populate_texture_and_animation = [&](
54-
const std::string &image_name, const TileAnimationParams &animation,
55-
video::ITexture *&texture,
54+
const ItemImageDef &image, video::ITexture *&texture,
5655
std::unique_ptr<ItemVisuals::OwnedAnimationInfo> &owned_animation)
5756
{
58-
texture = nullptr;
59-
if (!image_name.empty()) {
60-
texture = tsrc->getTexture(image_name);
61-
62-
// Get inventory texture frames
63-
if (animation.type != TileAnimationType::TAT_NONE && texture) {
64-
int frame_length_ms;
65-
owned_animation = std::make_unique<ItemVisuals::OwnedAnimationInfo>(
66-
createAnimationFrames(tsrc, image_name, animation, frame_length_ms),
67-
frame_length_ms);
68-
69-
// Set first frame
70-
texture = owned_animation->frames[0].texture;
71-
}
57+
if (image.name.empty()) {
58+
texture = nullptr;
59+
return;
7260
}
61+
62+
if (image.animation.type == TileAnimationType::TAT_NONE) {
63+
texture = tsrc->getTexture(image.name);
64+
return;
65+
}
66+
67+
// Get inventory texture frames
68+
int frame_length_ms;
69+
owned_animation = std::make_unique<ItemVisuals::OwnedAnimationInfo>(
70+
createAnimationFrames(tsrc, image.name, image.animation,
71+
frame_length_ms), frame_length_ms);
72+
73+
// Set first frame
74+
if (!owned_animation || !owned_animation->frames.empty())
75+
texture = owned_animation->frames[0].texture;
76+
else
77+
texture = nullptr;
7378
};
7479

75-
populate_texture_and_animation(inventory_image, inventory_image_animation,
76-
iv->inventory_texture, iv->inventory_animation);
80+
populate_texture_and_animation(inventory_image, iv->inventory_texture,
81+
iv->inventory_animation);
7782

78-
populate_texture_and_animation(inventory_overlay, inventory_overlay_animation,
79-
iv->inventory_overlay_texture, iv->inventory_overlay_animation);
83+
populate_texture_and_animation(inventory_overlay, iv->inventory_overlay_texture,
84+
iv->inventory_overlay_animation);
8085

8186
createItemMesh(client, def, iv->inventory_texture,
8287
iv->inventory_animation ? &(iv->inventory_animation->info) : nullptr,

src/client/wieldmesh.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -411,41 +411,41 @@ void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client, bool che
411411
m_buffer_info.clear();
412412
m_base_color = item_visuals->getItemstackColor(item, client);
413413

414-
const std::string wield_image = item.getWieldImage(idef);
415-
const std::string wield_overlay = item.getWieldOverlay(idef);
414+
const ItemImageDef wield_image = item.getWieldImage(idef);
415+
const ItemImageDef wield_overlay = item.getWieldOverlay(idef);
416416
const v3f wield_scale = item.getWieldScale(idef);
417-
const TileAnimationParams wield_image_animation = item.getWieldImageAnimation(idef);
418-
const TileAnimationParams wield_overlay_animation = item.getWieldOverlayAnimation(idef);
419417

420418
// If wield_image needs to be checked and is defined, it overrides everything else
421-
if (!wield_image.empty() && check_wield_image) {
422-
video::ITexture *wield_texture = tsrc->getTexture(wield_image);
423-
video::ITexture *wield_overlay_texture = wield_image.empty() ?
424-
nullptr : tsrc->getTexture(wield_overlay);
419+
if (!wield_image.name.empty() && check_wield_image) {
420+
video::ITexture *wield_texture;
421+
video::ITexture *wield_overlay_texture = nullptr;
425422

426-
// Animation
427423
int frame_length_ms;
428424
m_wield_image_frames = std::vector<FrameSpec>(createAnimationFrames(
429-
tsrc, wield_image, wield_image_animation, frame_length_ms));
425+
tsrc, wield_image.name, wield_image.animation, frame_length_ms));
430426
if (m_wield_image_frames.empty()) {
431427
m_buffer_info.emplace_back();
428+
wield_texture = tsrc->getTexture(wield_image.name);
432429
} else {
433430
m_buffer_info.emplace_back(&m_wield_image_frames, frame_length_ms);
434431
wield_texture = m_wield_image_frames[0].texture;
435432
}
436433

437-
// Overlay animation
438-
int overlay_frame_length_ms;
439-
m_wield_overlay_frames = std::vector<FrameSpec>(createAnimationFrames(
440-
tsrc, wield_overlay, wield_overlay_animation, overlay_frame_length_ms));
441-
if (m_wield_overlay_frames.empty()) {
442-
// overlay is white, if present
443-
m_buffer_info.emplace_back(true, video::SColor(0xFFFFFFFF));
444-
} else {
445-
m_buffer_info.emplace_back(
446-
&m_wield_overlay_frames, overlay_frame_length_ms,
447-
true, video::SColor(0xFFFFFFFF));
448-
wield_overlay_texture = m_wield_overlay_frames[0].texture;
434+
// Overlay
435+
if (!wield_overlay.name.empty()) {
436+
int overlay_frame_length_ms;
437+
m_wield_overlay_frames = std::vector<FrameSpec>(createAnimationFrames(
438+
tsrc, wield_overlay.name, wield_overlay.animation, overlay_frame_length_ms));
439+
if (m_wield_overlay_frames.empty()) {
440+
// overlay is white, if present
441+
m_buffer_info.emplace_back(true, video::SColor(0xFFFFFFFF));
442+
wield_overlay_texture = tsrc->getTexture(wield_overlay.name);
443+
} else {
444+
m_buffer_info.emplace_back(
445+
&m_wield_overlay_frames, overlay_frame_length_ms,
446+
true, video::SColor(0xFFFFFFFF));
447+
wield_overlay_texture = m_wield_overlay_frames[0].texture;
448+
}
449449
}
450450

451451
setExtruded(wield_texture, wield_overlay_texture, wield_scale);

src/inventory.cpp

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -256,24 +256,45 @@ std::string ItemStack::getShortDescription(const IItemDefManager *itemdef) const
256256
return desc;
257257
}
258258

259-
#define image_get_function(NAME, FIELD) \
260-
std::string ItemStack::get##NAME(const IItemDefManager *itemdef) const \
261-
{ \
262-
std::string texture = metadata.getString(#FIELD); \
263-
if (texture.empty()) \
264-
texture = getDefinition(itemdef).FIELD; \
265-
return texture; \
266-
} \
267-
const TileAnimationParams ItemStack::get##NAME##Animation(const IItemDefManager *itemdef) const \
268-
{ \
269-
return getDefinition(itemdef).FIELD##_animation; \
270-
}
271-
272-
image_get_function(InventoryImage, inventory_image)
273-
image_get_function(InventoryOverlay, inventory_overlay)
274-
image_get_function(WieldImage, wield_image)
275-
image_get_function(WieldOverlay, wield_overlay)
276-
#undef image_get_function
259+
ItemImageDef ItemStack::getInventoryImage(const IItemDefManager *itemdef) const
260+
{
261+
ItemImageDef image = getDefinition(itemdef).inventory_image;
262+
std::string meta_image = metadata.getString("inventory_image");
263+
if (!meta_image.empty())
264+
image.name = meta_image;
265+
266+
return image;
267+
}
268+
269+
ItemImageDef ItemStack::getInventoryOverlay(const IItemDefManager *itemdef) const
270+
{
271+
ItemImageDef image = getDefinition(itemdef).inventory_overlay;
272+
std::string meta_image = metadata.getString("inventory_overlay");
273+
if (!meta_image.empty())
274+
image.name = meta_image;
275+
276+
return image;
277+
}
278+
279+
ItemImageDef ItemStack::getWieldImage(const IItemDefManager *itemdef) const
280+
{
281+
ItemImageDef image = getDefinition(itemdef).wield_image;
282+
std::string meta_image = metadata.getString("wield_image");
283+
if (!meta_image.empty())
284+
image.name = meta_image;
285+
286+
return image;
287+
}
288+
289+
ItemImageDef ItemStack::getWieldOverlay(const IItemDefManager *itemdef) const
290+
{
291+
ItemImageDef image = getDefinition(itemdef).wield_overlay;
292+
std::string meta_image = metadata.getString("wield_overlay");
293+
if (!meta_image.empty())
294+
image.name = meta_image;
295+
296+
return image;
297+
}
277298

278299
v3f ItemStack::getWieldScale(const IItemDefManager *itemdef) const
279300
{

0 commit comments

Comments
 (0)