Guest User

Untitled

a guest
Sep 6th, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.26 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <meta name="referrer" content="no-referrer">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  5. <script src="https://cdn.jsdelivr.net/combine/npm/jszip@3/dist/jszip.min.js,npm/jquery@3"></script>
  6. <title>Ghetto PCIO Editor</title>
  7.  
  8. <div id="tools">
  9.   <input type="file" id="file"/><br>
  10.   X: <input id="x"/><input type="button" value="Set!" class="set"/><br>
  11.   Y: <input id="y"/><input type="button" value="Set!" class="set"/><br>
  12.   <input type="button" value="Save!" id="save"/>
  13. </div>
  14. <div id="room"></div>
  15.  
  16. <style>
  17.   #x, #y {
  18.     width: 50px;
  19.   }
  20.   #room {
  21.     position: absolute;
  22.     top: 0;
  23.     left: 300px;
  24.     width: 800px;
  25.     height: 500px;
  26.     border: 1px solid black;
  27.   }
  28.   .widget {
  29.     position: absolute;
  30.     background: black;
  31.     opacity: 0.5;
  32.   }
  33.   .selected.widget {
  34.     opacity: 1;
  35.   }
  36.   .type-board {
  37.     background: #ccc;
  38.     z-index: 0 !important;
  39.   }
  40.   .type-automationButton { background: pink }
  41. </style>
  42.  
  43. <script>
  44.   var zipFile;
  45.   var widgets;
  46.  
  47.   var defaultSizes = {
  48.     automationButton: [ 75, 75 ]
  49.   };
  50.   $("#file").on("change", function(evt) {
  51.     zipFile = evt.target.files[0];
  52.     JSZip.loadAsync(zipFile).then(function(zip) {
  53.       return zip.files["widgets.json"].async("text");
  54.     }).then(function(content) {
  55.       widgets = JSON.parse(content)
  56.       drawWidgets(widgets);
  57.     }, function (e) {
  58.       console.log("error loading zip file", e);
  59.     });
  60.   });
  61.   $("#room").on("click", ".widget", function() {
  62.     if(!$(".selected").length)
  63.       fillDetails(this);
  64.     $(this).toggleClass("selected");
  65.   })
  66.  
  67.   $(".set").on("click", function() {
  68.     var field = $(this).prev().prop("id");
  69.     $(".selected").each(function() {
  70.       widgets[$(this).data("index")][field] = $("#" + field).val();
  71.     });
  72.     drawWidgets(widgets);
  73.   });
  74.  
  75.   $("#save").on("click", function() {
  76.     JSZip.loadAsync(zipFile).then(function(zip) {
  77.       zip.file("widgets.json", JSON.stringify(widgets));
  78.  
  79.       zip.generateAsync({type:"base64"}).then(function (base64) {
  80.         downloadURI("data:application/zip;base64," + base64, zipFile.name);
  81.       }, function (e) {
  82.         console.log("error saving zip file", e);
  83.       });
  84.     });
  85.   });
  86.  
  87.   function drawWidgets(ws) {
  88.     $("#room").empty();
  89.     for(var i in ws) {
  90.       var w = ws[i];
  91.       if(w.type != "card") {
  92.         console.log(w.type, w.x, w.y);
  93.         var width  = w.width  || defaultSizes[w.type] && defaultSizes[w.type][0] || 40;
  94.         var height = w.height || defaultSizes[w.type] && defaultSizes[w.type][1] || 40;
  95.         $("#room").append("<div data-index='" + i + "' class='widget type-" + w.type + "' style='top: " + w.y/2 + "px; left: " + w.x/2 + "px; width: " + width/2 + "px; height: " + height/2 + "px; z-index: " + w.z + "'></div>");
  96.       }
  97.     }
  98.   }
  99.  
  100.   function fillDetails(w) {
  101.     $("#x").val(widgets[$(w).data("index")].x);
  102.     $("#y").val(widgets[$(w).data("index")].y);
  103.   }
  104.  
  105.   function downloadURI(uri, name) {
  106.     // https://stackoverflow.com/a/15832662
  107.     var link = document.createElement("a");
  108.     link.download = name;
  109.     link.href = uri;
  110.     document.body.appendChild(link);
  111.     link.click();
  112.     document.body.removeChild(link);
  113.     delete link;
  114.   }
  115. </script>
  116.  
Add Comment
Please, Sign In to add comment