From 2b550f18b8c12304fca1f7601a6e5a934990c000 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Tue, 18 Feb 2014 12:37:31 +0000 Subject: made 3d blocks a plugin --- diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index 4254967..a7dbd5b 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -276,21 +276,6 @@ towards the top of the screen.)')) call_afterwards=lambda value: self.after_set( 'heading', value))) - palette.add_block('setxyz', - style='basic-style-3arg', - # TRANS: xyz are coordinates in a 3-dimensional space - label=[_('set xyz') + '\n\n', - _('x'), _('y'), _('z')], - prim_name='setxyz', - default=[0, 0, 0], - help_string=_('sets the xyz-coordinates of the \ -turtle')) - self.tw.lc.def_prim( - 'setxyz', 3, - Primitive(Turtle.set_xyz, - arg_descs=[ArgSlot(TYPE_NUMBER), ArgSlot(TYPE_NUMBER), - ArgSlot(TYPE_NUMBER)])) - palette.add_block('xcor', style='box-style', label=_('xcor'), @@ -321,20 +306,6 @@ the turtle (can be used in place of a number block)'), ConstantArg(Primitive( self.tw.get_coord_scale))])) - palette.add_block('zcor', - style='box-style', - label=_('zcor'), - help_string=_('holds current z-coordinate value of \ -the turtle (can be used in place of a number block)'), - value_block=True, - prim_name='zcor') - self.tw.lc.def_prim( - 'zcor', 0, - Primitive(Primitive.divide, return_type=TYPE_FLOAT, - arg_descs=[ConstantArg(Primitive(Turtle.get_z)), - ConstantArg(Primitive( - self.tw.get_coord_scale))])) - palette.add_block('heading', style='box-style', label=_('heading'), diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py index 0a3cc23..87616d6 100644 --- a/TurtleArt/taconstants.py +++ b/TurtleArt/taconstants.py @@ -273,7 +273,8 @@ CONSTANTS = {'leftpos': None, 'toppos': None, 'rightpos': None, # Blocks that are expandable EXPANDABLE_STYLE = ['boolean-style', 'compare-porch-style', 'compare-style', 'number-style-porch', 'number-style', 'basic-style-2arg', - 'number-style-block', 'box-style-media'] + 'number-style-block', 'box-style-media', + 'basic-style-3arg'] # These are defined in add_block based on block style EXPANDABLE_FLOW = [] diff --git a/TurtleArt/taturtle.py b/TurtleArt/taturtle.py index 08a896e..ccdd60b 100644 --- a/TurtleArt/taturtle.py +++ b/TurtleArt/taturtle.py @@ -141,8 +141,10 @@ class Turtles: self._active_turtle.set_gray(100) if self.turtle_window.coord_scale == 1: self._active_turtle.set_pen_size(5) + self._active_turtle.set_z_scale = 100. else: self._active_turtle.set_pen_size(1) + self._active_turtle.set_z_scale = 20. self._active_turtle.reset_shapes() self._active_turtle.set_heading(0.0) self._active_turtle.set_pen_state(False) @@ -230,6 +232,7 @@ class Turtle: self._x = 0.0 self._y = 0.0 self._z = 0.0 + self._z_scale = 100. self._heading = 0.0 self._half_width = 0 self._half_height = 0 @@ -355,6 +358,9 @@ class Turtle: self._custom_shapes = False self._calculate_sizes() + def set_z_scale(self, scale): + self._z_scale = scale + def set_heading(self, heading, share=True): ''' Set the turtle heading (one shape per 360/SHAPES degrees) ''' self._heading = heading @@ -565,8 +571,8 @@ class Turtle: old = self.get_xy() if self._z > 0.0: - old[0] = old[0] * ((self._z / 100.) + 1) - old[1] = old[1] * ((self._z / 100.) + 1) + old[0] = old[0] * ((self._z / self._z_scale) + 1) + old[1] = old[1] * ((self._z / self._z_scale) + 1) xcor = old[0] + scaled_distance * sin(self._heading * DEGTOR) ycor = old[1] + scaled_distance * cos(self._heading * DEGTOR) @@ -587,8 +593,8 @@ class Turtle: self.set_xy(x, y, share, pendown) def set_z(self, z, share=True, pendown=True): - xcor = self._x * ((self._z / 100.) + 1) - ycor = self._y * ((self._z / 100.) + 1) + xcor = self._x * ((self._z / self._z_scale) + 1) + ycor = self._y * ((self._z / self._z_scale) + 1) self._z = z self.set_xy(xcor, ycor, share, pendown) @@ -602,8 +608,8 @@ class Turtle: ycor = y * self._turtles.turtle_window.coord_scale if not dragging and self._z > 0.0: - xcor /= ((self._z / 100.) + 1) - ycor /= ((self._z / 100.) + 1) + xcor /= ((self._z / self._z_scale) + 1) + ycor /= ((self._z / self._z_scale) + 1) self._draw_line(old, (xcor, ycor), pendown) self.move_turtle((xcor, ycor)) @@ -637,6 +643,9 @@ class Turtle: r = -r a = -a pos = self.get_xy() + if self._z > 0.0: + pos[0] = pos[0] * ((self._z / self._z_scale) + 1) + pos[1] = pos[1] * ((self._z / self._z_scale) + 1) cx = pos[0] + r * cos(self._heading * DEGTOR) cy = pos[1] - r * sin(self._heading * DEGTOR) if self._pen_state: @@ -661,6 +670,9 @@ class Turtle: r = -r a = -a pos = self.get_xy() + if self._z > 0.0: + pos[0] = pos[0] * ((self._z / self._z_scale) + 1) + pos[1] = pos[1] * ((self._z / self._z_scale) + 1) cx = pos[0] - r * cos(self._heading * DEGTOR) cy = pos[1] + r * sin(self._heading * DEGTOR) if self._pen_state: diff --git a/plugins/threed/__init__.py b/plugins/threed/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/plugins/threed/__init__.py diff --git a/plugins/threed/icons/turtleoff.svg b/plugins/threed/icons/turtleoff.svg new file mode 100644 index 0000000..9a70be3 --- /dev/null +++ b/plugins/threed/icons/turtleoff.svg @@ -0,0 +1,97 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/threed/icons/turtleon.svg b/plugins/threed/icons/turtleon.svg new file mode 100644 index 0000000..4d955de --- /dev/null +++ b/plugins/threed/icons/turtleon.svg @@ -0,0 +1,106 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/threed/threed.py b/plugins/threed/threed.py new file mode 100644 index 0000000..7cb303b --- /dev/null +++ b/plugins/threed/threed.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- +#Copyright (c) 2012, Walter Bender +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +from gettext import gettext as _ + +from plugins.plugin import Plugin +from TurtleArt.tapalette import make_palette +from TurtleArt.taprimitive import (ArgSlot, ConstantArg, Primitive) +from TurtleArt.tatype import (TYPE_FLOAT, TYPE_NUMBER) +from TurtleArt.taturtle import Turtle + + +class Threed(Plugin): + """ a class for defining the 3-D Turtle Blocks """ + + def __init__(self, turtle_window): + Plugin.__init__(self) + self.tw = turtle_window + + def setup(self): + self._turtle_palette() + + def _turtle_palette(self): + palette = make_palette('turtle', + colors=["#00FF00", "#00A000"], + help_string=_('Palette of turtle commands'), + translation=_('turtle')) + + palette.add_block('zcor', + style='box-style', + label=_('zcor'), + help_string=_( + 'holds current z-coordinate value of the turtle ' + '(can be used in place of a number block)'), + value_block=True, + prim_name='zcor') + self.tw.lc.def_prim( + 'zcor', 0, + Primitive(Primitive.divide, return_type=TYPE_FLOAT, + arg_descs=[ConstantArg(Primitive(Turtle.get_z)), + ConstantArg(Primitive( + self.tw.get_coord_scale))])) + + palette.add_block('setxyz', + style='basic-style-3arg', + # TRANS: xyz are coordinates in a 3-dimensional space + label=[_('set xyz') + '\n\n', + _('x'), _('y'), _('z')], + prim_name='setxyz', + default=[0, 0, 0], + help_string=_('sets the xyz-coordinates of the ' + 'turtle')) + self.tw.lc.def_prim( + 'setxyz', 3, + Primitive(Turtle.set_xyz, + arg_descs=[ArgSlot(TYPE_NUMBER), ArgSlot(TYPE_NUMBER), + ArgSlot(TYPE_NUMBER)], + call_afterwards=self.after_move)) + + def after_move(self, *ignored_args, **ignored_kwargs): + ''' Update labels after moving the turtle ''' + if self.tw.lc.update_values: + self.tw.lc.update_label_value( + 'xcor', + self.tw.turtles.get_active_turtle().get_xy()[0] / + self.tw.coord_scale) + self.tw.lc.update_label_value( + 'ycor', + self.tw.turtles.get_active_turtle().get_xy()[1] / + self.tw.coord_scale) + self.tw.lc.update_label_value( + 'zcor', + self.tw.turtles.get_active_turtle().get_xyz()[2] / + self.tw.coord_scale) -- cgit v0.9.1