Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tacanvas.py
diff options
context:
space:
mode:
authorWalter Bender <walter@walter-laptop.(none)>2010-01-27 11:39:25 (GMT)
committer Walter Bender <walter@walter-laptop.(none)>2010-01-27 11:39:25 (GMT)
commit9b6de0eb9e4f09cddd12129a2547dd1ad07552f0 (patch)
treed0cd8e6fedaa4c39b9dfc09cb54d2aa2be364e2f /tacanvas.py
parent1d0e4f6b7a8fee35983db7346ead9d752627f7ae (diff)
fixing turtle rotation
Diffstat (limited to 'tacanvas.py')
-rw-r--r--tacanvas.py315
1 files changed, 315 insertions, 0 deletions
diff --git a/tacanvas.py b/tacanvas.py
new file mode 100644
index 0000000..5bf62a1
--- /dev/null
+++ b/tacanvas.py
@@ -0,0 +1,315 @@
+#Copyright (c) 2007-8, Playful Invention Company.
+#Copyright (c) 2008-9, Walter Bender
+
+#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 tasetup import load_image
+import sprites
+import taturtle
+import pango
+
+from constants import *
+
+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 = sprites.Sprite(tw.sprite_list, 0, 0,
+ gtk.gdk.Pixmap(tw.area,w,h,-1))
+ (t.cx,t.cy) = t.canvas.get_xy()
+ t.canvas.type = 'canvas'
+ t.canvas.set_layer(CANVAS_LAYER)
+ t.shapelist = []
+ t.t = taturtle.Turtle(tw.turtle_list, tw.sprite_list, ["#800000", "#A00000"])
+ t.spr = t.t.spr
+ t.spr.type = 'turtle'
+ t.spr.set_layer(TURTLE_LAYER)
+ 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)
+ settextsize(t,32)
+ setshade(t,50)
+ t.pendown = True
+ move_turtle(t)
+ turn_turtle(t)
+ return None
+
+def forward(t, n):
+ n *= t.tw.coord_scale
+ 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)
+ r *= t.tw.coord_scale
+ 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*t.tw.coord_scale/2-3,y-t.pensize*t.tw.coord_scale/2-3,\
+ w+t.pensize*t.tw.coord_scale+6,h+t.pensize*t.tw.coord_scale+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*t.tw.coord_scale/2-3,y-t.pensize*t.tw.coord_scale/2-3,\
+ w+t.pensize*t.tw.coord_scale+6,h+t.pensize*t.tw.coord_scale+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):
+ x *= t.tw.coord_scale
+ y *= t.tw.coord_scale
+ 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*t.tw.coord_scale), \
+ gtk.gdk.LINE_SOLID, \
+ gtk.gdk.CAP_ROUND, gtk.gdk.JOIN_MITER)
+ return None
+
+def setcolor(t,c):
+ try:
+ t.color = c
+ t.tcolor = c
+ except:
+ pass
+ set_fgcolor(t)
+ set_textcolor(t)
+ return None
+
+def settextcolor(t,c):
+ try:
+ t.tcolor = c
+ except:
+ pass
+ set_textcolor(t)
+ return None
+
+def settextsize(t,c):
+ try:
+ t.tw.textsize = c
+ except:
+ pass
+ 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):
+ w *= t.tw.coord_scale
+ h *= t.tw.coord_scale
+ 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):
+ w *= t.tw.coord_scale
+ t.gc.set_foreground(t.tw.textcolor)
+ fd = pango.FontDescription('Sans')
+ try:
+ fd.set_size(int(size*t.tw.coord_scale)*pango.SCALE)
+ except:
+ pass
+ if type(label) == str or type(label) == unicode:
+ pl = t.tw.window.create_pango_layout(label.replace("\0"," "))
+ elif type(label) == float or type(label) == int:
+ pl = t.tw.window.create_pango_layout(str(label))
+ else:
+ print "draw text: Type Error: %s" % (type(label))
+ 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*t.tw.coord_scale/2-3,
+ miny-t.pensize*t.tw.coord_scale/2-3,
+ w+t.pensize*t.tw.coord_scale+6,
+ h+t.pensize*t.tw.coord_scale+6)
+
+def turn_turtle(t):
+ # t.spr.set_shape(t.shapelist[(int(t.heading+5)%360)/10])
+ print "turning turtle to position %d" % ((int(t.heading+5)%360)/10)
+ t.t.rotate((int(t.heading+5)%360)/10)
+
+def move_turtle(t):
+ x,y = t.width/2+int(t.xcor), t.height/2-int(t.ycor)
+ t.spr.move((t.cx+x-30, t.cy+y-30))
+ invalt(t, x-30, y-30, 60, 60)
+
+def invalt(t,x,y,w,h):
+ rect = gtk.gdk.Rectangle(int(x+t.cx), int(y+t.cy), int(w),int(h))
+ t.tw.area.invalidate_rect(rect, False)
+
+
+