Added template control over line colors (and presence).
authorW. Trevor King <wking@drexel.edu>
Sun, 28 Mar 2010 00:58:19 +0000 (20:58 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 28 Mar 2010 00:58:19 +0000 (20:58 -0400)
pyrisk/graphics.py
share/templates/earth/continent.col [moved from share/templates/earth/colors.ccl with 100% similarity]
share/templates/earth/line.col [new file with mode: 0644]

index c407c29..6461834 100644 (file)
@@ -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([
                     '<!-- %s -->' % r,
-                    '<polygon fill="%s" stroke="%s" stroke-width="%d"'
-                    % (c_col, self.line_color, self.line_width),
+                    '<polygon fill="%s" %s' % (c_col, b_col_attr),
                     '         points="%s" />'
                     % ' '.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([
                         '<polyline stroke="%s" stroke-width="%d"'
-                        % (self.route_color, self.line_width),
+                        % (color, self.line_width),
                         '         points="%s" />'
                         % ' '.join(['%d,%d' % ((region_pos[id(r)]+rt_start+p)
                                            *(1,-1) # svg y value increases down
diff --git a/share/templates/earth/line.col b/share/templates/earth/line.col
new file mode 100644 (file)
index 0000000..0db4841
--- /dev/null
@@ -0,0 +1,3 @@
+border grey
+route  black
+virtual route  -