From acb61a37f56038c51f88e7cf7029a7295c307eb5 Mon Sep 17 00:00:00 2001 From: Marion Date: Wed, 24 Jul 2013 20:58:13 +0000 Subject: HiddenBlock inherits from Block, which has new utility methods for value blocks --- (limited to 'TurtleArt/tablock.py') diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py index 8d8ed4a..e878a46 100644 --- a/TurtleArt/tablock.py +++ b/TurtleArt/tablock.py @@ -24,7 +24,8 @@ import cairo from taconstants import (EXPANDABLE, EXPANDABLE_ARGS, OLD_NAMES, CONSTANTS, STANDARD_STROKE_WIDTH, BLOCK_SCALE, BOX_COLORS, - GRADIENT_COLOR, EXPANDABLE_FLOW, COLORDICT) + GRADIENT_COLOR, EXPANDABLE_FLOW, COLORDICT, + PREFIX_DICTIONARY) from tapalette import (palette_blocks, block_colors, expandable_blocks, content_blocks, block_names, block_primitives, block_styles, special_block_colors) @@ -34,6 +35,9 @@ import sprites from tautils import (debug_output, error_output) +media_blocks_dictionary = {} # new media blocks get added here + + class Blocks: """ A class for the list of blocks and everything they share in common """ @@ -277,6 +281,37 @@ class Block: return False return True + def is_value_block(self): + """ Return True iff this block is a value block (numeric, string, + media, etc.) """ + return self.primitive is None and self.values + + def get_value(self): + """ Return the value stored in this value block """ + # TODO what error to raise if this is not a value block? + if self.name == 'number': + try: + return float(self.values[0]) + except ValueError: + return float(ord(self.values[0][0])) + elif (self.name == 'string' or + self.name == 'title'): # deprecated block + if isinstance(self.values[0], (float, int)): + if int(self.values[0]) == self.values[0]: + self.values[0] = int(self.values[0]) + return '#s' + str(self.values[0]) + else: + return '#s' + self.values[0] + elif self.name in PREFIX_DICTIONARY: + if self.values[0] is not None: + return PREFIX_DICTIONARY[self.name] + str(self.values[0]) + else: + return PREFIX_DICTIONARY[self.name] + 'None' + elif self.name in media_blocks_dictionary: + return '#smedia_' + self.name.upper() + else: + return '%nothing%' + def highlight(self): """ We may want to highlight a block... """ if self.spr is not None and self.status is not 'collapsed': -- cgit v0.9.1