Class: Shoes::Shape

Inherits:
Common::ArtElement show all
Defined in:
shoes-core/lib/shoes/shape.rb

Constant Summary collapse

STYLES =
{ fill: Shoes::COLORS[:black] }.freeze

Constants inherited from Common::ArtElement

Common::ArtElement::REDRAW_OFFSET_FACTOR, Common::ArtElement::REDRAW_SIZING_FACTOR

Constants included from Common::Style

Common::Style::DEFAULT_STYLES, Common::Style::STYLE_GROUPS

Instance Attribute Summary collapse

Attributes included from Common::Clickable

#pass_coordinates

Attributes inherited from Common::UIElement

#app, #dimensions, #gui, #parent

Instance Method Summary collapse

Methods inherited from Common::ArtElement

#painted?

Methods included from Common::Translate

#clear_translate, #translate_left, #translate_top

Methods included from Common::Stroke

#update_stroke

Methods included from Common::Rotate

#needs_rotate?

Methods included from Common::Fill

#update_fill

Methods included from Common::Clickable

#click, #pass_coordinates?, #register_click, #release

Methods inherited from Common::UIElement

#add_to_parent, #before_initialize, #create_backend, #needs_rotate?, #painted?, #update_fill, #update_stroke

Methods included from Common::Style

#applicable_app_styles, #create_style_hash, included, #style, #style_init

Methods included from Common::SafelyEvaluate

#safely_evaluate

Methods included from Common::Remove

#remove

Methods included from Common::Positioning

#_position, #displace

Methods included from Common::Visibility

#hidden?, #hidden_from_view?, #hide, #outside_parent_view?, #show, #toggle, #visible?

Methods included from Common::Inspect

#inspect, #to_s

Methods included from Common::Attachable

#attached_to

Constructor Details

#initialize(*args) ⇒ Shape

Returns a new instance of Shape.



10
11
12
13
14
15
16
17
18
# File 'shoes-core/lib/shoes/shape.rb', line 10

def initialize(*args)
  @bottom_bound = nil
  @left_bound = nil
  @right_bound = nil
  @top_bound = nil
  @x = nil
  @y = nil
  super
end

Instance Attribute Details

#blkObject (readonly)

Returns the value of attribute blk.



5
6
7
# File 'shoes-core/lib/shoes/shape.rb', line 5

def blk
  @blk
end

#bottom_boundObject (readonly)

Returns the value of attribute bottom_bound.



5
6
7
# File 'shoes-core/lib/shoes/shape.rb', line 5

def bottom_bound
  @bottom_bound
end

#left_boundObject (readonly)

Returns the value of attribute left_bound.



5
6
7
# File 'shoes-core/lib/shoes/shape.rb', line 5

def left_bound
  @left_bound
end

#right_boundObject (readonly)

Returns the value of attribute right_bound.



5
6
7
# File 'shoes-core/lib/shoes/shape.rb', line 5

def right_bound
  @right_bound
end

#top_boundObject (readonly)

Returns the value of attribute top_bound.



5
6
7
# File 'shoes-core/lib/shoes/shape.rb', line 5

def top_bound
  @top_bound
end

#xObject (readonly)

Returns the value of attribute x.



5
6
7
# File 'shoes-core/lib/shoes/shape.rb', line 5

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



5
6
7
# File 'shoes-core/lib/shoes/shape.rb', line 5

def y
  @y
end

Instance Method Details

#after_initialize(*_) ⇒ Object



34
35
36
37
38
39
40
# File 'shoes-core/lib/shoes/shape.rb', line 34

def after_initialize(*_)
  @before_drawing = true
  @app.eval_with_additional_context self, &@blk

  # If we haven't drawn enough to get our bounds, default them out
  update_bounds([0], [0]) if @left_bound.nil?
end

#arc_to(x, y, width, height, start_angle, arc_angle) ⇒ Shoes::Shape

Draws an arc

Parameters:

  • x (Integer)

    The left position

  • y (Integer)

    The top position

  • width (Integer)

    The width of the arc

  • height (Integer)

    The height of the arc

  • start_angle (Integer)

    The start angle

  • arc_angle (Integer)

    The angular extent of the arc, relative to the start angle

Returns:



147
148
149
150
151
152
153
# File 'shoes-core/lib/shoes/shape.rb', line 147

def arc_to(x, y, width, height, start_angle, arc_angle)
  update_bounds_rect(x - width / 2, y - height / 2, x + width / 2, y + height / 2)
  @x = x
  @y = y
  @gui.arc_to(x, y, width, height, start_angle, arc_angle)
  self
end

#create_dimensions(left, top) ⇒ Object



20
21
22
23
24
25
# File 'shoes-core/lib/shoes/shape.rb', line 20

def create_dimensions(left, top)
  left ||= @style[:left] || 0
  top  ||= @style[:top] || 0

  @dimensions = AbsoluteDimensions.new left, top
end

#curve_to(cx1, cy1, cx2, cy2, x, y) ⇒ Shoes::Shape

Draws a curve

Parameters:

  • cx1 (Integer)

    The first control point’s x-value

  • cy1 (Integer)

    The first control point’s y-value

  • cx2 (Integer)

    The second control point’s x-value

  • cy2 (Integer)

    The second control point’s y-value

  • x (Integer)

    The end point’s x-value

  • y (Integer)

    The end point’s y-value

Returns:



130
131
132
133
134
135
136
# File 'shoes-core/lib/shoes/shape.rb', line 130

def curve_to(cx1, cy1, cx2, cy2, x, y)
  update_bounds([@x, cx1, cx2, x], [@y, cy1, cy2, y])
  @x = x
  @y = y
  @gui.curve_to(cx1, cy1, cx2, cy2, x, y)
  self
end

#element_heightObject



55
56
57
58
# File 'shoes-core/lib/shoes/shape.rb', line 55

def element_height
  return super unless @bottom_bound && @top_bound
  @bottom_bound - @top_bound
end

#element_widthObject



50
51
52
53
# File 'shoes-core/lib/shoes/shape.rb', line 50

def element_width
  return super unless @right_bound && @left_bound
  @right_bound - @left_bound
end

#fixed_height?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'shoes-core/lib/shoes/shape.rb', line 60

def fixed_height?
  false
end

#handle_block(blk) ⇒ Object



27
28
29
30
31
32
# File 'shoes-core/lib/shoes/shape.rb', line 27

def handle_block(blk)
  # Will register click from styles if present, but blk is for drawing!
  register_click

  @blk = blk
end

#heightObject



46
47
48
# File 'shoes-core/lib/shoes/shape.rb', line 46

def height
  @app.height
end

#in_bounds?(x, y) ⇒ Boolean

Determines if a given point is in the boundary of the shape. Given the many possibilities of what a shape could contain, this just checks the outer bounding box of the shape, nothing more sophisticated.

Returns:

  • (Boolean)


158
159
160
161
# File 'shoes-core/lib/shoes/shape.rb', line 158

def in_bounds?(x, y)
  (@left_bound..@right_bound).cover?(x) &&
    (@top_bound..@bottom_bound).cover?(y)
end

#line_to(x, y) ⇒ Shoes::Shape

Draws a line from the current position to the given point

Parameters:

  • x (Integer)

    The new point’s x-value

  • y (Integer)

    The new point’s y-value

Returns:



101
102
103
104
105
106
107
# File 'shoes-core/lib/shoes/shape.rb', line 101

def line_to(x, y)
  update_bounds_rect(@x, @y, x, y)
  @x = x
  @y = y
  @gui.line_to(x, y)
  self
end

#move(left, top) ⇒ Shoes::Shape

Moves the shape

Parameters:

  • left (Integer)

    The new left value

  • top (Integer)

    The new top value

Returns:



89
90
91
92
93
94
# File 'shoes-core/lib/shoes/shape.rb', line 89

def move(left, top)
  self.left = left
  self.top = top
  @gui.update_position
  self
end

#move_to(x, y) ⇒ Shoes::Shape

Moves the drawing “pen” to the given point

Parameters:

  • x (Integer)

    The new point’s x-value

  • y (Integer)

    The new point’s y-value

Returns:



114
115
116
117
118
119
# File 'shoes-core/lib/shoes/shape.rb', line 114

def move_to(x, y)
  @x = x
  @y = y
  @gui.move_to(x, y)
  self
end

#redraw_heightObject



79
80
81
82
# File 'shoes-core/lib/shoes/shape.rb', line 79

def redraw_height
  return 0 unless element_height
  element_height + 2 * strokewidth.to_i
end

#redraw_leftObject



64
65
66
67
# File 'shoes-core/lib/shoes/shape.rb', line 64

def redraw_left
  return 0 unless @left_bound
  @left_bound - strokewidth.to_i
end

#redraw_topObject



69
70
71
72
# File 'shoes-core/lib/shoes/shape.rb', line 69

def redraw_top
  return 0 unless @top_bound
  @top_bound - strokewidth.to_i
end

#redraw_widthObject



74
75
76
77
# File 'shoes-core/lib/shoes/shape.rb', line 74

def redraw_width
  return 0 unless element_width
  element_width + 2 * strokewidth.to_i
end

#widthObject



42
43
44
# File 'shoes-core/lib/shoes/shape.rb', line 42

def width
  @app.width
end