Setting Tracking Live2d

This commit is contained in:
joe 2023-08-03 19:05:21 +09:00
parent f198f5eb6e
commit 9f44a72d76

View File

@ -45,7 +45,6 @@ let lastCharacter = undefined;
let lastMessage = null; let lastMessage = null;
let spriteCache = {}; let spriteCache = {};
let inApiCall = false; let inApiCall = false;
let live2d_var = false;
let previousSrc = null; let previousSrc = null;
@ -448,8 +447,7 @@ async function loadLiveChar() {
} }
} }
function handleImageChange() {
function handleImageChange(isChecked) {
const imgElement = document.querySelector('img#expression-image.expression'); const imgElement = document.querySelector('img#expression-image.expression');
if (!imgElement) { if (!imgElement) {
@ -457,31 +455,30 @@ function handleImageChange(isChecked) {
return; return;
} }
if (isChecked) { if (extension_settings.expressions.live2d) {
previousSrc = imgElement.src;
// Method get IP of endpoint // Method get IP of endpoint
if (imgElement.src !== getApiUrl() + '/api/live2d/result_feed') { const live2dResultFeedSrc = `${getApiUrl()}/api/live2d/result_feed`;
const expressionListItemElement = document.querySelector('#live2d'); if (imgElement.src !== live2dResultFeedSrc) {
const expressionImageElement = expressionListItemElement.querySelector('.expression_list_image'); const expressionImageElement = document.querySelector('.expression_list_image');
const newSrc = expressionImageElement.src;
doExtrasFetch(newSrc, { if (expressionImageElement) {
doExtrasFetch(expressionImageElement.src, {
method: 'HEAD', method: 'HEAD',
}) })
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
imgElement.src = getApiUrl() + '/api/live2d/result_feed'; imgElement.src = live2dResultFeedSrc;
} }
}) })
.catch(error => { .catch(error => {
console.error(error); // Log the error if necessary console.error(error); // Log the error if necessary
}); });
} else if (previousSrc) {
imgElement.src = previousSrc; // Revert the src to its previous value
} }
} else if (previousSrc !== null) {
imgElement.src = previousSrc; // Revert the src to its previous value
} }
live2d_var = isChecked; } else {
setExpression(getContext().name2, "neutral", true);
}
} }
async function moduleWorker() { async function moduleWorker() {
@ -497,22 +494,16 @@ async function moduleWorker() {
if (context.groupId !== lastCharacter && context.characterId !== lastCharacter) { if (context.groupId !== lastCharacter && context.characterId !== lastCharacter) {
removeExpression(); removeExpression();
spriteCache = {}; spriteCache = {};
previousSrc = null; previousSrc = null;
//uncheck live image
let checkbox = document.getElementById('image_type_toggle');
if (checkbox.checked) {
checkbox.click();
}
//clear expression //clear expression
let imgElement = document.getElementById('expression-image'); let imgElement = document.getElementById('expression-image');
imgElement.src = ""; imgElement.src = "";
//Load new char //set checkbox to global var
if (live2d_var) { $('#image_type_toggle').prop('checked', extension_settings.expressions.live2d);
loadLiveChar(); 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) { function getSpriteFolderName(message) {
const context = getContext(); const context = getContext();
let avatarPath = ''; let avatarPath = '';
@ -805,7 +804,7 @@ async function getExpressionsList() {
} }
async function setExpression(character, expression, force) { async function setExpression(character, expression, force) {
if (live2d_var == false) { if (extension_settings.expressions.live2d == false) {
console.debug('entered setExpressions'); console.debug('entered setExpressions');
await validateImages(character); await validateImages(character);
@ -915,7 +914,9 @@ async function setExpression(character, expression, force) {
} }
document.getElementById("expression-holder").style.display = ''; document.getElementById("expression-holder").style.display = '';
if (live2d_var == true) { } else {
if (extension_settings.expressions.live2d == true) {
// Find the <img> element with id="expression-image" and class="expression" // Find the <img> element with id="expression-image" and class="expression"
const imgElement = document.querySelector('img#expression-image.expression'); const imgElement = document.querySelector('img#expression-image.expression');
//console.log("searching"); //console.log("searching");
@ -1234,13 +1235,8 @@ function setExpressionOverrideHtml(forceClear = false) {
$(window).on("resize", updateVisualNovelModeDebounced); $(window).on("resize", updateVisualNovelModeDebounced);
$('.expression_settings').hide(); $('.expression_settings').hide();
$('#image_type_toggle').on('click', function () {
$('#image_type_toggle').on('change', function () { live2d(this.checked);
const isChecked = this.checked;
if (isChecked) {
loadLiveChar();
}
handleImageChange(isChecked);
}); });
} }
@ -1253,7 +1249,6 @@ function setExpressionOverrideHtml(forceClear = false) {
moduleWorker(); moduleWorker();
dragElement($("#expression-holder")) dragElement($("#expression-holder"))
eventSource.on(event_types.CHAT_CHANGED, () => { eventSource.on(event_types.CHAT_CHANGED, () => {
//console.log("checked: " + live2d_var);
setExpressionOverrideHtml(); setExpressionOverrideHtml();
if (isVisualNovelMode()) { if (isVisualNovelMode()) {