-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
197 lines (157 loc) · 5.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// USS reference: https://docs.unity3d.com/Manual/UIE-USS-Properties-Reference.html
const fs = require('fs');
const cheerio = require('cheerio');
const css = require('css');
const uss_properties = require('./uss_properties.json');
let config, html, cssContent, outputFolder;
let xmlheader = '<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">';
let xmlfooter = '</ui:UXML>';
let resetAll = 'body, div, p';
function html2uxml(name, h) {
const $ = cheerio.load(h);
let parsed = convertToXML($('body'), $);
parsed = parsed.split('<body>').join(xmlheader);
parsed = parsed.split('</body>').join(xmlfooter);
fs.writeFile(`${outputFolder}/` + name + '.uxml', formatXml(parsed), 'utf-8', err => {
if(err) console.log(err);
else {
console.log(name + ' UXML written ✓');
}
});
}
function convertToXML(element, $) {
let xmlString = '';
const tagMap = {
div: 'ui:VisualElement',
p: 'ui:Label',
input: getInputType(element.get(0).attribs.type || '')
};
const tagName = tagMap[element.get(0).tagName] || element.get(0).tagName;
xmlString += `<${tagName}`;
if (tagName == tagMap['p']) {
let text = element.first().text();
if(config.options.uppercase == true) text = text.toUpperCase();
xmlString += ' text="' + text + '"';
}
element.each((_, elem) => {
const attributes = elem.attribs;
for (const attr in attributes) {
xmlString += ` ${attr}="${attributes[attr]}"`;
}
});
if (tagName == tagMap['input']) {
if (config?.options?.focusable != undefined) xmlString += ` focusable="${config.options.focusable}"`;
}
xmlString += '>';
element.children().each((index, child) => {
const childElement = $(child);
xmlString += convertToXML(childElement, $);
});
xmlString += `</${tagName}>`;
return xmlString;
}
const inputTypes = {
'': 'ui:TextField',
'text': 'ui:TextField',
'checkbox': 'ui:Toggle',
}
function getInputType(type) {
return inputTypes[type];
}
function convertCss(name, data) {
let parsedCSS = css.parse(data);
fs.writeFile(`${outputFolder}/` + name + '.uss', css2uss(parsedCSS.stylesheet.rules), 'utf-8', err => {
if(err) console.log(err);
else console.log(name + ' USS written ✓');
});
}
function css2uss(rules) {
let result = '';
let not_implemented = {};
let unity_support = {};
for(let i = 0; i < rules.length; i++) {
let rule = rules[i];
let selector = rule.selectors.join(', ');
result += (selector == 'body' ? ':root' : selector == resetAll ? '*' : selector) + ' {\n';
for(let d = 0; d < rule.declarations.length; d++) {
let declaration = rule.declarations[d];
let property = transformProperty(declaration.property);
//console.log(property);
if (uss_properties[property]) {
if(uss_properties[property].native == true) {
let value = translateValue(declaration.value, property);
result += ' ' + property + ': ' + value + ';\n';
result += getExtras(property, value);
}
else not_implemented[declaration.property] = true;
}
else unity_support[declaration.property] = true;
}
result += '}\n';
}
if(Object.keys(unity_support).length > 0) console.warn("- UI Toolkit doesn't support: " + Object.keys(unity_support).join(', '));
if(Object.keys(not_implemented).length > 0) console.warn('- Not implemented yet: ' + Object.keys(not_implemented).join(', '));
return result;
}
function translateValue(value, property) {
value = value.split('vw').join('%');
value = value.split('vh').join('%');
value = property == "-unity-font" ? getAssetPath(value) : value;
value = property == "letter-spacing" ? (+(value.split('px')[0]) * 2).toFixed(0) + 'px' : value; // dont know why but unity renders letter spacing 2x smaller
return value;
}
function transformProperty(property) {
property = property == 'background' ? 'background-color' : property;
property = property == 'font-family' ? '-unity-font' : property;
return property
}
function getAssetPath(value) {
if(config.assets[value]) return config.assets[value].path;
}
function getExtras(property, value) {
let extras = '';
extras += property == '-unity-font' ? ' -unity-font-definition: none;\n' : '';
return extras;
}
function convert(argv) {
config = require(argv.config);
outputFolder = argv.output;
html = [];
for (let i = 0; i < argv.input.length; i++) {
let path = argv.input[i];
let splitted = path.split('\\');
if (splitted.length == 1) splitted = path.split('/')
html.push({
name: splitted.length > 1 ? splitted[splitted.length - 1] : path,
data: fs.readFileSync(path, 'utf8')
})
}
cssContent = [];
for (let i = 0; i < argv.css.length; i++) {
let path = argv.css[i];
let splitted = path.split('\\');
if (splitted.length == 1) splitted = path.split('/')
cssContent.push({
name: splitted.length > 1 ? splitted[splitted.length - 1] : path,
originalPath: path,
data: fs.readFileSync(path, 'utf8')
})
}
for(let i = 0; i < html.length; i++) {
html2uxml(html[i].name.split('.html').join(''), html[i].data);
}
for(let i = 0; i < cssContent.length; i++) {
convertCss(cssContent[i].name.split('.css').join(''), cssContent[i].data);
}
};
function formatXml(xml, tab) { // tab = optional indent value, default is tab (\t)
var formatted = '', indent= '';
tab = tab || '\t';
xml.split(/>\s*</).forEach(function(node) {
if (node.match( /^\/\w/ )) indent = indent.substring(tab.length); // decrease indent by one 'tab'
formatted += indent + '<' + node + '>\r\n';
if (node.match( /^<?\w[^>]*[^\/]$/ )) indent += tab; // increase indent
});
return formatted.substring(1, formatted.length-3);
}
module.exports = convert;