(()=>{"use strict";new ModelFormat({id:"makriva_shape",name:"Makriva Shape",description:"Model for Makriva mod",icon:"icon-player",rotate_cubes:!0,box_uv:!0,optional_box_uv:!1,single_texture:!0,bone_rig:!0,centered_grid:!0,locators:!0,display_mode:!1,animation_mode:!0,codec:Codecs.project});let o={skinUrl:"",boundingBoxJson:"",exprAnimationJson:"",slimModel:!1,hideHead:!1,hideBody:!1,hideLeftArm:!1,hideRightArm:!1,hideLeftLeg:!1,hideRightLeg:!1};const e={head:[0,24,0],body:[0,24,0],left_arm:[-5,22,0],right_arm:[5,22,0],left_leg:[-1.9,12,0],right_leg:[1.9,12,0]},i=o=>!o||0==o[0]&&0==o[1]&&0==o[2],t=(o,e)=>Math.abs((o-e)/e)<1e-6,n=o=>t(o.to[0],o.from[0])||t(o.to[1],o.from[1])||t(o.to[2],o.from[2]);function r(o,e){return{pos:[e.origin[0]-o.to[0],e.origin[1]-o.to[1],o.from[2]-e.origin[2]],size:[o.to[0]-o.from[0],o.to[1]-o.from[1],o.to[2]-o.from[2]],uv:o.uv_offset,delta:0!==o.inflate?o.inflate:void 0,mirror:!!o.mirror_uv||void 0}}function a(o,e){return{pos:[e.origin[0]-o.to[0],e.origin[1]-o.to[1],o.from[2]-e.origin[2]],size:[o.to[0]-o.from[0],o.to[1]-o.from[1],o.to[2]-o.from[2]],uv:[o.uv_offset[0]+(o.to[1]==o.from[1]?o.to[2]-o.from[2]:0),o.uv_offset[1]+(o.to[0]==o.from[0]?o.to[2]-o.from[2]:0)],delta:0!==o.inflate?o.inflate:void 0,mirror:!!o.mirror_uv||void 0}}function d(o,e,t){const d={id:o.name,parent:t,rotation:[-o.rotation[0],-o.rotation[1],o.rotation[2]],rotationPoint:[e.origin[0]-o.origin[0],e.origin[1]-o.origin[1],o.origin[2]-e.origin[2]]},l=o=>{o.pos[0]-=d.rotationPoint[0],o.pos[1]-=d.rotationPoint[1],o.pos[2]-=d.rotationPoint[2]};if(n(o)){const i=a(o,e);l(i),d.quads=[i]}else{const i=r(o,e);l(i),d.cubes=[i]}return i(d.rotationPoint)&&(d.rotationPoint=void 0),d}function l(o,e,t){const s={id:o.name,parent:t,rotationPoint:[e.origin[0]-o.origin[0],e.origin[1]-o.origin[1],o.origin[2]-e.origin[2]],rotation:[-o.rotation[0],-o.rotation[1],o.rotation[2]],cubes:[],quads:[],bones:[]};for(const e of Object.values(o.children)){if(!e.export)continue;if(e instanceof Group){s.bones.push(l(e,o));continue}const t=e;i(t.rotation)?n(t)?s.quads.push(a(t,o)):s.cubes.push(r(t,o)):s.bones.push(d(t,o))}return i(s.rotationPoint)&&(s.rotationPoint=void 0),i(s.rotation)&&(s.rotation=void 0),0==s.cubes.length&&(s.cubes=void 0),0==s.quads.length&&(s.quads=void 0),0==s.bones.length&&(s.bones=void 0),s}function s(){const o={};for(const t of Object.values(Outliner.root)){if(!(t instanceof Group))continue;if(!(t.name in e))continue;if(!t.export)continue;const n=t,r=t.name,a=e[r],d=[a[0]-n.origin[0],a[1]-n.origin[1],n.origin[2]-a[2]];i(d)||(o[r]=d)}return o}const u=new Codec("makriva",{name:"Makriva shape",extension:"json",remember:!1,compile:function(){if(!Project)return"{}";const i={metadata:{},textures:{skin:o.skinUrl},textureSize:[Project.texture_width,Project.texture_height],hide:[],skeleton:s(),boundingBox:o.boundingBoxJson?JSON.parse(o.boundingBoxJson):void 0,animation:o.exprAnimationJson?JSON.parse(o.exprAnimationJson):void 0,bones:[]};o.slimModel&&(i.metadata.model="slim"),o.hideHead&&i.hide.push("head"),o.hideBody&&i.hide.push("body"),o.hideLeftArm&&i.hide.push("left_arm"),o.hideRightArm&&i.hide.push("right_arm"),o.hideLeftLeg&&i.hide.push("left_leg"),o.hideRightLeg&&i.hide.push("right_leg");for(const o of Object.values(Outliner.root)){if(!(o instanceof Group))continue;if(!(o.name in e))continue;if(!(o.children instanceof Array))continue;if(!o.export)continue;const t=o,n=o.name,r={origin:[t.origin[0],t.origin[1],t.origin[2]]};for(const e of Object.values(o.children))e.export&&(e instanceof Group?i.bones.push(l(e,r,n)):e instanceof Cube&&i.bones.push(d(e,r,n)))}return autoStringify(i)}});let c=[];function h(o){c=[];try{o.boundingBoxJson&&JSON.parse(o.boundingBoxJson)}catch(o){c.push("Invalid boundingBox JSON")}try{o.exprAnimationJson&&JSON.parse(o.exprAnimationJson)}catch(o){c.push("Invalid animation JSON")}}const m=new Action("makriva_export",{name:"Export Makriva Shape",category:"file",icon:"icon-player",click:function(){if(Project){if(""===o.skinUrl&&Project.textures.length>0){const e=Project.textures[0];o.skinUrl="file:makriva/"+e.name}new Dialog({id:"makriva_export",title:"Makriva Shape Export",form:{skinUrl:{label:"Skin URL",type:"text",value:o.skinUrl},slimModel:{label:"Slim model",type:"checkbox",value:o.slimModel},hideHead:{label:"Hide Head",type:"checkbox",value:o.hideHead},hideBody:{label:"Hide Body",type:"checkbox",value:o.hideBody},hideRightArm:{label:"Hide Right Arm",type:"checkbox",value:o.hideRightArm},hideLeftArm:{label:"Hide Left Arm",type:"checkbox",value:o.hideLeftArm},hideRightLeg:{label:"Hide Right Leg",type:"checkbox",value:o.hideRightLeg},hideLeftLeg:{label:"Hide Left Leg",type:"checkbox",value:o.hideLeftLeg},boundingBoxJson:{label:"boundingBox JSON",type:"text",value:o.boundingBoxJson},exprAnimationJson:{label:"animation JSON",type:"text",value:o.exprAnimationJson}},onFormChange:h,onConfirm:function(e){o=e,0===c.length?u.export():new Dialog({id:"makriva_export_error",title:"Invalid export parameters",singleButton:!0,lines:c}).show()}}).show()}}}),p=new Toggle("paint_exploded_view",{icon:()=>"open_in_full",category:"edit",condition:{modes:["paint"]},name:"Exploded View (Makriva)",description:"Toggles an exploded view that allows you to edit covered faces",value:!1,onChange(o){Project&&(Undo.initEdit({elements:Cube.all,exploded_view:!o}),Cube.all.forEach((e=>{const i=e.name.toLowerCase().includes("leg")?1:.5,t=o?i:-i/(1+i),n=[e.from[0]+(e.to[0]-e.from[0])/2,e.from[1],e.from[2]+(e.to[2]-e.from[2])/2];n.V3_multiply(t,t,t),e.from.V3_add(n),e.to.V3_add(n)})),Project.exploded_view=o,Undo.finishEdit(o?"Explode skin model":"Revert exploding skin model",{elements:Cube.all,exploded_view:o}),Canvas.updateView({elements:Cube.all,element_aspects:{geometry:!0}}),this.setIcon(this.icon))}});BBPlugin.register("makriva",{title:"Makriva plugin",author:"msifeed",description:"Export model to Makriva shape",icon:"icon-player",version:"0.2.2",variant:"both",onload:()=>{MenuBar.addAction(m,"file.export"),(()=>{if(Blockbench.on("select_project",(()=>{Project&&(p.value=Project.exploded_view,p.updateEnabledState())})),Toolbars.outliner.children.some((o=>o.id===p.id)))return;const o=Toolbars.outliner.children.map((o=>o.id)).indexOf("explode_skin_model");-1!==o?p.pushToolbar(Toolbars.outliner,o):p.pushToolbar(Toolbars.outliner)})()},onunload:()=>{p.delete(),m.delete()}})})();