Skip to content

Instantly share code, notes, and snippets.

@matthewryanscott
Created June 26, 2009 18:19
Show Gist options
  • Save matthewryanscott/136647 to your computer and use it in GitHub Desktop.
Save matthewryanscott/136647 to your computer and use it in GitHub Desktop.
class Canvas(m.Model):
"""A canvas for turtle(s) to draw on.
The origin of a canvas is the bottom left of the plane.
A heading of 0 degrees points upward.
"""
class Meta:
verbose_name_plural = 'canvases'
title = m.CharField(max_length=100)
width = m.IntegerField('width (pixels)')
height = m.IntegerField('height (pixels)')
def initial_state(self):
return CanvasState.objects.filter(
canvas=self, previous_state=None).get()
def __unicode__(self):
return self.title
def save(self, *args, **kw):
# Prevent alteration of width and height.
if self.pk is not None:
other = Canvas.objects.get(pk=self.pk)
if other.width != self.width or other.height != self.height:
raise ValueError('width and height cannot be altered')
# Save object.
m.Model.save(self)
# Store initial canvas state with centered turtle.
initial_state = CanvasState(
canvas=self,
previous_state=None,
turtle_x=float(self.width) / 2,
turtle_y=float(self.height) / 2,
turtle_heading=0.0,
)
initial_state.save()
class Canvas(E.Entity):
"""A canvas for turtle(s) to draw on.
The origin of a canvas is the bottom left of the plane.
A heading of 0 degrees points upward.
"""
_plural = 'Canvases'
title = f.string()
width = f.integer(units='pixels')
height = f.integer(units='pixels')
@f.entity('CanvasState')
def initial_state(self):
return db.CanvasState.findone(canvas=self, previous_state=UNASSIGNED)
def __unicode__(self):
return self.title
class _Create(T.Create):
def _after_execute(self, db, canvas):
db.execute(db.CanvasState.t.create(
canvas=canvas,
previous_state=UNASSIGNED,
turtle_x=float(canvas.width) / 2,
turtle_y=float(canvas.height) / 2,
turtle_heading=0.0,
))
class _Update(T.Update):
def _setup(self):
self.f.width.readonly = True
self.f.height.readonly = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment