Class: CTioga2::Graphics::Types::AlignedPoint
- Defined in:
- lib/ctioga2/graphics/types/point.rb
Overview
A Point, but with alignment facilities.
Constant Summary collapse
- AlignmentSpecification =
{ 'r' => :right, 'c' => :center, 'l' => :left, 't' => :top, 'b' => :bottom }
Instance Attribute Summary collapse
-
#halign ⇒ Object
Horizontal alignment (:left, :center, :right).
-
#valign ⇒ Object
Vertical alignement (:top, :center, :bottom).
Attributes inherited from Point
Class Method Summary collapse
-
.from_text(text, default = :figure) ⇒ Object
Creates an AlignedPoint object from a text specification.
Instance Method Summary collapse
-
#initialize(x = nil, y = nil, type = :figure, halign = :center, valign = :center) ⇒ AlignedPoint
constructor
Creates a AlignedPoint.
-
#to_frame_coordinates(t, width, height) ⇒ Object
Returns frame coordinates corresponding to the point, the alignment and the given size in figure coordinates.
-
#to_frame_margins(t, width, height) ⇒ Object
Returns a frame_margin corresponding to the point, the alignment and the given size in figure coordinates.
Methods inherited from Point
Constructor Details
#initialize(x = nil, y = nil, type = :figure, halign = :center, valign = :center) ⇒ AlignedPoint
Creates a AlignedPoint
125 126 127 128 129 130 |
# File 'lib/ctioga2/graphics/types/point.rb', line 125 def initialize(x = nil, y = nil, type = :figure, halign = :center, valign = :center) super(x,y,type) @halign = halign @valign = valign end |
Instance Attribute Details
#halign ⇒ Object
Horizontal alignment (:left, :center, :right)
122 123 124 |
# File 'lib/ctioga2/graphics/types/point.rb', line 122 def halign @halign end |
#valign ⇒ Object
Vertical alignement (:top, :center, :bottom)
119 120 121 |
# File 'lib/ctioga2/graphics/types/point.rb', line 119 def valign @valign end |
Class Method Details
.from_text(text, default = :figure) ⇒ Object
Creates an AlignedPoint object from a text specification. Splits up the text at a comma and
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/ctioga2/graphics/types/point.rb', line 189 def self.from_text(text, default = :figure) if not text =~ /^\s*([btlrc]{2})(?::([^,]+),\s*(.*))?\s*$/ raise "Invalid format for aligned point: #{text}" end specs = $1 specs = specs.chars.map {|x| AlignmentSpecification.fetch(x.downcase) } coord = AlignedPoint.new if specs[0] == :center specs.reverse! end case specs[0] when :center coord.halign = :center coord.valign = :center when :left, :right coord.halign = specs[0] coord.valign = specs[1] when :top, :bottom coord.valign = specs[0] coord.halign = specs[1] end if $2 x_spec,y_spec = $2,$3 coord.x = BaseCoordinate.from_text(x_spec, :x, default) coord.y = BaseCoordinate.from_text(y_spec, :y, default) else case coord.halign when :center coord.x = BaseCoordinate.new(:frame, 0.5, :x) when :left coord.x = BaseCoordinate.new(:frame, 0.05, :x) when :right coord.x = BaseCoordinate.new(:frame, 0.95, :x) end case coord.valign when :center coord.y = BaseCoordinate.new(:frame, 0.5, :y) when :bottom coord.y = BaseCoordinate.new(:frame, 0.05, :y) when :top coord.y = BaseCoordinate.new(:frame, 0.95, :y) end end return coord end |
Instance Method Details
#to_frame_coordinates(t, width, height) ⇒ Object
Returns frame coordinates corresponding to the point, the alignment and the given size in figure coordinates
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ctioga2/graphics/types/point.rb', line 134 def to_frame_coordinates(t, width, height) dx = t.convert_figure_to_frame_dx(width).abs dy = t.convert_figure_to_frame_dy(height).abs x,y = self.to_frame_xy(t) case @valign when :top yt = y yb = y - dy when :bottom yt = y + dy yb = y when :center yt = y + dy/2 yb = y - dy/2 else raise "Unknown vertical alignment: #{@valign}" end case @halign when :right xl = x - dx xr = x when :left xl = x xr = x + dx when :center xl = x - dx/2 xr = x + dx/2 else raise "Unknown horizontal alignment: #{@halign}" end return [xl, yt, xr, yb] end |
#to_frame_margins(t, width, height) ⇒ Object
Returns a frame_margin corresponding to the point, the alignment and the given size in figure coordinates.
See #to_frame_coordinates
173 174 175 176 |
# File 'lib/ctioga2/graphics/types/point.rb', line 173 def to_frame_margins(t, width, height) xl, yt, xr, yb = to_frame_coordinates(t, width, height) return [xl,1 - xr, 1 - yt, yb] end |