diff --git a/src/logic/scripting/lua/libs/lib__skeleton.cpp b/src/logic/scripting/lua/libs/lib__skeleton.cpp index 2cb9c44e..208c3483 100644 --- a/src/logic/scripting/lua/libs/lib__skeleton.cpp +++ b/src/logic/scripting/lua/libs/lib__skeleton.cpp @@ -13,25 +13,30 @@ static int index_range_check( return static_cast(index); } -static int l_get_model(lua::State* L) { +static rigging::Skeleton* get_skeleton(lua::State* L) { if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - auto* rigConfig = skeleton.config; - auto index = index_range_check(skeleton, lua::tointeger(L, 2)); - const auto& modelOverride = skeleton.modelOverrides[index]; + return &entity->getSkeleton(); + } + return nullptr; +} + +static int l_get_model(lua::State* L) { + if (auto skeleton = get_skeleton(L)) { + auto& rigConfig = *skeleton->config; + auto index = index_range_check(*skeleton, lua::tointeger(L, 2)); + const auto& modelOverride = skeleton->modelOverrides[index]; if (!modelOverride.model) { return lua::pushstring(L, modelOverride.name); } - return lua::pushstring(L, rigConfig->getBones()[index]->model.name); + return lua::pushstring(L, rigConfig.getBones()[index]->model.name); } return 0; } static int l_set_model(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - auto index = index_range_check(skeleton, lua::tointeger(L, 2)); - auto& modelOverride = skeleton.modelOverrides[index]; + if (auto skeleton = get_skeleton(L)) { + auto index = index_range_check(*skeleton, lua::tointeger(L, 2)); + auto& modelOverride = skeleton->modelOverrides[index]; if (lua::isnoneornil(L, 3)) { modelOverride = {"", nullptr, true}; } else { @@ -42,28 +47,25 @@ static int l_set_model(lua::State* L) { } static int l_get_matrix(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - auto index = index_range_check(skeleton, lua::tointeger(L, 2)); - return lua::pushmat4(L, skeleton.pose.matrices[index]); + if (auto skeleton = get_skeleton(L)) { + auto index = index_range_check(*skeleton, lua::tointeger(L, 2)); + return lua::pushmat4(L, skeleton->pose.matrices[index]); } return 0; } static int l_set_matrix(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - auto index = index_range_check(skeleton, lua::tointeger(L, 2)); - skeleton.pose.matrices[index] = lua::tomat4(L, 3); + if (auto skeleton = get_skeleton(L)) { + auto index = index_range_check(*skeleton, lua::tointeger(L, 2)); + skeleton->pose.matrices[index] = lua::tomat4(L, 3); } return 0; } static int l_get_texture(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - const auto& found = skeleton.textures.find(lua::require_string(L, 2)); - if (found != skeleton.textures.end()) { + if (auto skeleton = get_skeleton(L)) { + const auto& found = skeleton->textures.find(lua::require_string(L, 2)); + if (found != skeleton->textures.end()) { return lua::pushstring(L, found->second); } } @@ -71,18 +73,16 @@ static int l_get_texture(lua::State* L) { } static int l_set_texture(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - skeleton.textures[lua::require_string(L, 2)] = + if (auto skeleton = get_skeleton(L)) { + skeleton->textures[lua::require_string(L, 2)] = lua::require_string(L, 3); } return 0; } static int l_index(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - if (auto bone = skeleton.config->find(lua::require_string(L, 2))) { + if (auto skeleton= get_skeleton(L)) { + if (auto bone = skeleton->config->find(lua::require_string(L, 2))) { return lua::pushinteger(L, bone->getIndex()); } } @@ -90,58 +90,52 @@ static int l_index(lua::State* L) { } static int l_is_visible(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); + if (auto skeleton = get_skeleton(L)) { if (!lua::isnoneornil(L, 2)) { - auto index = index_range_check(skeleton, lua::tointeger(L, 2)); - return lua::pushboolean(L, skeleton.flags.at(index).visible); + auto index = index_range_check(*skeleton, lua::tointeger(L, 2)); + return lua::pushboolean(L, skeleton->flags.at(index).visible); } - return lua::pushboolean(L, skeleton.visible); + return lua::pushboolean(L, skeleton->visible); } return 0; } static int l_set_visible(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); + if (auto skeleton = get_skeleton(L)) { if (!lua::isnoneornil(L, 3)) { - auto index = index_range_check(skeleton, lua::tointeger(L, 2)); - skeleton.flags.at(index).visible = lua::toboolean(L, 3); + auto index = index_range_check(*skeleton, lua::tointeger(L, 2)); + skeleton->flags.at(index).visible = lua::toboolean(L, 3); } else { - skeleton.visible = lua::toboolean(L, 2); + skeleton->visible = lua::toboolean(L, 2); } } return 0; } static int l_get_color(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - return lua::pushvec(L, skeleton.tint); + if (auto skeleton = get_skeleton(L)) { + return lua::pushvec(L, skeleton->tint); } return 0; } static int l_set_color(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - skeleton.tint = lua::tovec3(L, 2); + if (auto skeleton = get_skeleton(L)) { + skeleton->tint = lua::tovec3(L, 2); } return 0; } static int l_is_interpolated(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - return lua::pushboolean(L, skeleton.interpolation.isEnabled()); + if (auto skeleton = get_skeleton(L)) { + return lua::pushboolean(L, skeleton->interpolation.isEnabled()); } return 0; } static int l_set_interpolated(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& skeleton = entity->getSkeleton(); - skeleton.interpolation.setEnabled(lua::toboolean(L, 2)); + if (auto skeleton = get_skeleton(L)) { + skeleton->interpolation.setEnabled(lua::toboolean(L, 2)); } return 0; } @@ -160,4 +154,5 @@ const luaL_Reg skeletonlib[] = { {"set_color", lua::wrap}, {"is_interpolated", lua::wrap}, {"set_interpolated", lua::wrap}, - {NULL, NULL}}; + {NULL, NULL} +};