Skip to content

Commit

Permalink
STCC-69 Set Rotation Style
Browse files Browse the repository at this point in the history
  • Loading branch information
rsamer committed Oct 27, 2016
1 parent 77a2ef4 commit b677748
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
40 changes: 26 additions & 14 deletions src/scratchtocatrobat/converter/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@ class _ScratchToCatrobat(object):
"bounceOffEdge": catbricks.IfOnEdgeBounceBrick,
"changeXposBy:": catbricks.ChangeXByNBrick,
"changeYposBy:": catbricks.ChangeYByNBrick,
"setRotationStyle": catbricks.SetRotationStyleBrick,

# variables
"setVar:to:": lambda *args: _create_variable_brick(*itertools.chain(args, [catbricks.SetVariableBrick])),
"changeVar:by:": lambda *args: _create_variable_brick(*itertools.chain(args, [catbricks.ChangeVariableBrick])),
"readVariable": lambda variable_name: _variable_for(variable_name),
"showVariable:": catbricks.ShowVariableBrick,
"hideVariable:": catbricks.HideVariableBrick,
"showVariable:": catbricks.ShowTextBrick,
"hideVariable:": catbricks.HideTextBrick,

# formula lists
"append:toList:": catbricks.AddItemToUserListBrick,
Expand Down Expand Up @@ -692,8 +693,16 @@ def _add_default_behaviour_to(sprite, sprite_context, catrobat_scene, catrobat_p
implicit_bricks_to_add += [catbricks.HideBrick()]

rotation_style = scratch_object.get_rotationStyle()
if rotation_style and rotation_style != "normal":
log.warning("Unsupported rotation style '{}' at object: {}".format(rotation_style, scratch_object.get_objName()))
if rotation_style is not None:
traverser = _BlocksConversionTraverser(sprite, catrobat_project)
if rotation_style == "leftRight":
set_rotation_style_brick = traverser._converted_helper_brick_or_formula_element(["left-right"], "setRotationStyle")
assert set_rotation_style_brick is not None
implicit_bricks_to_add += [set_rotation_style_brick]
elif rotation_style == "none":
set_rotation_style_brick = traverser._converted_helper_brick_or_formula_element(["don't rotate"], "setRotationStyle")
assert set_rotation_style_brick is not None
implicit_bricks_to_add += [set_rotation_style_brick]

if len(implicit_bricks_to_add) > 0:
catrobat.add_to_start_script(implicit_bricks_to_add, sprite)
Expand Down Expand Up @@ -907,6 +916,8 @@ def __init__(self, catrobat_sprite, catrobat_project, script_context=None):
self.sprite = catrobat_sprite
self.project = catrobat_project
self.scene = catrobat_project.getDefaultScene()
self.CatrobatClass = None
self.arguments = None
self._stack = []
self._child_stack = []

Expand Down Expand Up @@ -1226,11 +1237,8 @@ def _convert_show_variable_block(self):
assert user_variable is not None # the variable must exist at this stage!
assert user_variable.getName() == variable_name
show_variable_brick = self.CatrobatClass(0, 0)

#Commented out because it isn't present anymore
#show_variable_brick.userVariableName = variable_name
#show_variable_brick.userVariable = user_variable

show_variable_brick.setUserVariableName(variable_name)
show_variable_brick.setUserVariable(user_variable)
return show_variable_brick

@_register_handler(_block_name_to_handler_map, "hideVariable:")
Expand All @@ -1240,11 +1248,8 @@ def _convert_hide_variable_block(self):
assert user_variable is not None # the variable must exist at this stage!
assert user_variable.getName() == variable_name
hide_variable_brick = self.CatrobatClass()

#Commented out because it isn't present anymore
#hide_variable_brick.userVariableName = variable_name
#hide_variable_brick.userVariable = user_variable

hide_variable_brick.setUserVariableName(variable_name)
hide_variable_brick.setUserVariable(user_variable)
return hide_variable_brick

@_register_handler(_block_name_to_handler_map, "append:toList:")
Expand Down Expand Up @@ -1478,3 +1483,10 @@ def _convert_time_and_date_block(self):
if time_or_date == "day of week":
time_formula = self._converted_helper_brick_or_formula_element([time_formula, 1], "+")
return time_formula

@_register_handler(_block_name_to_handler_map, "setRotationStyle")
def _convert_set_rotation_style_block(self):
[style] = self.arguments
set_rotation_style_brick = catbricks.SetRotationStyleBrick()
set_rotation_style_brick.selection = ["left-right", "all around", "don't rotate"].index(style)
return set_rotation_style_brick
46 changes: 33 additions & 13 deletions src/scratchtocatrobat/converter/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,30 @@ def test_can_convert_stop_scripts_with_option_other_scripts_in_sprite_block(self
assert isinstance(catr_brick, catbricks.StopScriptBrick)
assert expected_index_of_option == catr_brick.spinnerSelection

# setRotationStyle ("left-right")
def test_can_convert_set_rotation_style_with_option_left_right_block(self):
expected_index_of_option = 0
scratch_block = ["setRotationStyle", "left-right"]
[catr_brick] = self.block_converter._catrobat_bricks_from(scratch_block, DUMMY_CATR_SPRITE)
assert isinstance(catr_brick, catbricks.SetRotationStyleBrick)
assert expected_index_of_option == catr_brick.selection

# setRotationStyle ("all around")
def test_can_convert_set_rotation_style_with_option_all_around_block(self):
expected_index_of_option = 1
scratch_block = ["setRotationStyle", "all around"]
[catr_brick] = self.block_converter._catrobat_bricks_from(scratch_block, DUMMY_CATR_SPRITE)
assert isinstance(catr_brick, catbricks.SetRotationStyleBrick)
assert expected_index_of_option == catr_brick.selection

# setRotationStyle ("don't rotate")
def test_can_convert_set_rotation_style_with_option_dont_rotate_block(self):
expected_index_of_option = 2
scratch_block = ["setRotationStyle", "don't rotate"]
[catr_brick] = self.block_converter._catrobat_bricks_from(scratch_block, DUMMY_CATR_SPRITE)
assert isinstance(catr_brick, catbricks.SetRotationStyleBrick)
assert expected_index_of_option == catr_brick.selection

# broadcast:
def test_can_convert_broadcast_block(self):
broadcast_message = "hello world"
Expand Down Expand Up @@ -1121,12 +1145,10 @@ def test_can_convert_show_variable_block(self):
# create and validate show variable brick
scratch_block = ["showVariable:", variable_name]
[catr_brick] = self.block_converter._catrobat_bricks_from(scratch_block, DUMMY_CATR_SPRITE)
assert isinstance(catr_brick, catbricks.ShowVariableBrick)

#Commented out because members aren't present anymore
#assert catr_brick.userVariableName == variable_name
#assert user_variable == catr_brick.userVariable
#assert catr_brick.userVariable.getName() == variable_name
assert isinstance(catr_brick, catbricks.ShowTextBrick)
assert catr_brick.userVariableName == variable_name
assert user_variable == catr_brick.getUserVariable()
assert catr_brick.getUserVariable().getName() == variable_name

# hideVariableBrick:
def test_can_convert_hide_variable_block(self):
Expand All @@ -1141,13 +1163,11 @@ def test_can_convert_hide_variable_block(self):
# create and validate show variable brick
scratch_block = ["hideVariable:", variable_name]
[catr_brick] = self.block_converter._catrobat_bricks_from(scratch_block, DUMMY_CATR_SPRITE)
assert isinstance(catr_brick, catbricks.HideVariableBrick)

#Commented out because members aren't present anymore
#assert catr_brick.userVariableName == variable_name
#assert user_variable == catr_brick.userVariable
#assert catr_brick.userVariable.getName() == variable_name

assert isinstance(catr_brick, catbricks.HideTextBrick)
assert catr_brick.userVariableName == variable_name
assert user_variable == catr_brick.getUserVariable()
assert catr_brick.getUserVariable().getName() == variable_name

# append:toList:
def test_can_convert_append_number_to_list_block(self):
value = "1.23"
Expand Down
6 changes: 3 additions & 3 deletions src/scratchtocatrobat/tools/common_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
# TODO: parse from Java annotations
FIELD_NAMES_TO_XML_NAMES = {"virtualScreenWidth": "screenWidth", "virtualScreenHeight": "screenHeight"}
IGNORED_XML_HEADER_CLASS_FIELDS = ["serialVersionUID"]
OPTIONAL_HEADER_TAGS = ["dateTimeUpload", "description", "tags", "url", "userHandle"]
OPTIONAL_HEADER_TAGS = ["dateTimeUpload", "description", "tags", "remixOf", "userHandle"]

_log = common.log

Expand Down Expand Up @@ -125,7 +125,7 @@ def assertScriptClasses(self, expected_script_class, expected_brick_classes, scr
assert len(bricks) == len(expected_brick_classes)
assert [brick.__class__ for brick in bricks] == expected_brick_classes

def __assertTagsAreNonempty(self, xml_root):
def __assertTagsAreNonEmpty(self, xml_root):
header_tags = [FIELD_NAMES_TO_XML_NAMES[field] if field in FIELD_NAMES_TO_XML_NAMES else field for field in common.fields_of(catbase.XmlHeader) if field not in IGNORED_XML_HEADER_CLASS_FIELDS]
mandatory_header_tags = set(header_tags) - set(OPTIONAL_HEADER_TAGS)
for header_tag in header_tags:
Expand All @@ -152,7 +152,7 @@ def assertValidCatrobatProgramStructure(self, project_path, project_name):

root = ElementTree.parse(project_xml_path).getroot()
assert root.tag == 'program'
self.__assertTagsAreNonempty(root)
self.__assertTagsAreNonEmpty(root)

project_name_from_xml = root.find("header/programName")
assert project_name_from_xml.text == project_name
Expand Down

0 comments on commit b677748

Please sign in to comment.