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

Inherits:
Object
  • Object
show all
Includes:
Packages, Properties
Defined in:
lib/glimmer/swt/custom/shape.rb,
lib/glimmer/swt/custom/shape/arc.rb,
lib/glimmer/swt/custom/shape/line.rb,
lib/glimmer/swt/custom/shape/oval.rb,
lib/glimmer/swt/custom/shape/text.rb,
lib/glimmer/swt/custom/shape/focus.rb,
lib/glimmer/swt/custom/shape/image.rb,
lib/glimmer/swt/custom/shape/point.rb,
lib/glimmer/swt/custom/shape/polygon.rb,
lib/glimmer/swt/custom/shape/polyline.rb,
lib/glimmer/swt/custom/shape/rectangle.rb

Overview

Represents a shape (graphics) 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

Direct Known Subclasses

Arc, Focus, Image, Line, Oval, Point, Polygon, Polyline, Rectangle, Text

Defined Under Namespace

Classes: Arc, Focus, Image, Line, Oval, Point, Polygon, Polyline, Rectangle, Text

Constant Summary collapse

String =
Text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Methods included from Packages

included

Constructor Details

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

Returns a new instance of Shape.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/glimmer/swt/custom/shape.rb', line 104

def initialize(parent, keyword, *args, &property_block)
  @parent = parent
  @drawable = @parent.is_a?(Drawable) ? @parent : @parent.drawable
  @name = keyword
  @options = self.class.arg_options(args, extract: true)
  @method_name = self.class.method_name(keyword, @options)
  @args = args
  @properties = {}
  @shapes = [] # nested shapes
  @options.reject {|key, value| %w[fill gradient round].include?(key.to_s)}.each do |property, property_args|
    @properties[property] = property_args
  end
  @parent.add_shape(self)
  post_add_content if property_block.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



406
407
408
409
410
411
412
413
414
# File 'lib/glimmer/swt/custom/shape.rb', line 406

def method_missing(method_name, *args, &block)
  if method_name.to_s.end_with?('=')
    set_attribute(method_name, *args)
  elsif has_attribute?(method_name)
    get_attribute(method_name)
  else
    super
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



101
102
103
# File 'lib/glimmer/swt/custom/shape.rb', line 101

def args
  @args
end

#drawableObject (readonly)

Returns the value of attribute drawable.



101
102
103
# File 'lib/glimmer/swt/custom/shape.rb', line 101

def drawable
  @drawable
end

#extentObject

Returns the value of attribute extent.



102
103
104
# File 'lib/glimmer/swt/custom/shape.rb', line 102

def extent
  @extent
end

#nameObject (readonly)

Returns the value of attribute name.



101
102
103
# File 'lib/glimmer/swt/custom/shape.rb', line 101

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



101
102
103
# File 'lib/glimmer/swt/custom/shape.rb', line 101

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



101
102
103
# File 'lib/glimmer/swt/custom/shape.rb', line 101

def parent
  @parent
end

#shapesObject (readonly)

Returns the value of attribute shapes.



101
102
103
# File 'lib/glimmer/swt/custom/shape.rb', line 101

def shapes
  @shapes
end

Class Method Details

.arg_options(args, extract: false) ⇒ Object



66
67
68
69
70
# File 'lib/glimmer/swt/custom/shape.rb', line 66

def arg_options(args, extract: false)
  arg_options_method = extract ? :pop : :last
  options = args.send(arg_options_method) if args.last.is_a?(Hash)
  options.nil? ? {} : options.symbolize_keys
end

.create(parent, keyword, *args, &property_block) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/glimmer/swt/custom/shape.rb', line 39

def create(parent, keyword, *args, &property_block)
  potential_shape_class_name = keyword.to_s.camelcase(:upper).to_sym
  if constants.include?(potential_shape_class_name)
    const_get(potential_shape_class_name).new(parent, keyword, *args, &property_block)
  else
    new(parent, keyword, *args, &property_block)
  end
end

.flyweight_method_namesObject



84
85
86
# File 'lib/glimmer/swt/custom/shape.rb', line 84

def flyweight_method_names
  @flyweight_method_names ||= {}
end

.flyweight_patternsObject



96
97
98
# File 'lib/glimmer/swt/custom/shape.rb', line 96

def flyweight_patterns
  @flyweight_patterns ||= {}
end

.gc_instance_methodsObject



52
53
54
# File 'lib/glimmer/swt/custom/shape.rb', line 52

def gc_instance_methods
  @gc_instance_methods ||= org.eclipse.swt.graphics.GC.instance_methods.map(&:to_s)
end

.keywordsObject



56
57
58
59
60
61
62
63
64
# File 'lib/glimmer/swt/custom/shape.rb', line 56

def keywords
  @keywords ||= gc_instance_methods.select do |method_name|
    !method_name.end_with?('=') && (method_name.start_with?('draw_') || method_name.start_with?('fill_'))
  end.reject do |method_name|
    gc_instance_methods.include?("#{method_name}=") || gc_instance_methods.include?("set_#{method_name}")
  end.map do |method_name|
    method_name.gsub(/(draw|fill|gradient|round)_/, '')
  end.uniq.compact.to_a
end

.method_name(keyword, method_arg_options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/glimmer/swt/custom/shape.rb', line 72

def method_name(keyword, method_arg_options)
  keyword = keyword.to_s
  method_arg_options = method_arg_options.select {|key, value| %w[fill gradient round].include?(key.to_s)}
  unless flyweight_method_names.keys.include?([keyword, method_arg_options])
    gradient = 'Gradient' if method_arg_options[:gradient]
    round = 'Round' if method_arg_options[:round]
    gc_instance_method_name_prefix = !['polyline', 'point', 'image', 'focus'].include?(keyword) && (method_arg_options[:fill] || method_arg_options[:gradient]) ? 'fill' : 'draw'
    flyweight_method_names[[keyword, method_arg_options]] = "#{gc_instance_method_name_prefix}#{gradient}#{round}#{keyword.capitalize}"
  end
  flyweight_method_names[[keyword, method_arg_options]]
end

.pattern(*args) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/glimmer/swt/custom/shape.rb', line 88

def pattern(*args)
  found_pattern = flyweight_patterns[args]
  if found_pattern.nil? || found_pattern.is_disposed
    found_pattern = flyweight_patterns[args] = org.eclipse.swt.graphics.Pattern.new(*args)
  end
  found_pattern
end

.valid?(parent, keyword, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid?(parent, keyword, *args, &block)
  gc_instance_methods.include?(method_name(keyword, arg_options(args)))
end

Instance Method Details

#absolute_xObject



719
720
721
722
723
724
725
726
# File 'lib/glimmer/swt/custom/shape.rb', line 719

def absolute_x
  x = calculated_x
  if parent.is_a?(Shape)
    parent.absolute_x + x
  else
    x
  end
end

#absolute_yObject



728
729
730
731
732
733
734
735
# File 'lib/glimmer/swt/custom/shape.rb', line 728

def absolute_y
  y = calculated_y
  if parent.is_a?(Shape)
    parent.absolute_y + y
  else
    y
  end
end

#add_shape(shape) ⇒ Object



120
121
122
123
# File 'lib/glimmer/swt/custom/shape.rb', line 120

def add_shape(shape)
  @shapes << shape
  calculated_args_changed_for_default_size!
end

#amend_method_name_options_based_on_properties!Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/glimmer/swt/custom/shape.rb', line 317

def amend_method_name_options_based_on_properties!
  return if @name == 'point'
  if @name != 'text' && @name != 'string' && has_some_background? && !has_some_foreground?
    @options[:fill] = true
  elsif !has_some_background? && has_some_foreground?
    @options[:fill] = false
  elsif @name == 'rectangle' && has_some_background? && has_some_foreground?
    @options[:fill] = true
    @options[:gradient] = true
  end
  if @name == 'rectangle' && @args.size > 4 && @args.last.is_a?(Numeric)
    @options[:round] = true
  end
  @method_name = self.class.method_name(@name, @options)
end

#apply_property_arg_conversions(method_name, property, args) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/glimmer/swt/custom/shape.rb', line 206

def apply_property_arg_conversions(method_name, property, args)
  args = args.dup
  the_java_method = org.eclipse.swt.graphics.GC.java_class.declared_instance_methods.detect {|m| m.name == method_name}
  if the_java_method.parameter_types.first == Color.java_class && args.first.is_a?(RGB)
    args[0] = [args[0].red, args[0].green, args[0].blue]
  end
  if ['setBackground', 'setForeground'].include?(method_name.to_s) && args.first.is_a?(Array)
    args[0] = ColorProxy.new(args[0])
  end
  if args.first.is_a?(Symbol) || args.first.is_a?(::String)
    if the_java_method.parameter_types.first == Color.java_class
      args[0] = ColorProxy.new(args[0])
    end
    if the_java_method.parameter_types.first == Java::int.java_class
      args[0] = SWTProxy.constant(args[0])
    end
  end
  if args.first.is_a?(ColorProxy)
    args[0] = args[0].swt_color
  end
  if (args.first.is_a?(Hash) || args.first.is_a?(FontData)) && the_java_method.parameter_types.first == Font.java_class
    args[0] = FontProxy.new(args[0])
  end
  if args.first.is_a?(FontProxy)
    args[0] = args[0].swt_font
  end
  if args.first.is_a?(TransformProxy)
    args[0] = args[0].swt_transform
  end
  if ['setBackgroundPattern', 'setForegroundPattern'].include?(method_name.to_s)
    @drawable.requires_shape_disposal = true
    args = args.first if args.first.is_a?(Array)
    args.each_with_index do |arg, i|
      arg = ColorProxy.new(arg.red, arg.green, arg.blue) if arg.is_a?(RGB)
      arg = ColorProxy.new(arg) if arg.is_a?(Symbol) || arg.is_a?(::String)
      arg = arg.swt_color if arg.is_a?(ColorProxy)
      args[i] = arg
    end
    @pattern_args ||= {}
    pattern_type = method_name.to_s.match(/set(.+)Pattern/)[1]
    if args.first.is_a?(Pattern)
      new_args = @pattern_args[pattern_type]
    else
      new_args = args.first.is_a?(Display) ? args : ([DisplayProxy.instance.swt_display] + args)
      @pattern_args[pattern_type] = new_args.dup
    end
    args[0] = pattern(*new_args, type: pattern_type)
    args[1..-1] = []
  end
  args
end

#apply_shape_arg_conversions!Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/glimmer/swt/custom/shape.rb', line 258

def apply_shape_arg_conversions!
  if @args.size > 1 && (['polygon', 'polyline'].include?(@name))
    @args[0] = @args.dup
    @args[1..-1] = []
  end
  if @name == 'image'
    if @args.first.is_a?(::String)
      @args[0] = ImageProxy.new(@args[0])
    end
    if @args.first.is_a?(ImageProxy)
      @image = @args[0] = @args[0].swt_image
    end
    if @args.first.nil?
      @image = nil
    end
  end
  if @name == 'text'
    if @args[3].is_a?(Symbol) || @args[3].is_a?(::String)
      @args[3] = [@args[3]]
    end
    if @args[3].is_a?(Array)
      if @args[3].size == 1 && @args[3].first.is_a?(Array)
        @args[3] = @args[3].first
      end
      @args[3] = SWTProxy[*@args[3]]
    end
  end
end

#apply_shape_arg_defaults!Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/glimmer/swt/custom/shape.rb', line 287

def apply_shape_arg_defaults!
  self.x = :default if current_parameter_name?(:x) && x.nil?
  self.y = :default if current_parameter_name?(:y) && y.nil?
  self.dest_x = :default if current_parameter_name?(:dest_x) && dest_x.nil?
  self.dest_y = :default if current_parameter_name?(:dest_y) && dest_y.nil?
  self.width = :default if current_parameter_name?(:width) && width.nil?
  self.height = :default if current_parameter_name?(:height) && height.nil?
  if @name.include?('rectangle') && round? && @args.size.between?(4, 5)
    (6 - @args.size).times {@args << 60}
  elsif @name.include?('rectangle') && gradient? && @args.size == 4
    set_attribute('vertical', true, redraw: false)
  elsif (@name.include?('text') || @name.include?('string')) && !@properties.keys.map(&:to_s).include?('background') && @args.size < 4
    set_attribute('is_transparent', true, redraw: false)
  end
  if @name.include?('image')
    @drawable.requires_shape_disposal = true
  end
end

#background_pattern_argsObject



439
440
441
# File 'lib/glimmer/swt/custom/shape.rb', line 439

def background_pattern_args
  pattern_args(type: 'background')
end

#boundsObject

The bounding box top-left x, y, width, height in absolute positioning



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

def bounds
  org.eclipse.swt.graphics.Rectangle.new(absolute_x, absolute_y, calculated_width, calculated_height)
end

#calculate_paint_args!Object



744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/glimmer/swt/custom/shape.rb', line 744

def calculate_paint_args!
  unless @calculated_paint_args
    if @name == 'pixel'
      @name = 'point'
      # optimized performance calculation for pixel points
      if !@properties[:foreground].is_a?(Color)
        if @properties[:foreground].is_a?(Array)
          @properties[:foreground] = ColorProxy.new(@properties[:foreground], ensure_bounds: false)
        end
        if @properties[:foreground].is_a?(Symbol) || @properties[:foreground].is_a?(::String)
         @properties[:foreground] = ColorProxy.new(@properties[:foreground], ensure_bounds: false)
        end
        if @properties[:foreground].is_a?(ColorProxy)
          @properties[:foreground] = @properties[:foreground].swt_color
        end
      end
    else
      @properties['background'] = [@drawable.background] if fill? && !has_some_background?
      @properties['foreground'] = [@drawable.foreground] if @drawable.respond_to?(:foreground) && draw? && !has_some_foreground?
      # TODO regarding alpha, make sure to reset it to parent stored alpha once we allow setting shape properties on parents directly without shapes
      @properties['alpha'] ||= [255]
      @properties['font'] = [@drawable.font] if @drawable.respond_to?(:font) && draw? && !@properties.keys.map(&:to_s).include?('font')
      # TODO regarding transform, make sure to reset it to parent stored alpha once we allow setting shape properties on parents directly without shapes
      # Also do that with all future-added properties
      @properties['transform'] = [nil] if @drawable.respond_to?(:transform) && !@properties.keys.map(&:to_s).include?('transform')
      @properties.each do |property, args|
        method_name = attribute_setter(property)
        converted_args = apply_property_arg_conversions(method_name, property, args)
        @properties[property] = converted_args
      end
      apply_shape_arg_conversions!
      apply_shape_arg_defaults!
      tolerate_shape_extra_args!
      @calculated_paint_args = true
    end
  end
end

#calculated_argsObject

args translated to absolute coordinates



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/glimmer/swt/custom/shape.rb', line 551

def calculated_args
  return @args if !default_x? && !default_y? && !default_width? && !default_height? && parent.is_a?(Drawable)
  # Note: Must set x and move_by because not all shapes have a real x and some must translate all their points with move_by
  # TODO change that by setting a bounding box for all shapes with a calculated top-left x, y and
  # a setter that does the moving inside them instead so that I could rely on absolute_x and absolute_y
  # here to get the job done of calculating absolute args
  @perform_redraw = false
  original_x = nil
  original_y = nil
  original_width = nil
  original_height = nil
  if default_width?
    original_width = width
    self.width = default_width + default_width_delta
  end
  if default_height?
    original_height = height
    self.height = default_height + default_height_delta
  end
  if default_x?
    original_x = x
    self.x = default_x + default_x_delta
  end
  if default_y?
    original_y = y
    self.y = default_y + default_y_delta
  end
  if parent.is_a?(Shape)
    @parent_absolute_x = parent.absolute_x
    @parent_absolute_y = parent.absolute_y
    move_by(parent.absolute_x, parent.absolute_y)
    result_args = @args.clone
    move_by(-1*parent.absolute_x, -1*parent.absolute_y)
  else
    result_args = @args.clone
  end
  if original_x
    self.x = original_x
  end
  if original_y
    self.y = original_y
  end
  if original_width
    self.width = original_width
  end
  if original_height
    self.height = original_height
  end
  @perform_redraw = true
  result_args
end

#calculated_args?Boolean

Returns:

  • (Boolean)


546
547
548
# File 'lib/glimmer/swt/custom/shape.rb', line 546

def calculated_args?
  !!@calculated_args
end

#calculated_args_changed!Object



530
531
532
533
534
# File 'lib/glimmer/swt/custom/shape.rb', line 530

def calculated_args_changed!
  # TODO add a children: true option to enable setting to false to avoid recalculating children args
  @calculated_args = nil
  shapes.each(&:calculated_args_changed!)
end

#calculated_args_changed_for_default_size!Object



536
537
538
539
540
541
542
543
544
# File 'lib/glimmer/swt/custom/shape.rb', line 536

def calculated_args_changed_for_default_size!
  @calculated_args = nil if default_width? || default_height?
  if parent.is_a?(Shape)
    parent.calculated_args_changed_for_default_size!
  elsif @content_added && !drawable.is_disposed
    # TODO consider optimizing in the future if needed by ensuring one redraw for all parents in the hierarchy at the end instead of doing one per parent that needs it
    drawable.redraw if !@painting && !drawable.is_a?(ImageProxy)
  end
end

#calculated_heightObject



663
664
665
# File 'lib/glimmer/swt/custom/shape.rb', line 663

def calculated_height
  default_height? ? default_height : height
end

#calculated_widthObject



659
660
661
# File 'lib/glimmer/swt/custom/shape.rb', line 659

def calculated_width
  default_width? ? default_width : width
end

#calculated_xObject



707
708
709
710
711
# File 'lib/glimmer/swt/custom/shape.rb', line 707

def calculated_x
  result = default_x? ? default_x : self.x
  result += default_x_delta
  result
end

#calculated_yObject



713
714
715
716
717
# File 'lib/glimmer/swt/custom/shape.rb', line 713

def calculated_y
  result = default_y? ? default_y : self.y
  result += default_y_delta
  result
end

#contain?(x, y) ⇒ Boolean

Returns if shape contains a point Subclasses (like polygon) may override to indicate if a point x,y coordinates falls inside the shape some shapes may choose to provide a fuzz factor to make usage of this method for mouse clicking more user friendly

Returns:

  • (Boolean)


160
161
162
163
# File 'lib/glimmer/swt/custom/shape.rb', line 160

def contain?(x, y)
  # assume a rectangular filled shape by default (works for several shapes like image, text, and focus)
  x.between?(self.absolute_x, self.absolute_x + calculated_width) && y.between?(self.absolute_y, self.absolute_y + calculated_height)
end

#current_parameter_name?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


352
353
354
# File 'lib/glimmer/swt/custom/shape.rb', line 352

def current_parameter_name?(attribute_name)
  parameter_names.include?(attribute_name.to_s.to_sym)
end

#default_heightObject



648
649
650
651
652
653
654
655
656
657
# File 'lib/glimmer/swt/custom/shape.rb', line 648

def default_height
  y_start_end_pairs = shapes.map do |shape|
    shape_y = shape.default_y? ? 0 : shape.y.to_f
    shape_height = shape.calculated_height.to_f
    [shape_y, shape_y + shape_height]
  end
  min_y = y_start_end_pairs.map(&:first).min.to_f
  max_y = y_start_end_pairs.map(&:last).max.to_f
  max_y - min_y
end

#default_height?Boolean

Returns:

  • (Boolean)


619
620
621
622
623
# File 'lib/glimmer/swt/custom/shape.rb', line 619

def default_height?
  return false unless current_parameter_name?(:height)
  height = self.height
  (height.nil? || height == :default || height == 'default' || (height.is_a?(Array) && (height.first.to_s == :default || height.first.to_s == 'default')))
end

#default_height_deltaObject



682
683
684
685
# File 'lib/glimmer/swt/custom/shape.rb', line 682

def default_height_delta
  return 0 unless default_height? && height.is_a?(Array)
  height[1].to_f
end

#default_height_delta=(delta) ⇒ Object



702
703
704
705
# File 'lib/glimmer/swt/custom/shape.rb', line 702

def default_height_delta=(delta)
  return unless default_height?
  self.height = [:default, delta]
end

#default_widthObject



637
638
639
640
641
642
643
644
645
646
# File 'lib/glimmer/swt/custom/shape.rb', line 637

def default_width
  x_start_end_pairs = shapes.map do |shape|
    shape_x = shape.default_x? ? 0 : shape.x.to_f
    shape_width = shape.calculated_width.to_f
    [shape_x, shape_x + shape_width]
  end
  min_x = x_start_end_pairs.map(&:first).min.to_f
  max_x = x_start_end_pairs.map(&:last).max.to_f
  max_x - min_x
end

#default_width?Boolean

Returns:

  • (Boolean)


613
614
615
616
617
# File 'lib/glimmer/swt/custom/shape.rb', line 613

def default_width?
  return false unless current_parameter_name?(:width)
  width = self.width
  (width.nil? || width == :default || width == 'default' || (width.is_a?(Array) && (width.first.to_s == :default || width.first.to_s == 'default')))
end

#default_width_deltaObject



677
678
679
680
# File 'lib/glimmer/swt/custom/shape.rb', line 677

def default_width_delta
  return 0 unless default_width? && width.is_a?(Array)
  width[1].to_f
end

#default_width_delta=(delta) ⇒ Object



697
698
699
700
# File 'lib/glimmer/swt/custom/shape.rb', line 697

def default_width_delta=(delta)
  return unless default_width?
  self.width = [:default, delta]
end

#default_xObject



625
626
627
628
629
# File 'lib/glimmer/swt/custom/shape.rb', line 625

def default_x
  result = ((parent.size.x - size.x) / 2)
  result += parent.bounds.x - parent.absolute_x if parent.is_a?(Shape)
  result
end

#default_x?Boolean

Returns:

  • (Boolean)


603
604
605
606
# File 'lib/glimmer/swt/custom/shape.rb', line 603

def default_x?
  current_parameter_name?(:x) and
    (x.nil? || x.to_s == 'default' || (x.is_a?(Array) && x.first.to_s == 'default'))
end

#default_x_deltaObject



667
668
669
670
# File 'lib/glimmer/swt/custom/shape.rb', line 667

def default_x_delta
  return 0 unless default_x? && x.is_a?(Array)
  x[1].to_f
end

#default_x_delta=(delta) ⇒ Object



687
688
689
690
# File 'lib/glimmer/swt/custom/shape.rb', line 687

def default_x_delta=(delta)
  return unless default_x?
  self.x = [:default, delta]
end

#default_yObject



631
632
633
634
635
# File 'lib/glimmer/swt/custom/shape.rb', line 631

def default_y
  result = ((parent.size.y - size.y) / 2)
  result += parent.bounds.y - parent.absolute_y if parent.is_a?(Shape)
  result
end

#default_y?Boolean

Returns:

  • (Boolean)


608
609
610
611
# File 'lib/glimmer/swt/custom/shape.rb', line 608

def default_y?
  current_parameter_name?(:y) and
    (y.nil? || y.to_s == 'default' || (y.is_a?(Array) && y.first.to_s == 'default'))
end

#default_y_deltaObject



672
673
674
675
# File 'lib/glimmer/swt/custom/shape.rb', line 672

def default_y_delta
  return 0 unless default_y? && y.is_a?(Array)
  y[1].to_f
end

#default_y_delta=(delta) ⇒ Object



692
693
694
695
# File 'lib/glimmer/swt/custom/shape.rb', line 692

def default_y_delta=(delta)
  return unless default_y?
  self.y = [:default, delta]
end

#dispose(dispose_images: true, dispose_patterns: true) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/glimmer/swt/custom/shape.rb', line 447

def dispose(dispose_images: true, dispose_patterns: true)
  if dispose_patterns
    @background_pattern&.dispose
    @background_pattern = nil
    @foreground_pattern&.dispose
    @foreground_pattern = nil
  end
  if dispose_images
    @image&.dispose
    @image = nil
  end
  @parent.shapes.delete(self)
end

#draw?Boolean Also known as: drawn?

Returns:

  • (Boolean)


125
126
127
# File 'lib/glimmer/swt/custom/shape.rb', line 125

def draw?
  !fill?
end

#ensure_extent(paint_event) ⇒ Object



502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/glimmer/swt/custom/shape.rb', line 502

def ensure_extent(paint_event)
  old_extent = @extent
  if ['text', 'string'].include?(@name)
    extent_args = [string]
    extent_flags = SWTProxy[:draw_transparent] if current_parameter_name?(:is_transparent) && is_transparent
    extent_flags = flags if current_parameter_name?(:flags)
    extent_args << extent_flags unless extent_flags.nil?
    self.extent = paint_event.gc.send("#{@name}Extent", *extent_args)
  end
  if !@extent.nil? && (old_extent&.x != @extent&.x || old_extent&.y != @extent&.y)
    parent.calculated_args_changed_for_default_size! if parent.is_a?(Shape)
  end
end

#expanded_shapesObject



516
517
518
519
520
521
522
523
524
# File 'lib/glimmer/swt/custom/shape.rb', line 516

def expanded_shapes
  if shapes.to_a.any?
    shapes.map do |shape|
      [shape] + shape.expanded_shapes
    end.flatten
  else
    []
  end
end

#fill?Boolean Also known as: filled?

Returns:

  • (Boolean)


130
131
132
# File 'lib/glimmer/swt/custom/shape.rb', line 130

def fill?
  @options[:fill]
end

#foreground_pattern_argsObject



443
444
445
# File 'lib/glimmer/swt/custom/shape.rb', line 443

def foreground_pattern_args
  pattern_args(type: 'foreground')
end

#get_attribute(attribute_name) ⇒ Object



395
396
397
398
399
400
401
402
403
404
# File 'lib/glimmer/swt/custom/shape.rb', line 395

def get_attribute(attribute_name)
  if parameter_name?(attribute_name)
    arg_index = parameter_index(attribute_name)
    @args[arg_index] if arg_index
  elsif (respond_to?(attribute_name, super: true) and respond_to?(ruby_attribute_setter(attribute_name), super: true))
    self.send(attribute_name)
  else
    @properties.symbolize_keys[attribute_name.to_s.to_sym]
  end
end

#gradient?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/glimmer/swt/custom/shape.rb', line 135

def gradient?
  @options[:gradient]
end

#has_attribute?(attribute_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


364
365
366
367
368
# File 'lib/glimmer/swt/custom/shape.rb', line 364

def has_attribute?(attribute_name, *args)
  self.class.gc_instance_methods.include?(attribute_setter(attribute_name)) or
    parameter_name?(attribute_name) or
    (respond_to?(attribute_name, super: true) and respond_to?(ruby_attribute_setter(attribute_name), super: true))
end

#has_some_background?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/glimmer/swt/custom/shape.rb', line 190

def has_some_background?
  @properties.keys.map(&:to_s).include?('background') || @properties.keys.map(&:to_s).include?('background_pattern')
end

#has_some_foreground?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/glimmer/swt/custom/shape.rb', line 194

def has_some_foreground?
  @properties.keys.map(&:to_s).include?('foreground') || @properties.keys.map(&:to_s).include?('foreground_pattern')
end

#include?(x, y) ⇒ Boolean

Returns if shape includes a point. When the shape is filled, this is the same as contain. When the shape is drawn, it only returns true if the point lies on the edge (boundary/border) Subclasses (like polygon) may override to indicate if a point x,y coordinates falls on the edge of a drawn shape or inside a filled shape some shapes may choose to provide a fuzz factor to make usage of this method for mouse clicking more user friendly

Returns:

  • (Boolean)


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

def include?(x, y)
  # assume a rectangular shape by default
  contain?(x, y)
end

#inspectObject

Overriding inspect to avoid printing very long shape hierarchies



738
739
740
741
742
# File 'lib/glimmer/swt/custom/shape.rb', line 738

def inspect
  "#<#{self.class.name}:0x#{self.hash.to_s(16)} args=#{@args.inspect}, properties=#{@properties.inspect}}>"
rescue => e
  "#<#{self.class.name}:0x#{self.hash.to_s(16)}"
end

#location_parameter_namesObject

subclasses may override to specify location parameter names if different from x and y (e.g. all polygon points are location parameters) used in calculating movement changes



340
341
342
# File 'lib/glimmer/swt/custom/shape.rb', line 340

def location_parameter_names
  [:x, :y]
end

#move_by(x_delta, y_delta) ⇒ Object

moves by x delta and y delta. Subclasses must implement provdies a default implementation that assumes moving x and y is sufficient by default (not for polygons though, which must override)



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/glimmer/swt/custom/shape.rb', line 175

def move_by(x_delta, y_delta)
  if respond_to?(:x) && respond_to?(:y) && respond_to?(:x=) && respond_to?(:y=)
    if default_x?
      self.default_x_delta += x_delta
    else
      self.x += x_delta
    end
    if default_y?
      self.default_y_delta += y_delta
    else
      self.y += y_delta
    end
  end
end

#paint(paint_event) ⇒ Object



461
462
463
464
465
466
467
468
469
# File 'lib/glimmer/swt/custom/shape.rb', line 461

def paint(paint_event)
  paint_children(paint_event) if default_width? || default_height?
  paint_self(paint_event)
  shapes.each(&:calculated_args_changed!) if default_width? || default_height?
  paint_children(paint_event)
rescue => e
  Glimmer::Config.logger.error {"Error encountered in painting shape (#{self.inspect}) with calculated args (#{@calculated_args}) and args (#{@args})"}
  Glimmer::Config.logger.error {e.full_message}
end

#paint_children(paint_event) ⇒ Object



496
497
498
499
500
# File 'lib/glimmer/swt/custom/shape.rb', line 496

def paint_children(paint_event)
  shapes.to_a.each do |shape|
    shape.paint(paint_event)
  end
end

#paint_self(paint_event) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/glimmer/swt/custom/shape.rb', line 471

def paint_self(paint_event)
  @painting = true
  calculate_paint_args!
  @properties.each do |property, args|
    method_name = attribute_setter(property)
    # TODO consider optimization of not setting a background/foreground/font if it didn't change from last shape
    paint_event.gc.send(method_name, *args)
    if property == 'transform' && args.first.is_a?(TransformProxy)
      args.first.swt_transform.dispose
    end
  end
  ensure_extent(paint_event)
  if !@calculated_args || parent_shape_absolute_location_changed?
    @calculated_args = calculated_args
  end
  # paint unless parent's calculated args are not calculated yet, meaning it is about to get painted and trigger a paint on this child anyways
  paint_event.gc.send(@method_name, *@calculated_args) unless parent.is_a?(Shape) && !parent.calculated_args?
  @painting = false
rescue => e
  Glimmer::Config.logger.error {"Error encountered in painting shape (#{self.inspect}) with calculated args (#{@calculated_args}) and args (#{@args})"}
  Glimmer::Config.logger.error {e.full_message}
ensure
  @painting = false
end

#parameter_index(attribute_name) ⇒ Object



356
357
358
# File 'lib/glimmer/swt/custom/shape.rb', line 356

def parameter_index(attribute_name)
  parameter_names.index(attribute_name.to_s.to_sym)
end

#parameter_name?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


348
349
350
# File 'lib/glimmer/swt/custom/shape.rb', line 348

def parameter_name?(attribute_name)
  possible_parameter_names.map(&:to_s).include?(ruby_attribute_getter(attribute_name))
end

#parameter_namesObject

parameter names for arguments to pass to SWT GC.xyz method for rendering shape (e.g. draw_image(image, x, y) yields :image, :x, :y parameter names)



334
335
336
# File 'lib/glimmer/swt/custom/shape.rb', line 334

def parameter_names
  []
end

#parent_shape_absolute_location_changed?Boolean

Returns:

  • (Boolean)


526
527
528
# File 'lib/glimmer/swt/custom/shape.rb', line 526

def parent_shape_absolute_location_changed?
  (parent.is_a?(Shape) && (parent.absolute_x != @parent_absolute_x || parent.absolute_y != @parent_absolute_y))
end

#pattern(*args, type: nil) ⇒ Object



426
427
428
429
430
431
432
433
# File 'lib/glimmer/swt/custom/shape.rb', line 426

def pattern(*args, type: nil)
  instance_variable_name = "@#{type}_pattern"
  the_pattern = instance_variable_get(instance_variable_name)
  if the_pattern.nil? || the_pattern.is_disposed
    the_pattern = self.class.pattern(*args)
  end
  the_pattern
end

#pattern_args(type: nil) ⇒ Object



435
436
437
# File 'lib/glimmer/swt/custom/shape.rb', line 435

def pattern_args(type: nil)
  @pattern_args && @pattern_args[type.to_s.capitalize]
end

#possible_parameter_namesObject



344
345
346
# File 'lib/glimmer/swt/custom/shape.rb', line 344

def possible_parameter_names
  parameter_names
end

#post_add_contentObject



198
199
200
201
202
203
204
# File 'lib/glimmer/swt/custom/shape.rb', line 198

def post_add_content
  unless @content_added
    amend_method_name_options_based_on_properties!
    @drawable.setup_shape_painting unless @drawable.is_a?(ImageProxy)
    @content_added = true
  end
end

#respond_to?(method_name, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


416
417
418
419
420
421
422
423
424
# File 'lib/glimmer/swt/custom/shape.rb', line 416

def respond_to?(method_name, *args, &block)
  options = args.last if args.last.is_a?(Hash)
  super_invocation = options && options[:super]
  if !super_invocation && has_attribute?(method_name)
    true
  else
    super
  end
end

#round?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/glimmer/swt/custom/shape.rb', line 139

def round?
  @options[:round]
end

#set_attribute(attribute_name, *args) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/glimmer/swt/custom/shape.rb', line 370

def set_attribute(attribute_name, *args)
  options = args.last if args.last.is_a?(Hash)
  args.pop if !options.nil? && !options[:redraw].nil?
  perform_redraw = @perform_redraw
  perform_redraw = options[:redraw] if perform_redraw.nil? && !options.nil?
  perform_redraw = true if perform_redraw.nil?
  if parameter_name?(attribute_name)
    set_parameter_attribute(attribute_name, *args)
  elsif (respond_to?(attribute_name, super: true) and respond_to?(ruby_attribute_setter(attribute_name), super: true))
    self.send(ruby_attribute_setter(attribute_name), *args)
  else
    @properties[ruby_attribute_getter(attribute_name)] = args
  end
  if @content_added && perform_redraw && !drawable.is_disposed
    @calculated_paint_args = false
    attribute_name = ruby_attribute_getter(attribute_name)
    @calculated_args = nil if location_parameter_names.map(&:to_s).include?(attribute_name)
    if ['width', 'height'].include?(attribute_name)
      calculated_args_changed_for_default_size!
    end
    # TODO consider redrawing an image proxy's gc in the future
    drawable.redraw unless drawable.is_a?(ImageProxy)
  end
end

#set_parameter_attribute(attribute_name, *args) ⇒ Object



360
361
362
# File 'lib/glimmer/swt/custom/shape.rb', line 360

def set_parameter_attribute(attribute_name, *args)
  @args[parameter_index(ruby_attribute_getter(attribute_name))] = args.size == 1 ? args.first : args
end

#sizeObject

The bounding box width and height (as a Point object with x being width and y being height)



149
150
151
# File 'lib/glimmer/swt/custom/shape.rb', line 149

def size
  org.eclipse.swt.graphics.Point.new(calculated_width, calculated_height)
end

#tolerate_shape_extra_args!Object

Tolerates shape extra args added by user by mistake (e.g. happens when switching from round rectangle to a standard one without removing all extra args)



308
309
310
311
312
313
314
315
# File 'lib/glimmer/swt/custom/shape.rb', line 308

def tolerate_shape_extra_args!
  the_java_method_arg_count = org.eclipse.swt.graphics.GC.java_class.declared_instance_methods.select do |m|
    m.name == @method_name.camelcase(:lower)
  end.map(&:parameter_types).map(&:size).max
  if the_java_method_arg_count && @args.to_a.size > the_java_method_arg_count
    @args[the_java_method_arg_count..-1] = []
  end
end