Merge pull request #1639 from Technologicat/talkinghead-nospritecheck

Talkinghead nospritecheck
This commit is contained in:
Cohee 2024-01-07 21:24:05 +02:00 committed by GitHub
commit ec86466d11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 24 deletions

View File

@ -51,6 +51,10 @@ let inApiCall = false;
let lastServerResponseTime = 0;
export let lastExpression = {};
function isTalkingHeadEnabled() {
return extension_settings.expressions.talkinghead && !extension_settings.expressions.local;
}
function isVisualNovelMode() {
return Boolean(!isMobile() && power_user.waifuMode && getContext().groupId);
}
@ -381,7 +385,7 @@ function onExpressionsShowDefaultInput() {
}
}
async function unloadLiveChar() {
async function unloadTalkingHead() {
if (!modules.includes('talkinghead')) {
console.debug('talkinghead module is disabled');
return;
@ -400,7 +404,7 @@ async function unloadLiveChar() {
}
}
async function loadLiveChar() {
async function loadTalkingHead() {
if (!modules.includes('talkinghead')) {
console.debug('talkinghead module is disabled');
return;
@ -450,7 +454,7 @@ function handleImageChange() {
return;
}
if (extension_settings.expressions.talkinghead && !extension_settings.expressions.local) {
if (isTalkingHeadEnabled()) {
// Method get IP of endpoint
const talkingheadResultFeedSrc = `${getApiUrl()}/api/talkinghead/result_feed`;
$('#expression-holder').css({ display: '' });
@ -611,21 +615,23 @@ async function moduleWorker() {
}
}
async function talkingHeadCheck() {
/**
* Checks whether the current character has a talkinghead image available.
* @returns {Promise<boolean>} True if the character has a talkinghead image available, false otherwise.
*/
async function isTalkingHeadAvailable() {
let spriteFolderName = getSpriteFolderName();
try {
await validateImages(spriteFolderName);
let talkingheadObj = spriteCache[spriteFolderName].find(obj => obj.label === 'talkinghead');
let talkingheadPath_f = talkingheadObj ? talkingheadObj.path : null;
let talkingheadPath = talkingheadObj ? talkingheadObj.path : null;
if (talkingheadPath_f != null) {
//console.log("talkingheadPath_f " + talkingheadPath_f);
if (talkingheadPath != null) {
return true;
} else {
//console.log("talkingheadPath_f is null");
unloadLiveChar();
await unloadTalkingHead();
return false;
}
} catch (err) {
@ -647,22 +653,22 @@ function getSpriteFolderName(characterMessage = null, characterName = null) {
return spriteFolderName;
}
function setTalkingHeadState(switch_var) {
extension_settings.expressions.talkinghead = switch_var; // Store setting
function setTalkingHeadState(newState) {
extension_settings.expressions.talkinghead = newState; // Store setting
saveSettingsDebounced();
if (extension_settings.expressions.local) {
return;
}
talkingHeadCheck().then(result => {
isTalkingHeadAvailable().then(result => {
if (result) {
//console.log("talkinghead exists!");
if (extension_settings.expressions.talkinghead) {
loadLiveChar();
loadTalkingHead();
} else {
unloadLiveChar();
unloadTalkingHead();
}
handleImageChange(); // Change image as needed
@ -732,22 +738,29 @@ async function setSpriteSlashCommand(_, spriteId) {
spriteId = spriteId.trim().toLowerCase();
// In talkinghead mode, don't check for the existence of the sprite
// (emotion names are the same as for sprites, but it only needs "talkinghead.png").
const currentLastMessage = getLastCharacterMessage();
const spriteFolderName = getSpriteFolderName(currentLastMessage, currentLastMessage.name);
await validateImages(spriteFolderName);
let label = spriteId;
if (!isTalkingHeadEnabled()) {
await validateImages(spriteFolderName);
// Fuzzy search for sprite
const fuse = new Fuse(spriteCache[spriteFolderName], { keys: ['label'] });
const results = fuse.search(spriteId);
const spriteItem = results[0]?.item;
// Fuzzy search for sprite
const fuse = new Fuse(spriteCache[spriteFolderName], { keys: ['label'] });
const results = fuse.search(spriteId);
const spriteItem = results[0]?.item;
if (!spriteItem) {
console.log('No sprite found for search term ' + spriteId);
return;
if (!spriteItem) {
console.log('No sprite found for search term ' + spriteId);
return;
}
label = spriteItem.label;
}
const vnMode = isVisualNovelMode();
await sendExpressionCall(spriteFolderName, spriteItem.label, true, vnMode);
await sendExpressionCall(spriteFolderName, label, true, vnMode);
}
/**
@ -1112,7 +1125,7 @@ async function setExpression(character, expression, force) {
// Set the talkinghead emotion to the specified expression
// TODO: For now, talkinghead emote only supported when VN mode is off; see also updateVisualNovelMode.
try {
let result = await talkingHeadCheck();
let result = await isTalkingHeadAvailable();
if (result) {
const url = new URL(getApiUrl());
url.pathname = '/api/talkinghead/set_emotion';
@ -1261,6 +1274,11 @@ async function onClickExpressionUpload(event) {
// Reset the input
e.target.form.reset();
// In talkinghead mode, when a new talkinghead image is uploaded, refresh the live char.
if (extension_settings.expressions.talkinghead && !extension_settings.expressions.local && id === 'talkinghead') {
await loadTalkingHead();
}
};
$('#expression_upload')