From: W. Trevor King Date: Sun, 28 Mar 2010 00:58:19 +0000 (-0400) Subject: Added template control over line colors (and presence). X-Git-Tag: 0.1~10 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=a14da3e235e4d997cd9bded84ed1b841ca02a607;p=pyrisk.git Added template control over line colors (and presence). --- diff --git a/pyrisk/graphics.py b/pyrisk/graphics.py index c407c29..6461834 100644 --- a/pyrisk/graphics.py +++ b/pyrisk/graphics.py @@ -30,10 +30,12 @@ from .base import NameMixin, ID_CmpMixin class Template (NameMixin): """Setup regions for a particular world. """ - def __init__(self, name, regions, continent_colors={}): + def __init__(self, name, regions, continent_colors={}, + line_colors={}): NameMixin.__init__(self, name) self.regions = regions self.continent_colors = continent_colors + self.line_colors = line_colors class TemplateLibrary (object): """Create Templates on demand from a directory of template data. @@ -43,10 +45,10 @@ class TemplateLibrary (object): def __init__(self, template_dir='share/templates/'): self.template_dir = os.path.abspath(os.path.expanduser(template_dir)) def get(self, name): - region_pointlists,route_pointlists,continent_colors = \ + region_pointlists,route_pointlists,continent_colors,line_colors = \ self._get_data(name) regions = self._generate_regions(region_pointlists, route_pointlists) - return Template('template', regions, continent_colors) + return Template('template', regions, continent_colors, line_colors) def _get_data(self, name): dirname = os.path.join(self.template_dir, name.lower()) try: @@ -63,9 +65,15 @@ class TemplateLibrary (object): elif extension in ['rt', 'vrt']: route_pointlists[name] = (self._read_pointlist(path), extension == 'vrt') - elif extension == 'ccl': - continent_colors = self._read_colors(path) - return (region_pointlists, route_pointlists, continent_colors) + elif extension == 'col': + c = self._read_colors(path) + if name == 'continent': + continent_colors = c + else: + assert name == 'line', name + line_colors = c + return (region_pointlists, route_pointlists, + continent_colors, line_colors) def _read_pointlist(self, filename): pointlist = [] for line in open(filename, 'r'): @@ -89,7 +97,9 @@ class TemplateLibrary (object): if len(line) == 0: continue fields = line.split('\t') - name,color = fields + name,color = [x.strip() for x in fields] + if color == '-': + color = None colors[name] = color return colors def _generate_regions(self, region_pointlists, route_pointlists): @@ -344,8 +354,6 @@ class WorldRenderer (object): self.template_lib = TEMPLATE_LIBRARY self.buf = buf self.line_width = line_width - self.line_color = 'grey' - self.route_color = 'black' self.dpcm = dpcm def render(self, world): template = self.template_lib.get(world.name) @@ -368,10 +376,14 @@ class WorldRenderer (object): for r in template.regions: t = self._matching_territory(world, r) c_col = template.continent_colors[t.continent.name] + if template.line_colors['border'] == None: + b_col_attr = '' + else: + b_col_attr = 'stroke="%s" stroke-width="%d"' \ + % (template.line_colors['border'], self.line_width) lines.extend([ '' % r, - '' % ' '.join(['%d,%d' % ((region_pos[id(r)]+p) *(1,-1) # svg y value increases down @@ -379,12 +391,18 @@ class WorldRenderer (object): for p in r.outline[:-1]]) ]) for rt,rt_start in zip(r.routes, r.route_starts): - if id(rt) in drawn_rts or rt.virtual == True: + if id(rt) in drawn_rts: continue drawn_rts[id(rt)] = rt + if rt.virtual == True: + color = template.line_colors['virtual route'] + else: + color = template.line_colors['route'] + if color == None: + continue lines.extend([ '' % ' '.join(['%d,%d' % ((region_pos[id(r)]+rt_start+p) *(1,-1) # svg y value increases down diff --git a/share/templates/earth/colors.ccl b/share/templates/earth/continent.col similarity index 100% rename from share/templates/earth/colors.ccl rename to share/templates/earth/continent.col diff --git a/share/templates/earth/line.col b/share/templates/earth/line.col new file mode 100644 index 0000000..0db4841 --- /dev/null +++ b/share/templates/earth/line.col @@ -0,0 +1,3 @@ +border grey +route black +virtual route -