// v120-runtime.js: de-duplicate copy/motion events + robust VPD pose hold. v117 stable internals untouched. (function(){ const VER='v120'; let lastCopyAt=0,lastCopyText=''; 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<900)return; lastCopyAt=now; const btn=document.getElementById('debug-copy-btn'); if(!btn)return; const txt=logText(); lastCopyText=txt; 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 btn=document.getElementById('debug-copy-btn');if(!btn||btn.__v120Copy)return;btn.__v120Copy=true;['touchend','click'].forEach(ev=>btn.addEventListener(ev,copyNow,{capture:true,passive:false}));log('[Copy:'+VER+'] single-event copy patch 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.__v120PoseHold;delete mesh.userData.__v120PoseBones;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.__v120ModelClear){m.__v120ModelClear=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.__v120SameFile)return;el.__v120SameFile=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.__v120PoseBones=mesh.skeleton.bones.map(b=>({b:b,p:b.position.clone(),q:b.quaternion.clone(),s:b.scale.clone()}));mesh.userData.__v120PoseHold=true;log('[VPD:'+VER+'] pose saved bones='+mesh.userData.__v120PoseBones.length,'info')} function applySavedPose(mesh){const arr=mesh&&mesh.userData&&mesh.userData.__v120PoseBones;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.__v120PosePatch)return;H.prototype.__v120PosePatch=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.__v120PoseHold)applySavedPose(m)})}catch(e){}return r};log('[VPD:'+VER+'] helper pose/update 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+'] copy + motion + VPD pose-hold patches enabled','info')} function wait(){if(window.__appReady)bind();else setTimeout(wait,100)} wait(); })();