1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# 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 <http://www.gnu.org/licenses/>.
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)
|