Class: DYI::Shape::Base Abstract

Inherits:
GraphicalElement show all
Defined in:
lib/dyi/shape/base.rb

Overview

This class is abstract.

Base class of all graphical shapes.

Since:

  • 0.0.0

Constant Summary

Constants inherited from GraphicalElement

GraphicalElement::CLASS_REGEXP

Constants inherited from Element

Element::ID_REGEXP

Instance Attribute Summary collapse

Attributes inherited from GraphicalElement

#css_class

Attributes inherited from Element

#description, #title

Instance Method Summary collapse

Methods inherited from GraphicalElement

#add_css_class, #add_event_listener, #css_classes, #event_listeners, #event_target?, #remove_css_class, #remove_event_listener, #to_reused_source

Methods inherited from Element

#canvas, #child_elements, #id, #id=, #include_external_file?, #inner_id

Instance Attribute Details

#anchor_hrefString

Returns a location of a reference of a source anchor for the link.

Returns:

  • (String)

    a location of a reference

Since:

  • 1.0.0



143
144
145
# File 'lib/dyi/shape/base.rb', line 143

def anchor_href
  @anchor_href
end

#anchor_targetString

Returns a relevant presentation context when the link is activated.

Returns:

  • (String)

    a relevant presentation context

Since:

  • 1.0.0



148
149
150
# File 'lib/dyi/shape/base.rb', line 148

def anchor_target
  @anchor_target
end

#attributesHash (readonly)

Returns optional attributes of the shape.

Returns:

  • (Hash)

    optional attributes

Since:

  • 0.0.0



129
130
131
# File 'lib/dyi/shape/base.rb', line 129

def attributes
  @attributes
end

#clippingDrawing::Clipping (readonly)

Returns clipping status of the shape.

Returns:

Since:

  • 0.0.0



133
134
135
# File 'lib/dyi/shape/base.rb', line 133

def clipping
  @clipping
end

#parentGraphicalElement (readonly)

Returns a parent element of the shape.

Returns:

Since:

  • 1.0.0



138
139
140
# File 'lib/dyi/shape/base.rb', line 138

def parent
  @parent
end

Instance Method Details

#add_animation(animation) ⇒ Object

Adds animation to the shape

Parameters:

Since:

  • 1.0.0



318
319
320
# File 'lib/dyi/shape/base.rb', line 318

def add_animation(animation)
  animations << animation
end

#add_painting_animation(options) ⇒ Object

Adds animation of painting to the shape

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :from (Painting)

    the starting painting of the animation

  • :to (Painting)

    the ending painting of the animation

  • :duration (Number)

    a simple duration in seconds

  • :begin_offset (Number)

    a offset that determine the animation begin, in seconds

  • :begin_event (Event)

    an event that determine the animation begin

  • :end_offset (Number)

    a offset that determine the animation end, in seconds

  • :end_event (Event)

    an event that determine the animation end

  • :fill (String)

    ‘freeze’ or ‘remove’

Since:

  • 1.0.0



336
337
338
# File 'lib/dyi/shape/base.rb', line 336

def add_painting_animation(options)
  add_animation(Animation::PaintingAnimation.new(self, options))
end

#add_transform_animation(type, options) ⇒ Object

Adds animation of transform to the shape

Parameters:

  • type (Symbol)

    a type of transformation which is to have values

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :from (Number, Array)

    the starting transform of the animation

  • :to (Number, Array)

    the ending transform of the animation

  • :duration (Number)

    a simple duration in seconds

  • :begin_offset (Number)

    a offset that determine the animation begin, in seconds

  • :begin_event (Event)

    an event that determine the animation begin

  • :end_offset (Number)

    a offset that determine the animation end, in seconds

  • :end_event (Event)

    an event that determine the animation end

  • :fill (String)

    ‘freeze’ or ‘remove’

Since:

  • 1.0.0



356
357
358
# File 'lib/dyi/shape/base.rb', line 356

def add_transform_animation(type, options)
  add_animation(Animation::TransformAnimation.new(self, type, options))
end

#animate?Boolean

Returns whether the shape is animated.

Returns:

  • (Boolean)

    true if the shape is animated, false otherwise

Since:

  • 1.0.0



311
312
313
# File 'lib/dyi/shape/base.rb', line 311

def animate?
  !(@animations.nil? || @animations.empty?)
end

#animationsArray<Animation::Base>

Returns registed animations. since 1.0.0

Returns:

Since:

  • 0.0.0



304
305
306
# File 'lib/dyi/shape/base.rb', line 304

def animations
  @animations ||= []
end

#clear_clippingObject

Crears clipping settings.

Since:

  • 0.0.0



282
283
284
# File 'lib/dyi/shape/base.rb', line 282

def clear_clipping
  @clipping = nil
end

#draw_on(parent) ⇒ Shape::Base

Draws the shape on a parent element.

Parameters:

  • parent (Element)

    a container element on which the shape is drawn

Returns:

Raises:

  • (ArgumentError)

    parent is nil

  • (RuntimeError)

    this shape already has a parent, or descendants of this shape include itself

Since:

  • 0.0.0



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/dyi/shape/base.rb', line 156

def draw_on(parent)
  raise ArgumentError, "parent is nil" if parent.nil?
  return self if @parent == parent
  raise RuntimeError, "this shape already has a parent" if @parent
  current_node = parent
  loop do
    break if current_node.nil? || current_node.root_element?
    if current_node == self
      raise RuntimeError, "descendants of this shape include itself"
    end
    current_node = current_node.parent
  end
  (@parent = parent).child_elements.push(self)
  self
end

#has_marker?(position) ⇒ Boolean

Returns whether this shape has a marker symbol.

Parameters:

  • position (Symbol)

    the position where a marker symbol is drawn. Specifies the following values: :start, :mid, :end

Returns:

  • (Boolean)

    always false

Since:

  • 0.0.0



297
298
299
# File 'lib/dyi/shape/base.rb', line 297

def has_marker?(position)
  return false
end

#has_uri_reference?Boolean

Returns whether the element has URI reference.

Returns:

  • (Boolean)

    true if the element has URI reference, false otherwise

Since:

  • 1.0.0



379
380
381
# File 'lib/dyi/shape/base.rb', line 379

def has_uri_reference?
  @anchor_href ? true : false
end

#root_element?Boolean

Returns whether this instance is root element of the shape.

Returns:

  • (Boolean)

    always false.

Since:

  • 1.0.0



183
184
185
# File 'lib/dyi/shape/base.rb', line 183

def root_element?
  false
end

#root_node?Boolean

Deprecated.

Use #root_element? instead.

Returns:

  • (Boolean)

Since:

  • 0.0.0



173
174
175
176
177
178
# File 'lib/dyi/shape/base.rb', line 173

def root_node?
  msg = [__FILE__, __LINE__, ' waring']
  msg << ' DYI::Shape::Base#root_node? is depricated; use DYI::Shape::Base#root_element?'
  warn(msg.join(':'))
  false
end

#rotate(angle, base_point = Coordinate::ZERO) ⇒ Object

Rotates this shape.

Parameters:

  • angle (Numeric)

    rotation angle. specifies degree

  • base_point (Coordinate) (defaults to: Coordinate::ZERO)

    based coordinate of rotetion

Since:

  • 0.0.0



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/dyi/shape/base.rb', line 234

def rotate(angle, base_point=Coordinate::ZERO)
  angle %= 360
  return if angle == 0
  base_point = Coordinate.new(base_point)
  translate(base_point.x, base_point.y) if base_point.nonzero?
  lt = transform.last
  if lt && lt.first == :rotate
    lt[1] = (lt[1] + angle) % 360
    transform.pop if lt[1] == 0
  else
    transform.push([:rotate, angle])
  end
  translate(- base_point.x, - base_point.y) if base_point.nonzero?
end

#scale(x, y = nil, base_point = Coordinate::ZERO) ⇒ Object

Scales up (or down) this shape.

Parameters:

  • x (Numeric)

    scaled ratio along x-axis

  • y (Numeric) (defaults to: nil)

    scaled ratio along y-axis. If this parameter is nil, uses value that equals to parameter ‘x’ value

  • base_point (Coordinate) (defaults to: Coordinate::ZERO)

    based coordinate of scaling up (or down)

Since:

  • 0.0.0



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/dyi/shape/base.rb', line 215

def scale(x, y=nil, base_point=Coordinate::ZERO)
  y ||= x
  return if x == 1 && y == 1
  base_point = Coordinate.new(base_point)
  translate(base_point.x, base_point.y) if base_point.nonzero?
  lt = transform.last
  if lt && lt.first == :scale
    lt[1] *= x
    lt[2] *= y
    transform.pop if lt[1] == 1 && lt[2] == 1
  else
    transform.push([:scale, x, y])
  end
  translate(- base_point.x, - base_point.y) if base_point.nonzero?
end

#set_clipping(clipping) ⇒ Object

Restricts the region to which paint can be applied.

Parameters:

Since:

  • 0.0.0



276
277
278
279
# File 'lib/dyi/shape/base.rb', line 276

def set_clipping(clipping)
  clipping.set_canvas(canvas)
  @clipping = clipping
end

#set_clipping_shapes(*shapes) ⇒ Object

Sets shapes that is used to estrict the region to which paint can be

applied.

Parameters:

  • shapes (Base)

    a shape that is used to clip

Since:

  • 0.0.0



289
290
291
# File 'lib/dyi/shape/base.rb', line 289

def set_clipping_shapes(*shapes)
  set_clipping(Drawing::Clipping.new(*shapes))
end

#set_event(event) ⇒ Object

Adds animation of painting to the shape

Parameters:

  • an (Event)

    event that is set to the shape

Since:

  • 1.0.0



363
364
365
366
# File 'lib/dyi/shape/base.rb', line 363

def set_event(event)
  super
  canvas.set_event(event)
end

#skew_x(angle, base_point = Coordinate::ZERO) ⇒ Object

Skews this shape along x-axis.

Parameters:

  • angle (Numeric)

    skew angle. specifies degree

  • base_point (Coordinate) (defaults to: Coordinate::ZERO)

    based coordinate of skew

Since:

  • 0.0.0



252
253
254
255
256
257
258
259
# File 'lib/dyi/shape/base.rb', line 252

def skew_x(angle, base_point=Coordinate::ZERO)
  angle %= 180
  return if angle == 0
  base_point = Coordinate.new(base_point)
  translate(base_point.x, base_point.y) if base_point.nonzero?
  transform.push([:skewX, angle])
  translate(- base_point.x, - base_point.y) if base_point.nonzero?
end

#skew_y(angle, base_point = Coordinate::ZERO) ⇒ Object

Skews this shape along y-axis.

Parameters:

  • angle (Numeric)

    skew angle. specifies degree

  • base_point (Coordinate) (defaults to: Coordinate::ZERO)

    based coordinate of skew

Since:

  • 0.0.0



264
265
266
267
268
269
270
271
272
# File 'lib/dyi/shape/base.rb', line 264

def skew_y(angle, base_point=Coordinate::ZERO)
  angle %= 180
  return if angle == 0
  base_point = Coordinate.new(base_point)
  translate(base_point.x, base_point.y) if base_point.nonzero?
  lt = transform.last
  transform.push([:skewY, angle])
  translate(- base_point.x, - base_point.y) if base_point.nonzero?
end

#transformArray

Returns transform list.

Returns:

  • (Array)

    transform list.

Since:

  • 0.0.0



189
190
191
# File 'lib/dyi/shape/base.rb', line 189

def transform
  @transform ||= []
end

#translate(x, y = 0) ⇒ Object

Translates the shape.

Parameters:

  • x (Numeric)

    translated value along x-axis

  • y (Numeric) (defaults to: 0)

    translated value along y-axis

Since:

  • 0.0.0



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/dyi/shape/base.rb', line 196

def translate(x, y=0)
  x = Length.new(x)
  y = Length.new(y)
  return if x.zero? && y.zero?
  lt = transform.last
  if lt && lt.first == :translate
    lt[1] += x
    lt[2] += y
    transform.pop if lt[1].zero? && lt[2].zero?
  else
    transform.push([:translate, x, y])
  end
end