Class: Wx::SF::PolygonShape

Inherits:
RectShape show all
Defined in:
lib/wx/shapes/shapes/polygon_shape.rb

Overview

Class extends the Wx::SF::RectShape and encapsulates general polygon shape defined by a set of its vertices. The class can be used as it is or as a base class for shapes with more complex form and functionality.

See Also:

Direct Known Subclasses

DiamondShape

Defined Under Namespace

Modules: DEFAULT

Instance Method Summary collapse

Methods inherited from RectShape

#create_handles, #do_begin_handle, #do_on_handle, #get_border, #get_bounding_box, #get_fill, #get_rect_size, #on_begin_handle, #on_bottom_handle, #on_left_handle, #on_right_handle, #on_top_handle, #scale, #set_border, #set_fill, #set_rect_size

Methods inherited from Shape

#accept_child, #accept_connection, #accept_currently_dragged_shapes, #accept_src_neighbour, #accept_trg_neighbour, #activate, #active?, #add_child_shape, #add_connection_point, #add_handle, #add_style, #ancestor?, #clear_accepted_childs, #clear_accepted_connections, #clear_accepted_src_neighbours, #clear_accepted_trg_neighbours, component, component_shapes, #contains?, #contains_style, #create_handles, #descendant?, #do_alignment, #does_not_accept_children?, #draw, #draw_selected, #get_absolute_position, #get_accepted_children, #get_accepted_connections, #get_accepted_src_neighbours, #get_accepted_trg_neighbours, #get_assigned_connections, #get_bounding_box, #get_center, #get_child_shapes, #get_children, #get_children_recursively, #get_complete_bounding_box, #get_connection_point, #get_connection_points, #get_custom_dock_point, #get_diagram, #get_grand_parent_shape, #get_h_align, #get_h_border, #get_handle, #get_handles, #get_hover_colour, #get_nearest_connection_point, #get_neighbours, #get_parent_absolute_position, #get_parent_canvas, #get_parent_shape, #get_relative_position, #get_shape_canvas, #get_style, #get_user_data, #get_v_align, #get_v_border, #has_children, #has_selected_parent?, #include_child_shape?, #inside?, #inspect, #intersects?, #is_child_accepted, #is_connection_accepted, #is_managed, #is_manager, #is_src_neighbour_accepted, #is_trg_neighbour_accepted, lines_intersection, #lines_intersection, #move_by, #move_to, #on_begin_drag, #on_begin_handle, #on_child_dropped, #on_dragging, #on_end_drag, #on_end_handle, #on_import, #on_key, #on_left_click, #on_left_double_click, #on_mouse_enter, #on_mouse_leave, #on_mouse_over, #on_right_click, #on_right_double_click, #refresh, #refresh_rect, #remove_connection_point, #remove_handle, #remove_style, #scale, #scale_children, #select, #selected?, #set_custom_dock_point, #set_diagram, #set_h_align, #set_h_border, #set_hover_colour, #set_parent_shape, #set_relative_position, #set_style, #set_user_data, #set_v_align, #set_v_border, #show, #show_handles, #to_s, #update, #visible?

Constructor Details

#initialize(pos = Shape::DEFAULT::POSITION, vertices: [], diagram: nil) ⇒ PolygonShape

Constructor.

Parameters:



26
27
28
29
30
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 26

def initialize(pos = Shape::DEFAULT::POSITION, vertices: [], diagram: nil)
  super(pos, Wx::RealPoint.new(1,1), diagram: diagram)
  @connect_to_vertex = DEFAULT::VERTEXCONNECTIONS
  set_vertices(vertices)
end

Instance Method Details

#deserialize_finalizeself (protected)

Deserialize attributes and recalculate rectangle size afterwards.

Returns:

  • (self)


278
279
280
281
282
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 278

def deserialize_finalize
  normalize_vertices
  fit_vertices_to_bounding_box
  self
end

#draw_highlighted(dc) ⇒ Object (protected)

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



248
249
250
251
252
253
254
255
256
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 248

def draw_highlighted(dc)
  # HINT: overload it for custom actions...

  dc.with_pen(Wx::Pen.new(get_hover_colour, 2)) do
    dc.with_brush(fill) do
      draw_polygon_shape(dc)
    end
  end
end

#draw_hover(dc) ⇒ Object (protected)

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



234
235
236
237
238
239
240
241
242
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 234

def draw_hover(dc)
  # HINT: overload it for custom actions...

  dc.with_pen(Wx::Pen.new(get_hover_colour, 1)) do
    dc.with_brush(fill) do
      draw_polygon_shape(dc)
    end
  end
end

#draw_normal(dc) ⇒ Object (protected)

Draw the shape in the normal way. The function can be overridden if necessary.

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



221
222
223
224
225
226
227
228
229
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 221

def draw_normal(dc)
  # HINT: overload it for custom actions...

  dc.with_pen(border) do
    dc.with_brush(fill) do
      draw_polygon_shape(dc)
    end
  end
end

#draw_polygon_shape(dc) ⇒ Object (protected)

Parameters:

  • dc (Wx::DC)

    Reference to the device context where the shape will be drawn to



214
215
216
217
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 214

def draw_polygon_shape(dc)
  pts = get_translated_vertice_points
  dc.draw_polygon(pts)
end

#draw_shadow(dc) ⇒ Object (protected)

Draw shadow under the shape. The function can be overridden if necessary.

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shadow will be drawn to



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 260

def draw_shadow(dc)
  # HINT: overload it for custom actions...

  if fill.get_style != Wx::BrushStyle::BRUSHSTYLE_TRANSPARENT
    dc.with_pen(Wx::TRANSPARENT_PEN) do
      dc.with_brush(get_parent_canvas.get_shadow_fill) do
        offset = get_parent_canvas.get_shadow_offset

        move_by(offset)
        draw_polygon_shape(dc)
        move_by(-offset.x, -offset.y)
      end
    end
  end
end

#fit_bounding_box_to_verticesObject (protected)

Scale the bounding rectangle to fit all polygons vertices.



173
174
175
176
177
178
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 173

def fit_bounding_box_to_vertices
  minx, miny, maxx, maxy = get_extents

  rect_size.x = maxx - minx
  rect_size.y = maxy - miny
end

#fit_to_childrenObject

Resize the rectangle to bound all child shapes. The function can be overridden if necessary.



66
67
68
69
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 66

def fit_to_children
  super
  fit_vertices_to_bounding_box
end

#fit_vertices_to_bounding_boxObject (protected)

Scale polygon’s vertices to fit into the rectangle bounding the polygon.



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 158

def fit_vertices_to_bounding_box
  minx, miny, maxx, maxy = get_extents

  width = (maxx - minx)
  height = (maxy - miny)
  sx = rect_size.x/width
  sy = rect_size.y/height

  @vertices.each do |pt|
    pt.x = (width != 0.0) ? pt.x*sx : minx
    pt.y = (height != 0.0) ? pt.y*sy : miny
  end
end

#get_border_point(start, end_pt) ⇒ Wx::RealPoint

Returns Intersection point.

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 76

def get_border_point(start, end_pt)
  # HINT: override it for custom actions ...
  start = start.to_real_point; end_pt.to_real_point
  return get_center if @vertices.empty?

  pts = get_translated_vertices
  intersection = start

  if @connect_to_vertex
    intersection = pts.shift
    min_dist = intersection.distance_to(end_pt)
    pts.each do |pt|
      tmp_min_dist = pt.distance_to(end_pt)
      if tmp_min_dist < min_dist
        min_dist = tmp_min_dist
        intersection = pt
      end
    end

    intersection
  else
    success = false
    min_dist = 0.0
    pts.each_with_index do |pt, i|
      if (tmp_intersection = Wx::SF::Shape.lines_intersection(pt, pts[(i+1) % pts.size], start, end_pt))
        if !success
          min_dist = intersection.distance_to(end_pt)
          intersection = tmp_intersection
        else
          tmp_min_dist = intersection.distance_to(end_pt)
          if tmp_min_dist < min_dist
            min_dist = tmp_min_dist
            intersection = tmp_intersection
          end
        end
        success = true
      end
    end

    success ? intersection : get_center
  end
end

#get_extentsArray(Float,Float,Float,Float) (protected)

Get polygon extents.

Returns:

  • (Array(Float,Float,Float,Float))

    Positions of the left, top, right and bottom side of polygon’s bounding box



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 182

def get_extents
  return [0.0,0.0,0.0,0.0] if @vertices.empty?

  @vertices.inject(nil) do |exts, pt|
    if exts
      exts[0] = pt.x if pt.x < exts[0]
      exts[1] = pt.y if pt.y < exts[1]
      exts[2] = pt.x if pt.x > exts[2]
      exts[3] = pt.y if pt.y > exts[3]
    else
      exts = [pt.x, pt.y, pt.x, pt.y]
    end
    exts
  end
end

#get_translated_vertice_pointsArray<Wx::Point> (protected)

Returns pts Array of translated polygon’s vertices.

Returns:



207
208
209
210
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 207

def get_translated_vertice_points
  abs_pos = get_absolute_position.to_point
  @vertices.collect { |pt| abs_pos + pt.to_point }
end

#get_translated_verticesArray<Wx::RealPoint> (protected)

Get absolute positions of the polygon’s vertices.

Returns:



200
201
202
203
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 200

def get_translated_vertices
  abs_pos = get_absolute_position
  @vertices.collect { |pt| abs_pos + pt }
end

#is_connected_to_vertexBoolean Also known as: connected_to_vertex?, get_connect_to_vertex

Get status of connecting mode.

Returns:

  • (Boolean)

    true if the line shapes will be connected to the polygon’s vertices



43
44
45
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 43

def is_connected_to_vertex
  @connect_to_vertex
end

#normalize_verticesObject (protected)

will be located in the origin.



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 143

def normalize_vertices
  # move all vertices so the polygon's relative bounding box will be located in the origin

  minx, miny, _, _ = get_extents

  dx = minx*(-1)
  dy = miny*(-1)

  @vertices.each do |pt|
    pt.x += dx
    pt.y += dy
  end
end

#on_handle(handle) ⇒ Object

Parameters:



124
125
126
127
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 124

def on_handle(handle)
  super
  fit_vertices_to_bounding_box
end

#scale_rectangle(x, y) ⇒ Object (protected)

Scale the rectangle size for this shape.

Parameters:

  • x (Float)

    Horizontal scale factor

  • y (Float)

    Vertical scale factor



134
135
136
137
138
139
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 134

def scale_rectangle(x, y)
  rect_size.x *= x
  rect_size.y *= y

  fit_vertices_to_bounding_box
end

#set_connect_to_vertex(enable) ⇒ Object Also known as: connect_to_vertex=

Set connecting mode. line shapes to the polygon’s vertices, otherwise the lines will be connected to the nearest point of the shape’s border.

Parameters:

  • enable (Boolean)

    Set this parameter to true if you want to connect



36
37
38
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 36

def set_connect_to_vertex(enable)
  @connect_to_vertex = enable
end

#set_vertices(pts) ⇒ Object

Parameters:



51
52
53
54
55
56
# File 'lib/wx/shapes/shapes/polygon_shape.rb', line 51

def set_vertices(pts)
  ::Kernel.raise ArgumentError, 'Expected an array of Wx::RealPoint' unless pts.all? { |pt| pt.is_a?(Wx::RealPoint) }
  @vertices = pts.collect { |pt| pt.dup }
  normalize_vertices
  fit_bounding_box_to_vertices
end