glimmer-dsl-gtk 0.0.2 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +40 -0
- data/README.md +1072 -27
- data/VERSION +1 -1
- data/glimmer-dsl-gtk.gemspec +0 -0
- data/images/breaking-blue-wave.png +0 -0
- data/lib/glimmer/dsl/gtk/observe_expression.rb +35 -0
- data/lib/glimmer/gtk/shape/arc_negative.rb +70 -0
- data/lib/glimmer/gtk/shape/path.rb +33 -0
- data/lib/glimmer/gtk/shape/square.rb +76 -0
- data/lib/glimmer/gtk/shape.rb +56 -10
- data/lib/glimmer/gtk/transformable.rb +93 -0
- data/lib/glimmer/gtk/widget_proxy/drawing_area_proxy.rb +16 -0
- data/lib/glimmer/gtk/widget_proxy.rb +2 -2
- data/samples/cairo/arc.rb +44 -0
- data/samples/cairo/arc_negative.rb +44 -0
- data/samples/cairo/clip.rb +34 -0
- data/samples/cairo/clip_image.rb +28 -0
- data/samples/cairo/curve_to.rb +39 -0
- data/samples/cairo/dashes.rb +30 -0
- data/samples/cairo/fill_and_stroke2.rb +36 -0
- data/samples/cairo/fill_style.rb +43 -0
- data/samples/cairo/gradient.rb +31 -0
- data/samples/cairo/image.rb +23 -0
- data/samples/cairo/image_gradient.rb +32 -0
- data/samples/cairo/multi_segment_caps.rb +27 -0
- data/samples/cairo/rounded_rectangle.rb +20 -0
- data/samples/cairo/set_line_cap.rb +53 -0
- data/samples/cairo/set_line_join.rb +43 -0
- data/samples/elaborate/tetris/model/block.rb +48 -0
- data/samples/elaborate/tetris/model/game.rb +320 -0
- data/samples/elaborate/tetris/model/past_game.rb +39 -0
- data/samples/elaborate/tetris/model/tetromino.rb +329 -0
- data/samples/elaborate/tetris.rb +338 -0
- data/samples/hello/hello_drawing_area.rb +1 -3
- data/samples/hello/hello_drawing_area_manual.rb +20 -21
- metadata +29 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
data/glimmer-dsl-gtk.gemspec
CHANGED
Binary file
|
Binary file
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Copyright (c) 2021-2022 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/dsl/static_expression'
|
23
|
+
require 'glimmer/dsl/top_level_expression'
|
24
|
+
require 'glimmer/dsl/observe_expression'
|
25
|
+
|
26
|
+
module Glimmer
|
27
|
+
module DSL
|
28
|
+
module Gtk
|
29
|
+
class ObserveExpression < StaticExpression
|
30
|
+
include TopLevelExpression
|
31
|
+
include Glimmer::DSL::ObserveExpression
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# Copyright (c) 2021 Andy Maleh
|
2
|
+
#
|
3
|
+
# GNU LESSER GENERAL PUBLIC LICENSE
|
4
|
+
# Version 3, 29 June 2007
|
5
|
+
#
|
6
|
+
# Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
7
|
+
#
|
8
|
+
# Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
9
|
+
#
|
10
|
+
# This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
11
|
+
#
|
12
|
+
# 0. Additional Definitions.
|
13
|
+
# As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
14
|
+
#
|
15
|
+
# “The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
16
|
+
#
|
17
|
+
# An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
18
|
+
#
|
19
|
+
# A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
20
|
+
#
|
21
|
+
# The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
22
|
+
#
|
23
|
+
# The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
24
|
+
#
|
25
|
+
# 1. Exception to Section 3 of the GNU GPL.
|
26
|
+
# You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
27
|
+
#
|
28
|
+
# 2. Conveying Modified Versions.
|
29
|
+
# If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
30
|
+
#
|
31
|
+
# a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
32
|
+
# b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
33
|
+
# 3. Object Code Incorporating Material from Library Header Files.
|
34
|
+
# The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
35
|
+
#
|
36
|
+
# a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
37
|
+
# b) Accompany the object code with a copy of the GNU GPL and this license document.
|
38
|
+
# 4. Combined Works.
|
39
|
+
# You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
40
|
+
#
|
41
|
+
# a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
42
|
+
# b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
43
|
+
# c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
44
|
+
# d) Do one of the following:
|
45
|
+
# 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
46
|
+
# 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
47
|
+
# e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
48
|
+
# 5. Combined Libraries.
|
49
|
+
# You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
50
|
+
#
|
51
|
+
# a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
52
|
+
# b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
53
|
+
# 6. Revised Versions of the GNU Lesser General Public License.
|
54
|
+
# The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
55
|
+
#
|
56
|
+
# Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
57
|
+
#
|
58
|
+
# If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
59
|
+
|
60
|
+
module Glimmer
|
61
|
+
module Gtk
|
62
|
+
# Represents Gtk shape objects drawn on area widget, like rectangle, arc, and path
|
63
|
+
class Shape
|
64
|
+
class ArcNegative < Shape
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
Dir[File.expand_path("./#{File.basename(__FILE__, '.rb')}/*.rb", __dir__)].each {|f| require f}
|
@@ -69,11 +69,44 @@ module Glimmer
|
|
69
69
|
@drawing_operations = []
|
70
70
|
end
|
71
71
|
|
72
|
+
def post_initialize_child(child)
|
73
|
+
child_paths << child if child.is_a?(Path)
|
74
|
+
end
|
75
|
+
|
76
|
+
def child_paths
|
77
|
+
@child_paths ||= []
|
78
|
+
end
|
79
|
+
|
72
80
|
def draw_shape(drawing_area_widget, cairo_context)
|
81
|
+
previous_matrix = cairo_context.matrix
|
82
|
+
apply_transforms(cairo_context, target: :shape)
|
73
83
|
cairo_context.new_path
|
74
84
|
@drawing_operations.each do |drawing_operation_details|
|
75
85
|
cairo_context.send(drawing_operation_details[0], *drawing_operation_details[1])
|
76
86
|
end
|
87
|
+
child_paths.each do |child|
|
88
|
+
cairo_context.new_sub_path
|
89
|
+
child.drawing_operations.each do |drawing_operation_details|
|
90
|
+
cairo_context.send(drawing_operation_details[0], *drawing_operation_details[1])
|
91
|
+
end
|
92
|
+
end
|
93
|
+
cairo_context.set_matrix(previous_matrix)
|
94
|
+
end
|
95
|
+
|
96
|
+
def arc(*args)
|
97
|
+
@drawing_operations << [:arc, args]
|
98
|
+
end
|
99
|
+
|
100
|
+
def arc_negative(*args)
|
101
|
+
@drawing_operations << [:arc_negative, args]
|
102
|
+
end
|
103
|
+
|
104
|
+
def rectangle(*args)
|
105
|
+
@drawing_operations << [:rectangle, args]
|
106
|
+
end
|
107
|
+
|
108
|
+
def rounded_rectangle(*args)
|
109
|
+
@drawing_operations << [:rounded_rectangle, args]
|
77
110
|
end
|
78
111
|
|
79
112
|
def move_to(*args)
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# Copyright (c) 2021 Andy Maleh
|
2
|
+
#
|
3
|
+
# GNU LESSER GENERAL PUBLIC LICENSE
|
4
|
+
# Version 3, 29 June 2007
|
5
|
+
#
|
6
|
+
# Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
7
|
+
#
|
8
|
+
# Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
9
|
+
#
|
10
|
+
# This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
11
|
+
#
|
12
|
+
# 0. Additional Definitions.
|
13
|
+
# As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
14
|
+
#
|
15
|
+
# “The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
16
|
+
#
|
17
|
+
# An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
18
|
+
#
|
19
|
+
# A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
20
|
+
#
|
21
|
+
# The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
22
|
+
#
|
23
|
+
# The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
24
|
+
#
|
25
|
+
# 1. Exception to Section 3 of the GNU GPL.
|
26
|
+
# You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
27
|
+
#
|
28
|
+
# 2. Conveying Modified Versions.
|
29
|
+
# If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
30
|
+
#
|
31
|
+
# a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
32
|
+
# b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
33
|
+
# 3. Object Code Incorporating Material from Library Header Files.
|
34
|
+
# The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
35
|
+
#
|
36
|
+
# a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
37
|
+
# b) Accompany the object code with a copy of the GNU GPL and this license document.
|
38
|
+
# 4. Combined Works.
|
39
|
+
# You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
40
|
+
#
|
41
|
+
# a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
42
|
+
# b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
43
|
+
# c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
44
|
+
# d) Do one of the following:
|
45
|
+
# 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
46
|
+
# 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
47
|
+
# e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
48
|
+
# 5. Combined Libraries.
|
49
|
+
# You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
50
|
+
#
|
51
|
+
# a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
52
|
+
# b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
53
|
+
# 6. Revised Versions of the GNU Lesser General Public License.
|
54
|
+
# The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
55
|
+
#
|
56
|
+
# Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
57
|
+
#
|
58
|
+
# If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
59
|
+
|
60
|
+
module Glimmer
|
61
|
+
module Gtk
|
62
|
+
# Represents Gtk shape objects drawn on area widget, like rectangle, arc, and path
|
63
|
+
class Shape
|
64
|
+
class Square < Shape
|
65
|
+
def draw_shape(drawing_area_widget, cairo_context)
|
66
|
+
# expecting 3 args (x, y, length), so duplicate length again to become width/height for rectangle
|
67
|
+
draw_args = args.dup
|
68
|
+
draw_args << draw_args.last
|
69
|
+
cairo_context.rectangle(*draw_args)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
Dir[File.expand_path("./#{File.basename(__FILE__, '.rb')}/*.rb", __dir__)].each {|f| require f}
|
data/lib/glimmer/gtk/shape.rb
CHANGED
@@ -57,6 +57,8 @@
|
|
57
57
|
#
|
58
58
|
# If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
59
59
|
|
60
|
+
require 'glimmer/gtk/transformable'
|
61
|
+
|
60
62
|
module Glimmer
|
61
63
|
module Gtk
|
62
64
|
# Represents Gtk shape objects drawn on area widget, like rectangle, arc, and path
|
@@ -102,10 +104,33 @@ module Glimmer
|
|
102
104
|
end
|
103
105
|
accumulator
|
104
106
|
end
|
107
|
+
|
108
|
+
def one_based_color_rgb(rgb)
|
109
|
+
return rgb if rgb.is_a?(Cairo::Pattern) || rgb.first.is_a?(Cairo::ImageSurface)
|
110
|
+
rgb.each_with_index.map {|single_color, i| i == 3 ? single_color : single_color / 255.0}
|
111
|
+
end
|
112
|
+
|
113
|
+
def set_source_dynamically(cairo_context, source_args)
|
114
|
+
source_args = one_based_color_rgb(source_args)
|
115
|
+
if source_args.is_a?(Cairo::Pattern) || source_args.first.is_a?(Cairo::ImageSurface)
|
116
|
+
cairo_context.set_source(*source_args)
|
117
|
+
elsif source_args.size == 3
|
118
|
+
cairo_context.set_source_rgb(*source_args)
|
119
|
+
elsif source_args.size == 4
|
120
|
+
cairo_context.set_source_rgba(*source_args)
|
121
|
+
end
|
122
|
+
end
|
105
123
|
end
|
106
124
|
|
125
|
+
prepend Transformable
|
126
|
+
|
127
|
+
SHAPE_FILL_PROPERTIES = [:fill_rule]
|
128
|
+
SHAPE_STROKE_PROPERTIES = [:dash, :font_face, :font_matrix, :font_options, :font_size, :line_cap, :line_join, :line_width, :miter_limit, :scaled_font]
|
129
|
+
SHAPE_GENERAL_PROPERTIES = [:matrix, :operator, :tolerance]
|
130
|
+
|
107
131
|
attr_reader :parent, :args, :keyword, :block
|
108
|
-
attr_accessor :fill, :stroke, :
|
132
|
+
attr_accessor :fill, :stroke, :clip
|
133
|
+
attr_accessor *(SHAPE_FILL_PROPERTIES + SHAPE_STROKE_PROPERTIES + SHAPE_GENERAL_PROPERTIES)
|
109
134
|
# TODO consider automatically setting attribute accessors by looking up set_xyz methods on cairo context
|
110
135
|
|
111
136
|
def initialize(keyword, parent, args, &block)
|
@@ -145,6 +170,11 @@ module Glimmer
|
|
145
170
|
draw_shape(drawing_area_widget, cairo_context)
|
146
171
|
draw_stroke(drawing_area_widget, cairo_context)
|
147
172
|
end
|
173
|
+
|
174
|
+
if clip
|
175
|
+
draw_shape(drawing_area_widget, cairo_context)
|
176
|
+
draw_clip(drawing_area_widget, cairo_context)
|
177
|
+
end
|
148
178
|
end
|
149
179
|
|
150
180
|
# Invokes cairo_context#underscored_shape_class_name method by default
|
@@ -152,28 +182,44 @@ module Glimmer
|
|
152
182
|
#
|
153
183
|
# Subclasses can override
|
154
184
|
def draw_shape(drawing_area_widget, cairo_context)
|
185
|
+
previous_matrix = cairo_context.matrix
|
186
|
+
apply_transforms(cairo_context, target: :shape)
|
155
187
|
class_symbol = self.class.name.split('::').last
|
156
188
|
keyword = self.class.keyword(class_symbol)
|
157
189
|
cairo_context.send(keyword, *args)
|
190
|
+
cairo_context.set_matrix(previous_matrix)
|
158
191
|
end
|
159
192
|
|
160
193
|
def draw_fill(drawing_area_widget, cairo_context)
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
194
|
+
previous_matrix = cairo_context.matrix
|
195
|
+
apply_transforms(cairo_context, target: :fill)
|
196
|
+
self.class.set_source_dynamically(cairo_context, fill)
|
197
|
+
(SHAPE_FILL_PROPERTIES + SHAPE_GENERAL_PROPERTIES).each do |property|
|
198
|
+
cairo_context.send("set_#{property}", *send(property)) if send(property)
|
165
199
|
end
|
166
200
|
cairo_context.fill
|
201
|
+
cairo_context.set_matrix(previous_matrix)
|
167
202
|
end
|
168
203
|
|
169
204
|
def draw_stroke(drawing_area_widget, cairo_context)
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
205
|
+
previous_matrix = cairo_context.matrix
|
206
|
+
apply_transforms(cairo_context, target: :stroke)
|
207
|
+
self.class.set_source_dynamically(cairo_context, stroke)
|
208
|
+
(SHAPE_STROKE_PROPERTIES + SHAPE_GENERAL_PROPERTIES).each do |property|
|
209
|
+
cairo_context.send("set_#{property}", *send(property)) if send(property)
|
174
210
|
end
|
175
|
-
cairo_context.set_line_width(line_width)
|
176
211
|
cairo_context.stroke
|
212
|
+
cairo_context.set_matrix(previous_matrix)
|
213
|
+
end
|
214
|
+
|
215
|
+
def draw_clip(drawing_area_widget, cairo_context)
|
216
|
+
previous_matrix = cairo_context.matrix
|
217
|
+
apply_transforms(cairo_context, target: :clip)
|
218
|
+
SHAPE_GENERAL_PROPERTIES.each do |property|
|
219
|
+
cairo_context.send("set_#{property}", *send(property)) if send(property)
|
220
|
+
end
|
221
|
+
cairo_context.clip
|
222
|
+
cairo_context.set_matrix(previous_matrix)
|
177
223
|
end
|
178
224
|
|
179
225
|
def respond_to?(method_name, include_private = false, &block)
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# Copyright (c) 2021 Andy Maleh
|
2
|
+
#
|
3
|
+
# GNU LESSER GENERAL PUBLIC LICENSE
|
4
|
+
# Version 3, 29 June 2007
|
5
|
+
#
|
6
|
+
# Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
7
|
+
#
|
8
|
+
# Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
9
|
+
#
|
10
|
+
# This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
11
|
+
#
|
12
|
+
# 0. Additional Definitions.
|
13
|
+
# As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
14
|
+
#
|
15
|
+
# “The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
16
|
+
#
|
17
|
+
# An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
18
|
+
#
|
19
|
+
# A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
20
|
+
#
|
21
|
+
# The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
22
|
+
#
|
23
|
+
# The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
24
|
+
#
|
25
|
+
# 1. Exception to Section 3 of the GNU GPL.
|
26
|
+
# You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
27
|
+
#
|
28
|
+
# 2. Conveying Modified Versions.
|
29
|
+
# If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
30
|
+
#
|
31
|
+
# a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
32
|
+
# b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
33
|
+
# 3. Object Code Incorporating Material from Library Header Files.
|
34
|
+
# The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
35
|
+
#
|
36
|
+
# a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
37
|
+
# b) Accompany the object code with a copy of the GNU GPL and this license document.
|
38
|
+
# 4. Combined Works.
|
39
|
+
# You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
40
|
+
#
|
41
|
+
# a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
42
|
+
# b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
43
|
+
# c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
44
|
+
# d) Do one of the following:
|
45
|
+
# 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
46
|
+
# 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
47
|
+
# e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
48
|
+
# 5. Combined Libraries.
|
49
|
+
# You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
50
|
+
#
|
51
|
+
# a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
52
|
+
# b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
53
|
+
# 6. Revised Versions of the GNU Lesser General Public License.
|
54
|
+
# The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
55
|
+
#
|
56
|
+
# Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
57
|
+
#
|
58
|
+
# If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
59
|
+
|
60
|
+
module Glimmer
|
61
|
+
module Gtk
|
62
|
+
# Represents transformable view elements like shapes and drawing_area
|
63
|
+
module Transformable
|
64
|
+
def initialize(keyword, parent, args, &block)
|
65
|
+
@transforms = []
|
66
|
+
super
|
67
|
+
end
|
68
|
+
|
69
|
+
# applies transform on target type (:shape, :drawing_area, :fill, :stroke, :clip)
|
70
|
+
def apply_transforms(cairo_context, target: )
|
71
|
+
@transforms.each do |transform|
|
72
|
+
operation = transform.first
|
73
|
+
args = transform.last.dup
|
74
|
+
options = args.pop
|
75
|
+
excluded_types = [options[:exclude]].flatten
|
76
|
+
cairo_context.send(operation, *args) if !excluded_types.include?(target)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def translate(x, y, options = {})
|
81
|
+
@transforms << [:translate, [x, y, options]]
|
82
|
+
end
|
83
|
+
|
84
|
+
def scale(x, y, options = {})
|
85
|
+
@transforms << [:scale, [x, y, options]]
|
86
|
+
end
|
87
|
+
|
88
|
+
def rotate(angle, options = {})
|
89
|
+
@transforms << [:rotate, [angle, options]]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -57,6 +57,8 @@
|
|
57
57
|
#
|
58
58
|
# If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
59
59
|
|
60
|
+
require 'glimmer/gtk/transformable'
|
61
|
+
|
60
62
|
module Glimmer
|
61
63
|
module Gtk
|
62
64
|
class WidgetProxy
|
@@ -64,6 +66,8 @@ module Glimmer
|
|
64
66
|
#
|
65
67
|
# Follows the Proxy Design Pattern
|
66
68
|
class DrawingAreaProxy < WidgetProxy
|
69
|
+
prepend Transformable
|
70
|
+
|
67
71
|
attr_reader :shapes
|
68
72
|
|
69
73
|
def initialize(keyword, parent, args, &block)
|
@@ -74,6 +78,14 @@ module Glimmer
|
|
74
78
|
def post_add_content
|
75
79
|
super
|
76
80
|
@gtk.signal_connect(:draw) do |drawing_area_widget, cairo_context|
|
81
|
+
if @paint
|
82
|
+
previous_matrix = cairo_context.matrix
|
83
|
+
apply_transforms(cairo_context, target: :drawing_area)
|
84
|
+
Shape.set_source_dynamically(cairo_context, @paint)
|
85
|
+
cairo_context.paint
|
86
|
+
cairo_context.set_matrix(previous_matrix)
|
87
|
+
end
|
88
|
+
|
77
89
|
shapes.each do |shape|
|
78
90
|
shape.draw(drawing_area_widget, cairo_context)
|
79
91
|
end
|
@@ -87,6 +99,10 @@ module Glimmer
|
|
87
99
|
super
|
88
100
|
end
|
89
101
|
end
|
102
|
+
|
103
|
+
def paint(*args)
|
104
|
+
@paint = args
|
105
|
+
end
|
90
106
|
end
|
91
107
|
end
|
92
108
|
end
|
@@ -147,8 +147,8 @@ module Glimmer
|
|
147
147
|
|
148
148
|
# Subclasses may override to perform post initialization work on an added child (normally must also call super)
|
149
149
|
def post_initialize_child(child)
|
150
|
-
@gtk.add(child.gtk)
|
151
|
-
child.gtk.show
|
150
|
+
@gtk.add(child.gtk) if @gtk.respond_to?(:add)
|
151
|
+
child.gtk&.show if child.gtk&.respond_to?(:show)
|
152
152
|
end
|
153
153
|
|
154
154
|
def window_proxy
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'glimmer-dsl-gtk'
|
2
|
+
|
3
|
+
include Glimmer
|
4
|
+
|
5
|
+
window {
|
6
|
+
title 'Hello, Drawing Area!'
|
7
|
+
default_size 256, 256
|
8
|
+
|
9
|
+
drawing_area {
|
10
|
+
# Surface Paint
|
11
|
+
paint 242.25, 242.25, 242.25
|
12
|
+
|
13
|
+
# Set up the parameters
|
14
|
+
xc = 128.0
|
15
|
+
yc = 128.0
|
16
|
+
radius = 100.0
|
17
|
+
angle1 = 45.0 * (Math::PI/180.0) # angles are specified
|
18
|
+
angle2 = 180.0 * (Math::PI/180.0) # in radians
|
19
|
+
|
20
|
+
# The main arc
|
21
|
+
arc(xc, yc, radius, angle1, angle2) {
|
22
|
+
stroke 0, 0, 0
|
23
|
+
line_width 10
|
24
|
+
}
|
25
|
+
|
26
|
+
# Draw helping lines
|
27
|
+
|
28
|
+
# First, the circle at the centre
|
29
|
+
arc(xc, yc, 10.0, 0, 2*Math::PI) {
|
30
|
+
fill 255, 51, 51, 0.6
|
31
|
+
}
|
32
|
+
|
33
|
+
# Then, the lines reaching out
|
34
|
+
path {
|
35
|
+
arc xc, yc, radius, angle1, angle1
|
36
|
+
line_to xc, yc
|
37
|
+
arc xc, yc, radius, angle2, angle2
|
38
|
+
line_to xc, yc
|
39
|
+
|
40
|
+
stroke 255, 51, 51, 0.6
|
41
|
+
line_width 6
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}.show
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'glimmer-dsl-gtk'
|
2
|
+
|
3
|
+
include Glimmer
|
4
|
+
|
5
|
+
window {
|
6
|
+
title 'Arc Negative'
|
7
|
+
default_size 256, 256
|
8
|
+
|
9
|
+
drawing_area {
|
10
|
+
# Surface Paint
|
11
|
+
paint 255, 255, 255
|
12
|
+
|
13
|
+
# Set up the parameters
|
14
|
+
xc = 128.0
|
15
|
+
yc = 128.0
|
16
|
+
radius = 100.0
|
17
|
+
angle1 = 45.0 * (Math::PI/180.0) # angles are specified
|
18
|
+
angle2 = 180.0 * (Math::PI/180.0) # in radians
|
19
|
+
|
20
|
+
# The main negative arc
|
21
|
+
arc_negative(xc, yc, radius, angle1, angle2) {
|
22
|
+
stroke 0, 0, 0
|
23
|
+
line_width 10
|
24
|
+
}
|
25
|
+
|
26
|
+
# Draw helping lines
|
27
|
+
|
28
|
+
# First, the circle at the centre
|
29
|
+
arc(xc, yc, 10.0, 0, 2*Math::PI) {
|
30
|
+
fill 255, 51, 51, 0.6
|
31
|
+
}
|
32
|
+
|
33
|
+
# Then, the lines reaching out
|
34
|
+
path {
|
35
|
+
arc(xc, yc, radius, angle1, angle1)
|
36
|
+
line_to(xc, yc)
|
37
|
+
arc(xc, yc, radius, angle2, angle2)
|
38
|
+
line_to(xc, yc)
|
39
|
+
|
40
|
+
stroke 255, 51, 51, 0.6
|
41
|
+
line_width 6
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}.show
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'glimmer-dsl-gtk'
|
2
|
+
|
3
|
+
include Glimmer
|
4
|
+
|
5
|
+
window {
|
6
|
+
title 'Clip'
|
7
|
+
default_size 256, 256
|
8
|
+
|
9
|
+
drawing_area {
|
10
|
+
# Surface Paint
|
11
|
+
paint 255, 255, 255
|
12
|
+
|
13
|
+
# Designate arc as the clipping area
|
14
|
+
arc(128.0, 128.0, 76.8, 0, 2 * Math::PI) {
|
15
|
+
clip true
|
16
|
+
}
|
17
|
+
|
18
|
+
# Rectangle will get clipped by arc
|
19
|
+
rectangle(0, 0, 256, 256) {
|
20
|
+
fill 0, 0, 0
|
21
|
+
}
|
22
|
+
|
23
|
+
# Path will get clipped by arc
|
24
|
+
path {
|
25
|
+
move_to 0, 0
|
26
|
+
line_to 256, 256
|
27
|
+
move_to 256, 0
|
28
|
+
line_to 0, 256
|
29
|
+
|
30
|
+
stroke 0, 255, 0
|
31
|
+
line_width 10
|
32
|
+
}
|
33
|
+
}
|
34
|
+
}.show
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'glimmer-dsl-gtk'
|
2
|
+
|
3
|
+
include Glimmer
|
4
|
+
|
5
|
+
window {
|
6
|
+
title 'Clip Image'
|
7
|
+
default_size 256, 256
|
8
|
+
|
9
|
+
drawing_area {
|
10
|
+
paint 242.25, 242.25, 242.25
|
11
|
+
|
12
|
+
arc(128.0, 128.0, 76.8, 0, 2 * Math::PI) {
|
13
|
+
clip true # designate arc as the clipping area
|
14
|
+
}
|
15
|
+
|
16
|
+
rectangle(0, 0, 256, 256) {
|
17
|
+
# Source image is from:
|
18
|
+
# - https://www.publicdomainpictures.net/en/view-image.php?image=7683&picture=breaking-blue-wave
|
19
|
+
# Converted to PNG before using it
|
20
|
+
image = Cairo::ImageSurface.from_png(File.expand_path(File.join('..', '..', 'images', 'breaking-blue-wave.png'), __dir__))
|
21
|
+
w = image.width
|
22
|
+
h = image.height
|
23
|
+
|
24
|
+
scale 256.0/w, 256.0/h, exclude: :shape # applies scale to fill source image only
|
25
|
+
fill image, 0, 0
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}.show
|