Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugins/threed/threed.py
blob: 5a37856a28c443c91abd6d4d8440407542e1c302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- 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))

        palette.add_block('setp',
                          style='basic-style-1arg',
                          label=_('set pitch'),
                          prim_name='setp',
                          default=0,
                          help_string=_('sets the pitch of the turtle (0 is \
towards the center of the screen (the horizon).)'))
        self.tw.lc.def_prim(
            'setp', 1,
            Primitive(Turtle.set_pitch,
                      arg_descs=[ArgSlot(TYPE_NUMBER)],
                      call_afterwards=lambda value: self.after_set(
                          'pitch', value)))

        palette.add_block('setw',
                          style='basic-style-1arg',
                          label=_('set yaw'),
                          prim_name='setw',
                          default=0,
                          help_string=_('sets the yaw of the turtle (0 is \
perpendicular to the plane of the screen.)'))
        self.tw.lc.def_prim(
            'setw', 1,
            Primitive(Turtle.set_pitch,
                      arg_descs=[ArgSlot(TYPE_NUMBER)],
                      call_afterwards=lambda value: self.after_set(
                          'yaw', value)))

        palette.add_block('pitch_',
                          style='box-style',
                          label=_('pitch'),
                          help_string=_('holds current pitch value of the \
turtle (can be used in place of a number block)'),
                          value_block=True,
                          prim_name='pitch_')
        self.tw.lc.def_prim('pitch', 0,
                            Primitive(
                                Turtle.get_pitch, return_type=TYPE_NUMBER))

        palette.add_block('yaw',
                          style='box-style',
                          label=_('yaw'),
                          help_string=_('holds current yaw value of the \
turtle (can be used in place of a number block)'),
                          value_block=True,
                          prim_name='yaw')
        self.tw.lc.def_prim('yaw', 0,
                            Primitive(
                                Turtle.get_yaw, return_type=TYPE_NUMBER))

    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)

    def after_set(self, name, value=None):
        ''' Update the associated value blocks '''
        if value is not None:
            if self.tw.lc.update_values:
                self.tw.lc.update_label_value(name, value)