~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/graphics/styles/drawable.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# drawable.rb: style objects pertaining to drawable objects.
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'ctioga2/utils'
 
15
require 'ctioga2/log'
 
16
 
 
17
# This module contains all the classes used by ctioga
 
18
module CTioga2
 
19
 
 
20
  Version::register_svn_info('$Revision: 158 $', '$Date: 2010-07-23 15:03:47 +0200 (Fri, 23 Jul 2010) $')
 
21
 
 
22
  module Graphics
 
23
 
 
24
    # All the styles
 
25
    module Styles
 
26
 
 
27
      # This class represents all the stylistic information to stroke
 
28
      # a Tioga path.
 
29
      class StrokeStyle < BasicStyle
 
30
        # The color
 
31
        attr_accessor :color
 
32
        
 
33
        # The line style
 
34
        attr_accessor :style
 
35
 
 
36
        # The line width
 
37
        attr_accessor :width
 
38
 
 
39
        # Sets the stroke style to a FigureMaker object, _t_.
 
40
        def set_stroke_style(t)
 
41
          t.stroke_color = @color if @color
 
42
          t.line_type = @style if @style
 
43
          t.line_width = @width if @width
 
44
        end
 
45
      end
 
46
 
 
47
      # This class represents all the stylistic information to draw a
 
48
      # Marker.
 
49
      #
 
50
      # \todo many things are still missing here...
 
51
      # 
 
52
      # * in particular, angles could be handled here, and they could
 
53
      #   be handled directly in the marker specification...
 
54
      class MarkerStyle < BasicStyle
 
55
 
 
56
        # The color
 
57
        attr_accessor :color
 
58
        
 
59
        # The marker
 
60
        attr_accessor :marker
 
61
 
 
62
        # The marker scale
 
63
        attr_accessor :scale
 
64
 
 
65
        # Shows the marker at a given location/set of locations.
 
66
        # 
 
67
        # \p override is a hash that can override part of the
 
68
        # dictionnary specification.
 
69
        def draw_markers_at(t, x, y, override = nil)
 
70
          t.context do
 
71
            ## \todo allow custom line types for markers ?
 
72
            t.line_type = LineStyles::Solid
 
73
            dict = { 
 
74
              'marker' => @marker, 
 
75
              'color' => @color
 
76
            }
 
77
            if x.is_a? Numeric
 
78
              dict['x'] = x
 
79
              dict['y'] = y
 
80
            else
 
81
              dict['Xs'] = x
 
82
              dict['Ys'] = y
 
83
            end
 
84
 
 
85
            if @scale
 
86
              dict['scale'] = @scale
 
87
            end
 
88
            if override
 
89
              dict.merge!(override)
 
90
            end
 
91
            t.show_marker(dict)
 
92
          end
 
93
        end
 
94
      end
 
95
 
 
96
      # A style that handles drawing a fill.
 
97
      #
 
98
      # \todo add ways to specify complex fills, such as patterned
 
99
      # fills and so on. Those would use clipping the path and base
 
100
      # themselves on the coordinates of the current frame.
 
101
      #
 
102
      # \todo more attributes ?
 
103
      class FillStyle < BasicStyle
 
104
 
 
105
        # The color.
 
106
        attr_accessor :color
 
107
 
 
108
        # The transparency
 
109
        attr_accessor :transparency
 
110
 
 
111
        # Sets up the parameters for the fill. Must be called before
 
112
        # any path drawing.
 
113
        #
 
114
        # \warning You *must* call FillStyle#do_fill for
 
115
        # filling. Directly calling FigureMaker#fill is not a good
 
116
        # idea, as you lose all 'hand-crafted' fills !
 
117
        def setup_fill(t)
 
118
          t.fill_color = @color if @color
 
119
          t.fill_transparency = @transparency if @transparency
 
120
        end
 
121
 
 
122
        # Does the actual filling step. Must be used within a context,
 
123
        # as it quite messes up with many things. Must be called after
 
124
        # a call to #setup_fill.
 
125
        def do_fill(t)
 
126
          t.fill
 
127
        end
 
128
 
 
129
      end
 
130
 
 
131
      # Same as FillStyle, but with additional parameters that handle
 
132
      # how the fill should be applied to curves.
 
133
      class CurveFillStyle < FillStyle
 
134
 
 
135
        # At which Y value we should draw the horizontal line for the
 
136
        # fill ? A float, or:
 
137
        # * :top, :bottom
 
138
        # * false, nil to disable filling altogether
 
139
        attr_accessor :y0
 
140
        
 
141
      end
 
142
 
 
143
    end
 
144
  end
 
145
end
 
146