-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.html
321 lines (286 loc) · 10.5 KB
/
index.html
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<html>
<head>
<script src="./skulpt_dist/skulpt.min.js" type="text/javascript"></script>
<script src="./skulpt_dist/skulpt-stdlib.js" type="text/javascript"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.1/css/all.css"
integrity="sha384-O8whS3fhG2OnA5Kas0Y9l3cfpmYjapjI0E4theH4iuMD+pLhbf6JI0jIMfYcK3yZ" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<a href="https://github.com/Petlja/pygame4skulpt"><img style="position: absolute; top: 0; left: 0; border: 0;"
src="https://s3.amazonaws.com/github/ribbons/forkme_left_red_aa0000.png"
alt="Fork me on GitHub"></a>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<img src="./logo.png" height="100" alt="logo">
</div>
<div class="col-md-4"></div>
</div>
<div class="col-md-2"></div>
<div class="col-md-8">
<div id="mycanvas"></div>
<div class="row">
<div class="col-xs-6" id="editorsplace">
<div id="editor" style="border: 1px solid black"></div>
</div>
<div class="col-xs-6">
<pre id="output" class="text-left"></pre>
</div>
</div>
<div class="col-md-5"></div>
<a class="btn btn-primary col-md-2" id="runbutton">Run code<span class="glyphicon glyphicon-play"
aria-hidden="true"></span></a>
<div class="container">
<div class="btn-group">
<div class="btn-group dropup">
<button type="button" class="btn btn-small btn-default dropdown-toggle" data-toggle="dropdown">
Select an example
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href=".?snake.py">Snake</a></li>
<li><a href=".?spirograph.py">Spirograph</a></li>
<li><a href=".?spaceship.py">Spaceship</a></li>
<li><a href=".?draw.py">Drawing</a></li>
</ul>
</div>
</div>
</div>
<div class="col-md-5"></div>
</div>
<div class="col-md-2"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.1/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/chrome");
editor.session.setMode("ace/mode/python");
</script>
<script>
document.getElementById("editor").style['width'] = document.getElementById("editorsplace").offsetWidth;
document.getElementById("editor").style['height'] = 0.7 * window.innerHeight;
document.getElementById("editor").style['margin'] = "0 auto";
document.getElementById("output").style['height'] = 0.7 * window.innerHeight;
</script>
<script>
var basePath = 'https://cdn.rawgit.com/Petlja/pygame4skulpt/3435847b/pygame/';
Sk.externalLibraries = {
'pygame': {
path: basePath + '__init__.js',
},
'pygame.display': {
path: basePath + 'display.js',
},
'pygame.draw': {
path: basePath + 'draw.js',
},
'pygame.event': {
path: basePath + 'event.js',
},
'pygame.font': {
path: basePath + 'font.js',
},
'pygame.image': {
path: basePath + 'image.js',
},
'pygame.key': {
path: basePath + 'key.js',
},
'pygame.mouse': {
path: basePath + 'mouse.js',
},
'pygame.time': {
path: basePath + 'time.js',
},
'pygame.transform': {
path: basePath + 'transform.js',
},
'pygame.version': {
path: basePath + 'version.js',
},
};
function resetTarget() {
var selector = Sk.TurtleGraphics.target;
var target = typeof selector === "string" ? document.getElementById(selector) : selector;
// clear canvas container
while (target.firstChild) {
target.removeChild(target.firstChild);
}
return target;
}
function createArrows(div) {
var arrows = new Array(4);
var direction = ["left", "right", "up", "down"];
$(div).addClass("d-flex justify-content-center");
for (var i = 0; i < 4; i++) {
arrows[i] = document.createElement("span");
div.appendChild(arrows[i]);
$(arrows[i]).addClass("btn btn-primary btn-arrow");
var ic = document.createElement("i");
$(ic).addClass("fas fa-arrow-" + direction[i]);
arrows[i].appendChild(ic);
}
var swapIcon = function (id) {
$(arrows[id].firstChild).removeClass("fa-arrow-" + direction[id]).addClass("fa-arrow-circle-" + direction[id]);
}
var returnIcon = function (id) {
$(arrows[id].firstChild).removeClass("fa-arrow-circle-" + direction[id]).addClass("fa-arrow-" + direction[id]);
}
$(arrows[0]).on('mousedown', function () {
Sk.insertEvent("left");
swapIcon(0);
});
$(arrows[0]).on('mouseup', function () {
returnIcon(0);
});
$(arrows[1]).on('mousedown', function () {
Sk.insertEvent("right");
swapIcon(1);
});
$(arrows[1]).on('mouseup', function () {
returnIcon(1);
});
$(arrows[2]).on('mousedown', function () {
Sk.insertEvent("up");
swapIcon(2);
});
$(arrows[2]).on('mouseup', function () {
returnIcon(2);
});
$(arrows[3]).on('mousedown', function () {
Sk.insertEvent("down");
swapIcon(3);
});
$(arrows[3]).on('mouseup', function () {
returnIcon(3);
});
$(document).keydown(function (e) {
switch (e.which) {
case 37:
swapIcon(0);
break;
case 38:
swapIcon(2);
break;
case 39:
swapIcon(1);
break;
case 40:
swapIcon(3);
break;
}
});
$(document).keyup(function (e) {
switch (e.which) {
case 37:
returnIcon(0);
break;
case 38:
returnIcon(2);
break;
case 39:
returnIcon(1);
break;
case 40:
returnIcon(3);
break;
}
});
};
function printString(text) {
var output = document.getElementById("output");
text = text.replace(/</g, '<');
output.innerHTML = output.innerHTML + text;
}
function clearOutput() {
var output = document.getElementById("output");
output.innerHTML = '';
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
function addModal() {
$(Sk.main_canvas).css("border", "1px solid blue");
var currentTarget = resetTarget();
var div1 = document.createElement("div");
currentTarget.appendChild(div1);
$(div1).addClass("modal");
$(div1).css("text-align", "center");
var btn1 = document.createElement("span");
$(btn1).addClass("btn btn-primary btn-sm pull-right");
var ic = document.createElement("i");
$(ic).addClass("fas fa-times");
btn1.appendChild(ic);
$(btn1).on('click', function (e) {
Sk.insertEvent("quit");
});
var div2 = document.createElement("div");
$(div2).addClass("modal-dialog modal-lg");
$(div2).css("display", "inline-block");
$(div2).width(self.width + 42);
$(div2).attr("role", "document");
div1.appendChild(div2);
var div3 = document.createElement("div");
$(div3).addClass("modal-content");
div2.appendChild(div3);
var div4 = document.createElement("div");
$(div4).addClass("modal-header d-flex justify-content-between");
var div5 = document.createElement("div");
$(div5).addClass("modal-body");
var div6 = document.createElement("div");
$(div6).addClass("modal-footer");
var div7 = document.createElement("div");
$(div7).addClass("col-md-8");
var div8 = document.createElement("div");
$(div8).addClass("col-md-4");
var header = document.createElement("h5");
Sk.title_container = header;
$(header).addClass("modal-title");
div3.appendChild(div4);
div3.appendChild(div5);
div3.appendChild(div6);
div4.appendChild(header);
div4.appendChild(btn1);
// div7.appendChild(header);
// div8.appendChild(btn1);
div5.appendChild(Sk.main_canvas);
createArrows(div6);
$(div1).modal({
backdrop: 'static',
keyboard: false
});
}
function runCode() {
Sk.main_canvas = document.createElement("canvas");
Sk.quitHandler = function () {
$('.modal').modal('hide');
};
addModal();
var prog = ace.edit("editor").getValue();
Sk.misceval.asyncToPromise(function () {
try {
return Sk.importMainWithBody("<stdin>", false, prog, true);
} catch (e) {
alert(e)
}
});
}
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
Sk.configure({read: builtinRead, output: printString});
$("#runbutton").click(function () {
runCode();
});
var sourcePath = "./test/snake.py";
if (window.location.search.substring(1).length > 0) {
sourcePath = "./test/" + window.location.search.substring(1)
}
$.get(sourcePath, function (data) {
editor.setValue(data, -1);
});
</script>
</body>
</html>