Activities/Turtle Art/Under the hood: Difference between revisions

 
(6 intermediate revisions by one other user not shown)
Line 5: Line 5:
The json encoding of a repeat 4 forward 100 right 90 project:
The json encoding of a repeat 4 forward 100 right 90 project:
                                        
                                        
  [[0,"repeat",331,158,[null,1,2,null]],
  [
[1,["number","4"],417,167,[0,null]],
  [0,"repeat",331,158,[null,1,2,null]],
[2,"forward",426,207,[0,3,4]],
  [1,["number","4"],417,167,[0,null]],
[3,["number","100"],500,216,[2,null]],
  [2,"forward",426,207,[0,3,4]],
[4,"right",426,246,[2,5,null]],
  [3,["number","100"],500,216,[2,null]],
[5,["number","90"],500,255,[4,null]],
  [4,"right",426,246,[2,5,null]],
[-1,"turtle",0,0,0,0,50,5]]
  [5,["number","90"],500,255,[4,null]],
  [-1,"turtle",0,0,0,0,50,5]
]


The basic structure of the encoding is a list of lists, where each block has these elements:
The basic structure of the encoding is a list of lists, where each block has these elements:
# block number
# block number
# block name or [block name, block value]
# block name or [block name, block value]
# x position (depreciated)
# x position (deprecated)
# y position (depreciated)
# y position (deprecated)
# list of connections to other blocks
# list of connections to other blocks


Line 38: Line 40:
'''Note:''' As of Version 106, there is also support for plugins. If you can use the plugin mechanism to add support for additional devices, e.g., Arduino, or for making modifications such as are described below without making changes to the standard code base. (The advantage to the latter is that your changes will remain intact even after you upgrade to a newer version.)
'''Note:''' As of Version 106, there is also support for plugins. If you can use the plugin mechanism to add support for additional devices, e.g., Arduino, or for making modifications such as are described below without making changes to the standard code base. (The advantage to the latter is that your changes will remain intact even after you upgrade to a newer version.)


taconstants.py contains the constants that by-in-large determine the behavior of Turtle Art. Notably, the block palettes are defined below. If you want to add a new block to Turtle Art, it is generally a matter of modifying some tables below and then adding the primitive to talogo.py. For example, if we want to add a new turtle command, 'uturn', we'd make the following changes:
The tabasics.py file contains the constants that by-in-large determine the behavior of Turtle Art. Notably, the block palettes are defined
below. If you want to add a new block to Turtle Art, you could simply add a block of code to that file or to turtle_block_plugin.py,
which contains additional blocks. (Even better, write your own plugin!!)


(1) We'd add 'uturn' to the PALETTES list of lists:
Adding a new palette is simply a matter of:
    palette = make_palette('mypalette',  # the name of your palette
                          colors=["#00FF00", "#00A000"],
                          help_string=_('Palette of my custom commands'))


PALETTES = [['forward', 'back', 'clean', 'left', 'right', 'show',  
For example, if we want to add a new turtle command, 'uturn', we'd use the
              'seth', 'setxy', 'heading', 'xcor', 'ycor', 'setscale',
add_block method in the Palette class.
              'arc', 'scale', 'left', 'top', 'right', 'bottom', 'uturn'],
    palette.add_block('uturn', # the name of your block
            ['penup','pendown', 'setpensize', 'fillscreen', 'pensize',...
                      style='basic-style', # the block style
                      label=_('u turn'), # the label for the block
                      prim_name='uturn', # code reference (see below)
                      help_string=_('turns the turtle 180 degrees'))


(2) Then we'd add it to one of the block-style definitions. Since it takes
Next, you need to define what your block will do. def_prim takes 3 arguments: the primitive name, the number of arguments—0 in this case—and the function to call—in this case, the canvas.seth function to set the heading.
no arguments, we'd add it here:


BASIC_STYLE = ['clean', 'penup', 'pendown', 'stack1', 'stack2', 'vspace',
    self.tw.lc.def_prim('uturn', 0,
    'hideblocks', 'showblocks', 'clearheap', 'printheap', 'kbinput', 'uturn']
        lambda self: self.tw.canvas.seth(self.tw.canvas.heading + 180))


(3) Then we give it a name (Note the syntax _('string to be
That's it. When you next run Turtle Art, you will have a 'uturn' block on the 'mypalette' palette.
translated') used by the language-internationalization system; also
note that the name is an array, as some blocks contain multiple
strings.):


BLOCK_NAMES = {
You will have to create icons for the palette-selector buttons. These
...
are kept in the icons subdirectory. You need two icons:
    'uturn':[_('u-turn')],
mypaletteoff.svg and mypaletteon.svg, where 'mypalette' is the same
...
string as the entry you used in instantiating the Palette class. Note
              }
that the icons should be the same size (55x55) as the others. (This is
the default icon size for Sugar toolbars.)


(4) and a help-menu entry:
=== How to write a plugin ===


HELP_STRINGS = {
In Spanish: http://valentinbasel.fedorapeople.org/pdfs/turtle_art.pdf
...
    'uturn':_('change the heading of the turtle 180 degrees'),
...
              }
 
(5) Next, we need to define it as a primitive for the Logo command
parser (generally just the same name):
 
PRIMITIVES = {
...
    'uturn':'uturn',
...
            }
 
(6) Since there are no default arguments, we don't need to do anything
else here. But we do need to define the actual function in talogo.py
 
DEFPRIM = {
...
    'uturn':[0, lambda self: self.tw.canvas.seth(self.tw.canvas.heading+180)],
...
          }
 
That's it. When you next run Turtle Art, you will have a 'uturn' block
on the Turtle Palette.
 
[[Image:TAuturn.png|300px]]
 
Adding a new palette is simply a matter of: (1) adding an additional entry to PALETTE_NAMES; (2) new list of blocks to PALETTES; and (3) an additional entry in COLORS. However you will have to: (4) create icons for the palette-selector buttons. These are kept in the icons subdirectory. You need two icons: yourpalettenameoff.svg and yourpalettenameon.svg, where yourpalettename is the same string as the entry you added to the PALETTE_NAMES list. Note that the icons should be the same size (55x55) as the others. This is the default icon size for Sugar toolbars.