not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
8.9 KiB

  1. Plugins
  2. =======
  3. How to get plugins
  4. ------------------
  5. You can find plugins in the [official app repository](https://github.com/walterbender/turtleblocksjs/plugins).
  6. The plugins are identified by the extension <code>**.json**</code>
  7. You need to download the plugins to load it.
  8. [(In this guide I will use this plugin)](https://github.com/walterbender/turtleblocksjs/blob/master/plugins/translate.json)
  9. > NOTE: You need to download the "raw" plugin by clicking on the "Raw"
  10. > link on the .JSON file page. In other words, don't download
  11. > <code>
  12. > https://github.com/walterbender/turtleblocksjs/blob/master/plugins/gmap.json
  13. > </code>,
  14. > but do download
  15. > <code>
  16. > https://raw.githubusercontent.com/walterbender/turtleblocksjs/master/plugins/gmap.json
  17. > </code>.
  18. >
  19. > If you see the error message "Syntax Error: unexpected <" it means you
  20. > probably did not download the "raw" plugin.
  21. ![Nutrition Plugin](https://github.com/walterbender/turtleblocksjs/raw/master/screenshots/foodplugin.png "The Nutrition plugin")
  22. How to load plugins
  23. -------------------
  24. On the Settings Palette found on the Option Toolbar (click it if it is
  25. not expanded) you will see this option:
  26. <img src='http://people.sugarlabs.org/walter/plugin-button.svg'>
  27. Click it and a file chooser will appear.
  28. In the file chooser select a plugin file (they have <code>**.json**</code> file suffixes) and click 'Open'.
  29. The file will open and load new blocks into the palettes. Many plugins define their own palettes, so you will likely see a new palette button at the bottom of the column of buttons on the left side of the screen. (In the case of the translate plugin, new blocks will be added to a new palette, *external*) <img src='http://people.sugarlabs.org/ignacio/externalrequests.svg'>
  30. The plugin is saved in the browser local storage so you don't need to
  31. reload it every time you run TurtleJS.
  32. How to make a plugin
  33. ====================
  34. Plugins allow developers add new palettes and blocks to support
  35. additional functionality without having to make any changes to the
  36. core code of Turtle Blocks. Anyone is free to create and distribute
  37. extensions. If a plugin is present, it is loaded when the activity is
  38. launched and any palettes or blocks defined by the plugin are made
  39. available to the user.
  40. Prerequisites
  41. -------------
  42. * It facilitates debugging if you must have turtleblocksjs up and
  43. running. Use the following command to run it from your cloned
  44. repository: <pre><code>python -m SimpleHTTPServer</code></pre>
  45. * To define the Turtle `blocks` in your plugin, you will need to know
  46. how to program in Javascript. The blocks are defined in a dictionary
  47. element. To understand better, check the [code of
  48. basicblocks.js]
  49. (https://github.com/walterbender/turtleblocksjs/blob/master/js/basicblocks.js)
  50. * We provide a tool to help you compile psuedo-code into JSON (see the
  51. section on Pluginify below). But you may also want to at least
  52. familiarize yourself with [JSON](http://en.wikipedia.org/wiki/JSON)
  53. * You may also want to familarize yourself with the Python plugin
  54. library [plugins in Turtle
  55. Art](http://wiki.sugarlabs.org/go/Activities/Turtle_Art/Plugins)
  56. The Plugin Dictionary
  57. ---------------------
  58. You should explore [some example
  59. plugins](https://github.com/walterbender/turtleblocksjs/blob/master/README.md#Plugins)
  60. and learn [how to install
  61. them.](https://github.com/walterbender/turtleblocksjs/blob/master/README.md#how-to-load-plugins)
  62. Plugins are a dictionary of JSON-encoded components that incorporates:
  63. a flow-block dictionary, an arg-block dictionary, a block dictionary,
  64. a globals dictionary, a palette dictionary, and color dictionaries.
  65. * `flow-block`: commands that are evaluated when
  66. a flow block is run;
  67. * `arg-block`: commands that are evaluated when
  68. an arg block is run;
  69. * `block`: new blocks defined in the plugin;
  70. * `globals`: globals that you can reference throughout
  71. your code (Please use a unique name for your globals -- by convention, we
  72. have been prepending the plugin name to global variables, e.g.,
  73. weatherSecretKey for the secretKey used in the weather plugin.);
  74. * `palette`: icons (in SVG format) associated with the
  75. palette;
  76. * `fill-colors`: hex color of the blocks;
  77. * `stroke-colors`: hex color for stroke of the blocks;
  78. * `highlight-colors`: hex color of the blocks when they are
  79. highlighted.
  80. Resources
  81. ---------
  82. Resources available to your plugin vary by section. `globals` have
  83. access to the `blocks` class, where as `flow-block` and `arg-block`
  84. have access to both the `blocks` class and the `logo` class. Note that
  85. you can access the `logo` class from the `blocks` class by referencing
  86. <code>blocks.logo</code>.
  87. Layout and Format
  88. -----------------
  89. <pre>
  90. <code>
  91. {
  92. "GLOBALS":{},
  93. "FLOWPLUGINS":{},
  94. "ARGSPLUGINS":{},
  95. "BLOCKPLUGINS":{},
  96. "PALETTEFILLCOLORS":{},
  97. "PALETTESTROKECOLORS":{},
  98. "PALETTEHIGHLIGHTCOLORS":{},
  99. "PALETTEPLUGINS":{}
  100. }
  101. </code>
  102. </pre>
  103. Format for `PALETTEFILLCOLORS`, `PALETTEHIGHLIGHTCOLORS` and
  104. `PALETTESTROKECOLORS`:
  105. <pre><code>{"[palette name]":"[color hex code]"}</code></pre>
  106. Example: ```"PALETTESTROKECOLORS":{"external":"#3771c8"}```
  107. Format for `PALETTEPLUGINS`:
  108. <pre><code>{"[palette name]":"[svg file code]"}</code></pre>
  109. Example: ```"PALETTEPLUGINS":{"external":"<?xml version........</svg>"}```
  110. Format for blocks:
  111. <pre><code>"{[name of the block]":"code of the block"}</code></pre>
  112. Example: ```"BLOCKPLUGINS":{"translate":"var ....", "detectlang":"var ....", "setlang":"var ...."}, ```
  113. Pluginify
  114. ---------
  115. You can use
  116. [pluginify.py](https://github.com/walterbender/turtleblocksjs/blob/master/pluginify.py)
  117. to convert a `.rtp` (Readable Turtleblocks Plugin) to a `.json`
  118. plugin.
  119. Writing plugins directly in JSON is tedious. To make the job easier
  120. for you, we have created the readable Turtle Blocks plugin (RTP)
  121. format. The syntax is available in `python pluginify.py syntax`
  122. [.rtp example](https://github.com/walterbender/turtleblocksjs/blob/master/plugins/finance.rtp)
  123. Once you have made an RTP file it is time to convert it to JSON so
  124. that it can be used in TurtleBlocksjs. To convert it to JSON, run
  125. `python pluginify.py filename.rtp`
  126. [.rtp syntax](https://github.com/walterbender/turtleblocksjs/blob/master/pluginify.py#L33)
  127. References
  128. ----------
  129. Valid blocks styles in turtleblocksjs:
  130. * `zeroArgBlock`: E.g., penup, pendown
  131. * `basicBlockNoFlow`: E.g., break
  132. * `oneArgBlock`: E.g., forward, right
  133. * `twoArgBlock`: E.g., setxy. These are expandable.
  134. * `oneArgMathBlock`: E.g., sqrt
  135. * `oneArgMathWithLabelBlock`: E.g., box
  136. * `twoArgMathBlock`: E.g., plus, minus, multiply, divide. These are also expandable.
  137. * `valueBlock`: E.g., number, string. Value blocks get DOM textareas associated with them so their values can be edited by the user.
  138. * `mediaBlock`: E.g., media. Media blocks invoke a chooser and a thumbnail image is overlayed to represent the data associated with the block.
  139. * `flowClampZeroArgBlock`: E.g., start. A "child" flow is docked in an expandable clamp. There are no additional arguments and no flow above or below.
  140. * `flowClampOneArgBlock`: E.g., repeat. Unlike action, there is a flow above and below.
  141. * `flowClampBooleanArgBlock`: E.g., if. A "child" flow is docked in an expandable clamp. The additional argument is a boolean. There is flow above and below.
  142. * `doubleFlowClampBooleanArgBlock`: E.g., if then else. Two "child" flows are docked in expandable clamps. The additional argument is a boolean. There is flow above and below.
  143. * `blockClampZeroArgBlock`: E.g., forever. Unlike start, there is flow above and below.
  144. * `blockClampOneArgBlock`: E.g., action. A "child" flow is docked in an expandable clamp. The additional argument is a name. Again, no flow above or below.
  145. * `booleanZeroArgBlock`: E.g., mouse button.
  146. * `booleanOneBooleanArgBlock`: E.g., not
  147. * `booleanTwoBooleanArgBlock`: E.g., and
  148. * `booleanOneArgBlock`: E.g.,
  149. * `booleanTwoArgBlock`: E.g., greater, less, equal.
  150. * `parameterBlock`: E.g., color, shade, pensize
  151. To use the block styles to create your blocks, let us go through [an example](https://github.com/walterbender/turtleblocksjs/blob/master/plugins/translate.json#L38)
  152. ```"translate":"var TranslateBlock = new ProtoBlock(\"translate\"); TranslateBlock.palette = palettes.dict[\"external\"]; blocks.protoBlockDict[\"translate\"] = TranslateBlock; TranslateBlock.oneArgMathBlock(); TranslateBlock.docks[0][2] = \"textout\"; TranslateBlock.docks[1][2] = \"textin\"; TranslateBlock.defaults.push(\"Hello\"); TranslateBlock.staticLabels.push(\"translate\");",```
  153. See the line ```TranslateBlock.oneArgMathBlock();``` That is how you define the block style `oneArgMathBlock` to `TranslateBlock`. To define your own block, use any of the style methods listed above.
  154. Example plugins
  155. ---------------
  156. [translate.json](https://github.com/walterbender/turtleblocksjs/blob/master/plugins/translate.json), [weather.json](https://github.com/walterbender/turtleblocksjs/blob/master/plugins/weather.json), [maths.rtp](https://github.com/walterbender/turtleblocksjs/blob/master/plugins/maths.rtp)