diff options
author | Wade Brainerd <wadetb@gmail.com> | 2009-04-06 11:53:32 (GMT) |
---|---|---|
committer | Wade Brainerd <wadetb@gmail.com> | 2009-04-06 11:53:32 (GMT) |
commit | 8791987ca75da157d176205c47d7f4a8abc25b03 (patch) | |
tree | 422785f3a85a0490b830f7538e876e7ba94d7bb2 | |
parent | d04ac9a774e8d91b3fa7e55e8e4afe78eb71e202 (diff) |
Factor move function into MovableObject, remove duplication.
Also simplify some of the SVG based objects.
-rw-r--r-- | amountobject.py | 13 | ||||
-rw-r--r-- | balanceobject.py | 30 | ||||
-rw-r--r-- | compare3lesson.py | 2 | ||||
-rw-r--r-- | cuttingproblem.py | 4 | ||||
-rw-r--r-- | draggableobject.py | 14 | ||||
-rw-r--r-- | faucetobject.py | 126 | ||||
-rw-r--r-- | groupobject.py | 14 | ||||
-rw-r--r-- | linesegmentmovableobject.py | 15 | ||||
-rw-r--r-- | linesegmentobject.py | 13 | ||||
-rw-r--r-- | movableobject.py | 34 | ||||
-rw-r--r-- | scissorsobject.py | 19 | ||||
-rw-r--r-- | shapeobject.py | 30 | ||||
-rw-r--r-- | symbolmovableobject.py | 13 | ||||
-rw-r--r-- | symbolobject.py | 13 | ||||
-rw-r--r-- | volumeproblem.py | 4 |
15 files changed, 67 insertions, 277 deletions
diff --git a/amountobject.py b/amountobject.py index 330ac9a..dfe8f0c 100644 --- a/amountobject.py +++ b/amountobject.py @@ -47,19 +47,6 @@ class AmountObject(MovableObject): else: self.draw_triangle(cr, self.color) - def move(self, pos): - self.queue_draw() - - # Tentatively place in the object in the new position - last_pos = self.pos - self.pos = pos - - # If any point is out of bounds, move object back to last position. - if not self.is_in_container(): - self.pos = last_pos - - self.queue_draw() - def get_bounds(self): return self.pos + Vector(-50, -50), self.pos + Vector(50, 50) diff --git a/balanceobject.py b/balanceobject.py index a85efa1..4d2bb1e 100644 --- a/balanceobject.py +++ b/balanceobject.py @@ -14,7 +14,6 @@ # You should have received a copy of the GNU General Public License # along with Math. If not, see <http://www.gnu.org/licenses/>. from objectarea import Object -from draggableobject import DraggableObject from vector import Vector import gtk, math, rsvg @@ -27,9 +26,6 @@ MIDDLE_SVG = rsvg.Handle('balance_middle.svg') LEFT_SVG = rsvg.Handle('balance_left.svg') RIGHT_SVG = rsvg.Handle('balance_right.svg') -from movableobject import MovableObject - - # Class containing various standard colors. Each color is a 3 element tuple. class Color: BLUE = (0.25, 0.25, 0.75) @@ -37,11 +33,11 @@ class Color: RED = (0.75, 0.25, 0.25) -class BalanceObject(MovableObject): +class BalanceObject(Object): """Balance scale for comparing masses.""" def __init__(self, pos, container): - MovableObject.__init__(self) + Object.__init__(self) self.pos = pos self.size = Vector (200, 200) @@ -97,28 +93,6 @@ class BalanceObject(MovableObject): self.queue_draw() - def move(self, pos): - #print "BalanceObject: move called: self.container = ", self.container - #print "BalanceObject: move called: pos = ", pos - - self.container.queue_draw() - - # Tentatively place in the object in the new position - last_pos = self.pos - self.pos = pos - - ## If any point is out of bounds, move object back to last position. - #if not self.is_in_container(): - # self.pos = last_pos - # - self.container.queue_draw() - - #def get_bounds(self): - #if self.shape1_in_pair: - # return self.pos, self.pos + Vector(200, 200) - #else: - # return self.pos, self.pos + Vector(100, 200) - def draw_circle(self, cr, pos, color): cr.save() # Draw the fill. diff --git a/compare3lesson.py b/compare3lesson.py index 552ffd4..2d1bbfa 100644 --- a/compare3lesson.py +++ b/compare3lesson.py @@ -85,7 +85,7 @@ class Compare3Lesson(ObjectArea): self.problem_type = random.choice(PROBLEM_TYPES) # Uncomment this to choose a particular problem type. - #self.problem_type = 'amount' + #self.problem_type = 'volume' if self.problem_type == 'length': self.problem = LengthProblem(self) diff --git a/cuttingproblem.py b/cuttingproblem.py index 524e361..8e8b06a 100644 --- a/cuttingproblem.py +++ b/cuttingproblem.py @@ -132,10 +132,8 @@ class CuttingProblem(Problem): self.shape1 = ShapeObject(color1, letter1, object1, original_position1, original_angle1, Vector(0, 0), self) self.shape2 = ShapeObject(color2, letter2, object2, original_position2, original_angle2, Vector(0, 0), self) - self.scissors_object = ScissorsObject(Vector(750, 100), self.container) + self.scissors_object = ScissorsObject(Vector(750, 100)) self.container.add_object(self.scissors_object) - - return def show_problem(self): self.container.configure_dragging_area(50, 24, 16, math.pi / 4) diff --git a/draggableobject.py b/draggableobject.py index 0c23b6a..7ccf12d 100644 --- a/draggableobject.py +++ b/draggableobject.py @@ -218,3 +218,17 @@ class DraggableObject(Object): mn.y < -1 or mx.y > self.container.DRAGGING_RECT_HEIGHT: return False return True + + def move(self, pos): + self.queue_draw() + + # Tentatively place in the object in the new position + last_pos = self.pos + self.pos = pos + + # If any point is out of bounds, move object back to last position. + if not self.is_in_container(): + self.pos = last_pos + + self.queue_draw() + diff --git a/faucetobject.py b/faucetobject.py index bf22ea1..8abf39a 100644 --- a/faucetobject.py +++ b/faucetobject.py @@ -14,133 +14,23 @@ # You should have received a copy of the GNU General Public License # along with Math. If not, see <http://www.gnu.org/licenses/>. from objectarea import Object -from draggableobject import DraggableObject from vector import Vector import gtk, math, rsvg FAUCET_SVG = rsvg.Handle('faucet.svg') -from movableobject import MovableObject - - -# Class containing various standard colors. Each color is a 3 element tuple. -class Color: - BLUE = (0.25, 0.25, 0.75) - GREEN = (0.25, 0.75, 0.25) - RED = (0.75, 0.25, 0.25) - - -class FaucetObject(MovableObject): +class FaucetObject(Object): """Faucet for filling volumes with water.""" - def __init__(self, pos, container): - MovableObject.__init__(self) - - self.pos = pos - self.size = Vector(200, 200) - - self.selectable = False - self.rotatable = False - self.draggable = False - - self.container = container - - self.amount_scale = 0.33333 - - #self.state = 'middle' - # - #self.left_pan_full = False - #self.right_pan_full = False - # - #self.pan1_position = Vector(200, 500) - #self.pan2_position = Vector(725, 500) + def __init__(self, pos): + Object.__init__(self) - def draw(self, cr): - cr.scale(self.amount_scale, self.amount_scale) - - self.draw_faucet(cr, Vector (0, 300)) - - #if self.state == 'left': - # self.draw_pan(cr, Vector (20, 255 + 68)) - # self.draw_pan(cr, Vector (539, 255 - 72)) - #elif self.state == 'right': - # self.draw_pan(cr, Vector (20, 255 - 72)) - # self.draw_pan(cr, Vector (539, 255 + 68)) - #else: - # self.draw_pan(cr, Vector (20, 255)) - # self.draw_pan(cr, Vector (539, 255)) - # - #self.draw_arm(cr, Vector (177, 210)) - - #def adjust_balance_state(self): - # print "BalanceObject: adjust_balance_state called, self.state =", self.state - # if self.state == 'left': - # self.pan1_position = Vector(200, 500 + 68) - # self.pan2_position = Vector(725, 500 - 72) - # elif self.state == 'right': - # self.pan1_position = Vector(200, 500 - 72) - # self.pan2_position = Vector(725, 500 + 68) - # else: - # self.pan1_position = Vector(200, 500) - # self.pan2_position = Vector(725, 500) - # - #def move(self, pos): - # #print "GroupObject: move called: self.container = ", self.container - # self.queue_draw() - # - # # Tentatively place in the object in the new position - # last_pos = self.pos - # self.pos = pos - # - # # If any point is out of bounds, move object back to last position. - # if not self.is_in_container(): - # self.pos = last_pos - # - # self.queue_draw() - # - #def get_bounds(self): - # if self.shape1_in_pair: - # return self.pos, self.pos + Vector(200, 200) - # else: - # return self.pos, self.pos + Vector(100, 200) - # - #def draw_circle(self, cr, pos, color): - # cr.save() - # # Draw the fill. - # #cr.set_source_rgb(color[0], color[1], color[2]) - # - # if self.selected: - # cr.set_source_rgb(color[0]*1.6, color[1]*1.6, color[2]*1.6) - # else: - # cr.set_source_rgb(color[0], color[1], color[2]) - # - # cr.arc(self.pos.x + pos.x, self.pos.y + pos.y, 35, 0.0, 2.0 * math.pi) - # cr.fill() - # - # # Draw the outline. - # #cr.set_source_rgb(color[0]*0.75, color[1]*0.75, color[2]*0.75) - # - # if self.selected: - # cr.set_dash((10, 10), 0) - # cr.set_source_rgb(color[0]*0.75, color[1]*0.75, color[2]*0.75) - # - # cr.set_line_width(4.0) - # cr.arc(self.pos.x + pos.x, self.pos.y + pos.y, 35, 0.0, 2.0 * math.pi) - # cr.stroke() - # cr.restore() - - + self.pos = pos - def draw_faucet(self, cr, pos): - cr.save() - - cr.translate(pos.x, pos.y) - - cr.scale(3.0, 3.0) + def draw(self, cr): + cr.translate(self.pos.x, self.pos.y) FAUCET_SVG.render_cairo(cr) - cr.restore() - - - + def get_bounds(self): + return self.pos, self.pos + Vector(FAUCET_SVG.props.width, FAUCET_SVG.props.height) diff --git a/groupobject.py b/groupobject.py index 4f4c31e..80fa471 100644 --- a/groupobject.py +++ b/groupobject.py @@ -86,20 +86,6 @@ class GroupObject(MovableObject): else: self.draw_triangle(cr, Vector(9 + 25 + 100, 83), self.color1) - def move(self, pos): - #print "GroupObject: move called: self.container = ", self.container - self.queue_draw() - - # Tentatively place in the object in the new position - last_pos = self.pos - self.pos = pos - - # If any point is out of bounds, move object back to last position. - if not self.is_in_container(): - self.pos = last_pos - - self.queue_draw() - def get_bounds(self): if self.shape1_in_pair: return self.pos + Vector(-30, 0), self.pos + Vector(200, 200) diff --git a/linesegmentmovableobject.py b/linesegmentmovableobject.py index f8df24b..30fbd10 100644 --- a/linesegmentmovableobject.py +++ b/linesegmentmovableobject.py @@ -67,20 +67,7 @@ class LineSegmentMovableObject(MovableObject): cr.set_source_rgb(0, 0, 0) cr.stroke() - - def move(self, pos): - self.queue_draw() - - # Tentatively place in the object in the new position - last_pos = self.pos - self.pos = pos - - # If any point is out of bounds, move object back to last position. - if not self.is_in_container(): - self.pos = last_pos - - self.queue_draw() - + def get_bounds(self): mn = Vector(self.pos.x - 32, self.pos.y - 12 + self.length - self.line_segment_length) mx = mn + Vector(64, self.line_segment_length + 24) diff --git a/linesegmentobject.py b/linesegmentobject.py index eec3654..71f17ba 100644 --- a/linesegmentobject.py +++ b/linesegmentobject.py @@ -75,19 +75,6 @@ class LineSegmentObject(DraggableObject): cr.set_source_rgb(0, 0, 0)
cr.stroke()
- def move(self, pos):
- self.queue_draw()
-
- # Tentatively place in the object in the new position
- last_pos = self.pos
- self.pos = pos
-
- # If any point is out of bounds, move object back to last position.
- if not self.is_in_container():
- self.pos = last_pos
-
- self.queue_draw()
-
def get_bounds(self):
mn = Vector(self.pos.x - 30, self.pos.y - 10 + self.length - self.line_segment_length)
mx = mn + Vector(60, self.line_segment_length + 20)
diff --git a/movableobject.py b/movableobject.py index 76bba15..2dfa741 100644 --- a/movableobject.py +++ b/movableobject.py @@ -252,3 +252,37 @@ class MovableObject(Object): mn.y < -1 or mx.y > self.container.DRAGGING_RECT_HEIGHT: return False return True + + # Default implementation which may be overridden by derived objects. + def calculate_bounds(self): + pass + + def move(self, pos): + self.queue_draw() + + # Tentatively place in the object in the new position + last_pos = self.pos + self.pos = pos + + # If any point is out of bounds, move object back to last position. + if not self.is_in_container(): + self.pos = last_pos + + self.calculate_bounds() + + self.queue_draw() + + def rotate(self, angle): + self.queue_draw() + + # Tentatively rotate the object to the new angle + last_angle = self.angle + self.angle = angle + + # If any point is out of bounds, move object back to last angle. + if not self.is_in_container(): + self.angle = last_angle + + self.calculate_bounds() + + self.queue_draw() diff --git a/scissorsobject.py b/scissorsobject.py index 08bde9e..9c8ba1a 100644 --- a/scissorsobject.py +++ b/scissorsobject.py @@ -14,29 +14,20 @@ # You should have received a copy of the GNU General Public License # along with Math. If not, see <http://www.gnu.org/licenses/>. from objectarea import Object -from draggableobject import DraggableObject from vector import Vector import gtk, math, rsvg SCISSORS_SVG = rsvg.Handle('scissors.svg') -from movableobject import MovableObject - -class ScissorsObject(MovableObject): +class ScissorsObject(Object): """Scissors for cutting areas.""" - def __init__(self, pos, container): - MovableObject.__init__(self) - - self.pos = pos - - self.selectable = False - self.rotatable = False - self.draggable = False - - self.container = container + def __init__(self, pos): + Object.__init__(self) + self.pos = pos + def draw(self, cr): cr.translate(self.pos.x, self.pos.y) cr.scale(0.75, 0.75) diff --git a/shapeobject.py b/shapeobject.py index c3c3c7d..8218ac9 100644 --- a/shapeobject.py +++ b/shapeobject.py @@ -218,33 +218,3 @@ class ShapeObject(MovableObject): return False
return True
- def move(self, pos):
- self.queue_draw()
-
- # Tentatively place in the object in the new position
- last_pos = self.pos
- self.pos = pos
-
- # If any point is out of bounds, move object back to last position.
- if not self.is_in_container():
- self.pos = last_pos
-
- self.calculate_bounds()
-
- self.queue_draw()
-
- def rotate(self, angle):
- self.queue_draw()
-
- # Tentatively rotate the object to the new angle
- last_angle = self.angle
- self.angle = angle
-
- # If any point is out of bounds, move object back to last angle.
- if not self.is_in_container():
- self.angle = last_angle
-
- self.calculate_bounds()
-
- self.queue_draw()
-
diff --git a/symbolmovableobject.py b/symbolmovableobject.py index da9d0f2..819f992 100644 --- a/symbolmovableobject.py +++ b/symbolmovableobject.py @@ -85,19 +85,6 @@ class SymbolMovableObject(MovableObject): self.width = width
self.height = height
-
- def move(self, pos):
- self.queue_draw()
-
- # Tentatively place in the object in the new position
- last_pos = self.pos
- self.pos = pos
-
- # If any point is out of bounds, move object back to last position.
- if not self.is_in_container():
- self.pos = last_pos
-
- self.queue_draw()
def get_bounds(self):
hsz = Vector(self.width/2 + 5, self.height/2 + 5)
diff --git a/symbolobject.py b/symbolobject.py index 9040f2c..2b6d49e 100644 --- a/symbolobject.py +++ b/symbolobject.py @@ -89,19 +89,6 @@ class SymbolObject(DraggableObject): self.width = width
self.height = height
- def move(self, pos):
- self.queue_draw()
-
- # Tentatively place in the object in the new position
- last_pos = self.pos
- self.pos = pos
-
- # If any point is out of bounds, move object back to last position.
- if not self.is_in_container():
- self.pos = last_pos
-
- self.queue_draw()
-
def get_bounds(self):
hsz = Vector(self.width/2 + 5, self.height/2 + 5)
return self.pos - hsz, self.pos + hsz
diff --git a/volumeproblem.py b/volumeproblem.py index 9238e4f..3c031d4 100644 --- a/volumeproblem.py +++ b/volumeproblem.py @@ -126,10 +126,8 @@ class VolumeProblem(Problem): self.shape1 = VolumeObject(color1, letter2, original_position1, self.height1, self.lower_radius1, self.upper_radius1) self.shape2 = VolumeObject(color2, letter1, original_position2, self.height2, self.lower_radius2, self.upper_radius2) - self.faucet_object = FaucetObject(Vector(100, 100), self.container) + self.faucet_object = FaucetObject(Vector(0, 100)) self.container.add_object(self.faucet_object) - - return def calculate_volume(self, height, lower_radius, upper_radius): return (math.pi * height / 3.0) * \ |