Class: Shoes::Line
- Inherits:
-
Common::ArtElement
- Object
- Common::UIElement
- Common::ArtElement
- Shoes::Line
- Defined in:
- shoes-core/lib/shoes/line.rb
Constant Summary collapse
- STYLES =
{ fill: Shoes::COLORS[:black] }.freeze
- REDRAW_OFFSET_FACTOR =
Redrawing needs a bit of extra room. We offset by this factor, then extend our size by twice that to evenly surround the whole thing.
4
- REDRAW_SIZING_FACTOR =
REDRAW_OFFSET_FACTOR * 2
Constants included from Common::Style
Common::Style::DEFAULT_STYLES, Common::Style::STYLE_GROUPS
Instance Attribute Summary collapse
-
#point_a ⇒ Object
readonly
Returns the value of attribute point_a.
-
#point_b ⇒ Object
readonly
Returns the value of attribute point_b.
Attributes included from Common::Clickable
Attributes inherited from Common::UIElement
#app, #dimensions, #gui, #parent
Instance Method Summary collapse
- #bottom=(val) ⇒ Object (also: #y2=)
- #create_dimensions(x1, y1, x2, y2) ⇒ Object
- #default_coordinates(x1, y1, x2, y2) ⇒ Object
-
#in_bounds?(x, y) ⇒ Boolean
Check out math.stackexchange.com/questions/60070/checking-whether-a-point-lies-on-a-wide-line-segment for explanations how the algorithm works.
- #left=(val) ⇒ Object
- #move(x, y, x2 = nil, y2 = nil) ⇒ Object
- #redraw_height ⇒ Object
- #redraw_left ⇒ Object
- #redraw_top ⇒ Object
- #redraw_width ⇒ Object
- #right=(val) ⇒ Object (also: #x2=)
- #top=(val) ⇒ Object
- #update_style(new_styles) ⇒ Object
Methods inherited from Common::ArtElement
Methods included from Common::Translate
#clear_translate, #translate_left, #translate_top
Methods included from Common::Stroke
Methods included from Common::Rotate
Methods included from Common::Fill
Methods included from Common::Clickable
#click, #pass_coordinates?, #register_click, #release
Methods inherited from Common::UIElement
#add_to_parent, #after_initialize, #before_initialize, #create_backend, #handle_block, #initialize, #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
Methods included from Common::Remove
Methods included from Common::Positioning
Methods included from Common::Visibility
#hidden?, #hidden_from_view?, #hide, #outside_parent_view?, #show, #toggle, #visible?
Methods included from Common::Inspect
Methods included from Common::Attachable
Constructor Details
This class inherits a constructor from Shoes::Common::UIElement
Instance Attribute Details
#point_a ⇒ Object (readonly)
Returns the value of attribute point_a.
7 8 9 |
# File 'shoes-core/lib/shoes/line.rb', line 7 def point_a @point_a end |
#point_b ⇒ Object (readonly)
Returns the value of attribute point_b.
7 8 9 |
# File 'shoes-core/lib/shoes/line.rb', line 7 def point_b @point_b end |
Instance Method Details
#bottom=(val) ⇒ Object Also known as: y2=
78 79 80 |
# File 'shoes-core/lib/shoes/line.rb', line 78 def bottom=(val) set_point_b(:y, val) end |
#create_dimensions(x1, y1, x2, y2) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'shoes-core/lib/shoes/line.rb', line 12 def create_dimensions(x1, y1, x2, y2) x1, y1, x2, y2 = default_coordinates(x1, y1, x2, y2) @point_a = Shoes::Point.new(x1, y1) @point_b = Shoes::Point.new(x2, y2) enclosing_box_of_line style[:x2] = @point_b.x style[:y2] = @point_b.y end |
#default_coordinates(x1, y1, x2, y2) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'shoes-core/lib/shoes/line.rb', line 24 def default_coordinates(x1, y1, x2, y2) x1 ||= @style[:left] || 0 y1 ||= @style[:top] || 0 x2 ||= @style[:right] if x2 # With 3 arguments, draws horizontal line, so fallback to y1 y2 ||= @style[:bottom] || y1 else # If didn't get needed arguments, set start to end which draws nothing x2 = x1 y2 = y1 end [x1, y1, x2, y2] end |
#in_bounds?(x, y) ⇒ Boolean
Check out math.stackexchange.com/questions/60070/checking-whether-a-point-lies-on-a-wide-line-segment for explanations how the algorithm works
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'shoes-core/lib/shoes/line.rb', line 43 def in_bounds?(x, y) # c is (x, y) left_most, right_most = point_a.x < point_b.x ? [point_a, point_b] : [point_b, point_a] left_c = Vector.elements((left_most - [x, y]).to_a, false) left_right = Vector.elements((left_most - right_most).to_a, false) boldness = style[:strokewidth].to_i / 2 left_c_dot_left_right = left_c.inner_product(left_right) left_right_dot_left_right = left_right.inner_product(left_right) if left_c_dot_left_right.between?(0, left_right_dot_left_right) left_c_dot_left_c = left_c.inner_product(left_c) left_right_dot_left_right * left_c_dot_left_c <= boldness**2 * left_right_dot_left_right + left_c_dot_left_right**2 else false end end |
#left=(val) ⇒ Object
66 67 68 |
# File 'shoes-core/lib/shoes/line.rb', line 66 def left=(val) set_point_a(:x, val) end |
#move(x, y, x2 = nil, y2 = nil) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'shoes-core/lib/shoes/line.rb', line 106 def move(x, y, x2 = nil, y2 = nil) @point_a.x = x @point_a.y = y @point_b.x = x2 if x2 @point_b.y = y2 if y2 enclosing_box_of_line self end |
#redraw_height ⇒ Object
102 103 104 |
# File 'shoes-core/lib/shoes/line.rb', line 102 def redraw_height (@point_a.y - @point_b.y).abs + strokewidth.ceil * REDRAW_SIZING_FACTOR end |
#redraw_left ⇒ Object
90 91 92 |
# File 'shoes-core/lib/shoes/line.rb', line 90 def redraw_left [@point_a.x, @point_b.x].min - strokewidth.ceil * REDRAW_OFFSET_FACTOR end |
#redraw_top ⇒ Object
94 95 96 |
# File 'shoes-core/lib/shoes/line.rb', line 94 def redraw_top [@point_a.y, @point_b.y].min - strokewidth.ceil * REDRAW_OFFSET_FACTOR end |
#redraw_width ⇒ Object
98 99 100 |
# File 'shoes-core/lib/shoes/line.rb', line 98 def redraw_width (@point_a.x - @point_b.x).abs + strokewidth.ceil * REDRAW_SIZING_FACTOR end |
#right=(val) ⇒ Object Also known as: x2=
70 71 72 |
# File 'shoes-core/lib/shoes/line.rb', line 70 def right=(val) set_point_b(:x, val) end |
#top=(val) ⇒ Object
74 75 76 |
# File 'shoes-core/lib/shoes/line.rb', line 74 def top=(val) set_point_a(:y, val) end |
#update_style(new_styles) ⇒ Object
61 62 63 64 |
# File 'shoes-core/lib/shoes/line.rb', line 61 def update_style(new_styles) super enclosing_box_of_line end |