1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +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:
techno-sam 2024-02-02 12:21:00 -08:00 committed by GitHub
parent e10d8080ba
commit 176e674a51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 598 additions and 26 deletions

View file

@ -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 &params)
{
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 &params)
{