From bde0306a2345bddb991f8107c809136b6b0f5868 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Mon, 14 Mar 2011 01:54:12 +0000 Subject: add a label to the remote turtles (experimental); fix bug that prevented colors being set for remote turtles on resume --- diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index 39fb6d3..9e4a171 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -66,9 +66,8 @@ from gettext import gettext as _ from tapalette import make_palette from talogo import primitive_dictionary, logoerror -from taconstants import CONSTANTS, BLACK, WHITE from tautils import convert, chr_to_ord, round_int, strtype - +from taconstants import BLACK, WHITE, CONSTANTS def _num_type(x): """ Is x a number type? """ @@ -231,6 +230,11 @@ turtle (can be used in place of a number block)'), self.tw.lc.def_prim( 'heading', 0, lambda self: self.tw.canvas.heading) + palette.add_block('turtle-label', + hidden=True, + style='blank-style', + label=['turtle']) + # Deprecated palette.add_block('setxy', hidden=True, diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py index 2355b02..25490d6 100644 --- a/TurtleArt/tablock.py +++ b/TurtleArt/tablock.py @@ -122,6 +122,7 @@ class Block: def __init__(self, block_list, sprite_list, name, x, y, type='block', values=[], scale=BLOCK_SCALE[0], colors=['#FF0000', '#A00000']): + self.block_list = block_list self.spr = None self.shapes = [None, None] @@ -143,6 +144,7 @@ class Block: self.block_methods = { 'basic-style': self._make_basic_style, + 'blank-style': self._make_blank_style, 'basic-style-head': self._make_basic_style_head, 'basic-style-head-1arg': self._make_basic_style_head_1arg, 'basic-style-tail': self._make_basic_style_tail, @@ -361,7 +363,6 @@ class Block: return (self.ex, self.ey) def _new_block_from_factory(self, sprite_list, x, y, copy_block=None): - self.svg = SVG() self.svg.set_scale(self.scale) self.svg.set_innie([False]) @@ -484,6 +485,13 @@ class Block: self.svg.docks[0][1]], ['flow', False, self.svg.docks[1][0], self.svg.docks[1][1]]] + def _make_blank_style(self, svg, extend_x=0, extend_y=0): + self.svg.expand(self.dx + self.ex + extend_x, self.ey + extend_y) + self.svg.set_slot(False) + self.svg.set_tab(False) + self._make_block_graphics(svg, self.svg.basic_block) + self.docks = [] + def _make_basic_style_head(self, svg): self.svg.expand(10 + self.dx + self.ex, self.ey) self.svg.set_slot(False) diff --git a/TurtleArt/tacanvas.py b/TurtleArt/tacanvas.py index dea9ed6..05c4936 100644 --- a/TurtleArt/tacanvas.py +++ b/TurtleArt/tacanvas.py @@ -707,7 +707,11 @@ class TurtleGraphics: self.seth(0, False) self.setxy(0, 0, False, pendown=False) self.tw.active_turtle.set_pen_state(True) - self.tw.active_turtle = self.tw.turtles.get_turtle(k, False) + elif colors is not None: + self.tw.active_turtle = self.tw.turtles.get_turtle(k, False) + self.tw.active_turtle.set_turtle_colors(colors) + else: + self.tw.active_turtle = self.tw.turtles.get_turtle(k, False) self.tw.active_turtle.show() tx, ty = self.tw.active_turtle.get_xy() self.xcor, self.ycor = self.screen_to_turtle_coordinates(tx, ty) diff --git a/TurtleArt/tacollaboration.py b/TurtleArt/tacollaboration.py index 4a4d1fe..4955777 100644 --- a/TurtleArt/tacollaboration.py +++ b/TurtleArt/tacollaboration.py @@ -218,6 +218,7 @@ class Collaboration(): self._tw.turtle_dictionary = {nick: colors} # Add new turtle for the joiner. self._tw.canvas.set_turtle(nick, colors) + self._tw.label_remote_turtle(nick) # Sharer should send turtle dictionary. if self.initiating: event_payload = data_to_string(self._tw.turtle_dictionary) @@ -232,6 +233,7 @@ class Collaboration(): colors = self._tw.turtle_dictionary[nick] # add new turtle for the joiner self._tw.canvas.set_turtle(nick, colors) + self._tw.label_remote_turtle(nick) self.waiting_for_turtles = False def _draw_pixbuf(self, payload): diff --git a/TurtleArt/tapalette.py b/TurtleArt/tapalette.py index 3f94fec..43a5d25 100644 --- a/TurtleArt/tapalette.py +++ b/TurtleArt/tapalette.py @@ -31,6 +31,7 @@ content_blocks = ['number', 'string', 'description', 'audio', 'video', 'journal'] value_blocks = [] # blocks whose labels are updated get added here block_styles = {'basic-style': [], + 'blank-style': [], 'basic-style-head': [], 'basic-style-head-1arg': [], 'basic-style-tail': [], diff --git a/TurtleArt/taturtle.py b/TurtleArt/taturtle.py index 8a70638..0c334dd 100644 --- a/TurtleArt/taturtle.py +++ b/TurtleArt/taturtle.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -#Copyright (c) 2010 Walter Bender +#Copyright (c) 2010,11 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 @@ -25,6 +25,8 @@ from tacanvas import wrap100, color_table from sprites import Sprite from tautils import debug_output +from random import uniform +from math import sin, cos SHAPES = 36 @@ -118,11 +120,17 @@ class Turtle: self.pen_gray = 100 self.pen_size = 5 self.pen_state = True + self.label_block = None self._prep_shapes(key, turtles, turtle_colors) + # Choose a random angle from which to attach the turtle label if turtles.sprite_list is not None: self.spr = Sprite(turtles.sprite_list, 0, 0, self.shapes[0]) + angle = uniform(0, 6.14) + r = self.shapes[0].get_width() * 0.67 + self.label_xy = [int(r * sin(angle) + r / 2.0), + int(r * cos(angle) + r / 2.0)] else: self.spr = None turtles.add_to_dict(key, self) @@ -150,6 +158,13 @@ class Turtle: self.colors = DEFAULT_TURTLE_COLORS self.shapes = turtles.get_pixbufs() + def set_turtle_colors(self, turtle_colors): + ''' reset the colors of a preloaded turtle ''' + if turtle_colors is not None: + self.colors = turtle_colors[:] + self.shapes = generate_turtle_pixbufs(self.colors) + self.set_heading(self.heading) + def set_shapes(self, shapes): """ Reskin the turtle """ n = len(shapes) @@ -212,6 +227,8 @@ class Turtle: """ Hide the turtle. """ if self.spr is not None: self.spr.hide() + if self.label_block is not None: + self.label_block.spr.hide() self.hidden = True def show(self): @@ -221,12 +238,19 @@ class Turtle: self.hidden = False self.move((self.x, self.y)) self.set_heading(self.heading) + if self.label_block is not None: + self.label_block.spr.move((self.x + self.label_xy[0], + self.y + self.label_xy[1])) + self.label_block.spr.set_layer(TURTLE_LAYER) def move(self, pos): """ Move the turtle. """ self.x, self.y = int(pos[0]), int(pos[1]) if not self.hidden and self.spr is not None: self.spr.move(pos) + if self.label_block is not None: + self.label_block.spr.move((pos[0] + self.label_xy[0], + pos[1] + self.label_xy[1])) return(self.x, self.y) def get_name(self): diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index 0113cb3..669d93e 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -1563,6 +1563,17 @@ class TurtleArtWindow(): return True return False + def label_remote_turtle(self, name): + ''' Add a label to remote turtles ''' + turtle = self.turtles.get_turtle(name) + if turtle is not None: + turtle.label_block = Block(self.block_list, + self.sprite_list, 'turtle-label', 0, 0, + 'label', [], 1.0, + colors=['#A0A0A0', '#C0C0C0']) + turtle.label_block.spr.set_label(name) + turtle.show() + def _move_turtle(self, x, y): """ Move the selected turtle to (x, y). """ (cx, cy) = self.canvas.canvas.get_xy() -- cgit v0.9.1