synchro du labo (0bf0da65) : app.blade.php, README.md
This commit is contained in:
21
README.md
Normal file
21
README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# 🔌 Logique (classique)
|
||||
|
||||
L'ancien simulateur de portes (10.09), gardé pour ses circuits sauvés. Le nouveau : logisim.
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| id | `logique-classique` |
|
||||
| fournit | `logic-sim` |
|
||||
| bions qu'il nomme | `jOSApps` `jOSBoxion` `jOSGame` `jOSPorts` `jOSStore` |
|
||||
| fichiers | `app.blade.php` |
|
||||
| dans le labo | `resources/views/ploxions/logique-classique/` (dépôt `my_website2`) |
|
||||
|
||||
## Contribuer
|
||||
|
||||
- **Dans le xer** : https://labo.j0bot.ch/px/atelier?ploxion=logique-classique — prends-le, modifie-le, vois-le tourner, « ⧗ envoyer le tsoin ».
|
||||
- **Dans VS Code** : `xb prendre logique-classique` (ou l'extension xerboxion › ⇩ Prendre), puis ⧗ Envoyer.
|
||||
- Le chemin complet : https://labo.j0bot.ch/px/contribuer
|
||||
|
||||
Ce dépôt est un **miroir** du labo (synchronisé par `bin/ploxions-forge.mjs`, depuis le commit `0bf0da65`) : une modif se propose par un tsoin ou une branche du labo, pas directement ici.
|
||||
|
||||
© 2026 chill-tekk · ne pas nuire
|
||||
285
app.blade.php
Normal file
285
app.blade.php
Normal file
@@ -0,0 +1,285 @@
|
||||
{{-- PL0XION logique 🔌 — simulateur de circuits logiques (forge Chaoxion, source CircuitVerse/Gate Lab).
|
||||
Pose des portes (IN/CLK/AND/OR/NOT/NAND/NOR/XOR/XNOR/OUT), câble-les, vois le signal se propager.
|
||||
Combinatoire ET séquentiel (latches) via relaxation à état PERSISTANT (fixpoint). Table de vérité auto,
|
||||
horloge, presets (demi-add./add. complet/latch SR/mux 2:1), sauvegarde + capture tsoin. Pont vers le
|
||||
tech-tree (Dr Stone électronique). 100% déterministe, offline, zéro LLM, canvas 2D GPU-cheap.
|
||||
window.jOSApps['logique-classique']. Réutilise jOSStore/jOSPorts/jOSGame/jOSBoxion. Thémé --xr-*. --}}
|
||||
<style>
|
||||
.px-logique{display:flex;flex-direction:column;height:100%;background:var(--xr-bg,#060a12);color:var(--xr-text,#dbe9ff);font-family:var(--xr-font-ui,ui-sans-serif,system-ui,sans-serif);font-size:13px}
|
||||
.px-logique .lg-bar{flex:0 0 auto;display:flex;flex-wrap:wrap;align-items:center;gap:5px;padding:6px 8px;border-bottom:1px solid var(--xr-border,rgba(63,224,208,.16))}
|
||||
.px-logique .lg-title{font-weight:800;color:var(--xr-accent,#3fe0d0);margin-right:3px;letter-spacing:.3px}
|
||||
.px-logique .lg-btn{cursor:pointer;padding:4px 8px;border-radius:7px;border:1px solid var(--xr-border,rgba(63,224,208,.35));background:transparent;color:var(--xr-text,#cfe6ff);font:inherit;font-size:.74rem;line-height:1;white-space:nowrap}
|
||||
.px-logique .lg-btn:hover{background:var(--xr-accent-soft,rgba(63,224,208,.14));color:#fff}
|
||||
.px-logique .lg-btn.pri{border-color:var(--xr-accent,#3fe0d0);font-weight:700}
|
||||
.px-logique .lg-btn.on{background:var(--xr-accent,#3fe0d0);color:#04121a;font-weight:700}
|
||||
.px-logique select,.px-logique input[type=range]{background:#04121a;color:var(--xr-text,#e7f2ff);border:1px solid var(--xr-border,rgba(120,150,170,.35));border-radius:6px;padding:3px 5px;font:inherit;font-size:.72rem}
|
||||
.px-logique .lg-pal{flex:0 0 auto;display:flex;flex-wrap:wrap;gap:4px;padding:5px 8px;border-bottom:1px solid var(--xr-border,rgba(63,224,208,.1))}
|
||||
.px-logique .lg-chip{cursor:pointer;padding:3px 8px;border-radius:20px;border:1px solid var(--xr-border,rgba(120,150,170,.4));background:transparent;color:var(--xr-dim,rgba(200,222,235,.85));font:inherit;font-size:.72rem;font-family:ui-monospace,monospace}
|
||||
.px-logique .lg-chip:hover{border-color:var(--xr-accent,#3fe0d0);color:#fff}
|
||||
.px-logique .lg-main{flex:1 1 auto;display:flex;min-height:0}
|
||||
.px-logique .lg-stage{flex:1 1 auto;position:relative;overflow:hidden;min-width:0;background:var(--xr-panel,#0a1120)}
|
||||
.px-logique .lg-cv{position:absolute;inset:0;width:100%;height:100%;display:block;touch-action:none;cursor:crosshair}
|
||||
.px-logique .lg-hint{position:absolute;left:8px;bottom:6px;font-size:.66rem;color:var(--xr-dim,rgba(180,205,220,.5));pointer-events:none}
|
||||
.px-logique .lg-tt{flex:0 0 auto;width:180px;border-left:1px solid var(--xr-border,rgba(63,224,208,.14));background:var(--xr-panel,#0a1120);overflow:auto;padding:8px;display:none}
|
||||
.px-logique .lg-tt.on{display:block}
|
||||
.px-logique .lg-tt h4{margin:0 0 6px;font-size:.62rem;text-transform:uppercase;letter-spacing:.7px;color:var(--xr-dim,rgba(180,210,225,.6))}
|
||||
.px-logique table{border-collapse:collapse;font-family:ui-monospace,monospace;font-size:.72rem;width:100%}
|
||||
.px-logique th,.px-logique td{border:1px solid var(--xr-border,rgba(120,150,170,.25));padding:2px 5px;text-align:center}
|
||||
.px-logique th{color:var(--xr-accent,#3fe0d0)}
|
||||
.px-logique td.one{color:#37e0a0;font-weight:700}
|
||||
.px-logique .lg-note{font-size:.68rem;color:var(--xr-dim,rgba(190,215,230,.6));margin-top:6px}
|
||||
.px-logique .lg-rail{display:flex;flex-wrap:wrap;gap:4px;margin-top:8px}
|
||||
.px-logique .lg-tsoin{width:44px;height:30px;border-radius:5px;border:1px solid var(--xr-accent,rgba(63,224,208,.5));object-fit:cover;cursor:pointer;background:#04121a}
|
||||
.px-logique.compact .lg-tt{width:auto;border-left:none;border-top:1px solid var(--xr-border,rgba(63,224,208,.14))}
|
||||
.px-logique.compact .lg-main{flex-direction:column}
|
||||
</style>
|
||||
<script>
|
||||
(function(){
|
||||
'use strict';
|
||||
var NIN={ IN:0, CLK:0, OUT:1, NOT:1, BUF:1, AND:2, OR:2, NAND:2, NOR:2, XOR:2, XNOR:2 };
|
||||
var GATES=['AND','OR','NOT','NAND','NOR','XOR','XNOR'];
|
||||
function hasOut(t){ return t!=='OUT'; }
|
||||
function gate(t,a,b){ switch(t){ case'AND':return a&&b?1:0; case'OR':return a||b?1:0; case'NAND':return a&&b?0:1;
|
||||
case'NOR':return a||b?0:1; case'XOR':return (a^b)?1:0; case'XNOR':return (a^b)?0:1; case'NOT':return a?0:1; case'BUF':return a?1:0; } return 0; }
|
||||
|
||||
var W=64, H=44, PIN=11;
|
||||
function pinGeom(n){ var nin=NIN[n.type], ins=[]; for(var k=0;k<nin;k++) ins.push({ x:n.x, y:n.y+H*(k+1)/(nin+1) });
|
||||
return { out: hasOut(n.type)?{ x:n.x+W, y:n.y+H/2 }:null, ins:ins }; }
|
||||
|
||||
// presets (fabriquent des noeuds frais)
|
||||
function P_half(){ return [
|
||||
{ id:1,type:'IN',label:'A',state:0,x:30,y:40 },{ id:2,type:'IN',label:'B',state:0,x:30,y:130 },
|
||||
{ id:3,type:'XOR',x:180,y:50,ins:[{n:1,p:0},{n:2,p:0}] },{ id:4,type:'AND',x:180,y:150,ins:[{n:1,p:0},{n:2,p:0}] },
|
||||
{ id:5,type:'OUT',label:'S',x:320,y:60,ins:[{n:3,p:0}] },{ id:6,type:'OUT',label:'C',x:320,y:160,ins:[{n:4,p:0}] } ]; }
|
||||
function P_full(){ return [
|
||||
{ id:1,type:'IN',label:'A',state:0,x:24,y:24 },{ id:2,type:'IN',label:'B',state:0,x:24,y:104 },{ id:3,type:'IN',label:'Ci',state:0,x:24,y:184 },
|
||||
{ id:4,type:'XOR',x:150,y:44,ins:[{n:1,p:0},{n:2,p:0}] },{ id:5,type:'XOR',x:280,y:70,ins:[{n:4,p:0},{n:3,p:0}] },
|
||||
{ id:6,type:'AND',x:280,y:150,ins:[{n:4,p:0},{n:3,p:0}] },{ id:7,type:'AND',x:150,y:184,ins:[{n:1,p:0},{n:2,p:0}] },
|
||||
{ id:8,type:'OR',x:400,y:170,ins:[{n:6,p:0},{n:7,p:0}] },
|
||||
{ id:9,type:'OUT',label:'S',x:410,y:70,ins:[{n:5,p:0}] },{ id:10,type:'OUT',label:'Co',x:520,y:170,ins:[{n:8,p:0}] } ]; }
|
||||
function P_sr(){ return [
|
||||
{ id:1,type:'IN',label:'S',state:0,x:30,y:40 },{ id:2,type:'IN',label:'R',state:0,x:30,y:170 },
|
||||
{ id:3,type:'NOR',x:200,y:60,ins:[{n:2,p:0},{n:4,p:0}] },{ id:4,type:'NOR',x:200,y:160,ins:[{n:1,p:0},{n:3,p:0}] },
|
||||
{ id:5,type:'OUT',label:'Q',x:350,y:70,ins:[{n:3,p:0}] },{ id:6,type:'OUT',label:'Qb',x:350,y:170,ins:[{n:4,p:0}] } ]; }
|
||||
function P_mux(){ return [
|
||||
{ id:1,type:'IN',label:'A',state:0,x:24,y:24 },{ id:2,type:'IN',label:'B',state:0,x:24,y:104 },{ id:3,type:'IN',label:'Se',state:0,x:24,y:184 },
|
||||
{ id:4,type:'NOT',x:140,y:184,ins:[{n:3,p:0}] },
|
||||
{ id:5,type:'AND',x:260,y:40,ins:[{n:1,p:0},{n:4,p:0}] },{ id:6,type:'AND',x:260,y:150,ins:[{n:2,p:0},{n:3,p:0}] },
|
||||
{ id:7,type:'OR',x:390,y:90,ins:[{n:5,p:0},{n:6,p:0}] },{ id:8,type:'OUT',label:'Y',x:520,y:100,ins:[{n:7,p:0}] } ]; }
|
||||
var PRESETS={ 'demi-additionneur':P_half, 'additionneur complet':P_full, 'latch SR (NOR)':P_sr, 'mux 2:1':P_mux };
|
||||
|
||||
var LIVE=null; // posé au mount : { nodes, clockOn }
|
||||
function describe(){
|
||||
var nodes=LIVE?LIVE.nodes():[], on=LIVE?LIVE.clockOn():false;
|
||||
return { v:1, id:'logique', title:'Logique', icon:'🔌', items:[
|
||||
{ p:1, kind:'stat', label:'portes posées', value:nodes.length },
|
||||
{ p:2, kind:'stat', label:on?'▶ horloge':'⏸ arrêtée', value:on?'active':'stoppée' }
|
||||
] };
|
||||
}
|
||||
function mount(container, ctx){
|
||||
container.innerHTML='';
|
||||
var root=document.createElement('div'); root.className='px-logique';
|
||||
root.innerHTML=''
|
||||
+'<div class="lg-bar">'
|
||||
+'<span class="lg-title">🔌 Logique</span>'
|
||||
+'<select data-preset><option value="">charger un circuit…</option></select>'
|
||||
+'<button class="lg-btn" data-clock>▶ horloge</button>'
|
||||
+'<input type="range" min="1" max="12" value="3" data-rate title="Hz" style="width:70px">'
|
||||
+'<button class="lg-btn" data-tt>🧮 table</button>'
|
||||
+'<button class="lg-btn pri" data-tsoin>⧗ tsoin</button>'
|
||||
+'<button class="lg-btn" data-del title="supprimer la sélection">🗑</button>'
|
||||
+'<button class="lg-btn" data-clear title="tout effacer">✖</button>'
|
||||
+'</div>'
|
||||
+'<div class="lg-pal" data-pal></div>'
|
||||
+'<div class="lg-main">'
|
||||
+'<div class="lg-stage" data-stage><canvas class="lg-cv" data-cv></canvas>'
|
||||
+'<div class="lg-hint">clic sur une entrée = bascule · glisse une sortie → une entrée = câble · glisse un bloc = déplace</div></div>'
|
||||
+'<div class="lg-tt" data-ttpanel><h4>table de vérité</h4><div data-ttbody></div><div class="lg-rail" data-rail></div></div>'
|
||||
+'</div>';
|
||||
container.appendChild(root);
|
||||
|
||||
var stage=root.querySelector('[data-stage]'), cv=root.querySelector('[data-cv]'), palEl=root.querySelector('[data-pal]'),
|
||||
ttPanel=root.querySelector('[data-ttpanel]'), ttBody=root.querySelector('[data-ttbody]'), railEl=root.querySelector('[data-rail]'),
|
||||
presetSel=root.querySelector('[data-preset]'), rateIn=root.querySelector('[data-rate]'), clockBtn=root.querySelector('[data-clock]');
|
||||
var g=cv.getContext('2d'); var dpr=Math.min(window.devicePixelRatio||1,2);
|
||||
|
||||
var nodes=[], outState={}, sel=null, nextId=1, disposed=false, clockTimer=null;
|
||||
var drag=null, wiring=null, moved=false, ptr={x:0,y:0};
|
||||
|
||||
// palette
|
||||
['IN','CLK'].concat(GATES).concat(['OUT']).forEach(function(t){ var b=document.createElement('button'); b.className='lg-chip';
|
||||
b.textContent='+'+t; b.addEventListener('click',function(){ addNode(t); }); palEl.appendChild(b); });
|
||||
|
||||
function byId(id){ for(var i=0;i<nodes.length;i++) if(nodes[i].id===id) return nodes[i]; return null; }
|
||||
function labelFor(t){ var used={}, letters='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
nodes.forEach(function(n){ if(n.type===t&&n.label) used[n.label]=1; });
|
||||
for(var i=0;i<letters.length;i++){ if(!used[letters[i]]) return letters[i]; } return '?'; }
|
||||
function addNode(t){ var n={ id:nextId++, type:t, x:40+((nodes.length*26)%220), y:40+((nodes.length*20)%180) };
|
||||
if(NIN[t]===0) n.state=0; if(t==='IN'||t==='OUT') n.label=labelFor(t);
|
||||
if(NIN[t]>0) n.ins=new Array(NIN[t]).fill(null); nodes.push(n); sel=n.id; autosave(); render(); }
|
||||
|
||||
// ---- simulation (état persistant : gère les latches) ----
|
||||
function inVal(n,k){ var r=n.ins&&n.ins[k]; return r?(outState[r.n]||0):0; }
|
||||
function simulate(){ var ids={}; nodes.forEach(function(n){ ids[n.id]=1; if(outState[n.id]===undefined) outState[n.id]=0; });
|
||||
Object.keys(outState).forEach(function(k){ if(!ids[k]) delete outState[k]; });
|
||||
var maxIter=nodes.length*2+20, stable=false;
|
||||
for(var it=0;it<maxIter;it++){ var ch=false;
|
||||
for(var i=0;i<nodes.length;i++){ var n=nodes[i], o;
|
||||
if(n.type==='IN'||n.type==='CLK') o=n.state?1:0;
|
||||
else if(n.type==='OUT') o=inVal(n,0);
|
||||
else if(NIN[n.type]===1) o=gate(n.type,inVal(n,0),0);
|
||||
else o=gate(n.type,inVal(n,0),inVal(n,1));
|
||||
if(outState[n.id]!==o){ outState[n.id]=o; ch=true; } }
|
||||
if(!ch){ stable=true; break; } }
|
||||
return stable; }
|
||||
|
||||
// ---- table de vérité (éval fraîche par ligne = combinatoire) ----
|
||||
function truthTable(){ var ins=nodes.filter(function(n){return n.type==='IN';}),
|
||||
outs=nodes.filter(function(n){return n.type==='OUT';});
|
||||
if(!ins.length||!outs.length) return { empty:true };
|
||||
if(ins.length>8) return { tooMany:ins.length };
|
||||
var rows=[], nbits=ins.length, saveStates=ins.map(function(n){return n.state;}), saveOut={};
|
||||
Object.keys(outState).forEach(function(k){ saveOut[k]=outState[k]; });
|
||||
for(var m=0;m<(1<<nbits);m++){ ins.forEach(function(nd,k){ nd.state=(m>>(nbits-1-k))&1; });
|
||||
outState={}; simulate(); rows.push({ in:ins.map(function(nd){return nd.state;}), out:outs.map(function(nd){return outState[nd.id]||0;}) }); }
|
||||
ins.forEach(function(nd,k){ nd.state=saveStates[k]; }); outState=saveOut; simulate();
|
||||
return { ins:ins.map(function(n){return n.label||('n'+n.id);}), outs:outs.map(function(n){return n.label||('n'+n.id);}), rows:rows }; }
|
||||
function renderTT(){ var tt=truthTable();
|
||||
if(tt.empty){ ttBody.innerHTML='<div class="lg-note">ajoute au moins une entrée (IN) et une sortie (OUT).</div>'; return; }
|
||||
if(tt.tooMany){ ttBody.innerHTML='<div class="lg-note">'+tt.tooMany+' entrées = trop pour une table (max 8).</div>'; return; }
|
||||
var h='<table><tr>'; tt.ins.forEach(function(l){ h+='<th>'+esc(l)+'</th>'; }); h+='<th>·</th>';
|
||||
tt.outs.forEach(function(l){ h+='<th>'+esc(l)+'</th>'; }); h+='</tr>';
|
||||
tt.rows.forEach(function(r){ h+='<tr>'; r.in.forEach(function(v){ h+='<td class="'+(v?'one':'')+'">'+v+'</td>'; }); h+='<td></td>';
|
||||
r.out.forEach(function(v){ h+='<td class="'+(v?'one':'')+'">'+v+'</td>'; }); h+='</tr>'; });
|
||||
h+='</table>'; ttBody.innerHTML=h; }
|
||||
function esc(s){ try{ if(window.jOSb&&window.jOSb.esc) return window.jOSb.esc(s); }catch(e){} var d=document.createElement('div'); d.textContent=s==null?'':s; return d.innerHTML; }
|
||||
|
||||
// ---- rendu ----
|
||||
function resize(){ var r=stage.getBoundingClientRect(); cv.width=Math.max(1,Math.round(r.width*dpr)); cv.height=Math.max(1,Math.round(r.height*dpr)); }
|
||||
var C_ON='#37e0a0', C_OFF='#31455f', C_BOX='#0e1a2c', C_BORD='#2b3d5c', C_SEL='#ffcf5a', C_TXT='#cfe6ff';
|
||||
function render(){ if(disposed) return; simulate(); resize();
|
||||
g.setTransform(dpr,0,0,dpr,0,0); var r=stage.getBoundingClientRect(); g.clearRect(0,0,r.width,r.height);
|
||||
// wires
|
||||
for(var i=0;i<nodes.length;i++){ var n=nodes[i]; if(!n.ins) continue; var pg=pinGeom(n);
|
||||
for(var k=0;k<n.ins.length;k++){ var ref=n.ins[k]; if(!ref) continue; var src=byId(ref.n); if(!src) continue;
|
||||
var a=pinGeom(src).out, b=pg.ins[k]; if(!a||!b) continue; drawWire(a,b, outState[ref.n]?1:0); } }
|
||||
// nodes
|
||||
for(i=0;i<nodes.length;i++) drawNode(nodes[i]);
|
||||
// wire en cours
|
||||
if(wiring){ var s=byId(wiring.fromId); if(s){ var o=pinGeom(s).out; if(o){ g.strokeStyle=C_ON; g.lineWidth=2;
|
||||
g.beginPath(); g.moveTo(o.x,o.y); g.lineTo(ptr.x,ptr.y); g.stroke(); } } }
|
||||
}
|
||||
function drawWire(a,b,on){ g.strokeStyle=on?C_ON:C_OFF; g.lineWidth=on?2.4:1.6; g.beginPath(); g.moveTo(a.x,a.y);
|
||||
var mx=(a.x+b.x)/2; g.bezierCurveTo(mx,a.y,mx,b.y,b.x,b.y); g.stroke(); }
|
||||
function drawNode(n){ var pg=pinGeom(n), on=outState[n.id]?1:0;
|
||||
// corps
|
||||
g.fillStyle=C_BOX; g.strokeStyle=(sel===n.id)?C_SEL:C_BORD; g.lineWidth=(sel===n.id)?2:1.4;
|
||||
roundRect(n.x,n.y,W,H,7); g.fill(); g.stroke();
|
||||
// label principal
|
||||
g.fillStyle=C_TXT; g.font='700 12px ui-monospace,monospace'; g.textAlign='center'; g.textBaseline='middle';
|
||||
var main = (n.type==='IN'||n.type==='CLK')?(n.label||n.type) : (n.type==='OUT'?(n.label||'OUT'):n.type);
|
||||
if(n.type==='IN'||n.type==='CLK'){ g.fillText(n.label||n.type, n.x+W/2-8, n.y+H/2);
|
||||
g.fillStyle=(n.state?C_ON:C_OFF); g.font='800 15px ui-monospace,monospace'; g.fillText(n.state?'1':'0', n.x+W/2+14, n.y+H/2);
|
||||
if(n.type==='CLK'){ g.fillStyle=C_TXT; g.font='9px ui-monospace,monospace'; g.fillText('clk', n.x+W/2, n.y+H-8); } }
|
||||
else if(n.type==='OUT'){ g.fillStyle=on?C_ON:C_OFF; g.beginPath(); g.arc(n.x+W/2+10,n.y+H/2,8,0,6.283); g.fill();
|
||||
g.fillStyle=C_TXT; g.font='700 11px ui-monospace,monospace'; g.fillText(n.label||'', n.x+W/2-12, n.y+H/2); }
|
||||
else { g.font='700 '+(n.type.length>3?9:11)+'px ui-monospace,monospace'; g.fillText(n.type, n.x+W/2, n.y+H/2); }
|
||||
// pins
|
||||
if(pg.out){ g.fillStyle=on?C_ON:C_OFF; dot(pg.out.x,pg.out.y); }
|
||||
for(var k=0;k<pg.ins.length;k++){ var iv=inVal(n,k); g.fillStyle=iv?C_ON:C_OFF; dot(pg.ins[k].x,pg.ins[k].y); }
|
||||
}
|
||||
function dot(x,y){ g.beginPath(); g.arc(x,y,4.5,0,6.283); g.fill(); g.strokeStyle='#08131f'; g.lineWidth=1; g.stroke(); }
|
||||
function roundRect(x,y,w,h,r){ g.beginPath(); g.moveTo(x+r,y); g.arcTo(x+w,y,x+w,y+h,r); g.arcTo(x+w,y+h,x,y+h,r);
|
||||
g.arcTo(x,y+h,x,y,r); g.arcTo(x,y,x+w,y,r); g.closePath(); }
|
||||
|
||||
// ---- hit-testing / interactions ----
|
||||
function toPt(e){ var r=stage.getBoundingClientRect(); return { x:e.clientX-r.left, y:e.clientY-r.top }; }
|
||||
function hitPin(pt){ for(var i=nodes.length-1;i>=0;i--){ var n=nodes[i], pg=pinGeom(n);
|
||||
if(pg.out && Math.hypot(pt.x-pg.out.x,pt.y-pg.out.y)<PIN) return { node:n, kind:'out' };
|
||||
for(var k=0;k<pg.ins.length;k++){ if(Math.hypot(pt.x-pg.ins[k].x,pt.y-pg.ins[k].y)<PIN) return { node:n, kind:'in', pin:k }; } } return null; }
|
||||
function hitNode(pt){ for(var i=nodes.length-1;i>=0;i--){ var n=nodes[i]; if(pt.x>=n.x&&pt.x<=n.x+W&&pt.y>=n.y&&pt.y<=n.y+H) return n; } return null; }
|
||||
|
||||
cv.addEventListener('pointerdown',function(e){ try{ cv.setPointerCapture(e.pointerId); }catch(_){}
|
||||
var pt=toPt(e); ptr=pt; moved=false; var pin=hitPin(pt);
|
||||
if(pin){ if(pin.kind==='out'){ wiring={ fromId:pin.node.id }; }
|
||||
else { pin.node.ins[pin.pin]=null; sel=pin.node.id; autosave(); render(); } return; }
|
||||
var n=hitNode(pt);
|
||||
if(n){ sel=n.id; drag={ id:n.id, dx:pt.x-n.x, dy:pt.y-n.y }; render(); }
|
||||
else { sel=null; render(); }
|
||||
});
|
||||
cv.addEventListener('pointermove',function(e){ var pt=toPt(e); ptr=pt;
|
||||
if(drag){ var n=byId(drag.id); if(n){ if(Math.hypot(pt.x-(n.x+drag.dx),pt.y-(n.y+drag.dy))>3) moved=true;
|
||||
n.x=pt.x-drag.dx; n.y=pt.y-drag.dy; render(); } }
|
||||
else if(wiring){ moved=true; render(); } });
|
||||
cv.addEventListener('pointerup',function(e){ var pt=toPt(e);
|
||||
if(wiring){ var pin=hitPin(pt); if(pin && pin.kind==='in' && pin.node.id!==wiring.fromId){ pin.node.ins[pin.pin]={ n:wiring.fromId, p:0 }; autosave(); }
|
||||
wiring=null; render(); return; }
|
||||
if(drag){ var n=byId(drag.id); if(n && !moved && (n.type==='IN')){ n.state=n.state?0:1; emitState(); autosave(); }
|
||||
drag=null; render(); } });
|
||||
cv.addEventListener('pointercancel',function(){ wiring=null; drag=null; render(); });
|
||||
|
||||
// ---- barre ----
|
||||
Object.keys(PRESETS).forEach(function(k){ var o=document.createElement('option'); o.value=k; o.textContent=k; presetSel.appendChild(o); });
|
||||
presetSel.addEventListener('change',function(){ var k=presetSel.value; if(k&&PRESETS[k]){ loadCircuit(PRESETS[k]());
|
||||
presetSel.value=''; try{ if(window.jOSBoxion&&window.jOSBoxion.toast) window.jOSBoxion.toast('circuit chargé: '+k); }catch(e){} } });
|
||||
root.querySelector('[data-tt]').addEventListener('click',function(){ ttPanel.classList.toggle('on'); if(ttPanel.classList.contains('on')) renderTT(); });
|
||||
root.querySelector('[data-del]').addEventListener('click',function(){ if(sel==null) return; var id=sel;
|
||||
nodes=nodes.filter(function(n){ return n.id!==id; });
|
||||
nodes.forEach(function(n){ if(n.ins) n.ins=n.ins.map(function(r){ return (r&&r.n===id)?null:r; }); });
|
||||
delete outState[id]; sel=null; autosave(); render(); if(ttPanel.classList.contains('on')) renderTT(); });
|
||||
root.querySelector('[data-clear]').addEventListener('click',function(){ nodes=[]; outState={}; sel=null; nextId=1; autosave(); render(); if(ttPanel.classList.contains('on')) renderTT(); });
|
||||
clockBtn.addEventListener('click',function(){ if(clockTimer){ clearInterval(clockTimer); clockTimer=null; clockBtn.classList.remove('on'); clockBtn.textContent='▶ horloge'; }
|
||||
else { clockBtn.classList.add('on'); clockBtn.textContent='⏸ horloge'; tickClock(); } });
|
||||
function tickClock(){ if(clockTimer) clearInterval(clockTimer); var hz=parseInt(rateIn.value,10)||3;
|
||||
clockTimer=setInterval(function(){ if(disposed) return; var any=false;
|
||||
nodes.forEach(function(n){ if(n.type==='CLK'){ n.state=n.state?0:1; any=true; } }); if(any){ emitState(); render(); } }, Math.round(500/hz)); }
|
||||
rateIn.addEventListener('input',function(){ if(clockTimer) tickClock(); });
|
||||
|
||||
// ---- persist / bus / tsoin ----
|
||||
var store=null; try{ store=window.jOSStore?window.jOSStore('logique'):null; }catch(e){ store=null; }
|
||||
var AKEY='logique:auto', TKEY='logique:tsoins', saveT=null;
|
||||
function serialize(){ return JSON.stringify({ nodes:nodes, nextId:nextId }); }
|
||||
function autosave(){ if(saveT) clearTimeout(saveT); saveT=setTimeout(function(){ try{ localStorage.setItem(AKEY, serialize()); }catch(e){} },400); }
|
||||
function loadCircuit(arr){ nodes=arr||[]; outState={}; sel=null; nextId=1; nodes.forEach(function(n){ if(n.id>=nextId) nextId=n.id+1; if(NIN[n.type]>0&&!n.ins) n.ins=new Array(NIN[n.type]).fill(null); });
|
||||
autosave(); render(); if(ttPanel.classList.contains('on')) renderTT(); }
|
||||
var emitState=throttle(function(){ try{ if(window.jOSPorts&&window.jOSPorts.emit){ var outs={};
|
||||
nodes.forEach(function(n){ if(n.type==='OUT') outs[n.label||('n'+n.id)]=outState[n.id]||0; });
|
||||
window.jOSPorts.emit('logique','state',{ outputs:outs }); } }catch(e){} }, 200);
|
||||
function throttle(fn,ms){ var last=0; return function(){ var now=Date.now(); if(now-last>=ms){ last=now; fn(); } }; }
|
||||
|
||||
function jGet(){ if(store){ try{ return store.all()||[]; }catch(e){} } try{ return JSON.parse(localStorage.getItem(TKEY)||'[]'); }catch(e){ return []; } }
|
||||
function jAdd(it){ if(store){ try{ store.add(it); return; }catch(e){} } try{ var a=JSON.parse(localStorage.getItem(TKEY)||'[]'); a.unshift(it); a=a.slice(0,20); localStorage.setItem(TKEY,JSON.stringify(a)); }catch(e){} }
|
||||
function thumb(){ try{ var t=document.createElement('canvas'); t.width=88; t.height=60; var r=stage.getBoundingClientRect();
|
||||
t.getContext('2d').drawImage(cv,0,0,cv.width,cv.height,0,0,88,60); return t.toDataURL('image/webp',0.7)||t.toDataURL('image/jpeg',0.6); }catch(e){ return ''; } }
|
||||
function capture(){ var it={ id:'lg_'+Date.now(), t:0, circuit:JSON.parse(serialize()), thumb:thumb(),
|
||||
count:nodes.length, ins:nodes.filter(function(n){return n.type==='IN';}).length, outs:nodes.filter(function(n){return n.type==='OUT';}).length };
|
||||
jAdd(it); renderRail();
|
||||
try{ if(window.jOSPorts&&window.jOSPorts.emit) window.jOSPorts.emit('logique','tsoin',{ id:it.id, count:it.count }); }catch(e){}
|
||||
try{ if(window.jOSGame&&window.jOSGame.event) window.jOSGame.event('tsoin'); }catch(e){}
|
||||
try{ if(window.jOSBoxion&&window.jOSBoxion.toast) window.jOSBoxion.toast('⧗ tsoin capté — '+nodes.length+' blocs'); }catch(e){} }
|
||||
function renderRail(){ var a=jGet(); if(!a.length){ railEl.innerHTML=''; return; }
|
||||
railEl.innerHTML=''; a.slice(0,10).forEach(function(it){ var im=document.createElement('img'); im.className='lg-tsoin'; im.src=it.thumb||'';
|
||||
im.title=(it.count||0)+' blocs · '+(it.ins||0)+'→'+(it.outs||0); im.addEventListener('click',function(){ if(it.circuit&&it.circuit.nodes) loadCircuit(it.circuit.nodes); }); railEl.appendChild(im); }); }
|
||||
root.querySelector('[data-tsoin]').addEventListener('click',capture);
|
||||
|
||||
// resize débouncé (évite warning ResizeObserver)
|
||||
var ro=null, roQ=false; try{ ro=new ResizeObserver(function(){ if(disposed||roQ) return; roQ=true;
|
||||
requestAnimationFrame(function(){ roQ=false; if(disposed) return; var r=stage.getBoundingClientRect(); root.classList.toggle('compact', r.width<430); render(); }); }); ro.observe(stage); }catch(e){}
|
||||
|
||||
// boot : autosave ou preset demi-additionneur par défaut
|
||||
var boot=null; try{ boot=localStorage.getItem(AKEY); }catch(e){}
|
||||
if(boot){ try{ var o=JSON.parse(boot); if(o&&o.nodes&&o.nodes.length){ loadCircuit(o.nodes); nextId=o.nextId||nextId; } else loadCircuit(P_half()); }catch(e){ loadCircuit(P_half()); } }
|
||||
else loadCircuit(P_half());
|
||||
var wanted=(ctx&&ctx.config&&ctx.config.preset); if(wanted&&PRESETS[wanted]) loadCircuit(PRESETS[wanted]());
|
||||
renderRail(); render();
|
||||
try{ if(window.jOSGame&&window.jOSGame.bion) window.jOSGame.bion('logique'); }catch(e){}
|
||||
|
||||
LIVE={ nodes:function(){return nodes;}, clockOn:function(){return !!clockTimer;} };
|
||||
return { unmount:function(){ LIVE=null; disposed=true; if(clockTimer) clearInterval(clockTimer); if(saveT) clearTimeout(saveT);
|
||||
try{ if(ro) ro.disconnect(); }catch(e){} container.innerHTML=''; } };
|
||||
}
|
||||
|
||||
window.jOSApps=window.jOSApps||{};
|
||||
window.jOSApps['logique-classique']={ manifest:{ id:'logique-classique', name:'Logique (classique)', icon:'🔌' }, mount:mount, describe:describe };
|
||||
})();
|
||||
</script>
|
||||
Reference in New Issue
Block a user