Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/taturtle.py
blob: cf228b3a922dc73ca618473c6bac147a7326a3d2 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#Copyright (c) 2007-8, Playful Invention Company.

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

import gtk
from math import sin,cos,pi
class taTurtle: pass

from tasprites import *
from tasetup import load_image

colors = {}
DEGTOR = 2*pi/360

color_table = (
    0xFF0000,0xFF0D00,0xFF1A00,0xFF2600,0xFF3300,
    0xFF4000,0xFF4D00,0xFF5900,0xFF6600,0xFF7300,
    0xFF8000,0xFF8C00,0xFF9900,0xFFA600,0xFFB300,
    0xFFBF00,0xFFCC00,0xFFD900,0xFFE600,0xFFF200,
    0xFFFF00,0xE6FF00,0xCCFF00,0xB3FF00,0x99FF00,
    0x80FF00,0x66FF00,0x4DFF00,0x33FF00,0x1AFF00,
    0x00FF00,0x00FF0D,0x00FF1A,0x00FF26,0x00FF33,
    0x00FF40,0x00FF4D,0x00FF59,0x00FF66,0x00FF73,
    0x00FF80,0x00FF8C,0x00FF99,0x00FFA6,0x00FFB3,
    0x00FFBF,0x00FFCC,0x00FFD9,0x00FFE6,0x00FFF2,
    0x00FFFF,0x00F2FF,0x00E6FF,0x00D9FF,0x00CCFF,
    0x00BFFF,0x00B3FF,0x00A6FF,0x0099FF,0x008CFF,
    0x0080FF,0x0073FF,0x0066FF,0x0059FF,0x004DFF,
    0x0040FF,0x0033FF,0x0026FF,0x001AFF,0x000DFF,
    0x0000FF,0x0D00FF,0x1A00FF,0x2600FF,0x3300FF,
    0x4000FF,0x4D00FF,0x5900FF,0x6600FF,0x7300FF,
    0x8000FF,0x8C00FF,0x9900FF,0xA600FF,0xB300FF,
    0xBF00FF,0xCC00FF,0xD900FF,0xE600FF,0xF200FF,
    0xFF00FF,0xFF00E6,0xFF00CC,0xFF00B3,0xFF0099,
    0xFF0080,0xFF0066,0xFF004D,0xFF0033,0xFF001A)

def tNew(tw,w,h):
    t = taTurtle()
    t.tw, t.width, t.height = tw, w, h
    t.canvas = sprNew(tw,0,0,gtk.gdk.Pixmap(tw.area,w,h,-1))
    t.canvas.type = 'canvas'
    setlayer(t.canvas,600)
    t.shapelist = \
        [load_image(tw.path, 'shapes','t'+str(i)) for i in range(36)]
    t.spr = sprNew(tw,100,100,t.shapelist[0])
    t.spr.type = 'turtle'
    setlayer(t.spr, 630)
    t.gc = t.canvas.image.new_gc()
    t.shade = 0
    clearscreen(t)
    return t

def clearscreen(t):
    t.xcor, t.ycor, t.heading = 0,0,0
    rect = gtk.gdk.Rectangle(0,0,t.width,t.height)
    t.gc.set_foreground(t.tw.bgcolor)
    t.canvas.image.draw_rectangle(t.gc, True, *rect)
    invalt(t,0,0,t.width,t.height)
    setpensize(t,5)
    setcolor(t,0)
    settextcolor(t,70)
    setshade(t,50)
    t.pendown = True
    move_turtle(t)
    turn_turtle(t)
    return None

def forward(t, n):
    t.gc.set_foreground(t.tw.fgcolor)
    oldx, oldy = t.xcor, t.ycor
    try:
        t.xcor += n*sin(t.heading*DEGTOR)
        t.ycor += n*cos(t.heading*DEGTOR)
    except:
        pass
    if t.pendown:
        draw_line(t,oldx,oldy,t.xcor,t.ycor)
    move_turtle(t)
    return None

def seth(t,n):
    try:
        t.heading=n
    except:
        pass
    t.heading%=360
    turn_turtle(t)
    return None

def right(t,n):
    try:
        t.heading+=n
    except:
        pass
    t.heading%=360
    turn_turtle(t)
    return None

def arc(t,a,r):
    t.gc.set_foreground(t.tw.fgcolor)
    try:
        if a<0: larc(t,-a,r)
        else: rarc(t,a,r)
    except:
        pass
    move_turtle(t)
    turn_turtle(t)

def rarc(t,a,r):
    if r<0: r=-r; a=-a
    cx = t.xcor+r*cos(t.heading*DEGTOR)
    cy = t.ycor-r*sin(t.heading*DEGTOR)
    x,y,w,h=t.width/2+int(cx-r),t.height/2-int(cy+r),int(2*r),int(2*r)
    if t.pendown:
        t.canvas.image.draw_arc(t.gc,False,x,y,w,h, \
            int(180-t.heading-a)*64,int(a)*64)
    invalt(t,x-t.pensize/2-3,y-t.pensize/2-3,w+t.pensize+6,h+t.pensize+6)
    right(t,a)
    t.xcor=cx-r*cos(t.heading*DEGTOR)
    t.ycor=cy+r*sin(t.heading*DEGTOR)

def larc(t,a,r):
    if r<0: r=-r; a=-a
    cx = t.xcor-r*cos(t.heading*DEGTOR)
    cy = t.ycor+r*sin(t.heading*DEGTOR)
    x,y,w,h=t.width/2+int(cx-r),t.height/2-int(cy+r),int(2*r),int(2*r)
    if t.pendown:
        t.canvas.image.draw_arc(t.gc,False,x,y,w,h,int(360-t.heading)*64, \
            int(a)*64)
    invalt(t,x-t.pensize/2-3,y-t.pensize/2-3,w+t.pensize+6,h+t.pensize+6)
    right(t,-a)
    t.xcor=cx+r*cos(t.heading*DEGTOR)
    t.ycor=cy-r*sin(t.heading*DEGTOR)

def setxy(t,x,y):
    try:
        t.xcor,t.ycor = x,y
    except:
        pass
    move_turtle(t)

def setpensize(t,ps):
    try:
        if ps<0:
            ps=0;
        t.pensize = ps
    except:
        pass
    t.gc.set_line_attributes(int(t.pensize),gtk.gdk.LINE_SOLID, \
        gtk.gdk.CAP_ROUND, gtk.gdk.JOIN_MITER)
    return None

def setcolor(t,c):
    try:
        t.color = c
    except:
        pass
    set_fgcolor(t)
    return None

def settextcolor(t,c):
    try:
        t.tcolor = c
    except:
        pass
    set_textcolor(t)
    return None

def setshade(t,s):
    try:
        t.shade = s
    except:
        pass
    set_fgcolor(t)
    set_textcolor(t)
    return None

def fillscreen(t,c,s):
    oldc, olds = t.color,t.shade
    try:
        setcolor(t,c); setshade(t,s)
        rect = gtk.gdk.Rectangle(0,0,t.width,t.height)
        t.gc.set_foreground(t.tw.fgcolor)
        t.canvas.image.draw_rectangle(t.gc, True, *rect)
        invalt(t,0,0,t.width,t.height)
        setcolor(t,oldc); setshade(t,olds)
    except:
        pass
    return None

def set_fgcolor(t):
    sh = (wrap100(t.shade)-50)/50.0
    rgb = color_table[wrap100(t.color)]
    r,g,b = (rgb>>8)&0xff00,rgb&0xff00,(rgb<<8)&0xff00
    r,g,b = calc_shade(r,sh),calc_shade(g,sh),calc_shade(b,sh)
    t.tw.rgb = [r>>8,g>>8,b>>8]
    t.tw.fgcolor = t.tw.cm.alloc_color(r,g,b)

def set_textcolor(t):
    sh = (wrap100(t.shade)-50)/50.0
    rgb = color_table[wrap100(t.tcolor)]
    r,g,b = (rgb>>8)&0xff00,rgb&0xff00,(rgb<<8)&0xff00
    r,g,b = calc_shade(r,sh),calc_shade(g,sh),calc_shade(b,sh)
    t.tw.textcolor = t.tw.cm.alloc_color(r,g,b)

def wrap100(n):
    n = int(n)
    n %= 200
    if n>99: n=199-n
    return n

def calc_shade(c,s):
    if s<0: return int(c*(1+s*.8))
    return int(c+(65536-c)*s*.9)

def setpen(t,bool):
    t.pendown = bool

def draw_pixbuf(t,pixbuf,a,b,x,y,w,h):
    t.canvas.image.draw_pixbuf(t.gc, pixbuf, a, b, x, y)
    invalt(t,x,y,w,h)

def draw_text(t, label, x, y, size, w):
    t.gc.set_foreground(t.tw.textcolor)
    fd = pango.FontDescription('Sans')
    try:
        fd.set_size(int(size)*pango.SCALE)
    except:
        pass
    pl = t.tw.window.create_pango_layout(str(label))
    pl.set_font_description(fd)
    pl.set_width(int(w)*pango.SCALE)
    t.canvas.image.draw_layout(t.gc,int(x),int(y),pl)
    w,h = pl.get_pixel_size()
    invalt(t,x,y,w,h)

def draw_line(t,x1,y1,x2,y2):
    x1,y1 = t.width/2+int(x1), t.height/2-int(y1)
    x2,y2 = t.width/2+int(x2), t.height/2-int(y2)
    if x1<x2: minx,maxx=x1,x2
    else: minx,maxx=x2,x1
    if y1<y2: miny,maxy=y1,y2
    else: miny,maxy=y2,y1
    w,h=maxx-minx,maxy-miny
    t.canvas.image.draw_line(t.gc,x1,y1,x2,y2)
    invalt(t,minx-t.pensize/2-3,miny-t.pensize/2-3,w+t.pensize+6, \
        h+t.pensize+6)

def turn_turtle(t):
    setshape(t.spr, t.shapelist[(int(t.heading+5)%360)/10])

def move_turtle(t):
    x,y = t.width/2+int(t.xcor), t.height/2-int(t.ycor)
    move(t.spr, (t.canvas.x+x-30,t.canvas.y+y-30))
    invalt(t,x-30,y-30,60,60)

def invalt(t,x,y,w,h):
    rect = gtk.gdk.Rectangle(int(x+t.canvas.x),int(y+t.canvas.y), \
        int(w),int(h))
    t.tw.area.invalidate_rect(rect, False)