Class: CTioga2::Graphics::Styles::BoxStyle

Inherits:
StrokeStyle show all
Defined in:
lib/ctioga2/graphics/styles/box.rb

Overview

TODO:

Add rounded corners and the likeā€¦

This class represents styles attached to a box

Constant Summary

Constants inherited from BasicStyle

CTioga2::Graphics::Styles::BasicStyle::AllStyles, CTioga2::Graphics::Styles::BasicStyle::OldAttrAccessor

Instance Method Summary collapse

Methods inherited from StrokeStyle

#set_stroke_style

Methods inherited from LineStyle

#draw_line, #set_stroke_style

Methods inherited from BasicStyle

alias_for, aliases, attr_accessor, attribute_type, attribute_types, attributes, convert_string_hash, defined_aliases, deprecated_attribute, from_hash, inherited, #instance_variable_defined?, normalize_hash, normalize_in, normalize_out, options_hash, #set_from_hash, sub_style, sub_styles, #to_hash, typed_attribute, #update_from_other, #use_defaults_from

Constructor Details

#initializeBoxStyle

Returns a new instance of BoxStyle.



49
50
51
52
# File 'lib/ctioga2/graphics/styles/box.rb', line 49

def initialize
  @shape = :square
  @radius = Types::Dimension::new(:dy, 1.0)
end

Instance Method Details

#draw_box(t, x1, y1, x2, y2) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ctioga2/graphics/styles/box.rb', line 93

def draw_box(t, x1, y1, x2, y2)
  t.context do
    t.discard_path

    ## @todo Rounded rects!
    if fill && fill.color
      fill.setup_fill(t)
      prepare_path(t, x1, y1, x2, y2)
      fill.do_fill(t)
    end
    if color
      set_stroke_style(t)
      prepare_path(t, x1, y1, x2, y2)
      t.stroke
    end
  end

end

#draw_box_around(t, x1, y1, x2, y2, dx, dy = nil) ⇒ Object

Draws a box around the given box, leaving dx and dy around. If dy is omitted, it defaults to dx



114
115
116
117
118
119
120
# File 'lib/ctioga2/graphics/styles/box.rb', line 114

def draw_box_around(t, x1, y1, x2, y2, dx, dy = nil)
  dy ||= dx
  dx = dx.to_figure(t, :x)
  dy = dy.to_figure(t, :y)
  draw_box(t, x1 - dx, y1 + dy,
           x2 + dx, y2 - dy)
end

#prepare_path(t, x1, y1, x2, y2) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ctioga2/graphics/styles/box.rb', line 54

def prepare_path(t, x1, y1, x2, y2)
  case @shape
  when :square
    t.append_rect_to_path(x1, y1, x2 - x1, y2 - y1)
  when :round
    dx = @radius.to_figure(t, :x)
    dy = @radius.to_figure(t, :y)

    xl = x1
    xr = x2
    xl,xr = xr, xl if xl > xr

    yt = y1
    yb = y2
    yb,yt = yt,yb if yb > yt
    
    t.move_to_point(xl, yt - dy)
    t.append_curve_to_path(xl, yt - 0.5 * dy, # First control point
                           xl + 0.5 * dx, yt, 
                           xl + dx, yt)
    t.append_point_to_path(xr - dx, yt)
    t.append_curve_to_path(xr - 0.5 * dx, yt,
                           xr, yt - 0.5 * dy, 
                           xr, yt - dy)

    t.append_point_to_path(xr, yb + dy)
    t.append_curve_to_path(xr, yb + 0.5 * dy, # First control point
                           xr - 0.5 * dx, yb, 
                           xr - dx, yb)
    t.append_point_to_path(xl + dx, yb)
    t.append_curve_to_path(xl + 0.5 * dx, yb, # First control point
                           xl, yb + 0.5 * dy, 
                           xl, yb + dy)
    t.close_path
  else
    raise "Unknown box shape: #{@shape}"
  end
end