Class: Glimmer::SWT::Custom::Shape::Path

Inherits:
Glimmer::SWT::Custom::Shape show all
Includes:
PathSegment
Defined in:
lib/glimmer/swt/custom/shape/path.rb

Overview

Represents a path to be drawn on a control/widget/canvas/display That is because Shape is drawn on a parent as graphics and doesn’t have an SWT widget for itself swt_path is not guaranteed to have any object in it till after rendering

Direct Known Subclasses

Cubic, Quad

Constant Summary

Constants inherited from Glimmer::SWT::Custom::Shape

String

Instance Attribute Summary collapse

Attributes inherited from Glimmer::SWT::Custom::Shape

#args, #drawable, #extent, #name, #options, #parent, #properties, #shapes

Instance Method Summary collapse

Methods included from PathSegment

#add_to_geometry, #add_to_swt_path, #default_connected_path_segment_arg_count, #default_path_segment_arg_count, #first_path_segment?, #part_of_path?, #path, #previous_path_segment, #previous_point_connected?, #root_path

Methods inherited from Glimmer::SWT::Custom::Shape

#absolute_x, #absolute_y, #all_parent_properties, #amend_method_name_options_based_on_properties!, #apply_property_arg_conversions, #apply_shape_arg_conversions!, #apply_shape_arg_defaults!, arg_options, #background_pattern_args, #calculate_paint_args!, #calculated_args?, #calculated_args_changed!, #calculated_args_changed_for_defaults!, #calculated_height, #calculated_width, #calculated_x, #calculated_y, #clear_shapes, #composite?, #container?, #content, #convert_properties!, #converted_properties, create, #current_parameter_name?, #default_height, #default_height?, #default_width, #default_width?, #default_x, #default_x?, #default_y, #default_y?, #draw?, #ensure_extent, #expanded_shapes, #fill?, flyweight_method_names, flyweight_patterns, #foreground_pattern_args, gc_instance_methods, #get_attribute, #get_data, #get_parameter_attribute, #gradient?, #has_attribute?, #has_some_background?, #has_some_foreground?, #height_delta, #height_delta=, #inspect, keywords, #location, #location_parameter_names, #max_height, #max_height?, #max_width, #max_width?, #method_missing, method_name, #paint, #paint_children, #paint_self, #parameter_index, #parameter_name?, #parent_shape_composites, #parent_shape_containers, #parent_shapes, #pattern, pattern, #pattern_args, #possible_parameter_names, #post_add_content, #respond_to?, #round?, #set_attribute, #set_data, #set_parameter_attribute, #tolerate_shape_extra_args!, valid?, #width_delta, #width_delta=, #x_delta, #x_delta=, #x_end, #y_delta, #y_delta=, #y_end

Methods included from Properties

attribute_getter, #attribute_getter, attribute_setter, #attribute_setter, normalized_attribute, #normalized_attribute, ruby_attribute_getter, #ruby_attribute_setter, ruby_attribute_setter

Constructor Details

#initialize(parent, keyword, *args, &property_block) ⇒ Path

Returns a new instance of Path.



43
44
45
46
47
# File 'lib/glimmer/swt/custom/shape/path.rb', line 43

def initialize(parent, keyword, *args, &property_block)
  super
  @path_segments = []
  @uncalculated_path_segments = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::SWT::Custom::Shape

Instance Attribute Details

#calculated_path_argsObject

Returns the value of attribute calculated_path_args.



41
42
43
# File 'lib/glimmer/swt/custom/shape/path.rb', line 41

def calculated_path_args
  @calculated_path_args
end

#path_segmentsObject (readonly)

Returns the value of attribute path_segments.



40
41
42
# File 'lib/glimmer/swt/custom/shape/path.rb', line 40

def path_segments
  @path_segments
end

#swt_pathObject (readonly)

Returns the value of attribute swt_path.



40
41
42
# File 'lib/glimmer/swt/custom/shape/path.rb', line 40

def swt_path
  @swt_path
end

Instance Method Details

#add_shape(shape) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/glimmer/swt/custom/shape/path.rb', line 53

def add_shape(shape)
  if shape.is_a?(PathSegment)
    Glimmer::SWT::DisplayProxy.instance.auto_exec do
      @path_segments << shape
      @uncalculated_path_segments << shape
    end
  else
    super
  end
end

#boundsObject



147
148
149
150
151
152
153
154
# File 'lib/glimmer/swt/custom/shape/path.rb', line 147

def bounds
  if @path_segments != @bounds_path_segments
    @bounds_path_segments = @path_segments
    shape_bounds = geometry.getBounds2D
    @bounds = org.eclipse.swt.graphics.Rectangle.new(shape_bounds.x, shape_bounds.y, shape_bounds.width, shape_bounds.height)
  end
  @bounds
end

#calculate_args!Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/glimmer/swt/custom/shape/path.rb', line 115

def calculate_args!
  new_swt_path = @swt_path.nil? || !@calculated_paint_args || !@calculated_path_args
  if new_swt_path
    Glimmer::SWT::DisplayProxy.instance.auto_exec do
      @swt_path&.dispose
      @swt_path = org.eclipse.swt.graphics.Path.new(Glimmer::SWT::DisplayProxy.instance.swt_display)
      @uncalculated_path_segments = @path_segments.dup
    end
  end
  # TODO recreate @swt_path only if one of the children get disposed (must notify parent on dispose)
  @args = [@swt_path]
  @uncalculated_path_segments.dup.each do |path_segment|
    Glimmer::SWT::DisplayProxy.instance.auto_exec do
      path_segment.add_to_swt_path(@swt_path)
      @uncalculated_path_segments.delete(path_segment)
    end
  end
  @calculated_path_args = true
  if new_swt_path
    @path_calculated_args = super
  else
    @path_calculated_args
  end
rescue => e
  Glimmer::Config.logger.error {e.full_message}
  @args
end

#clearObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/glimmer/swt/custom/shape/path.rb', line 95

def clear
  Glimmer::SWT::DisplayProxy.instance.auto_exec do
    @path_segments.each { |path_segments| path_segments.class == Path && path_segments.dispose }
    @path_segments.clear
    @uncalculated_path_segments = @path_segments.dup
    @swt_path&.dispose
    @swt_path = nil
    @args = []
    calculated_args_changed!(children: false)
    drawable.redraw unless drawable.is_a?(ImageProxy)
  end
end

#contain?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/glimmer/swt/custom/shape/path.rb', line 64

def contain?(x, y)
  makeshift_gc = org.eclipse.swt.graphics.GC.new(Glimmer::SWT::DisplayProxy.instance.swt_display)
  @swt_path.contains(x.to_f, y.to_f, makeshift_gc, false)
end

#dispose(redraw: true) ⇒ Object



108
109
110
111
112
113
# File 'lib/glimmer/swt/custom/shape/path.rb', line 108

def dispose(redraw: true)
  Glimmer::SWT::DisplayProxy.instance.auto_exec do
    clear if self.class == Path
    super(redraw: redraw) if (parent.is_a?(Shape) && (!parent.is_a?(PathSegment) || !parent.part_of_path?)) || parent.is_a?(Drawable)
  end
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


223
224
225
# File 'lib/glimmer/swt/custom/shape/path.rb', line 223

def eql?(other)
  (other.class == Path) && geometry.equals(other && other.respond_to?(:geometry) && other.geometry)
end

#geometryObject



187
188
189
190
191
192
193
194
195
196
# File 'lib/glimmer/swt/custom/shape/path.rb', line 187

def geometry
  if @path_segments != @geometry_path_segments
    @geometry_path_segments = @path_segments
    @geometry = Java::JavaAwtGeom::Path2D::Double.new
    @path_segments.each do |path_segment|
      path_segment.add_to_geometry(@geometry)
    end
  end
  @geometry
end

#hashObject



228
229
230
# File 'lib/glimmer/swt/custom/shape/path.rb', line 228

def hash
  geometry.hashCode
end

#heightObject



183
184
185
# File 'lib/glimmer/swt/custom/shape/path.rb', line 183

def height
  size.y
end

#include?(x, y, filled: nil) ⇒ Boolean

checks if drawn or filled rectangle includes the point denoted by x and y (if drawn, it only returns true if point lies on the edge)

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/glimmer/swt/custom/shape/path.rb', line 74

def include?(x, y, filled: nil)
  filled = filled? if filled.nil?
  makeshift_gc = org.eclipse.swt.graphics.GC.new(Glimmer::SWT::DisplayProxy.instance.swt_display)
  @swt_path.contains(x.to_f, y.to_f, makeshift_gc, !filled)
end

#irregular?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/glimmer/swt/custom/shape/path.rb', line 80

def irregular?
  true
end

#move_by(x_delta, y_delta) ⇒ Object



143
144
145
# File 'lib/glimmer/swt/custom/shape/path.rb', line 143

def move_by(x_delta, y_delta)
  @path_segments.each {|path_segment| path_segment.move_by(x_delta, y_delta)}
end

#parameter_namesObject



49
50
51
# File 'lib/glimmer/swt/custom/shape/path.rb', line 49

def parameter_names
  [:swt_path]
end

#path_segment_argsObject



202
203
204
# File 'lib/glimmer/swt/custom/shape/path.rb', line 202

def path_segment_args
  @args
end

#path_segment_geometry_argsObject



214
215
216
217
218
219
220
221
# File 'lib/glimmer/swt/custom/shape/path.rb', line 214

def path_segment_geometry_args
  if self.class == Path
    # TODO consider supporting connected true instead of false (2nd arg)
    [geometry, false]
  else
    super
  end
end

#path_segment_geometry_method_nameObject



206
207
208
209
210
211
212
# File 'lib/glimmer/swt/custom/shape/path.rb', line 206

def path_segment_geometry_method_name
  if self.class == Path
    'append'
  else
    super
  end
end

#path_segment_method_nameObject



198
199
200
# File 'lib/glimmer/swt/custom/shape/path.rb', line 198

def path_segment_method_name
  'addPath'
end

#post_dispose_content(path_segment) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/glimmer/swt/custom/shape/path.rb', line 84

def post_dispose_content(path_segment)
  Glimmer::SWT::DisplayProxy.instance.auto_exec do
    @path_segments.delete(path_segment)
    @uncalculated_path_segments = @path_segments.dup
    @swt_path&.dispose
    @swt_path = nil
    @args = []
    calculated_args_changed!(children: false)
  end
end

#sizeObject



156
157
158
159
160
161
162
163
# File 'lib/glimmer/swt/custom/shape/path.rb', line 156

def size
  if @path_segments != @size_path_segments
    @size_path_segments = @path_segments
    shape_bounds = geometry.getBounds2D
    @size = org.eclipse.swt.graphics.Point.new(shape_bounds.width, shape_bounds.height)
  end
  @size
end

#widthObject



179
180
181
# File 'lib/glimmer/swt/custom/shape/path.rb', line 179

def width
  size.x
end

#xObject

Logical x coordinate relative to parent



166
167
168
169
170
# File 'lib/glimmer/swt/custom/shape/path.rb', line 166

def x
  x_value = bounds.x
  x_value -= parent.absolute_x if parent.is_a?(Shape)
  x_value
end

#yObject

Logical y coordinate relative to parent



173
174
175
176
177
# File 'lib/glimmer/swt/custom/shape/path.rb', line 173

def y
  y_value = bounds.y
  y_value -= parent.absolute_y if parent.is_a?(Shape)
  y_value
end