mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-22 17:18:39 +00:00
Add wear bar color API (#13328)
--------- Co-authored-by: Muhammad Rifqi Priyo Susanto <muhammadrifqipriyosusanto@gmail.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com> Co-authored-by: grorp <gregor.parzefall@posteo.de>
This commit is contained in:
parent
e10d8080ba
commit
176e674a51
19 changed files with 598 additions and 26 deletions
|
@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "server/player_sao.h"
|
||||
#include "util/pointedthing.h"
|
||||
#include "debug.h" // For FATAL_ERROR
|
||||
#include <SColor.h>
|
||||
#include <json/json.h>
|
||||
|
||||
struct EnumString es_TileAnimationType[] =
|
||||
|
@ -94,6 +95,15 @@ void read_item_definition(lua_State* L, int index,
|
|||
def.tool_capabilities = new ToolCapabilities(
|
||||
read_tool_capabilities(L, -1));
|
||||
}
|
||||
lua_getfield(L, index, "wear_color");
|
||||
if (lua_istable(L, -1)) {
|
||||
def.wear_bar_params = read_wear_bar_params(L, -1);
|
||||
} else if (lua_isstring(L, -1)) {
|
||||
video::SColor color;
|
||||
read_color(L, -1, &color);
|
||||
def.wear_bar_params = WearBarParams({{0.0, color}},
|
||||
WearBarParams::BLEND_MODE_CONSTANT);
|
||||
}
|
||||
|
||||
// If name is "" (hand), ensure there are ToolCapabilities
|
||||
// because it will be looked up there whenever any other item has
|
||||
|
@ -213,6 +223,10 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
|
|||
push_tool_capabilities(L, *i.tool_capabilities);
|
||||
lua_setfield(L, -2, "tool_capabilities");
|
||||
}
|
||||
if (i.wear_bar_params.has_value()) {
|
||||
push_wear_bar_params(L, *i.wear_bar_params);
|
||||
lua_setfield(L, -2, "wear_color");
|
||||
}
|
||||
push_groups(L, i.groups);
|
||||
lua_setfield(L, -2, "groups");
|
||||
push_simplesoundspec(L, i.sound_place);
|
||||
|
@ -1454,6 +1468,22 @@ void push_tool_capabilities(lua_State *L,
|
|||
lua_setfield(L, -2, "damage_groups");
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void push_wear_bar_params(lua_State *L,
|
||||
const WearBarParams ¶ms)
|
||||
{
|
||||
lua_newtable(L);
|
||||
setstringfield(L, -1, "blend", WearBarParams::es_BlendMode[params.blend].str);
|
||||
|
||||
lua_newtable(L);
|
||||
for (const std::pair<const f32, const video::SColor> item: params.colorStops) {
|
||||
lua_pushnumber(L, item.first); // key
|
||||
push_ARGB8(L, item.second);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
lua_setfield(L, -2, "color_stops");
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void push_inventory_list(lua_State *L, const InventoryList &invlist)
|
||||
{
|
||||
|
@ -1732,6 +1762,54 @@ void push_pointabilities(lua_State *L, const Pointabilities &pointabilities)
|
|||
}
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
WearBarParams read_wear_bar_params(
|
||||
lua_State *L, int stack_idx)
|
||||
{
|
||||
if (lua_isstring(L, stack_idx)) {
|
||||
video::SColor color;
|
||||
read_color(L, stack_idx, &color);
|
||||
return WearBarParams(color);
|
||||
}
|
||||
|
||||
if (!lua_istable(L, stack_idx))
|
||||
throw LuaError("Expected wear bar color table or colorstring");
|
||||
|
||||
lua_getfield(L, stack_idx, "color_stops");
|
||||
if (!check_field_or_nil(L, -1, LUA_TTABLE, "color_stops"))
|
||||
throw LuaError("color_stops must be a table");
|
||||
|
||||
std::map<f32, video::SColor> colorStops;
|
||||
// color stops table is on the stack
|
||||
int table_values = lua_gettop(L);
|
||||
lua_pushnil(L);
|
||||
while (lua_next(L, table_values) != 0) {
|
||||
// key at index -2 and value at index -1 within table_values
|
||||
f32 point = luaL_checknumber(L, -2);
|
||||
if (point < 0 || point > 1)
|
||||
throw LuaError("Wear bar color stop key out of range");
|
||||
video::SColor color;
|
||||
read_color(L, -1, &color);
|
||||
colorStops.emplace(point, color);
|
||||
|
||||
// removes value, keeps key for next iteration
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_pop(L, 1); // pop color stops table
|
||||
|
||||
auto blend = WearBarParams::BLEND_MODE_CONSTANT;
|
||||
lua_getfield(L, stack_idx, "blend");
|
||||
if (check_field_or_nil(L, -1, LUA_TSTRING, "blend")) {
|
||||
int blendInt;
|
||||
if (!string_to_enum(WearBarParams::es_BlendMode, blendInt, std::string(lua_tostring(L, -1))))
|
||||
throw LuaError("Invalid wear bar color blend mode");
|
||||
blend = static_cast<WearBarParams::BlendMode>(blendInt);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
||||
return WearBarParams(colorStops, blend);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void push_dig_params(lua_State *L,const DigParams ¶ms)
|
||||
{
|
||||
|
|
|
@ -116,6 +116,9 @@ void push_pointabilities (lua_State *L, const Pointabilities
|
|||
ToolCapabilities read_tool_capabilities (lua_State *L, int table);
|
||||
void push_tool_capabilities (lua_State *L,
|
||||
const ToolCapabilities &prop);
|
||||
WearBarParams read_wear_bar_params (lua_State *L, int table);
|
||||
void push_wear_bar_params (lua_State *L,
|
||||
const WearBarParams &prop);
|
||||
|
||||
void read_item_definition (lua_State *L, int index, const ItemDefinition &default_def,
|
||||
ItemDefinition &def);
|
||||
|
|
|
@ -376,6 +376,22 @@ int LuaItemStack::l_add_wear_by_uses(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
// get_wear_bar_params(self) -> table
|
||||
// Returns the effective wear bar parameters.
|
||||
// Returns nil if this item has none associated.
|
||||
int LuaItemStack::l_get_wear_bar_params(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
LuaItemStack *o = checkObject<LuaItemStack>(L, 1);
|
||||
ItemStack &item = o->m_stack;
|
||||
auto params = item.getWearBarParams(getGameDef(L)->idef());
|
||||
if (params.has_value()) {
|
||||
push_wear_bar_params(L, *params);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// add_item(self, itemstack or itemstring or table or nil) -> itemstack
|
||||
// Returns leftover item stack
|
||||
int LuaItemStack::l_add_item(lua_State *L)
|
||||
|
@ -551,6 +567,7 @@ const luaL_Reg LuaItemStack::methods[] = {
|
|||
luamethod(LuaItemStack, get_tool_capabilities),
|
||||
luamethod(LuaItemStack, add_wear),
|
||||
luamethod(LuaItemStack, add_wear_by_uses),
|
||||
luamethod(LuaItemStack, get_wear_bar_params),
|
||||
luamethod(LuaItemStack, add_item),
|
||||
luamethod(LuaItemStack, item_fits),
|
||||
luamethod(LuaItemStack, take_item),
|
||||
|
|
|
@ -125,6 +125,11 @@ private:
|
|||
// Returns true if the item is (or was) a tool.
|
||||
static int l_add_wear_by_uses(lua_State *L);
|
||||
|
||||
// get_wear_bar_params(self) -> table
|
||||
// Returns the effective wear bar parameters.
|
||||
// Returns nil if this item has none associated.
|
||||
static int l_get_wear_bar_params(lua_State *L);
|
||||
|
||||
// add_item(self, itemstack or itemstring or table or nil) -> itemstack
|
||||
// Returns leftover item stack
|
||||
static int l_add_item(lua_State *L);
|
||||
|
|
|
@ -22,6 +22,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "lua_api/l_itemstackmeta.h"
|
||||
#include "lua_api/l_internal.h"
|
||||
#include "common/c_content.h"
|
||||
#include "common/c_converter.h"
|
||||
#include "tool.h"
|
||||
|
||||
/*
|
||||
ItemStackMetaRef
|
||||
|
@ -58,6 +60,20 @@ int ItemStackMetaRef::l_set_tool_capabilities(lua_State *L)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ItemStackMetaRef::l_set_wear_bar_params(lua_State *L)
|
||||
{
|
||||
ItemStackMetaRef *metaref = checkObject<ItemStackMetaRef>(L, 1);
|
||||
if (lua_isnoneornil(L, 2)) {
|
||||
metaref->clearWearBarParams();
|
||||
} else if (lua_istable(L, 2) || lua_isstring(L, 2)) {
|
||||
metaref->setWearBarParams(read_wear_bar_params(L, 2));
|
||||
} else {
|
||||
luaL_typerror(L, 2, "table, ColorString, or nil");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ItemStackMetaRef::ItemStackMetaRef(LuaItemStack *istack): istack(istack)
|
||||
{
|
||||
istack->grab();
|
||||
|
@ -102,5 +118,6 @@ const luaL_Reg ItemStackMetaRef::methods[] = {
|
|||
luamethod(MetaDataRef, from_table),
|
||||
luamethod(MetaDataRef, equals),
|
||||
luamethod(ItemStackMetaRef, set_tool_capabilities),
|
||||
luamethod(ItemStackMetaRef, set_wear_bar_params),
|
||||
{0,0}
|
||||
};
|
||||
|
|
|
@ -49,8 +49,19 @@ private:
|
|||
istack->getItem().metadata.clearToolCapabilities();
|
||||
}
|
||||
|
||||
void setWearBarParams(const WearBarParams ¶ms)
|
||||
{
|
||||
istack->getItem().metadata.setWearBarParams(params);
|
||||
}
|
||||
|
||||
void clearWearBarParams()
|
||||
{
|
||||
istack->getItem().metadata.clearWearBarParams();
|
||||
}
|
||||
|
||||
// Exported functions
|
||||
static int l_set_tool_capabilities(lua_State *L);
|
||||
static int l_set_wear_bar_params(lua_State *L);
|
||||
public:
|
||||
// takes a reference
|
||||
ItemStackMetaRef(LuaItemStack *istack);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue