From 9f44a72d768e12602775188749b284bfb06131c0 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 3 Aug 2023 19:05:21 +0900 Subject: [PATCH 1/3] Setting Tracking Live2d --- .../scripts/extensions/expressions/index.js | 81 +++++++++---------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/public/scripts/extensions/expressions/index.js b/public/scripts/extensions/expressions/index.js index 67cbfd0e8..4213516af 100644 --- a/public/scripts/extensions/expressions/index.js +++ b/public/scripts/extensions/expressions/index.js @@ -45,7 +45,6 @@ let lastCharacter = undefined; let lastMessage = null; let spriteCache = {}; let inApiCall = false; -let live2d_var = false; let previousSrc = null; @@ -448,8 +447,7 @@ async function loadLiveChar() { } } - -function handleImageChange(isChecked) { +function handleImageChange() { const imgElement = document.querySelector('img#expression-image.expression'); if (!imgElement) { @@ -457,31 +455,30 @@ function handleImageChange(isChecked) { return; } - if (isChecked) { + if (extension_settings.expressions.live2d) { + previousSrc = imgElement.src; // Method get IP of endpoint - if (imgElement.src !== getApiUrl() + '/api/live2d/result_feed') { - const expressionListItemElement = document.querySelector('#live2d'); - const expressionImageElement = expressionListItemElement.querySelector('.expression_list_image'); - const newSrc = expressionImageElement.src; + const live2dResultFeedSrc = `${getApiUrl()}/api/live2d/result_feed`; + if (imgElement.src !== live2dResultFeedSrc) { + const expressionImageElement = document.querySelector('.expression_list_image'); - doExtrasFetch(newSrc, { - method: 'HEAD', - }) - .then(response => { - if (response.ok) { - imgElement.src = getApiUrl() + '/api/live2d/result_feed'; - } + if (expressionImageElement) { + doExtrasFetch(expressionImageElement.src, { + method: 'HEAD', }) - .catch(error => { - console.error(error); // Log the error if necessary - }); - } else if (previousSrc) { - imgElement.src = previousSrc; // Revert the src to its previous value + .then(response => { + if (response.ok) { + imgElement.src = live2dResultFeedSrc; + } + }) + .catch(error => { + console.error(error); // Log the error if necessary + }); + } } - } else if (previousSrc !== null) { - imgElement.src = previousSrc; // Revert the src to its previous value + } else { + setExpression(getContext().name2, "neutral", true); } - live2d_var = isChecked; } async function moduleWorker() { @@ -497,22 +494,16 @@ async function moduleWorker() { if (context.groupId !== lastCharacter && context.characterId !== lastCharacter) { removeExpression(); spriteCache = {}; - previousSrc = null; - //uncheck live image - let checkbox = document.getElementById('image_type_toggle'); - if (checkbox.checked) { - checkbox.click(); - } - //clear expression let imgElement = document.getElementById('expression-image'); imgElement.src = ""; - //Load new char - if (live2d_var) { - loadLiveChar(); + //set checkbox to global var + $('#image_type_toggle').prop('checked', extension_settings.expressions.live2d); + if(extension_settings.expressions.live2d == true){ + live2d(extension_settings.expressions.live2d); } } @@ -618,6 +609,14 @@ async function moduleWorker() { } } +function live2d(switch_var){ + extension_settings.expressions.live2d = switch_var; // Store setting + saveSettingsDebounced(); + + if (extension_settings.expressions.live2d) { loadLiveChar(); } // load char as needed + handleImageChange(switch_var); // Change image as needed +} + function getSpriteFolderName(message) { const context = getContext(); let avatarPath = ''; @@ -805,7 +804,7 @@ async function getExpressionsList() { } async function setExpression(character, expression, force) { - if (live2d_var == false) { + if (extension_settings.expressions.live2d == false) { console.debug('entered setExpressions'); await validateImages(character); @@ -915,7 +914,9 @@ async function setExpression(character, expression, force) { } document.getElementById("expression-holder").style.display = ''; - if (live2d_var == true) { + } else { + + if (extension_settings.expressions.live2d == true) { // Find the element with id="expression-image" and class="expression" const imgElement = document.querySelector('img#expression-image.expression'); //console.log("searching"); @@ -1234,13 +1235,8 @@ function setExpressionOverrideHtml(forceClear = false) { $(window).on("resize", updateVisualNovelModeDebounced); $('.expression_settings').hide(); - - $('#image_type_toggle').on('change', function () { - const isChecked = this.checked; - if (isChecked) { - loadLiveChar(); - } - handleImageChange(isChecked); + $('#image_type_toggle').on('click', function () { + live2d(this.checked); }); } @@ -1253,7 +1249,6 @@ function setExpressionOverrideHtml(forceClear = false) { moduleWorker(); dragElement($("#expression-holder")) eventSource.on(event_types.CHAT_CHANGED, () => { - //console.log("checked: " + live2d_var); setExpressionOverrideHtml(); if (isVisualNovelMode()) { From aebdd6cd42f1d8162a2bbddead9154c544c49415 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 3 Aug 2023 19:23:15 +0900 Subject: [PATCH 2/3] Fallback expression and tweak --- public/scripts/extensions/expressions/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/scripts/extensions/expressions/index.js b/public/scripts/extensions/expressions/index.js index 4213516af..ee9749c87 100644 --- a/public/scripts/extensions/expressions/index.js +++ b/public/scripts/extensions/expressions/index.js @@ -477,7 +477,8 @@ function handleImageChange() { } } } else { - setExpression(getContext().name2, "neutral", true); + imgElement.src = ""; //remove incase char doesnt have expressions + setExpression(getContext().name2, FALLBACK_EXPRESSION, true); } } From 4b7c837fe37193814bb31fdcbfc672326beaa530 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 3 Aug 2023 19:29:48 +0900 Subject: [PATCH 3/3] Removed unneeded IF --- public/scripts/extensions/expressions/index.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/public/scripts/extensions/expressions/index.js b/public/scripts/extensions/expressions/index.js index ee9749c87..f6c593065 100644 --- a/public/scripts/extensions/expressions/index.js +++ b/public/scripts/extensions/expressions/index.js @@ -449,7 +449,7 @@ async function loadLiveChar() { function handleImageChange() { const imgElement = document.querySelector('img#expression-image.expression'); - + if (!imgElement) { console.log("Cannot find addExpressionImage()"); return; @@ -504,7 +504,7 @@ async function moduleWorker() { //set checkbox to global var $('#image_type_toggle').prop('checked', extension_settings.expressions.live2d); if(extension_settings.expressions.live2d == true){ - live2d(extension_settings.expressions.live2d); + setLive2dState(extension_settings.expressions.live2d); } } @@ -610,7 +610,7 @@ async function moduleWorker() { } } -function live2d(switch_var){ +function setLive2dState(switch_var){ extension_settings.expressions.live2d = switch_var; // Store setting saveSettingsDebounced(); @@ -916,16 +916,13 @@ async function setExpression(character, expression, force) { document.getElementById("expression-holder").style.display = ''; } else { - - if (extension_settings.expressions.live2d == true) { // Find the element with id="expression-image" and class="expression" const imgElement = document.querySelector('img#expression-image.expression'); //console.log("searching"); if (imgElement) { console.log("setting value"); imgElement.src = getApiUrl() + '/api/live2d/result_feed'; - } - } + } } } @@ -1237,7 +1234,7 @@ function setExpressionOverrideHtml(forceClear = false) { $('.expression_settings').hide(); $('#image_type_toggle').on('click', function () { - live2d(this.checked); + setLive2dState(this.checked); }); }