pmx-vmd-sync-player / v119-runtime.js
mg696's picture
Fix v119 runtime: clone copy button and robust pose hold
75246eb verified
Raw History Blame Contribute Delete
5.95 kB
// v119-runtime.js FIXED: single copy handler via button clone + VPD pose hold. v117 stable internals untouched.
(function(){
const VER='v119';
let lastCopyAt=0;
function log(m,l){try{(window.__dbg||console.log)(m,l||'info')}catch(e){console.log(m)}}
function setVer(){try{document.title='MMD pmx Player '+VER;const v=document.getElementById('version-label');if(v)v.textContent=VER}catch(e){}}
function logText(){const el=document.getElementById('debug-log');return el?(el.innerText||el.textContent||''):''}
function flash(btn,ok){try{btn.textContent=ok?'✅コピー完了':'⚠コピー失敗';btn.style.background=ok?'#064':'#700';btn.style.color='#fff';setTimeout(()=>{btn.textContent='📋 Copy All';btn.style.background='';btn.style.color=''},1800)}catch(e){}}
function fallbackCopy(txt){try{const ta=document.createElement('textarea');ta.value=txt;ta.readOnly=true;ta.setAttribute('readonly','');ta.style.position='fixed';ta.style.left='8px';ta.style.top='8px';ta.style.width='2px';ta.style.height='2px';ta.style.opacity='0.01';ta.style.zIndex='999999';document.body.appendChild(ta);ta.focus({preventScroll:true});ta.select();ta.setSelectionRange(0,ta.value.length);const ok=document.execCommand('copy');document.body.removeChild(ta);return !!ok}catch(e){return false}}
function copyNow(e){
if(e){try{e.preventDefault()}catch(_){ }try{e.stopImmediatePropagation()}catch(_){ }try{e.stopPropagation()}catch(_){ }}
const now=Date.now(); if(now-lastCopyAt<1000)return; lastCopyAt=now;
const btn=document.getElementById('debug-copy-btn'); if(!btn)return;
const txt=logText();
if(fallbackCopy(txt)){flash(btn,true);log('[Copy:'+VER+'] copy ok len='+txt.length,'info');return}
if(navigator.clipboard&&navigator.clipboard.writeText){navigator.clipboard.writeText(txt).then(()=>{flash(btn,true);log('[Copy:'+VER+'] async copy ok len='+txt.length,'info')}).catch(err=>{flash(btn,false);log('[Copy:'+VER+'] copy failed '+(err&&err.message?err.message:String(err)),'warn')})}
else{flash(btn,false);log('[Copy:'+VER+'] copy failed no clipboard','warn')}
}
function bindCopy(){
const old=document.getElementById('debug-copy-btn'); if(!old||old.__v119FixedCopy)return;
const btn=old.cloneNode(true); btn.__v119FixedCopy=true; old.replaceWith(btn);
btn.addEventListener('touchend',copyNow,{capture:true,passive:false});
btn.addEventListener('click',copyNow,{capture:true,passive:false});
log('[Copy:'+VER+'] fixed single-handler copy enabled','info')
}
function clearMotionInput(id,why){const el=document.getElementById(id);if(el){el.value='';log('[Motion:'+VER+'] '+id+' cleared ('+why+')','info')}}
function targetFromInput(el){return el&&el.id==='vmd-input2'?window.mesh2:window.mesh}
function clearPoseMode(mesh,why){if(mesh&&mesh.userData){delete mesh.userData.__v119PoseHold;delete mesh.userData.__v119PoseBones;log('[VPD:'+VER+'] pose hold cleared '+why,'info')}}
function bindMotionInputs(){
[['file-input','vmd-input'],['file-input2','vmd-input2']].forEach(([modelId,motionId])=>{const m=document.getElementById(modelId);if(m&&!m.__v119FixedModelClear){m.__v119FixedModelClear=true;m.addEventListener('change',()=>clearMotionInput(motionId,'model slot changed'),true)}});
['vmd-input','vmd-input2','cam-vmd-input'].forEach(id=>{const el=document.getElementById(id);if(!el||el.__v119FixedSameFile)return;el.__v119FixedSameFile=true;['click','touchstart','pointerdown'].forEach(ev=>el.addEventListener(ev,()=>{try{el.value=''}catch(e){};log('[Motion:'+VER+'] '+id+' cleared before picker','info')},{capture:true,passive:true}));el.addEventListener('change',()=>{const files=Array.from(el.files||[]);if(files.some(f=>/\.vmd$/i.test(f.name||'')))clearPoseMode(targetFromInput(el),'because VMD selected');setTimeout(()=>{try{el.value=''}catch(e){}},0)},false)});
}
function savePose(mesh){if(!mesh||!mesh.skeleton)return;mesh.userData.__v119PoseBones=mesh.skeleton.bones.map(b=>({b:b,p:b.position.clone(),q:b.quaternion.clone(),s:b.scale.clone()}));mesh.userData.__v119PoseHold=true;log('[VPD:'+VER+'] pose saved bones='+mesh.userData.__v119PoseBones.length,'info')}
function applySavedPose(mesh){const arr=mesh&&mesh.userData&&mesh.userData.__v119PoseBones;if(!arr)return;arr.forEach(d=>{if(!d.b)return;d.b.position.copy(d.p);d.b.quaternion.copy(d.q);d.b.scale.copy(d.s);d.b.updateMatrix()});if(mesh.skeleton)mesh.skeleton.update();mesh.updateMatrixWorld(true)}
function stopMixer(mesh){try{const obj=window.helper&&window.helper.objects&&window.helper.objects.get(mesh);if(obj&&obj.mixer){obj.mixer.stopAllAction();obj.mixer.timeScale=0;obj.mixer.time=0;log('[VPD:'+VER+'] mixer stopped for pose hold','info')}}catch(e){log('[VPD:'+VER+'] mixer stop failed '+e.message,'warn')}}
async function patchHelper(){try{const mod=await import('three/examples/jsm/animation/MMDAnimationHelper');const H=mod.MMDAnimationHelper;if(!H||H.prototype.__v119FixedPosePatch)return;H.prototype.__v119FixedPosePatch=true;const oldPose=H.prototype.pose;H.prototype.pose=function(mesh,vpd,params){const r=oldPose.call(this,mesh,vpd,params);setTimeout(()=>{stopMixer(mesh);savePose(mesh);applySavedPose(mesh)},0);setTimeout(()=>{stopMixer(mesh);applySavedPose(mesh)},250);return r};const oldUpdate=H.prototype.update;H.prototype.update=function(delta){const r=oldUpdate.call(this,delta);try{[window.mesh,window.mesh2].concat(window.__mmdMeshes||[]).filter(Boolean).forEach(m=>{if(m.userData&&m.userData.__v119PoseHold)applySavedPose(m)})}catch(e){}return r};log('[VPD:'+VER+'] helper pose/update fixed patch enabled','info')}catch(e){log('[VPD:'+VER+'] helper patch failed '+e.message,'error')}}
function bind(){setVer();setTimeout(setVer,500);setTimeout(setVer,1500);bindCopy();bindMotionInputs();patchHelper();log('[Runtime:'+VER+'] FIXED copy + motion + VPD pose-hold enabled','info')}
function wait(){if(window.__appReady)bind();else setTimeout(wait,100)}
wait();
})();