-
Notifications
You must be signed in to change notification settings - Fork 9
/
factory.js
237 lines (235 loc) · 8.04 KB
/
factory.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var remote = require('electron').remote
var { ipcRenderer, clipboard } = require('electron')
var fs = require('fs')
var mediaFactory = process.resourcesPath + "/../www/media/factory/"
var blockFactory = process.resourcesPath + "/../www/blocs&generateurs/factory/"
function loadBF(file) {
$.get(file, function(data){
if (data) {
var xml = Blockly.Xml.textToDom(data)
mainWorkspace.clear()
Blockly.Xml.domToWorkspace(xml, mainWorkspace)
mainWorkspace.render()
var elem = $(xml).find("language")[0]
var node = elem.childNodes[0]
localStorage.setItem("code_bf", node.nodeValue)
editor.setValue(node.nodeValue,1)
}
}, "text")
}
window.addEventListener('load', function load(event){
var window = remote.getCurrentWindow()
if(!window.isMaximized())window.maximize()
$('[rel="tooltip"]').tooltip({trigger: "hover"})
$('#btn_quit').on('click', function(){
window.close()
})
$('#btn_max').on('click', function(){
if(window.isMaximized()){
window.unmaximize()
$('#btn_max').html("<span class='fa fa-window-maximize fa-lg'></span>")
}else{
window.maximize()
$('#btn_max').html("<span class='fa fa-window-restore fa-lg'></span>")
}
})
$('#btn_append').on('click', function(){
$('#message').modal("show")
var selectList = document.createElement("select")
selectList.size = "10"
selectList.id = "toolchoice"
var messageDiv = document.getElementById('messageDIV')
messageDiv.append(selectList)
var list = localStorage.getItem('choice').split(",")
var sel = document.getElementById('toolchoice')
for (var i = 0; i < list.length; i++) {
var opt = document.createElement('option')
opt.value = i
opt.innerHTML = list[i]
sel.append(opt)
}
})
$('#message').on('hidden.bs.modal', function(e) {
$("#messageDIV").empty()
})
$('#btn_appendOK').on('click', function(){
if ($("#toolchoice").val()) {
var dataB = $('#languagePre').text()
var dataG = editor.getValue()
var xml = Blockly.Xml.workspaceToDom(mainWorkspace)
var new_element = document.createElement("language")
new_element.appendChild(document.createTextNode(dataG))
xml.insertBefore(new_element, xml.childNodes[0])
var dataX = Blockly.Xml.domToPrettyText(xml)
fs.writeFileSync(blockFactory + blockType + '.bf', dataX)
var appendData = dataB + "\n" + dataG + "\n////////////////////\n"
fs.appendFileSync(blockFactory + "append.js", appendData)
var place = $("#toolchoice").val()
ipcRenderer.send("appendBlock", blockType, appendData, place)
window.close()
} else $("#message").modal("hide")
})
$('#btn_redo').on('click', function(){
mainWorkspace.undo(1)
})
$('#btn_undo').on('click', function(){
mainWorkspace.undo(0)
})
$('#btn_print').on('click', function(){
ipcRenderer.send('save-png-factory')
})
$('#btn_save').on('click', function(){
ipcRenderer.send('save-bf')
})
$('#btn_new').on('click', function(){
mainWorkspace.clear()
var rootBlock = mainWorkspace.newBlock('factory_base')
rootBlock.initSvg()
rootBlock.render()
rootBlock.setMovable(false)
rootBlock.setDeletable(false)
})
$('#btn_open').on('click', function(){
ipcRenderer.send('openBF')
})
$('#btn_media').on('click', function(){
var list = document.getElementById("menu_media")
var files = fs.readdirSync(mediaFactory)
if (list.childNodes.length>3) for (var i=0 ; i<files.length ; i++) list.removeChild(list.lastChild)
files.forEach(function(file) {
var li = document.createElement("li")
var link = document.createElement("a")
var text = document.createTextNode(file)
link.appendChild(text)
link.href = "#"
link.onclick = function () {
var rootBlock = mainWorkspace.newBlock('field_image')
rootBlock.setFieldValue(file, 'SRC')
rootBlock.initSvg()
rootBlock.render()
}
li.appendChild(link)
list.appendChild(li)
})
})
$('#btn_export').on('click', function(){
ipcRenderer.send('export-js')
})
$('#lien1').on('click', function(){
loadBF("./examples/factory/model1.bf")
$("#exampleModal").modal("hide")
})
$('#lien2').on('click', function(){
loadBF("./examples/factory/model2.bf")
$("#exampleModal").modal("hide")
})
$('#lien3').on('click', function(){
loadBF("./examples/factory/model3.bf")
$("#exampleModal").modal("hide")
})
$('#lien4').on('click', function(){
loadBF("./examples/factory/model4.bf")
$("#exampleModal").modal("hide")
})
$('#lien5').on('click', function(){
ipcRenderer.send('addImg')
})
$('#lien6').on('click', function(){
fs.readdir(mediaFactory, function(err, files) {
var dir_img = document.getElementById('span_image_dir')
$("#span_image_dir").empty()
if(files.length%2==0){
for (var i=0; i < files.length; i=i+2) dir_img.innerHTML += "<tr><td>"+files[i]+"</td><td>"+files[i+1]+"</td></tr>"
}else{
for (var i=0; i < files.length-1; i=i+2) dir_img.innerHTML += "<tr><td>"+files[i]+"</td><td>"+files[i+1]+"</td></tr>"
dir_img.innerHTML += "<tr><td>"+files[files.length-1]+"</td></tr>"
}
$("#imageModal").modal("show")
})
})
$('#lien7').on('click', function(){
fs.readdir(mediaFactory, function(err, files){
var file1 = files.filter(word => word!="gamepad.png")
var file2 = file1.filter(word => word!="keyboard.png")
file2.forEach(function(file){
if (file2.length!=0){
fs.unlink(mediaFactory+file, function(err){ if (err) return console.log(err)})
}
})
})
})
$('#btn_open_code').on('click', function(){
var code = localStorage.getItem("code_bf")
editor.setValue(code,1)
})
ipcRenderer.on('added-img', function(event, path){
if (path === null){
return
} else {
path.forEach(function(pt) {
var name = pt.substring(pt.lastIndexOf("\\"))
fs.copyFile(pt, mediaFactory+name, function(err){ if (err) return console.log(err)})
})
}
})
ipcRenderer.on('saved-bf', function(event, path){
if (path === null) {
return
} else {
var xml = Blockly.Xml.workspaceToDom(mainWorkspace)
var new_element = document.createElement("language")
new_element.appendChild(document.createTextNode(editor.getValue()))
xml.insertBefore(new_element, xml.childNodes[0])
var code = Blockly.Xml.domToPrettyText(xml)
fs.writeFile(path, code, function(err){
if (err) return console.log(err)
})
}
})
ipcRenderer.on('saved-png-factory', function(event, path){
if (path === null) {
return
} else {
var ws = mainWorkspace.svgBlockCanvas_.cloneNode(true);
ws.removeAttribute("width");
ws.removeAttribute("height");
ws.removeAttribute("transform");
var styleElem = document.createElementNS("http://www.w3.org/2000/svg", "style");
styleElem.textContent = Blockly.Css.CONTENT.join('') ;
ws.insertBefore(styleElem, ws.firstChild);
var bbox = mainWorkspace.svgBlockCanvas_.getBBox();
var canvas = document.createElement( "canvas" );
canvas.width = Math.ceil(bbox.width+10);
canvas.height = Math.ceil(bbox.height+10);
var ctx = canvas.getContext( "2d" );
ctx.fillStyle = 'rgb(255, 255, 255)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
var xml = new XMLSerializer().serializeToString(ws);
xml = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="'+bbox.width+'" height="'+bbox.height+'" viewBox="' + bbox.x + ' ' + bbox.y + ' ' + bbox.width + ' ' + bbox.height + '"><rect width="100%" height="100%" fill="white"></rect>'+xml+'</svg>';
var img = new Image();
var code = btoa(unescape(encodeURIComponent(xml)));
img.setAttribute( "src", 'data:image/svg+xml;base64,' + code);
img.onload = function() {
ctx.drawImage( img, 5, 5 );
var canvasdata = canvas.toDataURL("image/png",1);
var data = canvasdata.replace(/^data:image\/png;base64,/,"")
fs.writeFile(path, data, 'base64', function(err){
if (err) return console.log(err)
})
}
}
})
ipcRenderer.on('saved-js', function(event, path){
if (path === null) {
return
} else {
var code = $("#languagePre").text() + "\n" + editor.getValue()
fs.writeFile(path, code, function(err){
if (err) return console.log(err)
})
}
})
ipcRenderer.on('openedBF', function(event, path){
if (path) loadBF(path)
})
})