# Copyright 2008 by Peter Moxhay and Wade Brainerd. # This file is part of Math. # # Math is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Math is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Math. If not, see . from objectarea import Object from vector import Vector from movableobject import MovableObject import gtk, math, rsvg MOON_SVG = rsvg.Handle('moon.svg') OVAL_SVG = rsvg.Handle('oval.svg') class AmountObject(MovableObject): """Draggable discrete shape.""" def __init__(self, pos, color, shape_name, container): MovableObject.__init__(self) self.pos = pos self.selectable = True self.rotatable = False self.color = color self.shape = shape_name self.container = container self.amount_scale = 1.0 def draw(self, cr): cr.scale(self.amount_scale, self.amount_scale) if self.shape == 'circle': self.draw_circle(cr, self.color) elif self.shape == 'square': self.draw_square(cr, self.color) else: self.draw_triangle(cr, self.color) def get_bounds(self): return (self.pos + Vector(-50, -50)).scaled(self.amount_scale), (self.pos + Vector(50, 50)).scaled(self.amount_scale) def draw_shape(self, cr): # Draw the fill. if self.selected: cr.set_source_rgb(self.color[0]*1.6, self.color[1]*1.6, self.color[2]*1.6) else: cr.set_source_rgb(self.color[0], self.color[1], self.color[2]) cr.fill_preserve() # Draw the outline. cr.set_source_rgb(self.color[0]*0.75, self.color[1]*0.75, self.color[2]*0.75) if self.selected: cr.set_dash((10, 10), 0) cr.set_line_width(4.0) cr.stroke() def draw_circle(self, cr, color): cr.arc(self.pos.x, self.pos.y, 35, 0.0, 2.0 * math.pi) self.draw_shape(cr) def draw_square(self, cr, color): square_side = 65 cr.move_to(self.pos.x - square_side/2, self.pos.y - square_side/2) cr.line_to(self.pos.x + square_side/2, self.pos.y - square_side/2) cr.line_to(self.pos.x + square_side/2, self.pos.y + square_side/2) cr.line_to(self.pos.x - square_side/2, self.pos.y + square_side/2) cr.close_path() self.draw_shape(cr) def draw_triangle(self, cr, color): triangle_width = 80 triangle_height = triangle_width * math.sqrt(3)/2 cr.move_to(self.pos.x, self.pos.y - triangle_height/2) cr.line_to(self.pos.x + triangle_width/2, self.pos.y + triangle_height/2) cr.line_to(self.pos.x - triangle_width/2, self.pos.y + triangle_height/2) cr.close_path() self.draw_shape(cr)