├── .gitignore ├── COPYING ├── README.md ├── appengine ├── README.txt ├── app.yaml ├── closure-library-read-only ├── favicon.ico ├── index.yaml ├── index_redirect.py ├── redirect.html ├── report.py ├── robots.txt ├── storage.js └── storage.py ├── apps ├── code │ ├── block.js │ ├── blocks-bit.js │ ├── blocks-colours.js │ ├── blocks-disk.js │ ├── blocks-fs.js │ ├── blocks-help.js │ ├── blocks-keys.js │ ├── blocks-old.js │ ├── blocks-os.js │ ├── blocks-paintutils.js │ ├── blocks-peripheral.js │ ├── blocks-rednet.js │ ├── blocks-redstone.js │ ├── blocks-shell.js │ ├── blocks-table.js │ ├── blocks-term.js │ ├── blocks-test.js │ ├── blocks-textutils.js │ ├── blocks-turtle.js │ ├── blocks-vector.js │ ├── code.js │ ├── dependent_input_block.js │ ├── icons.png │ ├── index.html │ ├── side_input_block.js │ ├── style.css │ ├── value_block.js │ └── var_args_block.js ├── common.css ├── common.js ├── lang-lua.js ├── prettify.css └── prettify.js ├── blockly_compressed.js ├── blockly_uncompressed.js ├── blocks ├── colour.js ├── lists.js ├── logic.js ├── loops.js ├── math.js ├── procedures.js ├── text.js └── variables.js ├── blocks_compressed.js ├── build.py ├── core ├── block.js ├── block_svg.js ├── blockly.js ├── blocks.js ├── bubble.js ├── comment.js ├── connection.js ├── contextmenu.js ├── css.js ├── field.js ├── field_angle.js ├── field_checkbox.js ├── field_colour.js ├── field_dropdown.js ├── field_image.js ├── field_label.js ├── field_textinput.js ├── field_variable.js ├── flyout.js ├── generator.js ├── icon.js ├── inject.js ├── input.js ├── msg.js ├── mutator.js ├── names.js ├── procedures.js ├── scrollbar.js ├── toolbox.js ├── tooltip.js ├── trashcan.js ├── utils.js ├── variables.js ├── warning.js ├── widgetdiv.js ├── workspace.js └── xml.js ├── generators ├── lua.js └── lua │ ├── colour.js │ ├── lists.js │ ├── logic.js │ ├── loops.js │ ├── math.js │ ├── procedures.js │ ├── text.js │ └── variables.js ├── lua_compressed.js ├── media ├── 1x1.gif ├── click.mp3 ├── click.ogg ├── click.wav ├── delete.mp3 ├── delete.ogg ├── delete.wav ├── handclosed.cur ├── handopen.cur ├── quote0.png ├── quote1.png ├── trashbody.png ├── trashlid.png └── tree.png ├── msg ├── js │ ├── de.js │ ├── en.js │ ├── en_us.js │ ├── pt_br.js │ ├── vi.js │ └── zh_tw.js ├── json │ ├── de.json │ ├── en.json │ ├── keys.json │ ├── pt_br.json │ ├── qqq.json │ ├── synonyms.json │ ├── vi.json │ └── zh_tw.json └── messages.js └── tests ├── blockly_test.html ├── blockly_test.js ├── generator_test.js ├── generators ├── colour.xml ├── index.html ├── lists.xml ├── logic.xml ├── loops1.xml ├── loops2.xml ├── math.xml ├── procedures.xml ├── text.xml ├── unittest.js ├── unittest_lua.js └── variables.xml ├── names_test.js └── playground.html /.gitignore: -------------------------------------------------------------------------------- 1 | appengine/static/** 2 | key.js 3 | **~ 4 | **#* 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Blockly Lua allows you to program ComputerCraft turtles in Blockly for use within Minecraft. 2 | 3 | For more information, see the wiki. 4 | 5 | Blockly Lua is no longer active. 6 | -------------------------------------------------------------------------------- /appengine/README.txt: -------------------------------------------------------------------------------- 1 | 2 | Running an App Engine server 3 | 4 | This directory contains the files needed to setup the optional Blockly server. 5 | Although Blockly itself is 100% client-side, the server enables cloud storage 6 | and sharing. Store your programs in Datastore and get a unique URL that allows 7 | you to load the program on any computer. 8 | 9 | To run your own App Engine instance you'll need to create this directory 10 | structure: 11 | 12 | blockly/ 13 | |- app.yaml 14 | |- index.yaml 15 | |- index_redirect.py 16 | |- README.txt 17 | |- storage.js 18 | |- storage.py 19 | |- closure-library-read-only/ 20 | `- static/ 21 | |- apps/ 22 | |- core/ 23 | |- demos/ 24 | |- generators/ 25 | |- language/ 26 | |- media/ 27 | |- tests/ 28 | `- blockly_compressed.js 29 | 30 | Instructions for fetching Closure may be found here: 31 | http://code.google.com/p/blockly/wiki/Closure 32 | 33 | Finally, upload this directory structure to your App Engine account, 34 | wait a minute, then go to http://YOURNAME.appspot.com/ 35 | -------------------------------------------------------------------------------- /appengine/app.yaml: -------------------------------------------------------------------------------- 1 | application: blockly-lua 2 | version: 1 3 | runtime: python27 4 | api_version: 1 5 | threadsafe: no 6 | 7 | handlers: 8 | # Storage API. 9 | - url: /storage 10 | script: storage.py 11 | - url: /storage\.js 12 | static_files: storage.js 13 | upload: storage\.js 14 | 15 | # Blockly files. 16 | - url: /static 17 | static_dir: static 18 | 19 | # Closure library for uncompiled Blockly. 20 | - url: /closure-library-read-only 21 | static_dir: closure-library-read-only 22 | 23 | # Redirect for root directory. 24 | - url: / 25 | script: index_redirect.py 26 | 27 | # Favicon. 28 | - url: /favicon\.ico 29 | static_files: favicon.ico 30 | upload: favicon\.ico 31 | 32 | # robot.txt 33 | - url: /robots\.txt 34 | static_files: robots.txt 35 | upload: robots\.txt 36 | 37 | -------------------------------------------------------------------------------- /appengine/closure-library-read-only: -------------------------------------------------------------------------------- 1 | /home/spertus/src/closure-library-read-only/ -------------------------------------------------------------------------------- /appengine/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/espertus/blockly-lua/a3fe3f0a273a3e3d494b983d0afdba8cb2b2dff9/appengine/favicon.ico -------------------------------------------------------------------------------- /appengine/index.yaml: -------------------------------------------------------------------------------- 1 | indexes: 2 | 3 | # AUTOGENERATED 4 | 5 | # This index.yaml is automatically updated whenever the dev_appserver 6 | # detects that a new type of query is run. If you want to manage the 7 | # index.yaml file manually, remove the above marker line (the line 8 | # saying "# AUTOGENERATED"). If you want to manage some indexes 9 | # manually, move them above the marker line. The index.yaml file is 10 | # automatically uploaded to the admin console when you next deploy 11 | # your application using appcfg.py. 12 | -------------------------------------------------------------------------------- /appengine/index_redirect.py: -------------------------------------------------------------------------------- 1 | print("Status: 302") 2 | print("Location: /static/apps/code/index.html") 3 | -------------------------------------------------------------------------------- /appengine/redirect.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /appengine/report.py: -------------------------------------------------------------------------------- 1 | """Blockly Demo: Report 2 | 3 | Copyright 2012 Google Inc. 4 | http://blockly.googlecode.com/ 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | """ 18 | 19 | """Store reports about code written by users. 20 | """ 21 | 22 | __author__ = "ellen.spertus@gmail.com (Ellen Spertus)" 23 | 24 | import cgi 25 | import logging 26 | 27 | from google.appengine.ext import db 28 | 29 | print "Content-type: text/plain\n" 30 | 31 | class Report(db.Model): 32 | identifier = db.FloatProperty() 33 | application = db.StringProperty() 34 | date = db.DateTimeProperty(auto_now_add=True) 35 | level = db.IntegerProperty() 36 | result = db.IntegerProperty() 37 | # StringProperty is limited to 500 characters, so use TextProperty. 38 | program = db.TextProperty() 39 | 40 | # Catch errors extracting form fields or converting to numeric types. 41 | # Let any other errors propagate up. 42 | try: 43 | forms = cgi.FieldStorage() 44 | identifier = float(forms["id"].value) 45 | application = forms["app"].value 46 | level = int(forms["level"].value) 47 | result = int(forms["result"].value) 48 | program = forms["program"].value 49 | row = Report(identifier = identifier, application = application, 50 | level = level, result = result, program = program) 51 | row.put() 52 | except ValueError, KeyError: 53 | logging.error("Unable to extract all form fields.") 54 | -------------------------------------------------------------------------------- /appengine/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * 2 | Disallow: /storage 3 | -------------------------------------------------------------------------------- /appengine/storage.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Demo: Storage 3 | * 4 | * Copyright 2012 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Loading and saving blocks with localStorage and cloud storage. 22 | * @author q.neutron@gmail.com (Quynh Neutron) 23 | */ 24 | 'use strict'; 25 | 26 | // Create a namespace. 27 | var BlocklyStorage = {}; 28 | 29 | /** 30 | * Backup code blocks to localStorage. 31 | * @private 32 | */ 33 | BlocklyStorage.backupBlocks_ = function() { 34 | if ('localStorage' in window) { 35 | var xml = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace()); 36 | // Gets the current URL, not including the hash. 37 | var url = window.location.href.split('#')[0]; 38 | window.localStorage.setItem(url, Blockly.Xml.domToText(xml)); 39 | } 40 | }; 41 | 42 | /** 43 | * Bind the localStorage backup function to the unload event. 44 | */ 45 | BlocklyStorage.backupOnUnload = function() { 46 | window.addEventListener('unload', BlocklyStorage.backupBlocks_, false); 47 | }; 48 | 49 | /** 50 | * Restore code blocks from localStorage. 51 | */ 52 | BlocklyStorage.restoreBlocks = function() { 53 | var url = window.location.href.split('#')[0]; 54 | if ('localStorage' in window && window.localStorage[url]) { 55 | var xml = Blockly.Xml.textToDom(window.localStorage[url]); 56 | Blockly.Xml.domToWorkspace(Blockly.getMainWorkspace(), xml); 57 | } 58 | }; 59 | 60 | /** 61 | * Save blocks to database and return a link containing key to XML. 62 | */ 63 | BlocklyStorage.link = function() { 64 | var xml = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace()); 65 | var data = Blockly.Xml.domToText(xml); 66 | BlocklyStorage.makeRequest_('/storage', 'xml', data); 67 | }; 68 | 69 | /** 70 | * Retrieve XML text from database using given key. 71 | * @param {string} key Key to XML, obtained from href. 72 | */ 73 | BlocklyStorage.retrieveXml = function(key) { 74 | var xml = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace()); 75 | BlocklyStorage.makeRequest_('/storage', 'key', key); 76 | }; 77 | 78 | /** 79 | * Global reference to current AJAX request. 80 | * @type XMLHttpRequest 81 | * @private 82 | */ 83 | BlocklyStorage.httpRequest_ = null; 84 | 85 | /** 86 | * Fire a new AJAX request. 87 | * @param {string} url URL to fetch. 88 | * @param {string} name Name of parameter. 89 | * @param {string} content Content of parameter. 90 | * @private 91 | */ 92 | BlocklyStorage.makeRequest_ = function(url, name, content) { 93 | if (BlocklyStorage.httpRequest_) { 94 | // AJAX call is in-flight. 95 | BlocklyStorage.httpRequest_.abort(); 96 | } 97 | BlocklyStorage.httpRequest_ = new XMLHttpRequest(); 98 | BlocklyStorage.httpRequest_.name = name; 99 | BlocklyStorage.httpRequest_.onreadystatechange = 100 | BlocklyStorage.handleRequest_; 101 | BlocklyStorage.httpRequest_.open('POST', url); 102 | BlocklyStorage.httpRequest_.setRequestHeader('Content-Type', 103 | 'application/x-www-form-urlencoded'); 104 | BlocklyStorage.httpRequest_.send(name + '=' + encodeURIComponent(content)); 105 | }; 106 | 107 | /** 108 | * Callback function for AJAX call. 109 | * @private 110 | */ 111 | BlocklyStorage.handleRequest_ = function() { 112 | if (BlocklyStorage.httpRequest_.readyState == 4) { 113 | if (BlocklyStorage.httpRequest_.status != 200) { 114 | BlocklyStorage.alert(BlocklyStorage.HTTPREQUEST_ERROR + '\n' + 115 | 'httpRequest_.status: ' + BlocklyStorage.httpRequest_.status); 116 | } else { 117 | var data = BlocklyStorage.httpRequest_.responseText.trim(); 118 | if (BlocklyStorage.httpRequest_.name == 'xml') { 119 | window.location.hash = data; 120 | BlocklyStorage.alert(BlocklyStorage.LINK_ALERT.replace('%1', 121 | window.location.href)); 122 | } else if (BlocklyStorage.httpRequest_.name == 'key') { 123 | if (!data.length) { 124 | BlocklyStorage.alert(BlocklyStorage.HASH_ERROR.replace('%1', 125 | window.location.hash)); 126 | } else { 127 | BlocklyStorage.loadXml_(data); 128 | } 129 | } 130 | BlocklyStorage.monitorChanges_(); 131 | } 132 | BlocklyStorage.httpRequest_ = null; 133 | } 134 | }; 135 | 136 | /** 137 | * Start monitoring the workspace. If a change is made that changes the XML, 138 | * clear the key from the URL. Stop monitoring the workspace once such a 139 | * change is detected. 140 | * @private 141 | */ 142 | BlocklyStorage.monitorChanges_ = function() { 143 | var startXmlDom = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace()); 144 | var startXmlText = Blockly.Xml.domToText(startXmlDom); 145 | function change() { 146 | var xmlDom = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace()); 147 | var xmlText = Blockly.Xml.domToText(xmlDom); 148 | if (startXmlText != xmlText) { 149 | window.location.hash = ''; 150 | Blockly.removeChangeListener(bindData); 151 | } 152 | } 153 | var bindData = Blockly.addChangeListener(change); 154 | }; 155 | 156 | /** 157 | * Load blocks from XML. 158 | * @param {string} xml Text representation of XML. 159 | * @private 160 | */ 161 | BlocklyStorage.loadXml_ = function(xml) { 162 | try { 163 | xml = Blockly.Xml.textToDom(xml); 164 | } catch (e) { 165 | BlocklyStorage.alert(BlocklyStorage.XML_ERROR + '\nXML: ' + xml); 166 | return; 167 | } 168 | // Clear the workspace to avoid merge. 169 | Blockly.getMainWorkspace().clear(); 170 | Blockly.Xml.domToWorkspace(Blockly.getMainWorkspace(), xml); 171 | }; 172 | 173 | /** 174 | * Present a text message to the user. 175 | * Designed to be overridden if an app has custom dialogs, or a butter bar. 176 | * @param {string} message Text to alert. 177 | */ 178 | BlocklyStorage.alert = function(message) { 179 | window.alert(message); 180 | }; 181 | -------------------------------------------------------------------------------- /appengine/storage.py: -------------------------------------------------------------------------------- 1 | """Blockly Demo: Storage 2 | 3 | Copyright 2012 Google Inc. 4 | http://blockly.googlecode.com/ 5 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); 7 | you may not use this file except in compliance with the License. 8 | You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, software 13 | distributed under the License is distributed on an "AS IS" BASIS, 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | See the License for the specific language governing permissions and 16 | limitations under the License. 17 | """ 18 | 19 | """Store and retrieve XML with App Engine. 20 | """ 21 | 22 | __author__ = "q.neutron@gmail.com (Quynh Neutron)" 23 | 24 | import cgi 25 | from random import randint 26 | from google.appengine.ext import db 27 | from google.appengine.api import memcache 28 | import logging 29 | 30 | print "Content-Type: text/plain\n" 31 | 32 | def keyGen(): 33 | # Generate a random string of length KEY_LEN. 34 | KEY_LEN = 6 35 | CHARS = "abcdefghijkmnopqrstuvwxyz23456789" # Exclude l, 0, 1. 36 | max_index = len(CHARS) - 1 37 | return "".join([CHARS[randint(0, max_index)] for x in range(KEY_LEN)]) 38 | 39 | class Xml(db.Model): 40 | # A row in the database. 41 | xml_hash = db.IntegerProperty() 42 | xml_content = db.TextProperty() 43 | 44 | forms = cgi.FieldStorage() 45 | if "xml" in forms: 46 | # Store XML and return a generated key. 47 | xml_content = forms["xml"].value 48 | xml_hash = hash(xml_content) 49 | lookup_query = db.Query(Xml) 50 | lookup_query.filter("xml_hash =", xml_hash) 51 | lookup_result = lookup_query.get() 52 | if lookup_result: 53 | xml_key = lookup_result.key().name() 54 | else: 55 | trials = 0 56 | result = True 57 | while result: 58 | trials += 1 59 | if trials == 100: 60 | raise Exception("Sorry, the generator failed to get a key for you.") 61 | xml_key = keyGen() 62 | result = db.get(db.Key.from_path("Xml", xml_key)) 63 | xml = db.Text(xml_content, encoding="utf_8") 64 | row = Xml(key_name = xml_key, xml_hash = xml_hash, xml_content = xml) 65 | row.put() 66 | print xml_key 67 | 68 | if "key" in forms: 69 | # Retrieve stored XML based on the provided key. 70 | key_provided = forms["key"].value 71 | # Normalize the string. 72 | key_provided = key_provided.lower().strip() 73 | # Check memcache for a quick match. 74 | xml = memcache.get("XML_" + key_provided) 75 | if xml is None: 76 | # Check datastore for a definitive match. 77 | result = db.get(db.Key.from_path("Xml", key_provided)) 78 | if not result: 79 | xml = "" 80 | else: 81 | xml = result.xml_content 82 | # Save to memcache for next hit. 83 | if not memcache.add("XML_" + key_provided, xml, 3600): 84 | logging.error("Memcache set failed.") 85 | print xml.encode("utf-8") 86 | -------------------------------------------------------------------------------- /apps/code/blocks-bit.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Bit API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Bit API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.ValueBlock'); 26 | 27 | Blockly.ComputerCraft.BIT_BLOCK_COLOUR_ = 290; 28 | 29 | Blockly.ComputerCraft.BIT_FUNCS_ = [ 30 | {blockName: 'shift', 31 | text: 'shift %1 %2 %3 bits', 32 | args: [['N', 'Number'], 33 | ['OPTION', new Blockly.FieldDropdown( 34 | [['left', 'blshift'], 35 | ['right arithmetically', 'brshift'], 36 | ['right logically', 'blogic_rshift']])], 37 | ['BITS', 'Number']], 38 | output: 'Number', 39 | ddFuncName: 'OPTION', 40 | tooltip: 41 | 'Shift the number by the specified number of bits.\n' + 42 | 'This inputs are treated as 32-bit unsigned integers.\n' + 43 | 'An error is raises if either input is greater than 2^32 - 1.', 44 | helpUrlType: Blockly.ComputerCraft.HelpUrlType.PREFIX_DD 45 | }, 46 | {blockName: 'bitwise', 47 | text: '%1 %2 %3', 48 | args: [['X', 'Number'], 49 | ['FUNCTION', new Blockly.FieldDropdown( 50 | [['XOR', 'bxor'], 51 | ['OR', 'bor'], 52 | ['AND', 'band']])], 53 | ['Y', 'Number']], 54 | output: 'Number', 55 | ddFuncName: 'FUNCTION', 56 | tooltip: function(block) { 57 | var HELP = {'bxor': {name: 'exclusive OR', description: 'exactly one'}, 58 | 'bor': {name: 'inclusive OR', description: 'at least one'}, 59 | 'band': {name: 'AND', description: 'both'}}; 60 | var help = HELP[block.getTitleValue('FUNCTION')]; 61 | return 'Compute the bitwise ' + help.name.toUpperCase() 62 | + ' of the two numeric inputs.\n' + 63 | 'For each of the 32 bit positions, the value is 1 if and only if\n' + 64 | help.description.toUpperCase() 65 | + ' of the corresponding bits in the inputs are 1.'; 66 | } 67 | }, 68 | {funcName: 'bnot', 69 | text: 'bitwise not %1', 70 | args: [['X', 'Number']], 71 | output: 'Number', 72 | tooltip: 73 | 'Compute the bitwise inverse of a number, taken in\n' + 74 | 'the domain and range of 32-bit unsigned integers.'} 75 | ]; 76 | 77 | Blockly.ComputerCraft.BIT_FUNCS_.forEach(function(info) { 78 | Blockly.ComputerCraft.buildValueBlock( 79 | 'bit', Blockly.ComputerCraft.BIT_BLOCK_COLOUR_, info)}); 80 | -------------------------------------------------------------------------------- /apps/code/blocks-colours.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Colours API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Colours blocks for ComputerCraft. 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.ValueBlock'); 26 | goog.require('ComputerCraft.VarArgsBlock'); 27 | 28 | Blockly.ComputerCraft.COLOURS_BLOCK_COLOUR_ = 80; 29 | 30 | /** 31 | * An array of colours for the palette. The rgb attributes may be tweaked, 32 | * but the value attributes must not, since they correspond to ones on 33 | * "http://computercraft.info/wiki/Colors_%28API%29". 34 | */ 35 | var COLOURS_ = { 36 | white: {rgb: '#f0f0f0', value: 1 << 0 }, 37 | orange: {rgb: '#f2b233', value: 1 << 1 }, 38 | magenta: {rgb: '#e57fd8', value: 1 << 2 }, 39 | lightBlue: {rgb: '#99b2f2', value: 1 << 3 }, 40 | yellow: {rgb: '#dede6c', value: 1 << 4 }, 41 | lime: {rgb: '#7fcc19', value: 1 << 5 }, 42 | pink: {rgb: '#f2b2cc', value: 1 << 6 }, 43 | grey: {rgb: '#4c4c4c', value: 1 << 7 }, 44 | lightGrey: {rgb: '#999999', value: 1 << 8 }, 45 | cyan: {rgb: '#4c99b2', value: 1 << 9 }, 46 | purple: {rgb: '#b266e5', value: 1 << 10 }, 47 | blue: {rgb: '#253192', value: 1 << 11 }, 48 | brown: {rgb: '#7f664c', value: 1 << 12 }, 49 | green: {rgb: '#57a64e', value: 1 << 13 }, 50 | red: {rgb: '#cc4c4c', value: 1 << 14 }, 51 | black: {rgb: '#191919', value: 1 << 15 } 52 | }; 53 | 54 | /** 55 | * The order in which colours should appear on the palette. 56 | * Array.prototype.map is supported by IE 9+ and all other major browsers. 57 | * This overrides a value defined in core/field_colour.js 58 | * (via blockly_[un]compressed.js). 59 | */ 60 | Blockly.FieldColour.COLOURS = Object.keys(COLOURS_).map( 61 | function(name) { 62 | return COLOURS_[name].rgb; 63 | }); 64 | 65 | /** 66 | * Number of columns in the palette. 67 | * This overrides a value defined in core/field_colour.js 68 | * (via blockly_[un]compressed.js). 69 | */ 70 | Blockly.FieldColour.COLUMNS = 4; 71 | 72 | Blockly.Blocks['colours_picker'] = { 73 | // Colour picker. 74 | init: function() { 75 | this.setHelpUrl('http://computercraft.info/wiki/Colors_%28API%29'); 76 | this.setColour(Blockly.ComputerCraft.COLOURS_BLOCK_COLOUR_); 77 | this.appendDummyInput() 78 | .appendTitle(new Blockly.FieldColour('#cc4c4c'), 'COLOUR'); 79 | this.setOutput(true, 'Number'); 80 | this.setTooltip( 81 | 'Choose among colours (from left to right, top to bottom):\n' + 82 | 'white, orange, magenta, light blue,\n' + 83 | 'yellow, lime, pink, grey,\n' + 84 | 'light gray, cyan purple, blue,\n' + 85 | 'brown, green, red, black.'); 86 | } 87 | }; 88 | 89 | // TODO: Change block colour to match chosen colour, if not too hard. Neil? 90 | Blockly.ComputerCraft.buildValueBlock( 91 | 'colours', 92 | Blockly.ComputerCraft.COLOURS_BLOCK_COLOUR_, 93 | {blockName: 'list', 94 | text: '%1', 95 | args: [['COLOUR', 96 | new Blockly.FieldDropdown(Object.keys(COLOURS_).map( 97 | function(name) { 98 | return [Blockly.ComputerCraft.convertFromCamelCase(name). 99 | replace('_', ' '), 100 | name]; 101 | }))]], 102 | output: 'Number', 103 | tooltip: 'Choose among colours by name.'}); 104 | 105 | Blockly.Lua['colours_list'] = function(block) { 106 | var code = 'colours.' + block.getTitleValue('COLOUR'); 107 | return [code, Blockly.Lua.ORDER_HIGH]; 108 | }; 109 | 110 | Blockly.Lua['colours_picker'] = function(block) { 111 | // Generate Lua code for the selected ComputerCraft colour. 112 | var colour = block.inputList[0].titleRow[0].colour_; 113 | var keys = Object.keys(COLOURS_); 114 | for (var x = 0; x < keys.length; x++) { 115 | var key = keys[x]; 116 | var entry = COLOURS_[key]; 117 | if (entry.rgb == colour) { 118 | return ['colours.' + key, Blockly.Lua.ORDER_HIGH]; 119 | } 120 | } 121 | goog.asserts.fail('Error in colour_picker'); 122 | }; 123 | 124 | Blockly.ComputerCraft.buildVarArgsBlock( 125 | 'colours', 126 | Blockly.ComputerCraft.COLOURS_BLOCK_COLOUR_, 127 | {funcName: 'combine', 128 | text: 'combine colours %v into set', 129 | varArgName: 'colour', 130 | varArgType: 'Number', 131 | varArgTooltip: 'A colour to add to the set.', 132 | varArgCount: 2, 133 | varContainerName: 'colours', 134 | varContainerTooltip: 'Add or remove colours.', 135 | output: 'Number', 136 | tooltip: 137 | 'Combine one or more colours (or sets of colours) into a set.\n' + 138 | 'To change the number of colours, click on the star.'}); 139 | 140 | Blockly.ComputerCraft.buildVarArgsBlock( 141 | 'colours', 142 | Blockly.ComputerCraft.COLOURS_BLOCK_COLOUR_, 143 | {funcName: 'subtract', 144 | text: 'subtract colours %v from set %1', 145 | args: [['Set', 'Number']], 146 | varArgName: 'colour', 147 | varArgType: 'Number', 148 | varArgTooltip: 'A colour to subtract.', 149 | varArgCount: 1, 150 | varContainerName: 'colours to remove', 151 | varContainerTooltip: 'Add or remove colours.', 152 | output: 'Number', 153 | tooltip: 154 | 'Remove one or more colours from the specified set of colours.\n' + 155 | 'To change the number of colours to remove, click on the star.\n' + 156 | 'A set can be built with the "combine" block.'}); 157 | 158 | Blockly.ComputerCraft.buildValueBlock( 159 | 'colours', 160 | Blockly.ComputerCraft.COLOURS_BLOCK_COLOUR_, 161 | {funcName: 'test', 162 | text: 'is colour %2 in set %1?', 163 | args: [['Set', 'Number'], 164 | ['Colour', 'Number']], 165 | output: 'Boolean', 166 | tooltip: 167 | 'Test whether the given colour is in the set.\n' + 168 | 'A set can be built with the "combine" block.'}); 169 | -------------------------------------------------------------------------------- /apps/code/blocks-disk.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Disk API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Disk API. 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.SideInputBlock'); 26 | 27 | Blockly.ComputerCraft.DISK_BLOCK_COLOUR_ = 290; 28 | 29 | Blockly.ComputerCraft.DISK_FUNCS_ = [ 30 | {funcName: 'isPresent', 31 | output: 'Boolean', 32 | text: 'an item is in disk drive', 33 | tooltip: 34 | 'Check whether any item (such as a music disk or floppy disk)\n'+ 35 | 'is present in the adjacent disk drive.'}, 36 | {funcName: 'hasData', 37 | output: 'Boolean', 38 | text: 'a floppy disk is in drive', 39 | tooltip: 40 | 'Check whether a floppy disk (as opposed to a music disk) is in the drive.' 41 | }, 42 | {funcName: 'getMountPath', 43 | output: 'String', 44 | text: 'get mount path for floppy disk in drive', 45 | tooltip: 46 | 'Get the directory name where the contents\n' + 47 | 'of the floppy disk can be accessed.'}, 48 | {funcName: 'getLabel', 49 | text: 'read label on floppy disk in drive', 50 | output: 'String', 51 | tooltip: 'Read the label of the floppy disk in the drive.'}, 52 | {funcName: 'getID', 53 | text: 'get ID of floppy disk in drive', 54 | output: 'Number', 55 | tooltip: 'Get the floppy disk\'s unique numeric identifier.'}, 56 | {funcName: 'hasAudio', 57 | text: 'music disk is in drive', 58 | output: 'Boolean', 59 | tooltip: 'Check if a music disk is in the drive.'}, 60 | {funcName: 'getAudioTitle', 61 | text: 'get title of music disk in drive', 62 | output: 'String', 63 | tooltip: 'Get the title of the music disk in the drive.'}, 64 | {funcName: 'playAudio', 65 | text: 'play music disk in drive', 66 | tooltip: 'Starts playing the music disk in the drive.'}, 67 | {funcName: 'stopAudio', 68 | text: 'stop music disk in drive', 69 | tooltip: 'Stop playing the music disk in the drive.'}, 70 | {funcName: 'eject', 71 | text: 'eject from drive', 72 | tooltip: 73 | 'Eject any item currently in the drive,\n' + 74 | 'spilling it into the world as a loose item.'}, 75 | {funcName: 'setLabel', 76 | // The label input is added below. 77 | text: 'Set the label on the disk in drive to %1', 78 | args: [['LABEL', 'String']], 79 | tooltip: 'Write to the label to the floppy disk in the attached drive.'}]; 80 | 81 | Blockly.ComputerCraft.DISK_FUNCS_.forEach(function(info) { 82 | Blockly.ComputerCraft.buildSideInputBlock( 83 | 'disk', Blockly.ComputerCraft.DISK_BLOCK_COLOUR_, info);}); 84 | -------------------------------------------------------------------------------- /apps/code/blocks-fs.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft File System API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft File System (FS) API. 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.ValueBlock'); 26 | 27 | Blockly.ComputerCraft.FS_BLOCK_COLOUR_ = 410; 28 | 29 | Blockly.ComputerCraft.FS_FUNCS_ = [ 30 | {funcName: 'list', 31 | output: 'Table', 32 | text: 'list files in %1', 33 | args: [['DIR', 'String']], 34 | tooltip: 35 | 'Returns a list of all the files\n' + 36 | '(including subdirectories but not their contents)\n' + 37 | 'contained in a directory, as a numerically indexed table.'}, 38 | {funcName: 'exists', 39 | output: 'Boolean', 40 | text: 'path %1 exists?', 41 | args: [['PATH', 'String']], 42 | tooltip: 43 | 'Checks if its input refers to an existing file or directory,\n' + 44 | 'returning true if it does, false otherwise'}, 45 | {funcName: 'isReadOnly', 46 | output: 'Boolean', 47 | text: 'is path %1 read-only?', 48 | args: [['PATH', 'String']], 49 | tooltip: 50 | 'Checks if a file or directory is read-only (cannot be modified),\n' + 51 | 'returning true if it is, false otherwise'}, 52 | {funcName: 'getName', 53 | output: 'String', 54 | text: 'get file name from path %1', 55 | args: [['PATH', 'String']], 56 | tooltip: 57 | 'Gets the final part (file name) from a path'}, 58 | {funcName: 'getDrive', 59 | output: 'String', 60 | text: 'get drive type for path %1', 61 | args: [['PATH', 'String']], 62 | tooltip: 63 | 'Returns the type of the storage medium holding a path,\n' + 64 | 'or nil if the path does not exist. This is one of\n' + 65 | '“hdd” if the path is stored on the computer’s local hard drive,\n' + 66 | '“rom” if the path is in ROM, or the side on which a disk drive\n' + 67 | 'is attached if the path is on a floppy disk.'}, 68 | {funcName: 'getSize', 69 | output: 'Number', 70 | text: 'get size of file %1', 71 | args: [['PATH', 'String']], 72 | tooltip: 'Gets the amount of space taken up by the file, in bytes.\n'}, 73 | {funcName: 'getFreeSpace', 74 | output: 'Number', 75 | text: 'get amount of free space in %1', 76 | args: [['DIR', 'String']], 77 | tooltip: 'Gets the amount of free space in the given directory, in bytes.'}, 78 | {funcName: 'makeDir', 79 | stmtConns: Blockly.ComputerCraft.StmtConns.BOTH, 80 | text: 'create directory %1', 81 | args: [['DIR', 'String']], 82 | tooltip: 83 | 'Creates a directory with the given path,\n' + 84 | 'creating any missing parent components.\n' + 85 | 'If the location is already a directory, nothing changes.\n' + 86 | 'If the location is already a file, an error occurs.'}, 87 | {funcName: 'move', 88 | stmtConns: Blockly.ComputerCraft.StmtConns.BOTH, 89 | text: 'move file/directory from %1 to %2', 90 | args: [['PATH1', 'String'], ['PATH2', 'String']], 91 | tooltip: 'Moves a file or directory to a new location.\n' + 92 | 'The parent of the new location must be an existing writeable directory.\n' + 93 | 'The second input must include a file name and that file must not yet exist.' 94 | }, 95 | {funcName: 'copy', 96 | stmtConns: Blockly.ComputerCraft.StmtConns.BOTH, 97 | text: 'copy file/directory from %1 to %2', 98 | args: [['PATH1', 'String'], ['PATH2', 'String']], 99 | tooltip: 'Copies a file or directory to a new location.\n' + 100 | 'The parent of the new location must be an existing writeable directory.\n' + 101 | 'The second input must include a file name and that file must not yet exist.' 102 | }, 103 | {funcName: 'delete', 104 | stmtConns: Blockly.ComputerCraft.StmtConns.BOTH, 105 | text: 'delete file/directory %1', 106 | args: [['PATH', 'String']], 107 | tooltip: 'Deletes a file or directory and its contents.\n' + 108 | 'Nothing happens if the file/directory does not exist.'}, 109 | {funcName: 'combine', 110 | output: 'String', 111 | text: 'combine file paths %1 and %2', 112 | args: [['PATH1', 'String'], ['PATH2', 'String']], 113 | tooltip: 'Combines two path components, returning a path\n' + 114 | 'consisting of the second path nexted in the first path.\n' + 115 | 'Neither path needs to exist; this function only manipulates strings.'} 116 | ]; 117 | 118 | Blockly.ComputerCraft.FS_FUNCS_.forEach(function(info) { 119 | Blockly.ComputerCraft.buildValueBlock( 120 | 'fs', Blockly.ComputerCraft.FS_BLOCK_COLOUR_, info);}); 121 | -------------------------------------------------------------------------------- /apps/code/blocks-help.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Help API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Help API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.ValueBlock'); 26 | 27 | Blockly.ComputerCraft.HELP_BLOCK_COLOUR_ = 290; 28 | 29 | Blockly.ComputerCraft.HELP_FUNCS_ = [ 30 | {funcName: 'path', 31 | text: 'get help path', 32 | output: 'String', 33 | tooltip: 34 | 'Get the list of locations that will be searched for help files,\n' + 35 | 'as a string containing multiple search paths separated by colons.'}, 36 | {funcName: 'setPath', 37 | text: 'set help path to %1', 38 | args: [['PATH', 'String']], 39 | tooltip: 40 | 'Set the list of locations to be searched for help files.\n' + 41 | 'The input should be a string containing search paths separated by colons.'}, 42 | {funcName: 'lookup', 43 | text: 'get help path for %1', 44 | args: [['TOPIC', 'String']], 45 | tooltip: 46 | 'Get the path to the file containing the help page for the given topic.\n' + 47 | 'If it cannot be found, the result will be null.'}, 48 | {funcName: 'topics', 49 | text: 'get list of help topics', 50 | tooltip: 'Get a list of all available help topics.', 51 | output: 'List'}]; 52 | 53 | Blockly.ComputerCraft.HELP_FUNCS_.forEach(function(info) { 54 | Blockly.ComputerCraft.buildValueBlock( 55 | 'help', Blockly.ComputerCraft.HELP_BLOCK_COLOUR_, info);}); 56 | -------------------------------------------------------------------------------- /apps/code/blocks-keys.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Keys API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Keys API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.ValueBlock'); 26 | 27 | Blockly.ComputerCraft.KEYS_BLOCK_COLOUR_ = 290; 28 | 29 | Blockly.ComputerCraft.buildValueBlock( 30 | 'keys', 31 | Blockly.ComputerCraft.KEYS_BLOCK_COLOUR_, 32 | {funcName: 'getName', 33 | text: 'get key name from code %1', 34 | args: [['CODE', 'Number']], 35 | output: 'String', 36 | tooltip: 'Get the name of a keyboard key from its numeric scan code.' 37 | } 38 | ); 39 | -------------------------------------------------------------------------------- /apps/code/blocks-old.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua 3 | * 4 | * Copyright 2012 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Deprecated blocks for ComputerCraft turtles. 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | 26 | Blockly.ComputerCraft.deprecatedOnChange = function(newBlock) { 27 | return function() { 28 | if (!this.workspace) { 29 | // Block has been deleted. 30 | return; 31 | } 32 | this.setWarningText('This block has been deprecated. Please replace it with the "' + newBlock + '" block.'); 33 | }; 34 | }; 35 | 36 | 37 | Blockly.Blocks['turtle_get_item_count'] = { 38 | // Block for returning the number of items in the supplied slot. 39 | init: function() { 40 | this.setColour(Blockly.ComputerCraft.TURTLE_BLOCK_COLOUR_); 41 | this.appendValueInput('VALUE') 42 | .setCheck('Number') 43 | .appendTitle('get item count in slot'); 44 | this.setOutput(true, 'Number'); 45 | this.setTooltip('Get the count of items in the supplied slot number.'); 46 | }, 47 | onchange: Blockly.ComputerCraft.deprecatedOnChange('get [item count/free space] in slot') 48 | }; 49 | 50 | Blockly.Lua['turtle_get_item_count'] = function(block) { 51 | // Generate Lua for getting the number of items in the supplied slot number 52 | var argument0 = Blockly.Lua.valueToCode( 53 | block, 'VALUE', Blockly.Lua.ORDER_NONE) || ''; 54 | var code = 'turtle.getItemCount(' + argument0 + ')'; 55 | return [code, Blockly.Lua.ORDER_NONE]; 56 | } 57 | 58 | Blockly.Blocks['turtle_get_item_space'] = { 59 | // Block for getting the number of items that can be put in the numbered 60 | // slot. 61 | init: function() { 62 | this.setColour(Blockly.ComputerCraft.TURTLE_BLOCK_COLOUR_); 63 | this.appendValueInput('VALUE') 64 | .setCheck('Number') 65 | .appendTitle('get free space in slot'); 66 | this.setOutput(true, 'Number'); 67 | this.setTooltip('Get the number of items that can be placed in the numbered slot.'); 68 | }, 69 | onchange: Blockly.ComputerCraft.deprecatedOnChange('get [item count/free space] in slot') 70 | }; 71 | 72 | Blockly.Lua['turtle_get_item_space'] = function(block) { 73 | // Generate Lua for getting the number of items that can be put in the 74 | // numbered slot. 75 | var argument0 = Blockly.Lua.valueToCode( 76 | block, 'VALUE', Blockly.Lua.ORDER_NONE) || ''; 77 | var code = 'turtle.getItemSpace(' + argument0 + ')'; 78 | return [code, Blockly.Lua.ORDER_NONE]; 79 | } 80 | -------------------------------------------------------------------------------- /apps/code/blocks-os.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Operating System (OS) API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Operating System (OS) API. 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.DependentInputBlock'); 26 | goog.require('ComputerCraft.ValueBlock'); 27 | goog.require('ComputerCraft.VarArgsBlock'); 28 | 29 | Blockly.ComputerCraft.OS_BLOCK_COLOUR_ = 30; 30 | 31 | Blockly.ComputerCraft.OS_FUNCS_ = [ 32 | {funcName: 'terminate', 33 | // This has a previous, but not a following, statement, which is rare. 34 | stmtConns: Blockly.ComputerCraft.StmtConns.PREVIOUS, 35 | text: 'terminate with error %1', 36 | args: [['MSG', null]], 37 | tooltip: 38 | 'End the program with the provided error message. ' + 39 | 'This should not be used for normal termination.' 40 | }, 41 | {funcName: 'sleep', 42 | text: 'sleep %1 seconds', 43 | args: [['VALUE', 'Number']], 44 | tooltip: 'Sleep for the specified number of seconds.' 45 | }, 46 | {funcName: 'version', 47 | text: 'get OS version', 48 | output: 'String', 49 | tooltip: 'Get the name of the version of CraftOS being used.'}, 50 | {funcName: 'getComputerID', 51 | text: 'get computer ID', 52 | output: 'Number', 53 | tooltip: 'Get the unique numeric ID of this computer.'}, 54 | {funcName: 'getComputerLabel', 55 | text: 'get computer label', 56 | output: 'String', 57 | tooltip: 58 | 'Get the label of this computer.\n' + 59 | 'The label can be set with "set computer label".'}, 60 | {funcName: 'setComputerLabel', 61 | text: 'set computer label to %1', 62 | args: [['LABEL', 'String']], 63 | tooltip: 64 | 'Set the label of this computer.\n' + 65 | 'The label can be read with "get computer label".'}, 66 | {blockName: 'api', 67 | text: '%1 API %2', 68 | args: [['OPTION', 69 | new Blockly.FieldDropdown([['load', 'loadAPI'], 70 | ['unload', 'unloadAPI']])], 71 | ['NAME', 'String']], 72 | ddFuncName: 'OPTION', 73 | tooltip: 'Load or unload a Lua script as an API in its own namespace.'}, 74 | {funcName: 'clock', 75 | text: 'get running time', 76 | output: 'Number', 77 | tooltip: 'Get the amount of time this computer has been running, in seconds.'}, 78 | {funcName: 'setAlarm', 79 | text: 'set alarm for %1 s', 80 | args: [['TIME', 'Number']], 81 | output: 'Table', 82 | tooltip: 83 | 'Queue an alarm event to occur after the specified number of seconds.\n' + 84 | 'The ID of the alarm, which is a table, is returned.'}, 85 | {funcName: 'time_day', 86 | ddFuncName: 'OPTION', 87 | text: 'get in-game %1', 88 | args: [['OPTION', 89 | new Blockly.FieldDropdown( 90 | [['time', 'time'], ['day', 'day']])]], 91 | output: 'Number', 92 | tooltip: 93 | 'Get the current in-game time or day.\n' + 94 | 'Time can be converted into a string with\n' + 95 | 'the "format time" block in Text Utilities.\n' + 96 | 'Day is the number of days since the world creation.'}, 97 | {funcName: 'shutdown', 98 | text: 'shut down computer', 99 | tooltip: 'Turn off this computer.'}, 100 | {funcName: 'reboot', 101 | text: 'reboot computer', 102 | tooltip: 'Reboot this computer.'}, 103 | {funcName: 'startTimer', 104 | text: 'start timer for %1 s', 105 | args: [['TIME', 'Number']], 106 | output: 'Number', 107 | expStmt: true, 108 | tooltip: 109 | 'Queue a timer event to occur after\n' + 110 | 'the specified number of seconds.\n' + 111 | 'The ID of the timer is returned.'} 112 | ]; 113 | 114 | Blockly.ComputerCraft.OS_FUNCS_.forEach(function(info) { 115 | Blockly.ComputerCraft.buildValueBlock( 116 | 'os', Blockly.ComputerCraft.OS_BLOCK_COLOUR_, info);}); 117 | 118 | 119 | // Added in order to continue to support programs with the old block name. 120 | Blockly.Blocks['terminate'] = Blockly.Blocks['os_terminate']; 121 | Blockly.Lua['terminate'] = Blockly.Lua['os_terminate']; 122 | 123 | Blockly.ComputerCraft.buildVarArgsBlock( 124 | 'os', 125 | Blockly.ComputerCraft.OS_BLOCK_COLOUR_, 126 | {funcName: 'queueEvent', 127 | text: 'queue event %1 %v', 128 | args: [['EVENT', 'String']], 129 | varArgName: 'name', 130 | varArgType: true, 131 | varArgTooltip: 'A parameter to pass to the event.', 132 | varArgTitle: 'with parameters', 133 | varArgCount: 1, 134 | varContainerName: 'parameters', 135 | varContainerTitle: 'Add, remove, or reorder parameters to pass to the event.', 136 | tooltip: 137 | 'Add an event to the event queue with the given name and parameters.'}); 138 | 139 | Blockly.ComputerCraft.buildDependentInputBlock( 140 | 'os', 141 | Blockly.ComputerCraft.OS_BLOCK_COLOUR_, 142 | {funcName: 'pullEvent', 143 | text: 'pull %1 %2', 144 | output: ['String', 'Number', 'Colour', 'Table', 'Vector', 'Function'], 145 | multipleOutputs: Infinity, // Not really infinite, but unbounded. 146 | args: [['OPTION*', 147 | [['any event', 'any'], 148 | ['named event', 'named*'], 149 | ['raw event', 'raw']]], 150 | ['EVENT^', 'String']], 151 | // pullEvent is used if OPTION is 'any' or 'named'; 152 | // pullEventRaw is used if OPTION is 'raw'. 153 | suppressLua: true, 154 | tooltip: 155 | 'Block until the computer receives an event,\n' + 156 | 'outputting the name of the event and its parameters.\n' + 157 | 'The "terminate" event is only caught in raw mode.'}); 158 | 159 | Blockly.Lua['os_pull_event'] = function(block) { 160 | var code = block.generateLua(); 161 | if (block.getTitleValue('OPTION') == 'raw') { 162 | code[0] = code[0].replace('pullEvent', 'pullEventRaw'); 163 | } 164 | return code; 165 | }; 166 | 167 | Blockly.ComputerCraft.buildVarArgsBlock( 168 | 'os', 169 | Blockly.ComputerCraft.OS_BLOCK_COLOUR_, 170 | {funcName: 'run', 171 | text: 'run program %2 with environment %1 %v', 172 | args: [['ENV', 'Table'], 173 | ['PATH', 'String']], 174 | varArgName: 'parameter', 175 | varArgType: true, 176 | varArgTooltip: 'A parameter to pass to the program.', 177 | varArgTitle: 'and parameters', 178 | varArgCount: 1, 179 | varContainerName: 'parameters', 180 | varContainerTooltip: 'Add, remove, or reorder parameters.', 181 | tooltip: 182 | 'Run the program at the specified path with the given\n' + 183 | 'environment table. Providing parameters is optional.\n' + 184 | 'Parameters can be added or removed by clicking on the star.'}); 185 | -------------------------------------------------------------------------------- /apps/code/blocks-paintutils.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Paintutils API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Paintutils API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.ValueBlock'); 26 | 27 | Blockly.ComputerCraft.PAINTUTILS_BLOCK_COLOUR_ = 310; 28 | 29 | Blockly.ComputerCraft.PAINTUTILS_FUNCS_ = [ 30 | {funcName: 'loadImage', 31 | text: 'load image from path %1', 32 | args: [['PATH', 'String']], 33 | output: 'Image', 34 | tooltip: 35 | 'Load an image object from the specified path.'}, 36 | {funcName: 'drawImage', 37 | text: 'draw image %1 at (%2, %3)', 38 | args: [['IMAGE', 'Image'], 39 | ['X', 'Number'], 40 | ['Y', 'Number']], 41 | tooltip: 42 | 'Draw an image at the specified (x, y) coordinates.\n' + 43 | 'An image can be obtained with the "load image" block.'}, 44 | {funcName: 'drawPixel', 45 | text: 'draw %3 pixel at (%1, %2)', 46 | args: [['X', 'Number'], 47 | ['Y', 'Number'], 48 | ['COLOUR', 'Colour']], 49 | tooltip: 50 | 'Draw a pixel (dot) at the specified (x, y) coordinates\n' + 51 | 'in the specified colour.'}, 52 | {funcName: 'drawLine', 53 | text: 'draw %5 line from (%1, %2) to (%3, %4)', 54 | args: [['X1', 'Number'], 55 | ['Y1', 'Number'], 56 | ['X2', 'Number'], 57 | ['Y2', 'Number'], 58 | ['COLOUR', 'Colour']], 59 | tooltip: 60 | 'Draw a line in the specified colour from the first pair\n' + 61 | 'of (x, y) coordinates to the second pair of (x, y) coordinates.'}]; 62 | 63 | Blockly.ComputerCraft.PAINTUTILS_FUNCS_.forEach(function(info) { 64 | Blockly.ComputerCraft.buildValueBlock( 65 | 'paintutils', Blockly.ComputerCraft.PAINTUTILS_BLOCK_COLOUR_, info);}); 66 | -------------------------------------------------------------------------------- /apps/code/blocks-rednet.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Rednet API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Rednet API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.DependentInputBlock'); 26 | goog.require('ComputerCraft.ValueBlock'); 27 | 28 | Blockly.ComputerCraft.REDNET_BLOCK_COLOUR_ = 290; 29 | 30 | Blockly.ComputerCraft.REDNET_FUNCS_ = [ 31 | {funcName: 'open', 32 | text: 'open rednet on %1', 33 | args: [['SIDE', new Blockly.FieldDropdown(Blockly.ComputerCraft.ValueBlock.SIDES)]], 34 | tooltip: 35 | 'Tell the computer that the side can be used for networking.'}, 36 | {funcName: 'close', 37 | text: 'close rednet on %1', 38 | args: [['SIDE', new Blockly.FieldDropdown(Blockly.ComputerCraft.ValueBlock.SIDES)]], 39 | tooltip: 40 | 'Tell the computer that the side can no longer be used for networking.'}, 41 | {funcName: 'isOpen', 42 | text: 'is rednet open on %1?', 43 | args: [['SIDE', new Blockly.FieldDropdown(Blockly.ComputerCraft.ValueBlock.SIDES)]], 44 | output: 'Boolean', 45 | tooltip: 46 | 'Check if rednet is open on the specified side.'}, 47 | {funcName: 'run', 48 | text: 'run rednet', 49 | tooltip: 'Run rednet.'}]; 50 | 51 | Blockly.ComputerCraft.REDNET_FUNCS_.forEach(function(info) { 52 | Blockly.ComputerCraft.buildValueBlock( 53 | 'rednet', Blockly.ComputerCraft.REDNET_BLOCK_COLOUR_, info);}); 54 | 55 | Blockly.ComputerCraft.buildDependentInputBlock( 56 | 'rednet', 57 | Blockly.ComputerCraft.REDNET_BLOCK_COLOUR_, 58 | {blockName: 'broadcast', 59 | text:'broadcast %1 %2 on rednet', 60 | args: [['OPTION*', 61 | [['empty message', 'announce'], 62 | ['message', 'broadcast*']]], 63 | ['MESSAGE^', 'String']], 64 | ddFuncName: 'OPTION'}); 65 | 66 | Blockly.ComputerCraft.buildDependentInputBlock( 67 | 'rednet', 68 | Blockly.ComputerCraft.REDNET_BLOCK_COLOUR_, 69 | {funcName: 'receive', 70 | text:'wait %1 %2 for rednet message', 71 | args: [['SECONDS^', 'Number'], 72 | ['OPTION*', 73 | [['forever', 'forever'], 74 | ['seconds', 'seconds*']]]], 75 | multipleOutputs: 3}); 76 | -------------------------------------------------------------------------------- /apps/code/blocks-redstone.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Redstone API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Redstone API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.ValueBlock'); 26 | 27 | Blockly.ComputerCraft.REDSTONE_BLOCK_COLOUR_ = 290; 28 | 29 | Blockly.ComputerCraft.REDSTONE_FUNCS_ = [ 30 | {funcName: 'getSides', 31 | text: 'get sides', 32 | output: 'Table', 33 | tooltip: 34 | 'Get a table whose values are the names of the sides\n' + 35 | '(top, bottom, left, right, front, and back) through\n' + 36 | 'which RedPower cables and redstone could be connected.'}, 37 | {blockName: 'get_input', 38 | text: 'get %1 redstone signal through %2', 39 | args: [['OPTION', 40 | new Blockly.FieldDropdown( 41 | [['digital', 'getInput'], 42 | ['analog', 'getAnalogInput']], 43 | function(value) { 44 | this.sourceBlock_.changeOutput( 45 | value == 'getInput' ? 'Boolean' : 'Number'); 46 | })], 47 | ['SIDE', new Blockly.FieldDropdown(Blockly.ComputerCraft.ValueBlock.SIDES)]], 48 | ddFuncName: 'OPTION', 49 | output: 'Boolean', 50 | tooltip: 51 | 'Get the redstone signal from the cable on the specified side.\n' + 52 | 'Digital signals are either of the boolean values "true" or "false".\n' + 53 | 'Analog signals are in the numeric range 0-15.'}, 54 | {blockName: 'set_output', 55 | text: 'output %1 signal %3 through %2', 56 | args: [['OPTION', 57 | new Blockly.FieldDropdown( 58 | [['digital', 'setOutput'], 59 | ['analog', 'setAnalogOutput']], 60 | function(value) { 61 | this.sourceBlock_.getInput('SIGNAL').setCheck( 62 | value == 'setOutput' ? 'Boolean' : 'Number'); 63 | })], 64 | ['SIDE', new Blockly.FieldDropdown(Blockly.ComputerCraft.ValueBlock.SIDES)], 65 | ['SIGNAL', 'Boolean']], 66 | ddFuncName: 'OPTION', 67 | tooltip: 68 | 'Output the specified signal through an attached redstone cable.\n' + 69 | 'Digital signals are either of the boolean values "true" or "false".\n' + 70 | 'Analog signals are numbers in the range 0-15.'}, 71 | {blockName: 'get_bundled_input', 72 | text: 'get %1 colours on bundled cable on %2', 73 | args: [['OPTION', 74 | new Blockly.FieldDropdown([['all', 'getBundledInput'], 75 | ['activated', 'getBundledOutput']])], 76 | ['SIDE', new Blockly.FieldDropdown(Blockly.ComputerCraft.ValueBlock.SIDES)]], 77 | ddFuncName: 'OPTION', 78 | output: 'Number', 79 | tooltip: 80 | 'Get the set of colours either present (all) or activated\n' + 81 | 'on a RedPower bundled cable connected on the specified side.'}, 82 | {funcName: 'setBundledOutput', 83 | text: 'activate colours %2 on bundled cable on %1', 84 | args: [['SIDE', new Blockly.FieldDropdown(Blockly.ComputerCraft.ValueBlock.SIDES)], 85 | ['COLOURS', 'Number']], 86 | tooltip: 87 | 'Activate the specified coloured wires in the attached bundled cable.\n' + 88 | 'The colour input may be an individual colour, the sum of colours, or\n' + 89 | 'a set of colours, created with the "combine" block in the "Colours" drawer.'}, 90 | {funcName: 'testBundledInput', 91 | text: 'is colour %2 active on bundled cable on %1?', 92 | args: [['SIDE', new Blockly.FieldDropdown(Blockly.ComputerCraft.ValueBlock.SIDES)], 93 | ['COLOURS', 'Number']], 94 | output: 'Boolean', 95 | tooltip: 96 | 'Has the value "true" if the specified colour is\n' + 97 | 'active in the attached bundled cable; otherwise, false.'}]; 98 | 99 | Blockly.ComputerCraft.REDSTONE_FUNCS_.forEach(function(info) { 100 | Blockly.ComputerCraft.buildValueBlock( 101 | 'redstone', Blockly.ComputerCraft.REDSTONE_BLOCK_COLOUR_, info);}); 102 | -------------------------------------------------------------------------------- /apps/code/blocks-shell.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Shell API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Shell API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.DependentInputBlock'); 26 | goog.require('ComputerCraft.ValueBlock'); 27 | goog.require('ComputerCraft.VarArgsBlock'); 28 | 29 | Blockly.ComputerCraft.SHELL_BLOCK_COLOUR_ = 290; 30 | 31 | Blockly.ComputerCraft.SHELL_FUNCS_ = [ 32 | {funcName: 'exit', 33 | text: 'exit program', 34 | tooltip: 35 | 'Exit the current program, or the shell if no programs are running.'}, 36 | {blockName: 'dir', 37 | text: 'get directory', 38 | output: 'String', 39 | tooltip: 'Get the path to the current directory.'}, 40 | {funcName: 'setDir', 41 | text: 'set directory to %1', 42 | args: [['PATH', 'String']], 43 | tooltip: 'Set the working directory.'}, 44 | {funcName: 'path', 45 | text: 'get path', 46 | output: 'String', 47 | tooltip: 'Get a colon-separated string of the directories in the path.'}, 48 | {funcName: 'setPath', 49 | text: 'set path to %1', 50 | args: [['PATH', 'String']], 51 | tooltip: 'Set the path to the colon-separated string.'}, 52 | {funcName: 'resolve', 53 | text: 'resolve %1 %2', 54 | args: [['CHOICE', 55 | new Blockly.FieldDropdown( 56 | [['path', 'resolve'], 57 | ['program', 'resolveProgram']])], 58 | ['PATH', 'String']], 59 | ddFuncName: 'CHOICE', 60 | tooltip: 'Resolve the given local path or program to an absolute path.'}, 61 | {funcName: 'aliases', 62 | output: 'Table', 63 | text: 'get aliases', 64 | tooltip: 65 | 'Get a table containing the default aliases\n' + 66 | 'and any user-specified aliases. The key of\n' + 67 | 'each entry is the alias name, and the value\n' + 68 | 'the name of the associated program.'}, 69 | {funcName: 'programs', 70 | text: 'get %1 files', 71 | output: 'Table', 72 | args: [['FILES', 73 | new Blockly.FieldDropdown( 74 | [['regular', 'false'], 75 | ['all', 'true']])]], 76 | quoteDropdownValues: false, 77 | tooltip: 78 | 'Get a table of files in the current directory and in rom/programs.\n' + 79 | 'Files whose names start with a period will be shown if "all" is selected.'}, 80 | {funcName: 'getRunningProgram', 81 | text: 'get running program', 82 | tooltip: 'Get the path of the currently running shell or program.', 83 | output: 'String'}]; 84 | 85 | Blockly.ComputerCraft.SHELL_FUNCS_.forEach(function(info) { 86 | Blockly.ComputerCraft.buildValueBlock( 87 | 'shell', Blockly.ComputerCraft.SHELL_BLOCK_COLOUR_, info);}); 88 | 89 | Blockly.ComputerCraft.buildDependentInputBlock( 90 | 'shell', 91 | Blockly.ComputerCraft.SHELL_BLOCK_COLOUR_, 92 | {blockName: 'change_alias', 93 | text:'%1 %2 %3', 94 | args: [['OPTION*', 95 | [['set alias', 'setAlias*'], 96 | ['clear alias', 'clearAlias']]], 97 | ['ALIAS', 'String'], 98 | ['VALUE^', 'String']], 99 | depTitle: 'to', 100 | ddFuncName: 'OPTION'}); 101 | 102 | Blockly.ComputerCraft.buildVarArgsBlock( 103 | 'shell', 104 | Blockly.ComputerCraft.SHELL_BLOCK_COLOUR_, 105 | {funcName: 'run', 106 | text: 'run program %1 %v', 107 | args: [['PATH', 'String']], 108 | varArgName: 'argument', 109 | varArgType: 'String', 110 | varArgTitle: 'with arguments', 111 | varArgTooltip: 'Argument to the program.', 112 | varArgCount: 1, 113 | varContainerName: 'arguments', 114 | varContainerTooltip: 'Add, delete, or reorder arguments to the program.', 115 | tooltip: 116 | 'Run the specified program with the provided arguments.\n' + 117 | 'Increase or decrease the number of arguments by clicking the star.'}); 118 | -------------------------------------------------------------------------------- /apps/code/blocks-table.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Table blocks for Blockly Lua. 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | 26 | Blockly.ComputerCraft.TABLE_BLOCK_COLOUR_ = 230; 27 | 28 | Blockly.Blocks['tables_create_empty'] = { 29 | // Create an empty table. 30 | init: function() { 31 | this.setHelpUrl(Blockly.Msg.LISTS_CREATE_EMPTY_HELPURL); 32 | this.setColour(260); 33 | this.setOutput(true, 'Array'); 34 | this.appendDummyInput() 35 | .appendTitle(Blockly.Msg.LISTS_CREATE_EMPTY_TITLE); 36 | this.setTooltip(Blockly.Msg.LISTS_CREATE_EMPTY_TOOLTIP); 37 | } 38 | }; 39 | 40 | Blockly.Blocks['lists_create_with'] = { 41 | // Create a list with any number of elements of any type. 42 | init: function() { 43 | this.setColour(260); 44 | this.appendValueInput('ADD0') 45 | .appendTitle(Blockly.Msg.LISTS_CREATE_WITH_INPUT_WITH); 46 | this.appendValueInput('ADD1'); 47 | this.appendValueInput('ADD2'); 48 | this.setOutput(true, 'Array'); 49 | this.setMutator(new Blockly.Mutator(['lists_create_with_item'])); 50 | this.setTooltip(Blockly.Msg.LISTS_CREATE_WITH_TOOLTIP); 51 | this.itemCount_ = 3; 52 | }, 53 | mutationToDom: function(workspace) { 54 | var container = document.createElement('mutation'); 55 | container.setAttribute('items', this.itemCount_); 56 | return container; 57 | }, 58 | domToMutation: function(container) { 59 | for (var x = 0; x < this.itemCount_; x++) { 60 | this.removeInput('ADD' + x); 61 | } 62 | -------------------------------------------------------------------------------- /apps/code/blocks-term.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Terminal (term) API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Terminal (term) API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.ValueBlock'); 26 | 27 | Blockly.ComputerCraft.TERM_BLOCK_COLOUR_ = 160; 28 | 29 | Blockly.ComputerCraft.TERM_FUNCS_ = [ 30 | {funcName: 'write', 31 | text: 'write %1 to screen', 32 | args: [['Text', 'String']], 33 | tooltip: 34 | 'Write the given string to the screen (terminal or monitor).' 35 | }, 36 | {funcName: 'clear', 37 | text: 'clear screen', 38 | tooltip: 'Clear the entire screen (terminal or monitor).' 39 | }, 40 | {funcName: 'clearLine', 41 | text: 'clear line on screen', 42 | tooltip: 'Clear the line the cursor is on.' 43 | }, 44 | {funcName: 'getCursorPos', 45 | text: 'get cursor position', 46 | output: 'Number', // first output 47 | tooltip: 'Get the x and y coodinates of the cursor.', 48 | multipleOutputs: 2 49 | }, 50 | {funcName: 'setCursorPos', 51 | text: 'set cursor position to (%1, %2)', 52 | args: [['X', 'Number'], ['Y', 'Number']], 53 | tooltip: 'Set the x and y coodinates, respecitvely, of the cursor.' 54 | }, 55 | {funcName: 'isColor', 56 | text: 'computer supports colour?', 57 | output: 'Boolean', 58 | tooltip: 59 | 'Return true if the computer supports colour, false otherwise.\n' + 60 | 'This indicates whether an Advanced Computer is being used.' 61 | }, 62 | {funcName: 'getSize', 63 | text: 'get width and height of screen', 64 | output: 'Number', // first output 65 | multipleOutputs: 2, 66 | tooltip: 67 | 'Get the maximum x-coordinate (width) and y-coordinate (height)\n' + 68 | 'of the screen (terminal or monitor)' 69 | }, 70 | {funcName: 'scroll', 71 | text: 'scroll the screen %1 line(s)', 72 | args: [['NUM', 'Number']], 73 | tooltip: 74 | 'Scroll the screen (terminal or monitor)\n' + 75 | 'the specified number of lines.' 76 | }, 77 | {funcName: 'redirect', 78 | text: 'redirect display to %1', 79 | args: [['TERM', 'Peripheral']], 80 | tooltip: 81 | 'Redirect terminal output to a monitor or other redirect target.\n' + 82 | 'The input is usually the output of "wrap peripheral"\n' + 83 | 'Display can be restored to the original with the "restore display" block.' 84 | }, 85 | {funcName: 'restore', 86 | text: 'restore display', 87 | tooltip: 88 | 'Restore the display to the previous target, \n' + 89 | 'after changing it with the "redirect display" block.' 90 | }, 91 | {funcName: 'setTextColor', 92 | text: 'set text colour to %1', 93 | args: [['COLOUR', ['Colour', 'Number']]], 94 | tooltip: 95 | 'Set the text colour of the terminal.\n' + 96 | 'This is only available for Advanced Computers\n' + 97 | 'and Advanced Monitors.' 98 | }, 99 | {funcName: 'setBackgroundColor', 100 | text: 'set background colour to %1', 101 | args: [['COLOUR', ['Colour', 'Number']]], 102 | tooltip: 103 | 'Set the background colour of the screen.\n' + 104 | 'This is only available for Advanced Computers\n' + 105 | 'and Advanced Monitors.' 106 | }, 107 | {funcName: 'setTextScale', 108 | text: 'scale text by %1', 109 | args: [['SCALE', 'Number']], 110 | tooltip: 111 | 'Set the size of all text on the connected monitor.\n' + 112 | 'The input must be a multiple of .5 between .5 and 5 (inclusive).\n' + 113 | 'This is only available on monitors, not terminals.' 114 | } 115 | ]; 116 | 117 | Blockly.ComputerCraft.TERM_FUNCS_.forEach(function(info) { 118 | Blockly.ComputerCraft.buildValueBlock( 119 | 'term', Blockly.ComputerCraft.TERM_BLOCK_COLOUR_, info);}); 120 | 121 | // Make sure that a numeric literal argument is in the range [.5, 5], 122 | // and divisible by .5. 123 | Blockly.Blocks['term_set_text_scale'].onchange = function() { 124 | if (!this.workspace) { 125 | // Block has been deleted. 126 | return; 127 | } 128 | // If there's no child block, no warning. 129 | if (!this.childBlocks_) { 130 | this.setWarningText(null); 131 | return; 132 | } 133 | // Don't display (or remove) a warning if the child 134 | // block is selected, since editing might be in progress. 135 | // Flaw: No warning is displayed if after typing (but not entering) 136 | // a bad value, user clicks on 'Lua'. 137 | if (Blockly.selected == this.childBlocks_[0]) { 138 | return; 139 | } 140 | // If the input is a constant, make sure it is divisible by .5 141 | // and in the range [.5, 5]. 142 | var code = Blockly.Lua.valueToCode(this, 'SCALE', Blockly.Lua.ORDER_NONE); 143 | if (code) { 144 | var num = Number(code); 145 | if (num != NaN) { 146 | if (num * 2 != Math.floor(num * 2) || num < .5 || num > 5) { 147 | this.setWarningText( 148 | 'The scale value must be a multiple of .5 in the range .5 to 5.'); 149 | return; 150 | } 151 | } 152 | } 153 | this.setWarningText(null); 154 | }; 155 | 156 | // TODO: Convert to ValueBlock. 157 | Blockly.Blocks['term_set_cursor_blink'] = new Blockly.ComputerCraft.Block( 158 | 'term', Blockly.ComputerCraft.TERM_BLOCK_COLOUR_, { 159 | funcName: 'setCursorBlink', 160 | helpUrlType: Blockly.ComputerCraft.HelpUrlType.PREFIX_NAME, 161 | tooltip: 162 | 'Enable or disable blinking of the\n' + 163 | 'cursor on a screen (terminal or monitor).'}); 164 | 165 | Blockly.Blocks['term_set_cursor_blink'].init = function() { 166 | Blockly.ComputerCraft.Block.prototype.init.call(this); 167 | this.appendDummyInput() 168 | .appendTitle( 169 | new Blockly.FieldDropdown( 170 | [['turn on cursor blinking', 'true'], 171 | ['turn off cursor blinking', 'false']]), 172 | 'STATUS') 173 | }; 174 | 175 | Blockly.Lua['term_set_cursor_blink'] = Blockly.ComputerCraft.generateLuaInner_; 176 | -------------------------------------------------------------------------------- /apps/code/blocks-test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft tests 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Test blocks for ComputerCraft. 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | 26 | Blockly.ComputerCraft.buildDependentInputBlock( 27 | 'test', 28 | 200, { 29 | funcName: 'vote', 30 | text: 'Vote for %1: %2 %3', 31 | args: [['OFFICE', [['President', 'president'], 32 | ['Vice President', 'vp']]], 33 | ['CANDIDATE*', [['George Washington', 'washington*'], 34 | ['Abraham Lincoln', 'lincoln'], 35 | ['write-in candidate', 'writein']]], 36 | ['WRITE_IN^', 'String']] 37 | }); 38 | 39 | Blockly.ComputerCraft.buildDependentInputBlock( 40 | 'test', 41 | 200, { 42 | funcName: 'count', 43 | text: '%1 %2', 44 | args: [['ENABLE*', [['Disable dependent input', 'disable'], 45 | ['Enable dependent input', 'enable*']]], 46 | ['AMOUNT^', 'Number']], 47 | addChild: Blockly.ComputerCraft.InputAddType.FIRST}); 48 | 49 | Blockly.ComputerCraft.buildVarArgsBlock( 50 | 'test', 51 | 200, 52 | {blockName: 'varargs', 53 | funcName: 'giveMeMore', 54 | text: 'give me more %v', 55 | varArgName: 'argument', 56 | varArgType: 'String', 57 | varContainerName: 'arguments'}); 58 | -------------------------------------------------------------------------------- /apps/code/blocks-textutils.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Textutils API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Textutils API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | goog.require('ComputerCraft.DependentInputBlock'); 26 | goog.require('ComputerCraft.ValueBlock'); 27 | goog.require('ComputerCraft.VarArgsBlock'); 28 | 29 | Blockly.ComputerCraft.TEXTUTILS_BLOCK_COLOUR_ = 190; 30 | 31 | Blockly.ComputerCraft.TEXTUTILS_FUNCS_ = [ 32 | {blockName: 'write', 33 | text: 'write string %1 %2 to screen at rate %3', 34 | args: [['TEXT', 'String'], 35 | ['MODE', 36 | new Blockly.FieldDropdown([['without newline', 'slowWrite'], 37 | ['with newline', 'slowPrint']])], 38 | ['RATE', 'Number']], 39 | ddFuncName: 'MODE', 40 | tooltip: 41 | 'Write the given string to the screen (terminal or monitor)\n'+ 42 | 'at the current cursor position character-by-character.\n' + 43 | 'The higher the rate, the faster the string is output.\n' + 44 | 'A value of 1 outputs one character per second.\n' + 45 | 'If a newline is used, the next output will be to a new line.' 46 | }, 47 | {funcName: 'slowPrint', 48 | text: 'write line %1 to screen at rate %2', 49 | args: [['TEXT', 'String'], ['RATE', 'Number']], 50 | tooltip: 51 | 'Write the given string to the screen (terminal or monitor)\n'+ 52 | 'at the current cursor position character-by-character.\n' + 53 | 'The higher the rate, the faster the string is output.\n' + 54 | 'A value of 1 outputs one character per second.' + 55 | 'Unlike with the "write string" block, a newline is printed at the end.\n' 56 | }, 57 | {funcName: 'pagedPrint', 58 | text: 'write string %1 paginated', 59 | args: [['TEXT', 'String']], 60 | tooltip: 61 | 'Print the string to the screen (terminal or monitor),\n' + 62 | 'waiting for confirmation before scrolling down.'}, 63 | {funcName: 'serialize', 64 | text: 'convert table %1 to string', 65 | args: [['TABLE', 'Table']], 66 | output: 'String', 67 | tooltip: 68 | 'Serialize the table into a string for\n' + 69 | 'display, storage, or transmission.\n' + 70 | 'The table can be recovered with the\n' + 71 | '"convert string to table" block.' 72 | }, 73 | {funcName: 'unserialize', 74 | text: 'convert string %1 to table', 75 | args: [['TEXT', 'String']], 76 | output: 'Table', 77 | tooltip: 78 | 'Unserialize a string representation of a table\n' + 79 | 'created with the "convert table to string" block.' 80 | }, 81 | {funcName: 'urlEncode', 82 | text: 'encode %1 for URL', 83 | args: [['TEXT', 'String']], 84 | tooltip: 85 | 'Encode a string for transmission within a URL.\n' + 86 | 'Spaces are replaced with "+s". Unsafe characters,\n' + 87 | 'such as quotation marks, are replaced by their ASCII\n' + 88 | 'values and preceded with a percent sign (%).' 89 | } 90 | ]; 91 | 92 | Blockly.ComputerCraft.TEXTUTILS_FUNCS_.forEach(function(info) { 93 | Blockly.ComputerCraft.buildValueBlock( 94 | 'textutils', Blockly.ComputerCraft.TEXTUTILS_BLOCK_COLOUR_, info);}); 95 | 96 | Blockly.ComputerCraft.buildDependentInputBlock( 97 | 'textutils', 98 | Blockly.ComputerCraft.TEXTUTILS_BLOCK_COLOUR_, 99 | {funcName: 'formatTime', 100 | text: 'format %1 %2', 101 | args: [['CHOICE*', 102 | [['current time', 'current'], 103 | ['time...', 'time*']]], 104 | ['TIME^', 'Number']], 105 | addChild: Blockly.ComputerCraft.InputAddType.NEVER, 106 | tooltip: 'Format the current or given time as a printable string.'}); 107 | 108 | Blockly.ComputerCraft.buildVarArgsBlock( 109 | 'textutils', 110 | Blockly.ComputerCraft.TEXTUTILS_BLOCK_COLOUR_, 111 | {blockName: 'tabulate', 112 | text: 'display tables %v %1', 113 | args: [['MODE', new Blockly.FieldDropdown( 114 | [['with pagination', 'pagedTabulate'], 115 | ['without pagination', 'tabulate']])]], 116 | ddFuncName: 'MODE', 117 | varArgName: 'table', 118 | varArgType: ['Table', 'List'], 119 | varArgTooltip: 'A table to display.', 120 | varArgCount: 1, 121 | varContainerName: 'tables', 122 | varContainerTooltip: 'Add, remove, or reorder the tables to display.', 123 | tooltip: 124 | 'Print tables to the screen in an ordered form.\n' + 125 | 'If pagination is used, this pauses for confirmation before scrolling.\n' + 126 | 'Click on the star to add (or remove) tables.'}); 127 | -------------------------------------------------------------------------------- /apps/code/blocks-vector.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft Vector API 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Blocks for ComputerCraft Vector API 22 | * @author ellen.spertus@gmail.com (Ellen Spertus) 23 | */ 24 | 'use strict'; 25 | 26 | Blockly.ComputerCraft.VECTOR_BLOCK_COLOUR_ = 290; 27 | 28 | Blockly.ComputerCraft.VECTOR_FUNCS_ = [ 29 | {funcName: 'new', 30 | text: 'create vector (%1, %2, %3)', 31 | args: [['X', 'Number'], ['Y', 'Number'], ['Z', 'Number']], 32 | output: 'Vector', 33 | tooltip: 34 | 'Create a vector with the given x, y, and z co-ordinates.'}, 35 | {blockName: 'add_sub', 36 | text: 'vector %1 %2 vector %3', 37 | args: [['X', 'Vector'], 38 | ['OP', new Blockly.FieldDropdown( 39 | [['+', 'add'], 40 | ['-', 'sub']])], 41 | ['Y', 'Vector']], 42 | ddFuncName: 'OP', 43 | output: 'Vector', 44 | tooltip: 45 | 'Compute the sum or difference of two vectors.\n' + 46 | 'This does not modify either of the input vectors.'}, 47 | {funcName: 'mul', 48 | text: 'vector %1 x scalar %2', 49 | args: [['V', 'Vector'], 50 | ['N', 'Number']], 51 | output: 'Vector', 52 | tooltip: 53 | 'Create a vector that scales the input vector by the input number.\n' + 54 | 'This does not modify the input vector.'}, 55 | {funcName: 'dot', 56 | text: 'vector %1 · vector %2', 57 | args: [['X', 'Vector'], 58 | ['Y', 'Vector']], 59 | output: 'Vector', 60 | tooltip: 61 | 'Compute the dot product of the two inputs vectors.\n' + 62 | 'This does not modify either of the input vectors.\n'}, 63 | {funcName: 'cross', 64 | text: 'vector %1 x vector %2', 65 | args: [['X', 'Vector'], 66 | ['Y', 'Vector']], 67 | output: 'Vector', 68 | tooltip: 69 | 'Create a vector that is the cross product of the two inputs vectors.\n' + 70 | 'This does not modify either of the input vectors.\n'}, 71 | {funcName: 'length', 72 | text: 'length of vector %1', 73 | args: [['X', 'Vector']], 74 | output: 'Number', 75 | tooltip: 76 | 'Calculate the length of the input vector.\n' + 77 | 'This is defined as the distance from the origin.\n'}, 78 | {funcName: 'normalize', 79 | text: 'normalize vector %1', 80 | args: [['X', 'Vector']], 81 | output: 'Number', 82 | tooltip: 83 | 'Create a vector that is a normalized version of the input vector.\n' + 84 | 'In other words, each of the values will be scaled proportionately\n' + 85 | 'such that the length of the new vector is 1. The input vector is unchanged.'}, 86 | {funcName: 'tostring', 87 | text: 'create string for vector %1', 88 | args: [['X', 'Vector']], 89 | output: 'String', 90 | tooltip: 'Create a string representation of the input vector.'}, 91 | // A custom code generator for the block is defined below. 92 | {blockName: 'get_coord', 93 | text: 'get the %1 co-ordinate of vector %2', 94 | args: [['COORD', 95 | new Blockly.FieldDropdown([['x', 'x'], ['y', 'y'], ['z', 'z']])], 96 | ['X', 'Vector']], 97 | output: 'Number', 98 | suppressLua: true, 99 | tooltip: 100 | 'Get the specified co-ordinate from the vector.'}]; 101 | 102 | Blockly.ComputerCraft.VECTOR_FUNCS_.forEach(function(info) { 103 | Blockly.ComputerCraft.buildValueBlock( 104 | 'vector', Blockly.ComputerCraft.VECTOR_BLOCK_COLOUR_, info);}); 105 | 106 | Blockly.Lua['vector_get_coord'] = function(block) { 107 | var code = Blockly.Lua.valueToCode(block, 'X', Blockly.Lua.ORDER_NONE) + 108 | '["' + block.getTitleValue('COORD') + '"]'; 109 | return [code, Blockly.Lua.ORDER_HIGH]; 110 | }; 111 | -------------------------------------------------------------------------------- /apps/code/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/espertus/blockly-lua/a3fe3f0a273a3e3d494b983d0afdba8cb2b2dff9/apps/code/icons.png -------------------------------------------------------------------------------- /apps/code/side_input_block.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Blockly Lua: ComputerCraft SideInputBlock class 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Class for ComputerCraft blocks having an input that can 22 | * specify either a side or a named cable. 23 | * @author ellen.spertus@gmail.com (Ellen Spertus) 24 | */ 25 | 'use strict'; 26 | goog.provide('ComputerCraft.SideInputBlock'); 27 | 28 | goog.require('ComputerCraft.DependentInputBlock'); 29 | 30 | /** 31 | * Class for ComputerCraft blocks with a 'side' input (one of 'front', 'back', 32 | * 'left', 'right', 'top', 'bottom', or an arbitrary string identifying a 33 | * cable). The block may also have both earlier inputs, provided through 34 | * info.text and info.args, and later inputs. If an input is added after 35 | * the block is constructed, the method inputAdded() must be called. 36 | * 37 | * The side input and the dependent string input will be added on to the 38 | * end of the provided inputs, mutating the text and args arguments. They 39 | * are named SIDE and CABLE, respectively. 40 | * 41 | * A question mark will be automatically added to the end of the block name if 42 | * the output (as specified in the 'info' parameter) is Boolean. 43 | * 44 | * @param {string} prefix A ComputerCraft API name, such as 'turtle'. 45 | * @param {number} colour The block colour, an HSV hue value. 46 | * @param {Object} info Information about the block being defined. 47 | * The same fields are used as in the constructor of the parent class, 48 | * Blockly.ComputerCraft.DependentInputBlock. Notes: 49 | * @return {Blockly.ComputerCraft.SideInputBlock} The new block. 50 | */ 51 | Blockly.ComputerCraft.SideInputBlock = function(prefix, colour, info) { 52 | info.codeGenType = 53 | Blockly.ComputerCraft.ControllingInputCodeGenType.ONLY_IF_DEP_HIDDEN; 54 | Blockly.ComputerCraft.SideInputBlock.superClass_.constructor.call( 55 | this, prefix, colour, info); 56 | }; 57 | goog.inherits(Blockly.ComputerCraft.SideInputBlock, 58 | Blockly.ComputerCraft.DependentInputBlock); 59 | 60 | Blockly.ComputerCraft.SideInputBlock.SIDES_ = 61 | [['in front', 'front'], 62 | ['in back', 'back'], 63 | ['to the left', 'left'], 64 | ['to the right', 'right'], 65 | ['above', 'top'], 66 | ['below', 'bottom'], 67 | ['through cable...', 'cable']]; 68 | 69 | /** 70 | * Create a block that has a side input. 71 | * 72 | * @param {string} prefix A ComputerCraft API name, such as 'turtle'. 73 | * @param {number} colour The block colour, an HSV hue value. 74 | * @param {Object} info Information about the block being defined. 75 | * In addition to the fields used by the Blockly.ComputerCraft.SideInputBlock 76 | * constructor, this has: 77 | *(my Lua code)25 | * 26 | * 27 | * I used http://www.lua.org/manual/5.1/manual.html#2.1 28 | * Because of the long-bracket concept used in strings and comments, Lua does 29 | * not have a regular lexical grammar, but luckily it fits within the space 30 | * of irregular grammars supported by javascript regular expressions. 31 | * 32 | * @author mikesamuel@gmail.com 33 | */ 34 | 35 | PR['registerLangHandler']( 36 | PR['createSimpleLexer']( 37 | [ 38 | // Whitespace 39 | [PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'], 40 | // A double or single quoted, possibly multi-line, string. 41 | [PR['PR_STRING'], /^(?:\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)|\'(?:[^\'\\]|\\[\s\S])*(?:\'|$))/, null, '"\''] 42 | ], 43 | [ 44 | // A comment is either a line comment that starts with two dashes, or 45 | // two dashes preceding a long bracketed block. 46 | [PR['PR_COMMENT'], /^--(?:\[(=*)\[[\s\S]*?(?:\]\1\]|$)|[^\r\n]*)/], 47 | // A long bracketed block not preceded by -- is a string. 48 | [PR['PR_STRING'], /^\[(=*)\[[\s\S]*?(?:\]\1\]|$)/], 49 | [PR['PR_KEYWORD'], /^(?:and|break|do|else|elseif|end|false|for|function|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b/, null], 50 | // A number is a hex integer literal, a decimal real literal, or in 51 | // scientific notation. 52 | [PR['PR_LITERAL'], 53 | /^[+-]?(?:0x[\da-f]+|(?:(?:\.\d+|\d+(?:\.\d*)?)(?:e[+\-]?\d+)?))/i], 54 | // An identifier 55 | [PR['PR_PLAIN'], /^[a-z_]\w*/i], 56 | // A run of punctuation 57 | [PR['PR_PUNCTUATION'], /^[^\w\t\n\r \xA0][^\w\t\n\r \xA0\"\'\-\+=]*/] 58 | ]), 59 | ['lua']); 60 | -------------------------------------------------------------------------------- /apps/prettify.css: -------------------------------------------------------------------------------- 1 | .pln{color:#000}@media screen{.str{color:#080}.kwd{color:#008}.com{color:#800}.typ{color:#606}.lit{color:#066}.pun,.opn,.clo{color:#660}.tag{color:#008}.atn{color:#606}.atv{color:#080}.dec,.var{color:#606}.fun{color:red}}@media print,projection{.str{color:#060}.kwd{color:#006;font-weight:bold}.com{color:#600;font-style:italic}.typ{color:#404;font-weight:bold}.lit{color:#044}.pun,.opn,.clo{color:#440}.tag{color:#006;font-weight:bold}.atn{color:#404}.atv{color:#060}}pre.prettyprint{padding:2px;border:1px solid #888}ol.linenums{margin-top:0;margin-bottom:0}li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8{list-style-type:none}li.L1,li.L3,li.L5,li.L7,li.L9{background:#eee} -------------------------------------------------------------------------------- /blocks/colour.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Blocks Editor 3 | * 4 | * Copyright 2012 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Colour blocks for Blockly. 22 | * @author fraser@google.com (Neil Fraser) 23 | */ 24 | 'use strict'; 25 | 26 | goog.provide('Blockly.Blocks.colour'); 27 | 28 | goog.require('Blockly.Blocks'); 29 | 30 | 31 | Blockly.Blocks['colour_picker'] = { 32 | // Colour picker. 33 | init: function() { 34 | this.setHelpUrl(Blockly.Msg.COLOUR_PICKER_HELPURL); 35 | this.setColour(20); 36 | this.appendDummyInput() 37 | .appendTitle(new Blockly.FieldColour('#ff0000'), 'COLOUR'); 38 | this.setOutput(true, 'Colour'); 39 | this.setTooltip(Blockly.Msg.COLOUR_PICKER_TOOLTIP); 40 | } 41 | }; 42 | 43 | Blockly.Blocks['colour_random'] = { 44 | // Random colour. 45 | init: function() { 46 | this.setHelpUrl(Blockly.Msg.COLOUR_RANDOM_HELPURL); 47 | this.setColour(20); 48 | this.appendDummyInput() 49 | .appendTitle(Blockly.Msg.COLOUR_RANDOM_TITLE); 50 | this.setOutput(true, 'Colour'); 51 | this.setTooltip(Blockly.Msg.COLOUR_RANDOM_TOOLTIP); 52 | } 53 | }; 54 | 55 | Blockly.Blocks['colour_rgb'] = { 56 | // Compose a colour from RGB components. 57 | init: function() { 58 | this.setHelpUrl(Blockly.Msg.COLOUR_RGB_HELPURL); 59 | this.setColour(20); 60 | this.appendValueInput('RED') 61 | .setCheck('Number') 62 | .setAlign(Blockly.ALIGN_RIGHT) 63 | .appendTitle(Blockly.Msg.COLOUR_RGB_TITLE) 64 | .appendTitle(Blockly.Msg.COLOUR_RGB_RED); 65 | this.appendValueInput('GREEN') 66 | .setCheck('Number') 67 | .setAlign(Blockly.ALIGN_RIGHT) 68 | .appendTitle(Blockly.Msg.COLOUR_RGB_GREEN); 69 | this.appendValueInput('BLUE') 70 | .setCheck('Number') 71 | .setAlign(Blockly.ALIGN_RIGHT) 72 | .appendTitle(Blockly.Msg.COLOUR_RGB_BLUE); 73 | this.setOutput(true, 'Colour'); 74 | this.setTooltip(Blockly.Msg.COLOUR_RGB_TOOLTIP); 75 | } 76 | }; 77 | 78 | Blockly.Blocks['colour_blend'] = { 79 | // Blend two colours together. 80 | init: function() { 81 | this.setHelpUrl(Blockly.Msg.COLOUR_BLEND_HELPURL); 82 | this.setColour(20); 83 | this.appendValueInput('COLOUR1') 84 | .setCheck('Colour') 85 | .setAlign(Blockly.ALIGN_RIGHT) 86 | .appendTitle(Blockly.Msg.COLOUR_BLEND_TITLE) 87 | .appendTitle(Blockly.Msg.COLOUR_BLEND_COLOUR1); 88 | this.appendValueInput('COLOUR2') 89 | .setCheck('Colour') 90 | .setAlign(Blockly.ALIGN_RIGHT) 91 | .appendTitle(Blockly.Msg.COLOUR_BLEND_COLOUR2); 92 | this.appendValueInput('RATIO') 93 | .setCheck('Number') 94 | .setAlign(Blockly.ALIGN_RIGHT) 95 | .appendTitle(Blockly.Msg.COLOUR_BLEND_RATIO); 96 | this.setOutput(true, 'Colour'); 97 | this.setTooltip(Blockly.Msg.COLOUR_BLEND_TOOLTIP); 98 | } 99 | }; 100 | -------------------------------------------------------------------------------- /blocks/variables.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Blocks Editor 3 | * 4 | * Copyright 2012 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Variable blocks for Blockly. 22 | * @author fraser@google.com (Neil Fraser) 23 | */ 24 | 'use strict'; 25 | 26 | goog.provide('Blockly.Blocks.variables'); 27 | 28 | goog.require('Blockly.Blocks'); 29 | 30 | 31 | Blockly.Blocks['variables_get'] = { 32 | // Variable getter. 33 | init: function() { 34 | this.setHelpUrl(Blockly.Msg.VARIABLES_GET_HELPURL); 35 | this.setColour(330); 36 | this.appendDummyInput() 37 | .appendTitle(Blockly.Msg.VARIABLES_GET_TITLE) 38 | .appendTitle(new Blockly.FieldVariable( 39 | Blockly.Msg.VARIABLES_GET_ITEM), 'VAR') 40 | .appendTitle(Blockly.Msg.VARIABLES_GET_TAIL); 41 | this.setOutput(true); 42 | this.setTooltip(Blockly.Msg.VARIABLES_GET_TOOLTIP); 43 | this.contextMenuMsg_ = Blockly.Msg.VARIABLES_GET_CREATE_SET; 44 | this.contextMenuType_ = 'variables_set'; 45 | }, 46 | getVars: function() { 47 | return [this.getTitleValue('VAR')]; 48 | }, 49 | renameVar: function(oldName, newName) { 50 | if (Blockly.Names.equals(oldName, this.getTitleValue('VAR'))) { 51 | this.setTitleValue(newName, 'VAR'); 52 | } 53 | }, 54 | customContextMenu: function(options) { 55 | var option = {enabled: true}; 56 | var name = this.getTitleValue('VAR'); 57 | option.text = this.contextMenuMsg_.replace('%1', name); 58 | var xmlTitle = goog.dom.createDom('title', null, name); 59 | xmlTitle.setAttribute('name', 'VAR'); 60 | var xmlBlock = goog.dom.createDom('block', null, xmlTitle); 61 | xmlBlock.setAttribute('type', this.contextMenuType_); 62 | option.callback = Blockly.ContextMenu.callbackFactory(this, xmlBlock); 63 | options.push(option); 64 | } 65 | }; 66 | 67 | Blockly.Blocks['variables_set'] = { 68 | // Variable setter. 69 | init: function() { 70 | this.setHelpUrl(Blockly.Msg.VARIABLES_SET_HELPURL); 71 | this.setColour(330); 72 | this.appendValueInput('VALUE') 73 | .appendTitle(Blockly.Msg.VARIABLES_SET_TITLE) 74 | .appendTitle(new Blockly.FieldVariable( 75 | Blockly.Msg.VARIABLES_SET_ITEM), 'VAR') 76 | .appendTitle(Blockly.Msg.VARIABLES_SET_TAIL); 77 | this.setPreviousStatement(true); 78 | this.setNextStatement(true); 79 | this.setTooltip(Blockly.Msg.VARIABLES_SET_TOOLTIP); 80 | this.contextMenuMsg_ = Blockly.Msg.VARIABLES_SET_CREATE_GET; 81 | this.contextMenuType_ = 'variables_get'; 82 | }, 83 | getVars: function() { 84 | return [this.getTitleValue('VAR')]; 85 | }, 86 | renameVar: function(oldName, newName) { 87 | if (Blockly.Names.equals(oldName, this.getTitleValue('VAR'))) { 88 | this.setTitleValue(newName, 'VAR'); 89 | } 90 | }, 91 | customContextMenu: Blockly.Blocks['variables_get'].customContextMenu 92 | }; 93 | 94 | Blockly.Blocks['variables_set_two'] = { 95 | // Set two variables to the return values of a procedure call. 96 | init: function() { 97 | this.setColour(330); 98 | this.appendDummyInput() 99 | .appendTitle('set variables'); 100 | this.appendDummyInput() 101 | .appendTitle(new Blockly.FieldVariable('x'), 'VAR1'); 102 | this.appendDummyInput() 103 | .appendTitle(new Blockly.FieldVariable('y'), 'VAR2'); 104 | this.appendValueInput('VALUE'); 105 | this.setPreviousStatement(true); 106 | this.setNextStatement(true); 107 | this.setInputsInline(true); 108 | this.setTooltip('Set variables to the two values returned by a procedure.'); 109 | }, 110 | renameVar: function(oldName, newName) { 111 | var var1 = this.getTitleValue('VAR1'); 112 | var var2 = this.getTitleValue('VAR2'); 113 | if ((var1 == oldName || var1 == newName) && 114 | (var2 == oldName || var2 == newName)) { 115 | window.alert('Two variables in a set block may not have the same name.'); 116 | return; 117 | } 118 | if (Blockly.Names.equals(oldName, var1)) { 119 | this.setTitleValue(newName, 'VAR1'); 120 | } 121 | if (Blockly.Names.equals(oldName, var2)) { 122 | this.setTitleValue(newName, 'VAR2'); 123 | } 124 | }, 125 | onchange: function() { 126 | if (!this.workspace) { 127 | // Block has been deleted. 128 | return; 129 | } 130 | // If the value input socket is populated, make sure that block produces 131 | // at least two values. 132 | var source = this.getInputTargetBlock('VALUE'); 133 | if (source && !source.multipleOutputs) { 134 | this.setWarningText('The attached block only produces one value, ' + 135 | 'but this block requires two.'); 136 | } else { 137 | this.setWarningText(null); 138 | } 139 | } 140 | }; 141 | 142 | Blockly.Blocks['variables_set_three'] = { 143 | // Set three variables to the return values of a procedure call. 144 | init: function() { 145 | this.setColour(330); 146 | this.appendDummyInput() 147 | .appendTitle('set variables'); 148 | this.appendDummyInput() 149 | .appendTitle(new Blockly.FieldVariable('x'), 'VAR1'); 150 | this.appendDummyInput() 151 | .appendTitle(new Blockly.FieldVariable('y'), 'VAR2'); 152 | this.appendDummyInput() 153 | .appendTitle(new Blockly.FieldVariable('z'), 'VAR3'); 154 | this.appendValueInput('VALUE'); 155 | this.setPreviousStatement(true); 156 | this.setNextStatement(true); 157 | this.setInputsInline(true); 158 | this.setTooltip( 159 | 'Set variables to the three values returned by a procedure.'); 160 | }, 161 | renameVar: function(oldName, newName) { 162 | var var1 = this.getTitleValue('VAR1'); 163 | var var2 = this.getTitleValue('VAR2'); 164 | var var3 = this.getTitleValue('VAR3'); 165 | var matches = 0; 166 | if (var1 == oldName || var1 == newName) { 167 | matches++; 168 | } 169 | if (var2 == oldName || var2 == newName) { 170 | matches++; 171 | } 172 | if (var3 == oldName || var3 == newName) { 173 | matches++; 174 | } 175 | if (matches > 1) { 176 | window.alert( 177 | 'Multiple variables in a set block may not have the same name.'); 178 | return; 179 | } 180 | if (Blockly.Names.equals(oldName, var1)) { 181 | this.setTitleValue(newName, 'VAR1'); 182 | } 183 | if (Blockly.Names.equals(oldName, var2)) { 184 | this.setTitleValue(newName, 'VAR2'); 185 | } 186 | }, 187 | onchange: function() { 188 | if (!this.workspace) { 189 | // Block has been deleted. 190 | return; 191 | } 192 | // If the value input socket is populated, make sure that block produces 193 | // at least three values. 194 | var source = this.getInputTargetBlock('VALUE'); 195 | if (source && (!source.multipleOutputs || source.multipleOutputs < 3)) { 196 | this.setWarningText('The attached block only produces one value, ' + 197 | 'but this block requires three.'); 198 | } else { 199 | this.setWarningText(null); 200 | } 201 | } 202 | }; 203 | -------------------------------------------------------------------------------- /core/blocks.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Blocks Editor 3 | * 4 | * Copyright 2013 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Core JavaScript library for Blockly. 22 | * @author fraser@google.com (Neil Fraser) 23 | */ 24 | 'use strict'; 25 | 26 | /** 27 | * Name space for the Blocks singleton. 28 | * Blocks gets populated in the blocks files. 29 | */ 30 | goog.provide('Blockly.Blocks'); 31 | -------------------------------------------------------------------------------- /core/field_checkbox.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Blocks Editor 3 | * 4 | * Copyright 2012 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Checkbox field. Checked or not checked. 22 | * @author fraser@google.com (Neil Fraser) 23 | */ 24 | 'use strict'; 25 | 26 | goog.provide('Blockly.FieldCheckbox'); 27 | 28 | goog.require('Blockly.Field'); 29 | 30 | 31 | /** 32 | * Class for a checkbox field. 33 | * @param {string} state The initial state of the field ('TRUE' or 'FALSE'). 34 | * @param {Function} opt_changeHandler A function that is executed when a new 35 | * option is selected. 36 | * @extends {Blockly.Field} 37 | * @constructor 38 | */ 39 | Blockly.FieldCheckbox = function(state, opt_changeHandler) { 40 | Blockly.FieldCheckbox.superClass_.constructor.call(this, ''); 41 | 42 | this.changeHandler_ = opt_changeHandler; 43 | // The checkbox doesn't use the inherited text element. 44 | // Instead it uses a custom checkmark element that is either visible or not. 45 | this.checkElement_ = Blockly.createSvgElement('text', 46 | {'class': 'blocklyText', 'x': -3}, this.fieldGroup_); 47 | var textNode = document.createTextNode('\u2713'); 48 | this.checkElement_.appendChild(textNode); 49 | // Set the initial state. 50 | this.setValue(state); 51 | }; 52 | goog.inherits(Blockly.FieldCheckbox, Blockly.Field); 53 | 54 | /** 55 | * Clone this FieldCheckbox. 56 | * 57 | * @return {Blockly.FieldCheckbox} The result of calling the constructor again 58 | * with the current values of the arguments used during construction. 59 | */ 60 | Blockly.FieldCheckbox.prototype.clone = function() { 61 | return new Blockly.FieldCheckbox(this.getValue(), this.changeHandler_); 62 | } 63 | 64 | /** 65 | * Mouse cursor style when over the hotspot that initiates editability. 66 | */ 67 | Blockly.FieldCheckbox.prototype.CURSOR = 'default'; 68 | 69 | /** 70 | * Return 'TRUE' if the checkbox is checked, 'FALSE' otherwise. 71 | * @return {string} Current state. 72 | */ 73 | Blockly.FieldCheckbox.prototype.getValue = function() { 74 | return String(this.state_).toUpperCase(); 75 | }; 76 | 77 | /** 78 | * Set the checkbox to be checked if strBool is 'TRUE', unchecks otherwise. 79 | * @param {string} strBool New state. 80 | */ 81 | Blockly.FieldCheckbox.prototype.setValue = function(strBool) { 82 | var newState = (strBool == 'TRUE'); 83 | if (this.state_ !== newState) { 84 | this.state_ = newState; 85 | this.checkElement_.style.display = newState ? 'block' : 'none'; 86 | if (this.sourceBlock_ && this.sourceBlock_.rendered) { 87 | this.sourceBlock_.workspace.fireChangeEvent(); 88 | } 89 | } 90 | }; 91 | 92 | /** 93 | * Toggle the state of the checkbox. 94 | * @private 95 | */ 96 | Blockly.FieldCheckbox.prototype.showEditor_ = function() { 97 | var newState = !this.state_; 98 | if (this.changeHandler_) { 99 | // Call any change handler, and allow it to override. 100 | var override = this.changeHandler_(newState); 101 | if (override !== undefined) { 102 | newState = override; 103 | } 104 | } 105 | if (newState !== null) { 106 | this.setValue(String(newState).toUpperCase()); 107 | } 108 | }; 109 | -------------------------------------------------------------------------------- /core/field_colour.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Visual Blocks Editor 3 | * 4 | * Copyright 2012 Google Inc. 5 | * http://blockly.googlecode.com/ 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | /** 21 | * @fileoverview Colour input field. 22 | * @author fraser@google.com (Neil Fraser) 23 | */ 24 | 'use strict'; 25 | 26 | goog.provide('Blockly.FieldColour'); 27 | 28 | goog.require('Blockly.Field'); 29 | goog.require('goog.ui.ColorPicker'); 30 | 31 | 32 | /** 33 | * Class for a colour input field. 34 | * @param {string} colour The initial colour in '#rrggbb' format. 35 | * @param {Function} opt_changeHandler A function that is executed when a new 36 | * option is selected. 37 | * @extends {Blockly.Field} 38 | * @constructor 39 | */ 40 | Blockly.FieldColour = function(colour, opt_changeHandler) { 41 | Blockly.FieldColour.superClass_.constructor.call(this, '\u00A0\u00A0\u00A0'); 42 | 43 | this.changeHandler_ = opt_changeHandler; 44 | this.borderRect_.style.fillOpacity = 1; 45 | // Set the initial state. 46 | this.setValue(colour); 47 | }; 48 | goog.inherits(Blockly.FieldColour, Blockly.Field); 49 | 50 | /** 51 | * Clone this FieldColour. 52 | * 53 | * @return {Blockly.FieldColour} The result of calling the constructor again 54 | * with the current values of the arguments used during construction. 55 | */ 56 | Blockly.FieldColour.prototype.clone = function() { 57 | return new Blockly.FieldColour(this.getValue(), this.changeHandler_); 58 | }; 59 | 60 | /** 61 | * Mouse cursor style when over the hotspot that initiates the editor. 62 | */ 63 | Blockly.FieldColour.prototype.CURSOR = 'default'; 64 | 65 | /** 66 | * Dispose of all DOM objects belonging to this editable field. 67 | */ 68 | Blockly.FieldColour.prototype.dispose = function() { 69 | Blockly.WidgetDiv.hideIfField(this); 70 | Blockly.FieldColour.superClass_.dispose.call(this); 71 | }; 72 | 73 | /** 74 | * Return the current colour. 75 | * @return {string} Current colour in '#rrggbb' format. 76 | */ 77 | Blockly.FieldColour.prototype.getValue = function() { 78 | return this.colour_; 79 | }; 80 | 81 | /** 82 | * Set the colour. 83 | * @param {string} colour The new colour in '#rrggbb' format. 84 | */ 85 | Blockly.FieldColour.prototype.setValue = function(colour) { 86 | this.colour_ = colour; 87 | this.borderRect_.style.fill = colour; 88 | if (this.sourceBlock_ && this.sourceBlock_.rendered) { 89 | this.sourceBlock_.workspace.fireChangeEvent(); 90 | } 91 | }; 92 | 93 | /** 94 | * An array of colour strings for the palette. 95 | * See bottom of this page for the default: 96 | * http://docs.closure-library.googlecode.com/git/closure_goog_ui_colorpicker.js.source.html 97 | * @type {!Array.