|
10 | 10 | <body> |
11 | 11 | <!-- File Upload Button --> |
12 | 12 | <input type="file" id="uploadJson" accept=".json" /> |
13 | | - <label> |
14 | | - <input type="checkbox" id="fixScaleCheckbox" /> |
15 | | - fix scale (use world unit) |
16 | | - </label> |
17 | | - <button onclick="handleCombined()">make g</button> |
| 13 | + |
| 14 | + <div style="margin: 1em 0;"> |
| 15 | + <label> |
| 16 | + <input type="checkbox" id="fixScaleWorld" checked> |
| 17 | + fix scale (use world unit) |
| 18 | + </label> |
| 19 | + <br> |
| 20 | + <label> |
| 21 | + <input type="checkbox" id="fixScalePixel" checked> |
| 22 | + fix scale (use pixel unit) |
| 23 | + </label> |
| 24 | + <br> |
| 25 | + <label> |
| 26 | + <input type="checkbox" id="fixFixture" checked> |
| 27 | + fix fixtures in body settings |
| 28 | + </label> |
| 29 | + <br> |
| 30 | + <label> |
| 31 | + <input type="checkbox" id="moveUpHalfDepth" checked> |
| 32 | + move collider.offset.z up half depth (in fixture fix) |
| 33 | + </label> |
| 34 | + </div> |
| 35 | + |
| 36 | + <button onclick="handleJson()">remove deprecated properties</button> |
18 | 37 |
|
19 | 38 | <script src="bundle.js" type="text/javascript"></script> |
20 | 39 | <script> |
21 | 40 | let jsonObject = null; |
22 | 41 |
|
23 | | - // Combined handler for "make g" button, applies fixScale if checkbox is checked, then removeUnwantedProperties |
24 | | - function handleCombined() { |
| 42 | + // Function to handle the uploaded JSON file and apply selected fixes |
| 43 | + function handleJson() { |
25 | 44 | const fileInput = document.getElementById('uploadJson'); |
26 | 45 | const file = fileInput.files[0]; |
27 | | - const fixScaleChecked = document.getElementById('fixScaleCheckbox').checked; |
28 | 46 |
|
29 | 47 | if (!file) { |
30 | 48 | alert('Please select a JSON file first!'); |
31 | 49 | return; |
32 | 50 | } |
33 | 51 |
|
| 52 | + const fixScaleWorld = document.getElementById('fixScaleWorld').checked; |
| 53 | + const fixScalePixel = document.getElementById('fixScalePixel').checked; |
| 54 | + const fixFixture = document.getElementById('fixFixture').checked; |
| 55 | + const moveUpHalfDepth = document.getElementById('moveUpHalfDepth').checked; |
| 56 | + |
34 | 57 | const reader = new FileReader(); |
35 | | - const { fixScale, removeUnwantedProperties } = window.fixes; |
| 58 | + |
| 59 | + const { fixScale, removeUnwantedProperties, fixFixture: fixFixtureFn } = window.fixes; |
36 | 60 |
|
37 | 61 | reader.onload = function (event) { |
38 | 62 | try { |
39 | 63 | jsonObject = JSON.parse(event.target.result); |
40 | | - let processed = jsonObject; |
41 | | - let prefix = ''; |
| 64 | + let obj = JSON.parse(JSON.stringify(jsonObject)); // deep clone |
| 65 | + |
| 66 | + let prefixParts = []; |
42 | 67 |
|
43 | | - if (fixScaleChecked) { |
44 | | - processed = fixScale(processed, false); |
45 | | - prefix += 'fixed_scale(world_unit)_'; |
| 68 | + // Apply scale fixes if checked |
| 69 | + if (fixScaleWorld) { |
| 70 | + obj = fixScale(obj, false); |
| 71 | + prefixParts.push('fixed_scale(world_unit)'); |
| 72 | + } |
| 73 | + if (fixScalePixel) { |
| 74 | + obj = fixScale(obj, true); |
| 75 | + prefixParts.push('fixed_scale(pixel_unit)'); |
| 76 | + } |
| 77 | + |
| 78 | + // Apply fixture fix if checked |
| 79 | + if (fixFixture) { |
| 80 | + obj = fixFixtureFn(obj, moveUpHalfDepth); |
| 81 | + prefixParts.push('fixed_fixture' + (moveUpHalfDepth ? '(moveUpHalfDepth)' : '')); |
46 | 82 | } |
47 | 83 |
|
48 | | - processed = removeUnwantedProperties(processed); |
49 | | - prefix += 'remove_unwanted_properties_'; |
| 84 | + // Always remove unwanted properties |
| 85 | + obj = removeUnwantedProperties(obj); |
| 86 | + prefixParts.push('remove_unwanted_properties'); |
50 | 87 |
|
51 | | - downloadJson(processed, `${prefix}${file.name}`); |
| 88 | + const prefix = prefixParts.join('_'); |
| 89 | + downloadJson(obj, `${prefix}_${file.name}`); |
52 | 90 | } catch (error) { |
53 | 91 | console.error('Error parsing JSON:', error); |
54 | 92 | alert('Invalid JSON file!'); |
55 | 93 | } |
56 | 94 | }; |
57 | | - |
58 | 95 | reader.readAsText(file); |
59 | 96 | } |
60 | 97 |
|
61 | 98 | // Function to download the JSON object as a file |
62 | 99 | function downloadJson(jsonObj, name) { |
63 | | - const jsonString = JSON.stringify(jsonObj, null, 2); // Convert JSON object to string |
64 | | - const blob = new Blob([jsonString], { type: 'application/json' }); // Create a Blob |
65 | | - const url = URL.createObjectURL(blob); // Create a URL for the Blob |
| 100 | + const jsonString = JSON.stringify(jsonObj, null, 2); |
| 101 | + const blob = new Blob([jsonString], { type: 'application/json' }); |
| 102 | + const url = URL.createObjectURL(blob); |
66 | 103 |
|
67 | | - // Create a temporary anchor element to trigger the download |
68 | 104 | const a = document.createElement('a'); |
69 | 105 | a.href = url; |
70 | | - a.download = `${name}`; // Set the filename with the fixed prefix |
| 106 | + a.download = `${name}`; |
71 | 107 | document.body.appendChild(a); |
72 | | - a.click(); // Trigger the download |
73 | | - document.body.removeChild(a); // Clean up the DOM |
74 | | - URL.revokeObjectURL(url); // Revoke the Blob URL |
| 108 | + a.click(); |
| 109 | + document.body.removeChild(a); |
| 110 | + URL.revokeObjectURL(url); |
75 | 111 | } |
76 | 112 | </script> |
77 | 113 | </body> |
|
0 commit comments