Class: DYI::Painting
- Inherits:
-
Object
- Object
- DYI::Painting
- Defined in:
- lib/dyi/painting.rb,
lib/ironruby.rb
Overview
Painting is used to specify how the drawing of the interior and the outline of the shape. Shape::Base objects (or objects of its subclass) have painting attribute whose value is instance of this class.
Constant Summary collapse
- IMPLEMENT_ATTRIBUTES =
[:opacity, :fill, :fill_opacity, :fill_rule, :stroke, :stroke_dasharray, :stroke_dashoffset, :stroke_linecap, :stroke_linejoin, :stroke_miterlimit, :stroke_opacity, :stroke_width, :display, :visibility]
- VALID_VALUES =
{:fill_rule => ['nonzero','evenodd'], :stroke_linecap => ['butt','round','square'], :stroke_linejoin => ['miter','round','bevel'], :display => ['block','none'], :visibility => ['visible','hidden']}
Instance Attribute Summary collapse
- #array ⇒ Object
- #color ⇒ Object
-
#display ⇒ Object
writeonly
+++ +++ +++ +++.
-
#fill_rule ⇒ Object
writeonly
+++ +++ +++ +++.
- #miterlimit ⇒ Object
- #offset ⇒ Object
- #opacity ⇒ Object
-
#stroke_linecap ⇒ Object
writeonly
+++ +++ +++ +++.
-
#stroke_linejoin ⇒ Object
writeonly
+++ +++ +++ +++.
-
#visibility ⇒ Object
writeonly
+++ +++ +++ +++.
- #width ⇒ Object
Class Method Summary collapse
-
.new_or_nil(*args) ⇒ Painting?
Returns a new instace of
Paintingif the argments is notnil(callsPainting.newmethod), but returnsnilif the argument isnil.
Instance Method Summary collapse
-
#attributes ⇒ Hash{Symbol => Object}
Returns the hash of the attribute values.
- #cls_brush(shape) ⇒ Object
- #cls_dash_pattern ⇒ Object
- #cls_fill_mode ⇒ Object
- #cls_line_cap ⇒ Object
- #cls_line_join ⇒ Object
- #cls_pen ⇒ Object
-
#empty? ⇒ Boolean
Returns whether a value has been set some attribute in this object.
- #fill=(color) ⇒ Object
- #fill_opacity=(opacity) ⇒ Object
-
#initialize(options = {}) ⇒ Painting
constructor
Creates and returns a new instace of Paintng.
- #stroke=(color) ⇒ Object
- #stroke_dasharray=(array) ⇒ Object
- #stroke_dashoffset=(offset) ⇒ Object
- #stroke_miterlimit=(miterlimit) ⇒ Object
- #stroke_opacity=(opacity) ⇒ Object
- #stroke_width=(width) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Painting
Creates and returns a new instace of Paintng. If the argutment is a instance of Painting, returns a copy of the argument.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/dyi/painting.rb', line 143 def initialize(={}) case when Painting IMPLEMENT_ATTRIBUTES.each do |attr| instance_variable_set("@#{attr}", .__send__(attr)) end when Hash .each do |attr, value| __send__("#{attr}=", value) if IMPLEMENT_ATTRIBUTES.include?(attr.to_sym) end else raise TypeError, "#{.class} can't be coerced into #{self.class}" end end |
Instance Attribute Details
#array ⇒ Object
228 229 230 231 232 233 234 235 236 |
# File 'lib/dyi/painting.rb', line 228 def stroke_dasharray=(array) if (array.nil? || array.empty?) @stroke_dasharray = nil elsif array.kind_of?(String) @stroke_dasharray = array.split(/\s*,\s*/).map {|len| Length.new(len)} else @stroke_dasharray = array.map {|len| Length.new(len)} end end |
#color ⇒ Object
185 186 187 |
# File 'lib/dyi/painting.rb', line 185 def fill=(color) @fill = color.respond_to?(:color?) && color.color? ? color : Color.new_or_nil(color) end |
#display=(value) ⇒ Object (writeonly)
+++ +++ +++ +++
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dyi/painting.rb', line 172 VALID_VALUES.each do |attr, valid_values| define_method("#{attr.to_s}=") {|value| if (value = value.to_s).size == 0 instance_variable_set("@#{attr}", nil) else raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value) instance_variable_set("@#{attr}", value) end } end |
#fill_rule=(value) ⇒ Object (writeonly)
+++ +++ +++ +++
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dyi/painting.rb', line 172 VALID_VALUES.each do |attr, valid_values| define_method("#{attr.to_s}=") {|value| if (value = value.to_s).size == 0 instance_variable_set("@#{attr}", nil) else raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value) instance_variable_set("@#{attr}", value) end } end |
#miterlimit ⇒ Object
222 223 224 |
# File 'lib/dyi/painting.rb', line 222 def stroke_miterlimit=(miterlimit) @stroke_miterlimit = miterlimit.nil? ? nil : [miterlimit.to_f, 1].max end |
#offset ⇒ Object
240 241 242 |
# File 'lib/dyi/painting.rb', line 240 def stroke_dashoffset=(offset) @stroke_dashoffset = Length.new_or_nil(offset) end |
#opacity ⇒ Object
198 199 200 |
# File 'lib/dyi/painting.rb', line 198 def opacity=(opacity) @opacity = opacity.nil? ? nil : opacity.to_f end |
#stroke_linecap=(value) ⇒ Object (writeonly)
+++ +++ +++ +++
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dyi/painting.rb', line 172 VALID_VALUES.each do |attr, valid_values| define_method("#{attr.to_s}=") {|value| if (value = value.to_s).size == 0 instance_variable_set("@#{attr}", nil) else raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value) instance_variable_set("@#{attr}", value) end } end |
#stroke_linejoin=(value) ⇒ Object (writeonly)
+++ +++ +++ +++
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dyi/painting.rb', line 172 VALID_VALUES.each do |attr, valid_values| define_method("#{attr.to_s}=") {|value| if (value = value.to_s).size == 0 instance_variable_set("@#{attr}", nil) else raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value) instance_variable_set("@#{attr}", value) end } end |
#visibility=(value) ⇒ Object (writeonly)
+++ +++ +++ +++
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dyi/painting.rb', line 172 VALID_VALUES.each do |attr, valid_values| define_method("#{attr.to_s}=") {|value| if (value = value.to_s).size == 0 instance_variable_set("@#{attr}", nil) else raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value) instance_variable_set("@#{attr}", value) end } end |
#width ⇒ Object
216 217 218 |
# File 'lib/dyi/painting.rb', line 216 def stroke_width=(width) @stroke_width = Length.new_or_nil(width) end |
Class Method Details
.new_or_nil(*args) ⇒ Painting?
Returns a new instace of Painting if the argments is not nil (calls Painting.new method), but returns nil if the argument is nil.
273 274 275 |
# File 'lib/dyi/painting.rb', line 273 def new_or_nil(*args) (args.size == 1 && args.first.nil?) ? nil : new(*args) end |
Instance Method Details
#attributes ⇒ Hash{Symbol => Object}
Returns the hash of the attribute values. Even if the return value of this method is modified, the attribute value of the object is not modify.
247 248 249 250 251 252 253 254 255 |
# File 'lib/dyi/painting.rb', line 247 def attributes IMPLEMENT_ATTRIBUTES.inject({}) do |hash, attr| value = instance_variable_get("@#{attr}") unless value.nil? hash[attr] = value end hash end end |
#cls_brush(shape) ⇒ Object
113 114 115 |
# File 'lib/ironruby.rb', line 113 def cls_brush(shape) fill ? fill.create_cls_brush(fill_opacity, shape) : System::Drawing::SolidBrush.new(System::Drawing::Color.black) end |
#cls_dash_pattern ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/ironruby.rb', line 94 def cls_dash_pattern return nil if !stroke_dasharray || stroke_dasharray.size == 0 pattern = System::Array[System::Single].new(stroke_dasharray.size) stroke_dasharray.each_with_index do |dash, i| pattern[i] = dash.to_f end pattern end |
#cls_fill_mode ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/ironruby.rb', line 86 def cls_fill_mode case fill_rule when 'nonzero' then System::Drawing::Drawing2D::FillMode.winding when 'evenodd' then System::Drawing::Drawing2D::FillMode.alternate else System::Drawing::Drawing2D::FillMode.winding end end |
#cls_line_cap ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/ironruby.rb', line 77 def cls_line_cap case stroke_linecap when 'butt' then System::Drawing::Drawing2D::LineCap.flat when 'round' then System::Drawing::Drawing2D::LineCap.round when 'square' then System::Drawing::Drawing2D::LineCap.square else System::Drawing::Drawing2D::LineCap.flat end end |
#cls_line_join ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/ironruby.rb', line 68 def cls_line_join case stroke_linejoin when 'miter' then System::Drawing::Drawing2D::LineJoin.miter when 'round' then System::Drawing::Drawing2D::LineJoin.round when 'bevel' then System::Drawing::Drawing2D::LineJoin.bevel else System::Drawing::Drawing2D::LineJoin.miter end end |
#cls_pen ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/ironruby.rb', line 103 def cls_pen return nil unless stroke && (stroke_width != DYI::Length::ZERO) pen = stroke.create_cls_pen(stroke_opacity) pen.width = stroke_width ? stroke_width.to_f : 1.0 pen.start_cap = pen.end_cap = cls_line_cap pen.line_join = cls_line_join pen.dash_pattern = cls_dash_pattern if cls_dash_pattern pen end |
#empty? ⇒ Boolean
Returns whether a value has been set some attribute in this object.
260 261 262 263 264 |
# File 'lib/dyi/painting.rb', line 260 def empty? IMPLEMENT_ATTRIBUTES.all? do |attr| not instance_variable_get("@#{attr}") end end |
#fill=(color) ⇒ Object
185 186 187 |
# File 'lib/dyi/painting.rb', line 185 def fill=(color) @fill = color.respond_to?(:color?) && color.color? ? color : Color.new_or_nil(color) end |
#fill_opacity=(opacity) ⇒ Object
204 205 206 |
# File 'lib/dyi/painting.rb', line 204 def fill_opacity=(opacity) @fill_opacity = opacity.nil? ? nil : opacity.to_f end |
#stroke=(color) ⇒ Object
191 192 193 |
# File 'lib/dyi/painting.rb', line 191 def stroke=(color) @stroke = color.respond_to?(:color?) && color.color? ? color : Color.new_or_nil(color) end |
#stroke_dasharray=(array) ⇒ Object
228 229 230 231 232 233 234 235 236 |
# File 'lib/dyi/painting.rb', line 228 def stroke_dasharray=(array) if (array.nil? || array.empty?) @stroke_dasharray = nil elsif array.kind_of?(String) @stroke_dasharray = array.split(/\s*,\s*/).map {|len| Length.new(len)} else @stroke_dasharray = array.map {|len| Length.new(len)} end end |
#stroke_dashoffset=(offset) ⇒ Object
240 241 242 |
# File 'lib/dyi/painting.rb', line 240 def stroke_dashoffset=(offset) @stroke_dashoffset = Length.new_or_nil(offset) end |
#stroke_miterlimit=(miterlimit) ⇒ Object
222 223 224 |
# File 'lib/dyi/painting.rb', line 222 def stroke_miterlimit=(miterlimit) @stroke_miterlimit = miterlimit.nil? ? nil : [miterlimit.to_f, 1].max end |
#stroke_opacity=(opacity) ⇒ Object
210 211 212 |
# File 'lib/dyi/painting.rb', line 210 def stroke_opacity=(opacity) @stroke_opacity = opacity.nil? ? nil : opacity.to_f end |
#stroke_width=(width) ⇒ Object
216 217 218 |
# File 'lib/dyi/painting.rb', line 216 def stroke_width=(width) @stroke_width = Length.new_or_nil(width) end |