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

View File

@ -51,6 +51,10 @@ let inApiCall = false;
let lastServerResponseTime = 0; let lastServerResponseTime = 0;
export let lastExpression = {}; export let lastExpression = {};
function isTalkingHeadEnabled() {
return extension_settings.expressions.talkinghead && !extension_settings.expressions.local;
}
function isVisualNovelMode() { function isVisualNovelMode() {
return Boolean(!isMobile() && power_user.waifuMode && getContext().groupId); return Boolean(!isMobile() && power_user.waifuMode && getContext().groupId);
} }
@ -381,7 +385,7 @@ function onExpressionsShowDefaultInput() {
} }
} }
async function unloadLiveChar() { async function unloadTalkingHead() {
if (!modules.includes('talkinghead')) { if (!modules.includes('talkinghead')) {
console.debug('talkinghead module is disabled'); console.debug('talkinghead module is disabled');
return; return;
@ -400,7 +404,7 @@ async function unloadLiveChar() {
} }
} }
async function loadLiveChar() { async function loadTalkingHead() {
if (!modules.includes('talkinghead')) { if (!modules.includes('talkinghead')) {
console.debug('talkinghead module is disabled'); console.debug('talkinghead module is disabled');
return; return;
@ -450,7 +454,7 @@ function handleImageChange() {
return; return;
} }
if (extension_settings.expressions.talkinghead && !extension_settings.expressions.local) { if (isTalkingHeadEnabled()) {
// Method get IP of endpoint // Method get IP of endpoint
const talkingheadResultFeedSrc = `${getApiUrl()}/api/talkinghead/result_feed`; const talkingheadResultFeedSrc = `${getApiUrl()}/api/talkinghead/result_feed`;
$('#expression-holder').css({ display: '' }); $('#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(); let spriteFolderName = getSpriteFolderName();
try { try {
await validateImages(spriteFolderName); await validateImages(spriteFolderName);
let talkingheadObj = spriteCache[spriteFolderName].find(obj => obj.label === 'talkinghead'); 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) { if (talkingheadPath != null) {
//console.log("talkingheadPath_f " + talkingheadPath_f);
return true; return true;
} else { } else {
//console.log("talkingheadPath_f is null"); await unloadTalkingHead();
unloadLiveChar();
return false; return false;
} }
} catch (err) { } catch (err) {
@ -647,22 +653,22 @@ function getSpriteFolderName(characterMessage = null, characterName = null) {
return spriteFolderName; return spriteFolderName;
} }
function setTalkingHeadState(switch_var) { function setTalkingHeadState(newState) {
extension_settings.expressions.talkinghead = switch_var; // Store setting extension_settings.expressions.talkinghead = newState; // Store setting
saveSettingsDebounced(); saveSettingsDebounced();
if (extension_settings.expressions.local) { if (extension_settings.expressions.local) {
return; return;
} }
talkingHeadCheck().then(result => { isTalkingHeadAvailable().then(result => {
if (result) { if (result) {
//console.log("talkinghead exists!"); //console.log("talkinghead exists!");
if (extension_settings.expressions.talkinghead) { if (extension_settings.expressions.talkinghead) {
loadLiveChar(); loadTalkingHead();
} else { } else {
unloadLiveChar(); unloadTalkingHead();
} }
handleImageChange(); // Change image as needed handleImageChange(); // Change image as needed
@ -732,22 +738,29 @@ async function setSpriteSlashCommand(_, spriteId) {
spriteId = spriteId.trim().toLowerCase(); 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 currentLastMessage = getLastCharacterMessage();
const spriteFolderName = getSpriteFolderName(currentLastMessage, currentLastMessage.name); const spriteFolderName = getSpriteFolderName(currentLastMessage, currentLastMessage.name);
await validateImages(spriteFolderName); let label = spriteId;
if (!isTalkingHeadEnabled()) {
await validateImages(spriteFolderName);
// Fuzzy search for sprite // Fuzzy search for sprite
const fuse = new Fuse(spriteCache[spriteFolderName], { keys: ['label'] }); const fuse = new Fuse(spriteCache[spriteFolderName], { keys: ['label'] });
const results = fuse.search(spriteId); const results = fuse.search(spriteId);
const spriteItem = results[0]?.item; const spriteItem = results[0]?.item;
if (!spriteItem) { if (!spriteItem) {
console.log('No sprite found for search term ' + spriteId); console.log('No sprite found for search term ' + spriteId);
return; return;
}
label = spriteItem.label;
} }
const vnMode = isVisualNovelMode(); 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 // Set the talkinghead emotion to the specified expression
// TODO: For now, talkinghead emote only supported when VN mode is off; see also updateVisualNovelMode. // TODO: For now, talkinghead emote only supported when VN mode is off; see also updateVisualNovelMode.
try { try {
let result = await talkingHeadCheck(); let result = await isTalkingHeadAvailable();
if (result) { if (result) {
const url = new URL(getApiUrl()); const url = new URL(getApiUrl());
url.pathname = '/api/talkinghead/set_emotion'; url.pathname = '/api/talkinghead/set_emotion';
@ -1261,6 +1274,11 @@ async function onClickExpressionUpload(event) {
// Reset the input // Reset the input
e.target.form.reset(); 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') $('#expression_upload')