local Y=require("yaru.core") local gfx=require("yaru.gfx") local ui=require("yaru.ui") local component=require("component") local unicode=require("unicode") local M={} local SIDES={{0,"Bottom"},{1,"Top"},{2,"Back"},{3,"Front"},{4,"Right"},{5,"Left"}} local COLORS={0xF0F0F0,0xEB8844,0xC354CD,0x6689D3,0xDECF2A,0x41CD34,0xD88198,0x434343, 0xABABAB,0x287697,0x7B2FBE,0x253192,0x51301A,0x3B511A,0xB3312C,0x1E1B1B} local CNAMES={"white","orange","magenta","light blue","yellow","lime","pink","gray", "silver","cyan","purple","blue","brown","green","red","black"} function M.open() local st={dev=1,side=1} local win=Y.newWindow{app="redstone",title="Redstone",w=76,h=26,minW=60,minH=16} local function devices() local r={} for addr in component.list("redstone")do r[#r+1]=component.proxy(addr)end return r end function win.draw(w,cw,ch) local t=Y.theme local devs=devices() local rs=devs[st.dev]or devs[1] if not rs then Y.drawIcon((cw-8)//2,ch//2-5,"redstone") local s="No redstone card or redstone I/O block found" gfx.text((cw-unicode.len(s))//2,ch//2,s,t.dim) return end local x=2 for i,d in ipairs(devs)do x=x+ui.button(w,x,1,d.address:sub(1,6),function()st.dev=i end,i==st.dev and"suggested"or nil)+1 end gfx.text(2,3,"Side",t.dim);gfx.text(12,3,"Input",t.dim);gfx.text(36,3,"Output",t.dim) for i,sd in ipairs(SIDES)do local y=3+i*2 local ok,inp=pcall(rs.getInput,sd[1]) local _,out=pcall(rs.getOutput,sd[1]) inp,out=ok and inp or 0,out or 0 if st.side==i then ui.round(1,y,cw-2,1,t.dark and 0x383838 or 0xEBEBEB)end gfx.text(2,y,sd[2],t.fg) ui.progress(12,y,16,inp/15,0xFF2400) gfx.text(29,y,string.format("%2d",inp),inp>0 and 0xFF4940 or t.dim) ui.switch(w,36,y,out>0,function()pcall(rs.setOutput,sd[1],out>0 and 0 or 15)end) ui.button(w,44,y,"−",function()pcall(rs.setOutput,sd[1],math.max(0,out-1))end,nil,3) gfx.text(48,y,string.format("%2d",out),t.fg) ui.button(w,51,y,"+",function()pcall(rs.setOutput,sd[1],math.min(15,out+1))end,nil,3) ui.hit(w,1,y,34,1,function()st.side=i end) end if rs.getBundledOutput then local sd=SIDES[st.side] local y=18 gfx.text(2,y,"Bundled cable · "..sd[2],t.fg) for c=0,15 do local ok,v=pcall(rs.getBundledOutput,sd[1],c) local _,vin=pcall(rs.getBundledInput,sd[1],c) local bx,by=2+(c%8)*9,y+2+(c//8)*2 local on=ok and v and v>0 ui.pill(bx,by,7,on and COLORS[c+1]or(t.dark and 0x383838 or 0xDEDDDA)) gfx.text(bx+1,by,ui.trunc(CNAMES[c+1],5),on and(c==0 or c==4 or c==8)and 0x000000 or(on and 0xFFFFFF or t.dim)) if vin and vin>0 then gfx.text(bx+7,by,"•",COLORS[c+1])end ui.hit(w,bx,by,7,1,function()pcall(rs.setBundledOutput,sd[1],c,on and 0 or 255)end) end end end function win.onSignal(name)if name=="redstone_changed"then Y.redraw(win)end end function win.key(w,char,code) if code==Y.K.down then st.side=math.min(6,st.side+1)elseif code==Y.K.up then st.side=math.max(1,st.side-1)end end return win end return M