diff options
author | Wade Brainerd <wadetb@gmail.com> | 2009-04-06 11:23:08 (GMT) |
---|---|---|
committer | Wade Brainerd <wadetb@gmail.com> | 2009-04-06 11:23:08 (GMT) |
commit | 06a8fac4cb75a5d047a6002045a73b19c8e755e0 (patch) | |
tree | 4a855575ca35332c4e34a0a25d3ea5d49881b795 | |
parent | aba7e1f7a4964132bd27c53cbd84ffd28d10245b (diff) |
Tidy up grid code a bit.
-rw-r--r-- | amountobject.py | 15 | ||||
-rw-r--r-- | amountproblem.py | 4 | ||||
-rw-r--r-- | areaproblem.py | 2 | ||||
-rw-r--r-- | balanceobject.py | 15 | ||||
-rw-r--r-- | compare3lesson.py | 26 | ||||
-rw-r--r-- | cuttingproblem.py | 2 | ||||
-rw-r--r-- | draggableobject.py | 31 | ||||
-rw-r--r-- | droporiginobject.py | 1 | ||||
-rw-r--r-- | groupobject.py | 15 | ||||
-rw-r--r-- | lengthproblem.py | 2 | ||||
-rw-r--r-- | linesegmentmovableobject.py | 15 | ||||
-rw-r--r-- | linesegmentobject.py | 15 | ||||
-rw-r--r-- | massproblem.py | 2 | ||||
-rw-r--r-- | movableobject.py | 29 | ||||
-rw-r--r-- | objectarea.py | 26 | ||||
-rw-r--r-- | shapeobject.py | 14 | ||||
-rw-r--r-- | symbolmovableobject.py | 15 | ||||
-rw-r--r-- | symbolobject.py | 15 | ||||
-rw-r--r-- | threedobject.py | 15 | ||||
-rw-r--r-- | volumeobject.py | 15 | ||||
-rw-r--r-- | volumeproblem.py | 2 |
21 files changed, 49 insertions, 227 deletions
diff --git a/amountobject.py b/amountobject.py index 7d434ed..b303a88 100644 --- a/amountobject.py +++ b/amountobject.py @@ -47,21 +47,6 @@ class AmountObject(MovableObject): else: self.draw_triangle(cr, self.color) - def is_in_container(self): - bounds_inner, bounds_outer = self.get_bounds() - - GRID_SIZE = 25 - - DRAGGING_RECT_WIDTH = 48*GRID_SIZE - DRAGGING_RECT_HEIGHT = 32*GRID_SIZE - - if bounds_inner.x < -2 or bounds_outer.x > DRAGGING_RECT_WIDTH + 2 \ - or bounds_inner.y < -1 or bounds_outer.y > DRAGGING_RECT_HEIGHT: - #print "GroupObject: is_in_container returning False" - return False - #print "GroupObject: is_in_container returning True" - return True - def move(self, pos): self.queue_draw() diff --git a/amountproblem.py b/amountproblem.py index bee8f95..36d695c 100644 --- a/amountproblem.py +++ b/amountproblem.py @@ -217,7 +217,9 @@ class AmountProblem(Problem): if object2 == CIRCLE: self.shape2_model[i].draw_as_circle = True - def show_problem(self): + def show_problem(self): + self.container.configure_dragging_area(25, 48, 32, 2 * math.pi) + self.container.instructions = InstructionsObject(Vector(50, 25), 'Compare the things in amount') self.container.add_object(self.container.instructions) diff --git a/areaproblem.py b/areaproblem.py index de36ade..79720eb 100644 --- a/areaproblem.py +++ b/areaproblem.py @@ -261,6 +261,8 @@ class AreaProblem(Problem): return def show_problem(self): + self.container.configure_dragging_area(50, 24, 16, math.pi/4) + self.container.add_object(self.shape1) self.container.add_object(self.shape2) diff --git a/balanceobject.py b/balanceobject.py index a838713..a85efa1 100644 --- a/balanceobject.py +++ b/balanceobject.py @@ -82,21 +82,6 @@ class BalanceObject(MovableObject): self.draw_right(cr, Vector (self.pos.x, self.pos.y)) else: self.draw_middle(cr, Vector (self.pos.x, self.pos.y)) - - def is_in_container(self): - bounds_inner, bounds_outer = self.get_bounds() - - GRID_SIZE = 25 - - DRAGGING_RECT_WIDTH = 48*GRID_SIZE - DRAGGING_RECT_HEIGHT = 32*GRID_SIZE - - if bounds_inner.x < -2 or bounds_outer.x > DRAGGING_RECT_WIDTH + 2 \ - or bounds_inner.y < -1 or bounds_outer.y > DRAGGING_RECT_HEIGHT: - #print "GroupObject: is_in_container returning False" - return False - #print "GroupObject: is_in_container returning True" - return True def adjust_balance_state(self): #print "BalanceObject: adjust_balance_state called, self.state =", self.state diff --git a/compare3lesson.py b/compare3lesson.py index 1373581..552ffd4 100644 --- a/compare3lesson.py +++ b/compare3lesson.py @@ -85,44 +85,20 @@ class Compare3Lesson(ObjectArea): self.problem_type = random.choice(PROBLEM_TYPES) # Uncomment this to choose a particular problem type. - #self.problem_type = 'mass' + #self.problem_type = 'amount' if self.problem_type == 'length': self.problem = LengthProblem(self) - self.GRID_SIZE = 25 - self.DRAGGING_RECT_WIDTH = 48*self.GRID_SIZE - self.DRAGGING_RECT_HEIGHT = 32*self.GRID_SIZE - self.RADIAL_GRID_SIZE = math.pi/8 elif self.problem_type == 'amount': self.problem = AmountProblem(self) - self.GRID_SIZE = 25 - self.DRAGGING_RECT_WIDTH = 48*self.GRID_SIZE - self.DRAGGING_RECT_HEIGHT = 32*self.GRID_SIZE - self.RADIAL_GRID_SIZE = 2 * math.pi elif self.problem_type == 'mass': self.problem = MassProblem(self) - self.GRID_SIZE = 25 - self.DRAGGING_RECT_WIDTH = 48*self.GRID_SIZE - self.DRAGGING_RECT_HEIGHT = 32*self.GRID_SIZE - self.RADIAL_GRID_SIZE = 2 * math.pi elif self.problem_type == 'volume': self.problem = VolumeProblem(self) - self.GRID_SIZE = 25 - self.DRAGGING_RECT_WIDTH = 48*self.GRID_SIZE - self.DRAGGING_RECT_HEIGHT = 32*self.GRID_SIZE - self.RADIAL_GRID_SIZE = 2 * math.pi elif self.problem_type == 'cutting': self.problem = CuttingProblem(self) - self.GRID_SIZE = 50 - self.DRAGGING_RECT_WIDTH = 24*self.GRID_SIZE - self.DRAGGING_RECT_HEIGHT = 16*self.GRID_SIZE - self.RADIAL_GRID_SIZE = math.pi/4 else: self.problem = AreaProblem(self) - self.GRID_SIZE = 50 - self.DRAGGING_RECT_WIDTH = 24*self.GRID_SIZE - self.DRAGGING_RECT_HEIGHT = 16*self.GRID_SIZE - self.RADIAL_GRID_SIZE = math.pi/4 self.answer = self.problem.find_answer() self.problem_solved_stage1() diff --git a/cuttingproblem.py b/cuttingproblem.py index f6d5478..524e361 100644 --- a/cuttingproblem.py +++ b/cuttingproblem.py @@ -138,6 +138,8 @@ class CuttingProblem(Problem): return def show_problem(self): + self.container.configure_dragging_area(50, 24, 16, math.pi / 4) + self.container.add_object(self.shape1) self.container.add_object(self.shape2) diff --git a/draggableobject.py b/draggableobject.py index 4010789..0c23b6a 100644 --- a/draggableobject.py +++ b/draggableobject.py @@ -19,16 +19,6 @@ from vector import Vector import gtk, math -# The global grid unit size, in pixels. Objects will snap to multiples of this value. -#GRID_SIZE = 50 -# -## Width and height of region within which we can drag objects. -#DRAGGING_RECT_WIDTH = 24*GRID_SIZE -#DRAGGING_RECT_HEIGHT = 16*GRID_SIZE -# -## The global grid angle size, in radians. -#RADIAL_GRID_SIZE = math.pi/4 - class DraggableObject(Object): """ Extends Objects with the ability to Drag and Drop. @@ -52,11 +42,6 @@ class DraggableObject(Object): #self.drag_copy = False self.moving_by_key_press = False - #self.GRID_SIZE = 50 - #self.DRAGGING_RECT_WIDTH = 24*GRID_SIZE - #self.DRAGGING_RECT_HEIGHT = 16*GRID_SIZE - #self.RADIAL_GRID_SIZE = math.pi/4 - def get_bounds(self): pass @@ -144,9 +129,10 @@ class DraggableObject(Object): return True def on_key(self, event): - GRID_SIZE = 50 if self.container: GRID_SIZE = self.container.GRID_SIZE + else: + GRID_SIZE = 50 if not self.selectable or not self.draggable: return @@ -221,5 +207,14 @@ class DraggableObject(Object): self.drop_target.hilite = True if self.container: self.container.queue_draw() - - + + # Default implementation which may be overridden by derived objects. + def is_in_container(self): + if not self.container: + return False + + mn, mx = self.get_bounds() + if mn.x < -2 or mx.x > self.container.DRAGGING_RECT_WIDTH + 2 or \ + mn.y < -1 or mx.y > self.container.DRAGGING_RECT_HEIGHT: + return False + return True diff --git a/droporiginobject.py b/droporiginobject.py index aa3d6ea..1c25573 100644 --- a/droporiginobject.py +++ b/droporiginobject.py @@ -52,6 +52,7 @@ class DropOriginObject(Object): self.objects.remove(obj)
except:
pass
+
self.container.remove_object(obj)
def adjust_tab_order(self):
diff --git a/groupobject.py b/groupobject.py index c9c8eb9..4f4c31e 100644 --- a/groupobject.py +++ b/groupobject.py @@ -86,21 +86,6 @@ class GroupObject(MovableObject): else: self.draw_triangle(cr, Vector(9 + 25 + 100, 83), self.color1) - def is_in_container(self): - bounds_inner, bounds_outer = self.get_bounds() - - GRID_SIZE = 25 - - DRAGGING_RECT_WIDTH = 48*GRID_SIZE - DRAGGING_RECT_HEIGHT = 32*GRID_SIZE - - if bounds_inner.x < -2 or bounds_outer.x > DRAGGING_RECT_WIDTH + 2 \ - or bounds_inner.y < -1 or bounds_outer.y > DRAGGING_RECT_HEIGHT: - #print "GroupObject: is_in_container returning False" - return False - #print "GroupObject: is_in_container returning True" - return True - def move(self, pos): #print "GroupObject: move called: self.container = ", self.container self.queue_draw() diff --git a/lengthproblem.py b/lengthproblem.py index f43cad3..4d8c336 100644 --- a/lengthproblem.py +++ b/lengthproblem.py @@ -172,6 +172,8 @@ class LengthProblem(Problem): return def show_problem(self): + self.container.configure_dragging_area(25, 48, 32, math.pi/8) + self.container.add_object(self.shape1) self.container.add_object(self.shape2) diff --git a/linesegmentmovableobject.py b/linesegmentmovableobject.py index 8d5509e..f8df24b 100644 --- a/linesegmentmovableobject.py +++ b/linesegmentmovableobject.py @@ -21,13 +21,6 @@ import gtk from movableobject import MovableObject from linesegmentobject import LineSegmentObject -# The global grid unit size, in pixels. Objects will snap to multiples of this value. -GRID_SIZE = 50 - -# Width and height of region within which we can drag objects. -DRAGGING_RECT_WIDTH = 24*GRID_SIZE -DRAGGING_RECT_HEIGHT = 16*GRID_SIZE - class LineSegmentMovableObject(MovableObject): """Draggable vertical line segment.""" @@ -75,14 +68,6 @@ class LineSegmentMovableObject(MovableObject): cr.set_source_rgb(0, 0, 0) cr.stroke() - def is_in_container(self): - bounds_inner, bounds_outer = self.get_bounds() - - if bounds_inner.x < -2 or bounds_outer.x > DRAGGING_RECT_WIDTH + 2 \ - or bounds_inner.y < -1 or bounds_outer.y > DRAGGING_RECT_HEIGHT: - return False - return True - def move(self, pos): self.queue_draw() diff --git a/linesegmentobject.py b/linesegmentobject.py index 915d5db..eec3654 100644 --- a/linesegmentobject.py +++ b/linesegmentobject.py @@ -19,13 +19,6 @@ from vector import Vector import gtk
-# The global grid unit size, in pixels. Objects will snap to multiples of this value.
-GRID_SIZE = 50
-
-# Width and height of region within which we can drag objects.
-DRAGGING_RECT_WIDTH = 24*GRID_SIZE
-DRAGGING_RECT_HEIGHT = 16*GRID_SIZE
-
class LineSegmentObject(DraggableObject):
"""Draggable vertical line segment."""
@@ -82,14 +75,6 @@ class LineSegmentObject(DraggableObject): cr.set_source_rgb(0, 0, 0)
cr.stroke()
- def is_in_container(self):
- bounds_inner, bounds_outer = self.get_bounds()
-
- if bounds_inner.x < -2 or bounds_outer.x > DRAGGING_RECT_WIDTH + 2 \
- or bounds_inner.y < -1 or bounds_outer.y > DRAGGING_RECT_HEIGHT:
- return False
- return True
-
def move(self, pos):
self.queue_draw()
diff --git a/massproblem.py b/massproblem.py index 605a8f6..2c820f3 100644 --- a/massproblem.py +++ b/massproblem.py @@ -109,6 +109,8 @@ class MassProblem(Problem): return def show_problem(self): + self.container.configure_dragging_area(25, 48, 32, 2 * math.pi) + self.container.add_object(self.shape1) self.container.add_object(self.shape2) diff --git a/movableobject.py b/movableobject.py index 40fc9b7..76bba15 100644 --- a/movableobject.py +++ b/movableobject.py @@ -19,21 +19,9 @@ from vector import Vector import gtk, math -# The global grid unit size, in pixels. Objects will snap to multiples of this value. -#GRID_SIZE = 50 - -# Width and height of region within which we can drag objects. -#DRAGGING_RECT_WIDTH = 24*GRID_SIZE -#DRAGGING_RECT_HEIGHT = 16*GRID_SIZE - -# The global grid angle size, in radians. -#RADIAL_GRID_SIZE = math.pi/4 - class MovableObject(Object): """ Extends Objects with the ability to move. - Derived classes must implement: - get_bounds() - Returns min, max bounds of the Object. """ def __init__(self): Object.__init__(self) @@ -218,13 +206,12 @@ class MovableObject(Object): self.container.select_object(self) def on_key(self, event): - GRID_SIZE = 50 if self.container: GRID_SIZE = self.container.GRID_SIZE - - RADIAL_GRID_SIZE = math.pi/4 - if self.container: RADIAL_GRID_SIZE = self.container.RADIAL_GRID_SIZE + else: + GRID_SIZE = 50 + RADIAL_GRID_SIZE = math.pi/4 if not self.selectable or not self.draggable: return @@ -255,3 +242,13 @@ class MovableObject(Object): self.drop_origin.remove_object(self) return + # Default implementation which may be overridden by derived objects. + def is_in_container(self): + if not self.container: + return False + + mn, mx = self.get_bounds() + if mn.x < -2 or mx.x > self.container.DRAGGING_RECT_WIDTH + 2 or \ + mn.y < -1 or mx.y > self.container.DRAGGING_RECT_HEIGHT: + return False + return True diff --git a/objectarea.py b/objectarea.py index 202ee13..af3f556 100644 --- a/objectarea.py +++ b/objectarea.py @@ -19,26 +19,6 @@ import gtk, math GRID_VISIBLE = False
-# The global grid unit size, in pixels. Objects will snap to multiples of this value.
-# For lengths:
-#GRID_SIZE = 25
-# For areas:
-#GRID_SIZE = 50
-
-# Width and height of region within which we can drag objects.
-# For lengths:
-#DRAGGING_RECT_WIDTH = 48*GRID_SIZE
-#DRAGGING_RECT_HEIGHT = 32*GRID_SIZE
-# For areas:
-#DRAGGING_RECT_WIDTH = 24*GRID_SIZE
-#DRAGGING_RECT_HEIGHT = 16*GRID_SIZE
-
-# The global grid angle size, in radians.
-# For lengths:
-#RADIAL_GRID_SIZE = math.pi/8
-# For areas:
-#RADIAL_GRID_SIZE = math.pi/4
-
# Defined coordinate system of object areas. The canvas will scale objects to make
# this coordinate system match the size of the window, while preserving aspect ratio.
AREA_WIDTH = 1200
@@ -217,6 +197,12 @@ class ObjectArea(gtk.Layout): old_selected_object.queue_draw()
object.queue_draw()
+ def configure_dragging_area(self, grid_size, dragging_rect_width, dragging_rect_height, radial_grid_size):
+ self.GRID_SIZE = grid_size
+ self.DRAGGING_RECT_WIDTH = dragging_rect_width * self.GRID_SIZE
+ self.DRAGGING_RECT_HEIGHT = dragging_rect_height * self.GRID_SIZE
+ self.RADIAL_GRID_SIZE = radial_grid_size
+
def snap_to_grid(self, object):
x, y = object.pos.x, object.pos.y
angle = object.angle
diff --git a/shapeobject.py b/shapeobject.py index 587dbd4..c3c3c7d 100644 --- a/shapeobject.py +++ b/shapeobject.py @@ -19,16 +19,6 @@ from movableobject import MovableObject import gtk, math
-# The global grid unit size, in pixels. Objects will snap to multiples of this value.
-GRID_SIZE = 50
-
-# Width and height of region within which we can drag objects.
-DRAGGING_RECT_WIDTH = 24*GRID_SIZE
-DRAGGING_RECT_HEIGHT = 16*GRID_SIZE
-
-# The global grid angle size, in radians.
-RADIAL_GRID_SIZE = math.pi/4
-
class ShapeObject(MovableObject):
"""Movable convex shape object."""
@@ -223,8 +213,8 @@ class ShapeObject(MovableObject): def is_in_container(self):
for p in self.points:
p = self.transform_point(p)
- if p.x < -2 or p.x > DRAGGING_RECT_WIDTH + 2 or \
- p.y < -1 or p.y > DRAGGING_RECT_HEIGHT:
+ if p.x < -2 or p.x > self.container.DRAGGING_RECT_WIDTH + 2 or \
+ p.y < -1 or p.y > self.container.DRAGGING_RECT_HEIGHT:
return False
return True
diff --git a/symbolmovableobject.py b/symbolmovableobject.py index e2977f6..da9d0f2 100644 --- a/symbolmovableobject.py +++ b/symbolmovableobject.py @@ -20,13 +20,6 @@ from vector import Vector import gtk
from movableobject import MovableObject
-# The global grid unit size, in pixels. Objects will snap to multiples of this value.
-GRID_SIZE = 50
-
-# Width and height of region within which we can drag objects.
-DRAGGING_RECT_WIDTH = 24*GRID_SIZE
-DRAGGING_RECT_HEIGHT = 16*GRID_SIZE
-
class SymbolMovableObject(MovableObject):
"""Draggable symbol."""
@@ -93,14 +86,6 @@ class SymbolMovableObject(MovableObject): self.width = width
self.height = height
- def is_in_container(self):
- bounds_inner, bounds_outer = self.get_bounds()
-
- if bounds_inner.x < -2 or bounds_outer.x > DRAGGING_RECT_WIDTH + 2 \
- or bounds_inner.y < -1 or bounds_outer.y > DRAGGING_RECT_HEIGHT:
- return False
- return True
-
def move(self, pos):
self.queue_draw()
diff --git a/symbolobject.py b/symbolobject.py index 15005e7..9040f2c 100644 --- a/symbolobject.py +++ b/symbolobject.py @@ -19,13 +19,6 @@ from vector import Vector import gtk
-# The global grid unit size, in pixels. Objects will snap to multiples of this value.
-GRID_SIZE = 50
-
-# Width and height of region within which we can drag objects.
-DRAGGING_RECT_WIDTH = 24*GRID_SIZE
-DRAGGING_RECT_HEIGHT = 16*GRID_SIZE
-
class SymbolObject(DraggableObject):
"""Draggable symbol object."""
@@ -95,14 +88,6 @@ class SymbolObject(DraggableObject): self.width = width
self.height = height
-
- def is_in_container(self):
- bounds_inner, bounds_outer = self.get_bounds()
-
- if bounds_inner.x < -2 or bounds_outer.x > DRAGGING_RECT_WIDTH + 2 \
- or bounds_inner.y < -1 or bounds_outer.y > DRAGGING_RECT_HEIGHT:
- return False
- return True
def move(self, pos):
self.queue_draw()
diff --git a/threedobject.py b/threedobject.py index 7fb32fc..eebc348 100644 --- a/threedobject.py +++ b/threedobject.py @@ -19,13 +19,6 @@ from movableobject import MovableObject import gtk, math
-# The global grid unit size, in pixels. Objects will snap to multiples of this value.
-GRID_SIZE = 50
-
-# Width and height of region within which we can drag objects.
-DRAGGING_RECT_WIDTH = 24*GRID_SIZE
-DRAGGING_RECT_HEIGHT = 16*GRID_SIZE
-
class ThreeDObject(MovableObject):
"""Quasi three-dimensional object."""
@@ -46,14 +39,6 @@ class ThreeDObject(MovableObject): self.symbol_visible = True
self.rotatable = False
- def is_in_container(self):
- bounds_inner, bounds_outer = self.get_bounds()
-
- if bounds_inner.x < -2 or bounds_outer.x > DRAGGING_RECT_WIDTH + 2 \
- or bounds_inner.y < -1 or bounds_outer.y > DRAGGING_RECT_HEIGHT:
- return False
- return True
-
def draw_poly(self, cr, points):
# Generate the shape.
cr.move_to(points[0].x, points[0].y)
diff --git a/volumeobject.py b/volumeobject.py index c9b0fe0..de0c8f3 100644 --- a/volumeobject.py +++ b/volumeobject.py @@ -19,13 +19,6 @@ from movableobject import MovableObject import gtk, math
-# The global grid unit size, in pixels. Objects will snap to multiples of this value.
-GRID_SIZE = 50
-
-# Width and height of region within which we can drag objects.
-DRAGGING_RECT_WIDTH = 24*GRID_SIZE
-DRAGGING_RECT_HEIGHT = 16*GRID_SIZE
-
class VolumeObject(MovableObject):
"""Quasi three-dimensional container object."""
@@ -147,14 +140,6 @@ class VolumeObject(MovableObject): else:
return -pow(abs(x), 1.0/3.0)
- def is_in_container(self):
- bounds_inner, bounds_outer = self.get_bounds()
-
- if bounds_inner.x < -2 or bounds_outer.x > DRAGGING_RECT_WIDTH + 2 \
- or bounds_inner.y < -1 or bounds_outer.y > DRAGGING_RECT_HEIGHT:
- return False
- return True
-
#def draw_ellipse(self, cr, x, y, width, height):
# cr.save()
# cr.translate (x + width / 2., y + height / 2.)
diff --git a/volumeproblem.py b/volumeproblem.py index 178c602..9238e4f 100644 --- a/volumeproblem.py +++ b/volumeproblem.py @@ -140,6 +140,8 @@ class VolumeProblem(Problem): ( math.pi * float(lower_radius * lower_radius + lower_radius * upper_radius + upper_radius * upper_radius)) def show_problem(self): + self.container.configure_dragging_area(25, 48, 32, 2 * math.pi) + self.container.add_object(self.shape1) self.container.add_object(self.shape2) |