Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ink.py
blob: 544bc78cec8a2470821b7c622a693145ba36c9c4 (plain)
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
# ink.py
#
# B. Mayton <bmayton@cs.washington.edu>
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# -*- mode:python; tab-width:4; indent-tabs-mode:t;  -*-

# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import random
import logging

class Path:
	
	def __init__(self, inkstr=None):
		self.__logger = logging.getLogger('Path')
		self.points=[]
		self.color = (0,0,1.0)
		self.pen = 4
		self.uid = random.randint(0, 2147483647)
		if inkstr:
			try:
				i=0
				parts = inkstr.split('#')
				if len(parts) > 1:
					params = parts[i].split(';')
					self.uid = int(params[0])
					colorparts = params[1].split(',')
					self.color = (float(colorparts[0]),float(colorparts[1]),float(colorparts[2]))
					self.pen = float(params[2])
					i = i + 1
				pathstr = parts[i]
				pointstrs = pathstr.split(';')
				for pointstr in pointstrs:
					pparts = pointstr.split(',')
					if len(pparts) == 2:
						self.add((int(pparts[0]), int(pparts[1])))
			except Exception, e:
				self.__logger.debug('Could not unserialize ink string (old ink?)')
	
	def add(self, point):
		self.points.append(point)
	
	def __str__(self):
		s = str(self.uid) + ";"
		s = s +  str(self.color[0]) + "," + str(self.color[1]) + "," + str(self.color[2]) + ";"
		s = s + str(self.pen) + "#"
		for p in self.points:
			s = s + str(int(p[0])) + "," + str(int(p[1])) + ";"
		return s