diff --git a/content/lib/pc_py_wrapper.js b/content/lib/pc_py_wrapper.js index 6389f2b9..cbd0d0f5 100644 --- a/content/lib/pc_py_wrapper.js +++ b/content/lib/pc_py_wrapper.js @@ -17,7 +17,7 @@ var $builtinmodule = function (name) { // The doc string cannot be added in code as is usually done in Python, so we set it manually here. mod.__doc__ = "This module provides implemention of the Pencil Code internal functions in Python."; - mod.Window = window + mod.Window = window; // Sk.builtin.func creates a Python function out of a JS function mod.addalemon = new Sk.builtin.func(function (data) { @@ -149,7 +149,7 @@ var $builtinmodule = function (name) { if (sprite === Sk.builtin.none.none$) { return click(function(eventData){ var data = [eventData.x, eventData.y]; - Sk.misceval.callsim(func, Sk.ffi.remapToPy(data)); } ); + Sk.misceval.callsim(func); } ); } //return sprite.click( function () { Sk.misceval.callsim(fn); } ); }); @@ -223,6 +223,13 @@ var $builtinmodule = function (name) { return sprite.say(Sk.ffi.remapToJs(a)); }); + mod.audioplay = new Sk.builtin.func(function (sprite, url) { + Sk.builtin.pyCheckArgs("audioplay", arguments, 2, 2); + var urlplay = new Audio(Sk.ffi.remapToJs(url)); + + return urlplay.play(); + + }); // TEXT METHODS ///////////////////////////////////////////////////////// mod.write = new Sk.builtin.func(function (message) { Sk.builtin.pyCheckArgs("write", arguments, 1, 1); @@ -413,7 +420,7 @@ var $builtinmodule = function (name) { }); mod.fill = new Sk.builtin.func(function (sprite, color) { - Sk.builtin.pyCheckArgs("dot", arguments, 2, 2); + Sk.builtin.pyCheckArgs("fill", arguments, 2, 2); if (sprite === Sk.builtin.none.none$) { return fill(Sk.ffi.remapToJs(color)); } @@ -447,9 +454,9 @@ var $builtinmodule = function (name) { mod.drawon = new Sk.builtin.func(function (sprite, canvas) { Sk.builtin.pyCheckArgs("drawon", arguments, 2, 2); if (sprite === Sk.builtin.none.none$) { - return drawon(Sk.ffi.remapToJs(canvas)); + return drawon(canvas); } - return sprite.drawon(Sk.ffi.remapToJs(canvas)); + return sprite.drawon(canvas); }); mod.arrow = new Sk.builtin.func(function (sprite, color, size) { @@ -477,24 +484,25 @@ var $builtinmodule = function (name) { }); mod.touches = new Sk.builtin.func(function (sprite, obj) { - Sk.builtin.pyCheckArgs("touches", arguments, 2, 2); + Sk.builtin.pyCheckArgs("touches", arguments, 2, 2); if (sprite === Sk.builtin.none.none$) { return Sk.ffi.remapToPy(touches(Sk.ffi.remapToJs(obj))); } - return Sk.ffi.remapTOPy(sprite.touches(Sk.ffi.remapToJs(obj))); + return Sk.ffi.remapToPy(sprite.touches(Sk.ffi.remapToJs(obj))); }); mod.inside = new Sk.builtin.func(function (sprite, obj) { Sk.builtin.pyCheckArgs("inside", arguments, 2, 2); - if(Sk.ffi.remapToJs(obj) == "window") + if(Sk.ffi.remapToJs(obj) == "window"){ if (sprite === Sk.builtin.none.none$) { return Sk.ffi.remapToPy(inside(window)); } return Sk.ffi.remapToPy(sprite.inside(window)); + } if (sprite === Sk.builtin.none.none$) { - return Sk.ffi.remap.Py(inside(Sk.ffi.remapToJs(obj))); + return Sk.ffi.remapToPy(inside(obj)); } - return Sk.ffi.remap.Py(sprite.inside(Sk.ffi.remapToJs(obj))); + return Sk.ffi.remapToPy(sprite.inside(obj)); }); //Operators diff --git a/content/lib/pencilcode.py b/content/lib/pencilcode.py index b3500b3f..bea049e2 100644 --- a/content/lib/pencilcode.py +++ b/content/lib/pencilcode.py @@ -61,7 +61,7 @@ def jumpxy(self, x, y): pencilcode_internal.jumpxy(self.jsSpriteObject, x, y) def pause(self, value): - pencilcode_internal.sleep(self.jsSpriteObject, value) + pencilcode_internal.pause(self.jsSpriteObject, value) def getxy(self): return pencilcode_internal.getxy(self.jsSpriteObject) @@ -105,7 +105,9 @@ def pd(self): def box(self, a, b): pencilcode_internal.box(self.jsSpriteObject,a,b) - + def box(self, a): + pencilcode_internal.box(self.jsSpriteObject,a,10) + def grow(self, a): pencilcode_internal.grow(self.jsSpriteObject,a) @@ -124,8 +126,8 @@ def fill(self, color): def wear(self, name): pencilcode_internal.wear(self.jsSpriteObject,name) - def drawon(self, path): - pencilcode_internal.drawon(self.jsSpriteObject,path) + def drawon(self, canvas): + pencilcode_internal.drawon(self.jsSpriteObject,canvas.jsSpriteObject) #def cell(self, rows, columns): # pencilcode_internal.cell(self.jsSpriteObject, rows, columns) @@ -136,13 +138,13 @@ def shown(self): def hidden(self): return pencilcode_internal.hidden(self.jsSpriteObject) - def touches(self): + def touches(self, obj): return pencilcode_internal.touches(self.jsSpriteObject, obj) - def inside(self): - return pencilcode_internal.inside(self.jsSpriteObject, obj) + def inside(self, obj): + + return pencilcode_internal.inside(self.jsSpriteObject, obj if obj == window else obj.jsSpriteObject) - #################### ## Sound Commands ## #################### @@ -159,7 +161,9 @@ def silence(self): def say(self, a): pencilcode_internal.say(self.jsSpriteObject, a) - + def audioplay(self, url): + player = pencilcode_internal.audioplay(self.jsSpriteObject, url) + # These commands act on the default turtle object (which is not wrapped.). ################### @@ -211,7 +215,7 @@ def getxy(): return pencilcode_internal.getxy(None) def pause(value): - pencilcode_internal.sleep(None, value) + pencilcode_internal.pause(None, value) ################## ## Art Commands ## @@ -241,6 +245,9 @@ def pd(): def box(a, b): pencilcode_internal.box(None,a,b) +def box(a): + pencilcode_internal.box(None,a,10) + def grow(a): pencilcode_internal.grow(None,a) @@ -262,8 +269,8 @@ def fill(color): def wear(name): pencilcode_internal.wear(None,name) -def drawon(path): - pencilcode_internal.drawon(None,path) +def drawon(canvas): + pencilcode_internal.drawon(None,canvas.jsSpriteObject) def shown(): return pencilcode_internal.shown(None) @@ -275,8 +282,8 @@ def touches(obj): return pencilcode_internal.touches(None, obj) def inside(obj): - return pencilcode_internal.touches(None, obj) - + + return pencilcode_internal.inside(None, obj if obj == window else obj.jsSpriteObject) ################### ## Text Commands ## ################### @@ -341,7 +348,8 @@ def silence(): def say(a): pencilcode_internal.say(None, a) - +def audioplay(url): + pencilcode_internal.audioplay(None, url) ###################### ## Control Commands ## ###################### diff --git a/content/src/filetype.js b/content/src/filetype.js index db0ac21a..5d2fdd0f 100644 --- a/content/src/filetype.js +++ b/content/src/filetype.js @@ -185,9 +185,9 @@ function wrapTurtle(doc, domain, pragmasOnly, setupScript, instrumenter) { var originalLanguage = null; var seeline = '\n\n'; if (meta.type == "text/x-python") { - maintype = "text/x-python" + maintype = 'text/x-python'; seeline = '# Initialization / clearing goes here\n\n'; - originalLanguage = 'python' + originalLanguage = 'python'; } else { maintype = 'text/coffeescript'; @@ -208,9 +208,14 @@ function wrapTurtle(doc, domain, pragmasOnly, setupScript, instrumenter) { // Instruments the code for debugging, always producing javascript. var newText = instrumenter(text, originalLanguage); if (newText !== false) { - text = newText; - maintype = 'text/javascript'; - instrumented = true; + text = newText; + instrumented = true; + if(originalLanguage == 'python') { + maintype = 'text/x-python'; + } + else { + maintype = 'text/javascript'; + } } } var mainscript = seeline; @@ -384,8 +389,10 @@ function effectiveMeta(input) { if (meta && meta.type && meta.lib) { return meta; } meta = (meta && 'object' == typeof meta) ? JSON.parse(JSON.stringify(meta)) : {}; - if(doc && doc.mimeType){ - if (doc.mimeType.lastIndexOf('text/x-python', 0) === 0) { + //if(doc && doc.mimeType){ + if(doc && doc.mimeType && doc.meta){ + //if (doc.mimeType.lastIndexOf('text/x-python', 0) === 0) { + if (doc.meta.type == 'text/x-python') { meta.type = 'text/x-python'; meta.libs = [{name: 'turtle', src: '//{site}/turtlebits.js'}, {name: 'skulpt.min', src: '//{site}/lib/skulpt.min.js'}, diff --git a/content/src/palette.js b/content/src/palette.js index b1985321..6efb0564 100644 --- a/content/src/palette.js +++ b/content/src/palette.js @@ -816,7 +816,10 @@ module.exports = { { block: 'from pencilcode import *', title: 'Import Functions on the pencilcode file' - } + },{ + block: 'import re', + title: 'Import Functions related to comparing strings' + } ]) }, { name: 'Move', @@ -867,7 +870,10 @@ module.exports = { }, { block: 'jumpxy(30, 20)', title: 'Jump changing x and y without drawing' - }/* , { + }, { + block: 'pause(5)', + title: 'stop moveing for 5 seconds' + }/* , { block: 'pagexy()', title: 'Page (topleft-y-down {pageX:x, pageY:y}) coordinates' } */ @@ -892,6 +898,9 @@ module.exports = { block: 'while x < 10:\n x+=1\nelse:\n pass', title: 'Repeat while a condition is true' }, { + block: 'break', + title: 'use in a for/while loop to break out of loop' + }, { block: 'if 0 == 0:\n pass', title: 'Do something only if a condition is true' }, { @@ -911,18 +920,36 @@ module.exports = { }, { block: "click (e) ->\n ``", title: 'Do something when the mouse is clicked'*/ + }, { + block: 'forever(lambda:\n fd(100))', + title: 'Move forward forever' }, { - block: 'forever(main)', - title: 'Runs the function forever as fast as it can' - }, { block: 'tick(60, main)', title: 'Runs the function the number of times per second passed in, call tick(None, main) to stop running it' - }, { - block: 'def newfunction(x):\n fd(x)\n rt(90)', - title: 'Define a new function' + }, { + block: 'def buttonFunction():\n write(\'Button clicked\')', + title: 'Define a new function' }, { - block: 'newfunction(200)', - title: 'Use a custom function' + block: 'button(\'Write\', buttonFunction)', + title: 'Do something when a keyboard key is pressed' + }, { + block: 'keydown(lambda:\n write(\'Key pressed\'))', + title: 'Do something when a keyboard key is pressed' + }, { + block: 'def keydownFunction():\n write(\'Key pressed\')', + title: 'Define a new function' + }, { + block: 'keydown(keydownFunction, \'b\')', + title: 'Do something when a keyboard key is pressed' + }, { + block: 'def clickFunction():\n fd(100)', + title: 'Define a new function' + }, { + block: 'click(clickFunction)', + title: 'Do something when a keyboard key is pressed' + }, { + block: 'click(lambda:\n fd(100))', + title: 'Move to a location when document is clicked' } ]) }, { @@ -987,10 +1014,10 @@ module.exports = { block: 'inside(t)', title: 'True if the turtle is encircled by obj' } */, { - block: 'drawon(\'s\')', + block: 'drawon(s)', title: 'Draw on sprite s' }, { - block: 'drawon(\'document\')', + block: 'drawon(document)', title: 'Draw on the document' } ]) @@ -1022,16 +1049,21 @@ module.exports = { }, { block: 'say(\'hello\')', title: 'Speak a word' - } + }, { + block: 'audioplay("url")', + expansion: 'audioplay(\'https://upload.wikimedia.org/wikipedia/commons/1/11/06_-_Vivaldi_Summer_mvt_3_Presto_-_John_Harrison_violin.ogg\')' , + title: 'play an audio file' + } ]) }, { name: 'Operators', color: 'lightgreen', blocks: filterblocks([ { - block: 'x = 0;', + block: 'x = 0', title: 'Set a variable', id: 'assign' + }, { block: 'x += 1', title: 'Increase a variable', @@ -1085,10 +1117,33 @@ module.exports = { block: 'min(_,_)', title: 'Get the smaller on two numbers' }, { + block: 'max(_,_)', + title: 'Get the larger on two numbers' + }, { + block: 'abs(_)', + title: 'Get the absolute value of a number' + + }, { + block: 'round(_)', + title: 'round a number to the nearest integer' + }, { block: '_._', title: 'Call a function from inside of an object', id: 'dot' - } + }, { + block: 'def newfunction(x):\n fd(x)\n rt(90)', + title: 'Define a new function' + }, { + block: 'newfunction(200)', + title: 'Use a custom function' + }, { + block: 'return _', + title: 'Use inside a newfunction return a value' + }, { + block: 're.findall("pattern", x)', + title: 'find all instances of a pattern in x' + } + ]) }, { name: 'Sprites', @@ -1115,15 +1170,15 @@ module.exports = { }, { block: 'f = table(5, 5)', title: 'Outputs a table with m rows and n columns' - }//, { - // block: 'if @touches x:\n ``', - // title: 'Do something only if touching the object x' - // }, { - // block: 'if @inside window:\n ``', - // title: 'Do something only if inside the window' - //} + }, { + block: 'if touches("green"):\n pass', + title: 'Do something only if touching the color' + }, { + block: 'if inside(window):\n pass', + title: 'Do something only if inside the window' + }, ]) - }, { + }, { name: 'Text', color: 'pink', blocks: filterblocks([ @@ -1146,12 +1201,18 @@ module.exports = { block: 'label(\'Spot\')', title: 'Prints label at turtle position' },{ + block: 'input()', + title: 'reads an input from console' + } + + +/* { block: 'read(\'TextToBeRead\')', title: 'Type a new line to the document' },{ block: 'readnum(\'2016\')', title: 'Type a new line to the document' - }//,{ + }*/ //,{ // block: 'while True:\n x=input("Enter number")\n if x.isdigit():\n break', // title: 'Read numbers only' //},{ @@ -1598,7 +1659,7 @@ module.exports = { 'movexy': {color: 'lightblue', dropdown: [py_types.sdistances, py_types.sdistances]}, 'jumpto': {color: 'lightblue', dropdown: [py_types.sdistances, py_types.sdistances]}, 'jumpxy': {color: 'lightblue', dropdown: [py_types.sdistances, py_types.sdistances]}, - + 'pause': {color: 'lightblue'}, 'pen': {color: 'purple', dropdown: [py_types.colors, py_types.sizes]}, 'dot': {color: 'purple', dropdown: [py_types.colors, py_types.sizes]}, 'box': {color: 'purple', dropdown: [py_types.colors, py_types.sizes]}, @@ -1615,12 +1676,15 @@ module.exports = { 'arrow': {color: 'purple', dropdown: [py_types.colors]}, 'shown': {color: 'purple'}, 'hidden': {color: 'purple'}, - 'inside': {color: 'purple'}, + 'inside': {color: 'lightgreen'}, + 'touches': {color: 'lightgreen'}, + //Sound 'play': {color: 'indigo'}, 'tone': {color: 'indigo'}, 'silence': {color: 'indigo'}, 'say': {color: 'indigo'}, + 'audioplay': {color: 'indigo'}, //Control 'ht': {color: 'purple'}, 'cs': {color: 'purple'}, @@ -1633,6 +1697,12 @@ module.exports = { 'click': { color: 'orange' }, 'forever': { color: 'orange'}, 'tick': {color: 'orange', dropdown: [py_types.tickCount]}, + 'newfunction': {color: 'lightgreen'}, + 'min': {color: 'lightgreen'}, + 'max': {color: 'lightgreen'}, + 'abs': {color: 'lightgreen'}, + 'round': {color: 'lightgreen'}, + 're.findall': {color: 'lightgreen'}, //Sprites 'Sprite' : {color:'teal'}, 'Piano' : {color:'teal'}, @@ -1649,7 +1719,8 @@ module.exports = { 'label' : {color: 'pink'}, 'read': { color: 'pink' }, 'readnum': { color: 'pink' }, - + 'len': {color:'lightgreen'}, + 'input': {color:'pink'}, /* '?.slide': {color: 'lightblue', dropdown: [sdistances]}, '?.fill': {color: 'purple', dropdown: [colors]}, @@ -1788,17 +1859,18 @@ module.exports = { conditionals: {color: 'orange'}, value: {color: 'lightgreen'}, command: {color: 'lightgreen'}, - errors: {color: '#f00'} + errors: {color: '#f00'}, }, // PYTHON_CATEGORIES: { + // functions: {color: 'lightgreen'}, // returns: {color: 'yellow'}, // comments: {color: 'gray'}, // arithmetic: {color: 'lightgreen'}, // logic: {color: 'lightgreen'}, // containers: {color: 'teal'}, -// assignments: {color: 'lightgreen'}, + assignments: {color: 'lightgreen'}, // loops: {color: 'orange'}, // conditionals: {color: 'orange'}, // value: {color: 'lightgreen'}, @@ -1815,3 +1887,4 @@ module.exports = { center: {category: 'grouping'} } }; + diff --git a/content/src/view.js b/content/src/view.js index 815c56b0..a61a7631 100644 --- a/content/src/view.js +++ b/content/src/view.js @@ -2185,7 +2185,10 @@ function showPaneEditorLanguagesDialog(pane) { 'Coffeescript
' + '' + + 'Javascript
' + + '' + '' + '
' + '