glimmer-dsl-swt 4.18.5.3 → 4.18.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,133 @@
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/swt/custom/shape'
23
+
24
+ module Glimmer
25
+ module SWT
26
+ module Custom
27
+ # Represents a path to be drawn on a control/widget/canvas/display
28
+ # That is because Shape is drawn on a parent as graphics and doesn't have an SWT widget for itself
29
+ class Shape
30
+ # Represents path segments like point, line, quad, and cubic curves
31
+ # Shapes could mix in
32
+ module PathSegment
33
+ def root_path
34
+ current_parent = parent
35
+ until current_parent.class == Path && !current_parent.parent.is_a?(Path)
36
+ current_parent = current_parent.parent
37
+ return current_parent if current_parent.nil?
38
+ end
39
+ current_parent
40
+ end
41
+ def path
42
+ current_parent = parent
43
+ until current_parent.class == Path
44
+ current_parent = current_parent.parent
45
+ return current_parent if current_parent.nil?
46
+ end
47
+ current_parent
48
+ end
49
+ # this is needed to indicate if a shape is part of a path or not (e.g. line and point could be either)
50
+ def part_of_path?
51
+ !!root_path
52
+ end
53
+ # Subclasses must override and implement to indicate method name to invoke on SWT Path object to add segment
54
+ def path_segment_method_name
55
+ nil
56
+ end
57
+ # Subclasses must override and implement to indicate args to pass when invoking SWT Path object method
58
+ def path_segment_args
59
+ []
60
+ end
61
+ # Subclasses must override to indicate expected complete count of args when previous point is NOT connected (e.g. 4 for line, 6 for quad, 8 for cubic)
62
+ def default_path_segment_arg_count
63
+ end
64
+ # Subclasses must override to indicate expected count of args when previous point IS connected (e.g. 2 for line, 4 for quad, 6 for cubic)
65
+ def default_connected_path_segment_arg_count
66
+ end
67
+ # Subclasses may override to provide name of method to invoke for geometry object obtained from the Java AWT library java.awt.geom.Path2D.Double (e.g. curveTo vs cubicTo)
68
+ def path_segment_geometry_method_name
69
+ path_segment_method_name
70
+ end
71
+ # Subclasses must override and implement to indicate args to pass when invoking SWT Path object method
72
+ def path_segment_geometry_args
73
+ path_segment_args
74
+ end
75
+ # Subclasses must override to indicate otherwise
76
+ def previous_point_connected?
77
+ true
78
+ end
79
+
80
+ def dispose
81
+ parent.post_dispose_content(self) if parent.is_a?(Path)
82
+ super if !part_of_path?
83
+ drawable.redraw unless drawable.is_a?(ImageProxy)
84
+ end
85
+
86
+ def first_path_segment?
87
+ parent.path_segments.first == self
88
+ end
89
+
90
+ def previous_path_segment
91
+ parent.path_segments[parent.path_segments.index(self) - 1] || self
92
+ end
93
+
94
+ def add_to_swt_path(swt_path)
95
+ if @swt_path != swt_path
96
+ @swt_path = swt_path
97
+ the_path_segment_args = path_segment_args.dup
98
+ if !is_a?(Point) && self.class != Path
99
+ if !previous_point_connected?
100
+ if the_path_segment_args.count == default_path_segment_arg_count
101
+ point = the_path_segment_args.shift, the_path_segment_args.shift
102
+ @swt_path.moveTo(*point)
103
+ elsif first_path_segment?
104
+ point = the_path_segment_args[0..1]
105
+ @swt_path.moveTo(*point)
106
+ end
107
+ end
108
+ end
109
+ @swt_path.send(path_segment_method_name, *the_path_segment_args)
110
+ end
111
+ end
112
+
113
+ def add_to_geometry(geometry)
114
+ the_path_segment_geometry_args = path_segment_geometry_args.dup
115
+ if !is_a?(Point) && self.class != Path
116
+ if !previous_point_connected?
117
+ if the_path_segment_geometry_args.count == default_path_segment_arg_count
118
+ point = the_path_segment_geometry_args.shift, the_path_segment_geometry_args.shift
119
+ geometry.moveTo(point[0], point[1])
120
+ elsif first_path_segment?
121
+ point = the_path_segment_geometry_args[0..1]
122
+ geometry.moveTo(point[0], point[1])
123
+ end
124
+ end
125
+ end
126
+ geometry.send(path_segment_geometry_method_name, *the_path_segment_geometry_args)
127
+ end
128
+
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -20,6 +20,7 @@
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
22
  require 'glimmer/swt/custom/shape'
23
+ require 'glimmer/swt/custom/shape/path_segment'
23
24
  require 'glimmer/swt/swt_proxy'
24
25
  require 'glimmer/swt/display_proxy'
25
26
  require 'glimmer/swt/color_proxy'
@@ -33,15 +34,55 @@ module Glimmer
33
34
  # That is because Shape is drawn on a parent as graphics and doesn't have an SWT widget for itself
34
35
  class Shape
35
36
  class Point < Shape
37
+ include PathSegment
38
+
36
39
  def parameter_names
37
40
  [:x, :y]
38
41
  end
39
42
 
43
+ def width
44
+ 1
45
+ end
46
+
47
+ def height
48
+ 1
49
+ end
50
+
40
51
  def include?(x, y)
41
52
  # give it some fuzz (helps makes mouse clicking easier)
42
53
  x.to_i.between?(self.absolute_x.to_i - 2, self.absolute_x.to_i + 2) && y.to_i.between?(self.absolute_y.to_i - 2, self.absolute_y.to_i + 2)
43
54
  end
44
55
  alias contain? include?
56
+
57
+ def path_segment_method_name
58
+ 'addRectangle'
59
+ end
60
+
61
+ def path_segment_args
62
+ @args + [1, 1]
63
+ end
64
+
65
+ def path_segment_geometry_method_name
66
+ 'moveTo'
67
+ end
68
+
69
+ def path_segment_geometry_args
70
+ @args
71
+ end
72
+
73
+ def previous_point_connected?
74
+ false
75
+ end
76
+
77
+ def eql?(other)
78
+ other.is_a?(Point) && x == (other && other.respond_to?(:x) && other.x) && y == (other && other.respond_to?(:y) && other.y)
79
+ end
80
+ alias == eql?
81
+
82
+ def hash
83
+ [x, y].hash
84
+ end
85
+
45
86
  end
46
87
  end
47
88
  end
@@ -62,16 +62,6 @@ module Glimmer
62
62
  x_array.zip(y_array)
63
63
  end
64
64
 
65
- # Logical x coordinate. Always assumes the first point in the polygon to be the x coordinate.
66
- def x
67
- x_array.first
68
- end
69
-
70
- # Logical y coordinate. Always assumes the first point in the polygon to be the y coordinate.
71
- def y
72
- y_array.first
73
- end
74
-
75
65
  def absolute_point_array
76
66
  if parent.is_a?(Shape)
77
67
  point_array.each_with_index.map do |coordinate, i|
@@ -98,15 +88,74 @@ module Glimmer
98
88
  absolute_x_array.zip(absolute_y_array)
99
89
  end
100
90
 
101
- def include?(x, y)
102
- shape_geometry = java.awt.Polygon.new(absolute_x_array.to_java(:int), absolute_y_array.to_java(:int), point_count)
103
- shape_geometry.contains(x, y)
91
+ def bounds
92
+ the_point_array = point_array
93
+ if the_point_array != @bounds_point_array
94
+ @bounds_point_array = point_array
95
+ shape_bounds = geometry.getBounds2D
96
+ @bounds = org.eclipse.swt.graphics.Rectangle.new(shape_bounds.x, shape_bounds.y, shape_bounds.width, shape_bounds.height)
97
+ end
98
+ @bounds
99
+ end
100
+
101
+ def size
102
+ the_point_array = point_array
103
+ if the_point_array != @size_point_array
104
+ @size_point_array = point_array
105
+ shape_bounds = geometry.getBounds2D
106
+ @size = org.eclipse.swt.graphics.Point.new(shape_bounds.width, shape_bounds.height)
107
+ end
108
+ @size
109
+ end
110
+
111
+ def geometry
112
+ the_point_array = point_array
113
+ if the_point_array != @geometry_point_array
114
+ @geometry_point_array = point_array
115
+ @geometry = java.awt.Polygon.new(absolute_x_array.to_java(:int), absolute_y_array.to_java(:int), point_count)
116
+ end
117
+ @geometry
118
+ end
119
+
120
+ # Logical x coordinate relative to parent
121
+ def x
122
+ x_value = bounds.x
123
+ x_value -= parent.absolute_x if parent.is_a?(Shape)
124
+ x_value
125
+ end
126
+
127
+ # Logical y coordinate relative to parent
128
+ def y
129
+ y_value = bounds.y
130
+ y_value -= parent.absolute_y if parent.is_a?(Shape)
131
+ y_value
132
+ end
133
+
134
+ def width
135
+ size.x
136
+ end
137
+
138
+ def height
139
+ size.y
104
140
  end
105
- alias contain? include? # TODO make include do an outer/inner check of edge detection only
106
141
 
142
+ def contain?(x, y)
143
+ geometry.contains(x, y)
144
+ end
145
+
146
+ def include?(x, y)
147
+ comparison_lines = absolute_point_xy_array.zip(absolute_point_xy_array.rotate(1))
148
+ comparison_lines.any? {|line| Line.include?(line.first.first, line.first.last, line.last.first, line.last.last, x, y)}
149
+ end
150
+
107
151
  def move_by(x_delta, y_delta)
108
152
  self.point_array = point_array.each_with_index.map {|coordinate, i| i.even? ? coordinate + x_delta : coordinate + y_delta}
109
153
  end
154
+
155
+ def irregular?
156
+ true
157
+ end
158
+
110
159
  end
111
160
  end
112
161
  end
@@ -62,16 +62,6 @@ module Glimmer
62
62
  x_array.zip(y_array)
63
63
  end
64
64
 
65
- # Logical x coordinate. Always assumes the first point in the polyline to be the x coordinate.
66
- def x
67
- x_array.first
68
- end
69
-
70
- # Logical y coordinate. Always assumes the first point in the polyline to be the y coordinate.
71
- def y
72
- y_array.first
73
- end
74
-
75
65
  def absolute_point_array
76
66
  if parent.is_a?(Shape)
77
67
  point_array.each_with_index.map do |coordinate, i|
@@ -98,6 +88,42 @@ module Glimmer
98
88
  absolute_x_array.zip(absolute_y_array)
99
89
  end
100
90
 
91
+ def bounds
92
+ shape_bounds = geometry.getBounds2D
93
+ org.eclipse.swt.graphics.Rectangle.new(shape_bounds.x, shape_bounds.y, shape_bounds.width, shape_bounds.height)
94
+ end
95
+
96
+ def size
97
+ shape_bounds = geometry.getBounds2D
98
+ org.eclipse.swt.graphics.Point.new(shape_bounds.width, shape_bounds.height)
99
+ end
100
+
101
+ def geometry
102
+ java.awt.Polygon.new(absolute_x_array.to_java(:int), absolute_y_array.to_java(:int), point_count)
103
+ end
104
+
105
+ # Logical x coordinate relative to parent
106
+ def x
107
+ x_value = bounds.x
108
+ x_value -= parent.absolute_x if parent.is_a?(Shape)
109
+ x_value
110
+ end
111
+
112
+ # Logical y coordinate relative to parent
113
+ def y
114
+ y_value = bounds.y
115
+ y_value -= parent.absolute_y if parent.is_a?(Shape)
116
+ y_value
117
+ end
118
+
119
+ def width
120
+ bounds.width
121
+ end
122
+
123
+ def height
124
+ bounds.height
125
+ end
126
+
101
127
  def include?(x, y)
102
128
  comparison_lines = absolute_point_xy_array.zip(absolute_point_xy_array.rotate(1))
103
129
  comparison_lines.pop # ignore last pair since you don't want to compare last point with first point
@@ -108,6 +134,11 @@ module Glimmer
108
134
  def move_by(x_delta, y_delta)
109
135
  self.point_array = point_array.each_with_index.map {|coordinate, i| i.even? ? coordinate + x_delta : coordinate + y_delta}
110
136
  end
137
+
138
+ def irregular?
139
+ true
140
+ end
141
+
111
142
  end
112
143
  end
113
144
  end
@@ -0,0 +1,114 @@
1
+ # Copyright (c) 2007-2021 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'glimmer/swt/custom/shape'
23
+ require 'glimmer/swt/custom/shape/path'
24
+ require 'glimmer/swt/custom/shape/path_segment'
25
+ require 'glimmer/swt/swt_proxy'
26
+ require 'glimmer/swt/display_proxy'
27
+ require 'glimmer/swt/color_proxy'
28
+ require 'glimmer/swt/font_proxy'
29
+ require 'glimmer/swt/transform_proxy'
30
+
31
+ module Glimmer
32
+ module SWT
33
+ module Custom
34
+ # Represents a shape (graphics) to be drawn on a control/widget/canvas/display
35
+ # That is because Shape is drawn on a parent as graphics and doesn't have an SWT widget for itself
36
+ class Shape
37
+ class Quad < Path
38
+ def parameter_names
39
+ [:point_array]
40
+ end
41
+
42
+ def geometry
43
+ the_point_array = point_array
44
+ if the_point_array != @geometry_point_array
45
+ @geometry_point_array = the_point_array
46
+ @geometry = Java::JavaAwtGeom::Path2D::Double.new
47
+ add_to_geometry(@geometry)
48
+ end
49
+ @geometry
50
+ end
51
+
52
+ def contain?(x, y)
53
+ include?(x, y, filled: true)
54
+ end
55
+
56
+ # 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)
57
+ def include?(x, y, filled: nil)
58
+ filled = filled? if filled.nil?
59
+ makeshift_gc = org.eclipse.swt.graphics.GC.new(Glimmer::SWT::DisplayProxy.instance.swt_display)
60
+ swt_path = org.eclipse.swt.graphics.Path.new(Glimmer::SWT::DisplayProxy.instance.swt_display)
61
+ the_path_segment_args = path_segment_args.dup
62
+ if previous_point_connected?
63
+ the_previous_path_segment = previous_path_segment
64
+ swt_path.moveTo(the_previous_path_segment.x, the_previous_path_segment.y)
65
+ else
66
+ swt_path.moveTo(the_path_segment_args.shift, the_path_segment_args.shift)
67
+ end
68
+ swt_path.quadTo(*the_path_segment_args)
69
+ swt_path.contains(x.to_f, y.to_f, makeshift_gc, !filled)
70
+ ensure
71
+ swt_path.dispose
72
+ end
73
+
74
+ def move_by(x_delta, y_delta)
75
+ the_point_array = @args.compact
76
+ the_point_array = the_point_array.first if the_point_array.first.is_a?(Array)
77
+ self.point_array = the_point_array.each_with_index.map {|coordinate, i| i.even? ? coordinate + x_delta : coordinate + y_delta}
78
+ end
79
+
80
+ def path_segment_method_name
81
+ 'quadTo'
82
+ end
83
+
84
+ def path_segment_args
85
+ # TODO make args auto-infer control points if previous_point_connected is true or if there is only a point_array with 1 point
86
+ @args
87
+ end
88
+
89
+ def default_path_segment_arg_count
90
+ 6
91
+ end
92
+
93
+ def default_connected_path_segment_arg_count
94
+ 4
95
+ end
96
+
97
+ def previous_point_connected?
98
+ @args.compact.count == 4 && !first_path_segment?
99
+ end
100
+
101
+ def eql?(other)
102
+ other.is_a?(Quad) && point_array == (other && other.respond_to?(:point_array) && other.point_array)
103
+ end
104
+ alias == eql?
105
+
106
+ def hash
107
+ point_array.hash
108
+ end
109
+
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end