clutter 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/sample/canvas.rb ADDED
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This sample code is a port of clutter/examples/canvas.c.
4
+ # It is licensed under the terms of the GNU Lesser General Public
5
+ # License, version 2.1 or (at your option) later.
6
+ #
7
+ # Copyright (C) 2012 Ruby-GNOME2 Project Team
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+
23
+ require "clutter"
24
+
25
+ Clutter.init
26
+
27
+ stage = Clutter::Stage.new
28
+ stage.title = "2D Clock"
29
+ stage.user_resizable = true
30
+ stage.background_color = Clutter::Color.get_static(:sky_blue_light)
31
+ stage.set_size(300, 300)
32
+ stage.show
33
+
34
+ canvas = Clutter::Canvas.new
35
+ canvas.set_size(300, 300)
36
+
37
+ actor = Clutter::Actor.new
38
+ actor.content = canvas
39
+ actor.set_content_scaling_filters(:trilinear, :linear)
40
+ stage.add_child(actor)
41
+
42
+ actor.add_constraint(Clutter::BindConstraint.new(stage, :size, 0))
43
+ idle_resize_id = nil
44
+ actor.signal_connect("allocation-changed") do |_actor, allocation, flags|
45
+ idle_resize_id ||= Clutter::Threads.add_timeout(1000) do
46
+ size = _actor.size
47
+ _actor.content.set_size(size.width.ceil, size.height.ceil)
48
+ idle_resize_id = nil
49
+ GLib::Source::REMOVE
50
+ end
51
+ end
52
+
53
+ stage.signal_connect("destroy") do
54
+ Clutter.main_quit
55
+ end
56
+
57
+ canvas.signal_connect("draw") do |_canvas, cairo_context, width, height|
58
+ now = Time.now
59
+ seconds = now.sec * Math::PI / 30
60
+ minutes = now.min * Math::PI / 30
61
+ hours = now.hour * Math::PI / 6
62
+
63
+ cairo_context.save do
64
+ cairo_context.operator = :clear
65
+ cairo_context.paint
66
+ end
67
+
68
+ cairo_context.operator = :over
69
+ cairo_context.scale(width, height)
70
+
71
+ cairo_context.line_cap = :round
72
+ cairo_context.line_width = 0.1
73
+
74
+ # the black rail that holds the seconds indicator
75
+ cairo_context.set_source_clutter_color(Clutter::Color.get_static(:black))
76
+ cairo_context.translate(0.5, 0.5)
77
+ cairo_context.arc(0, 0, 0.4, 0, Math::PI * 2)
78
+ cairo_context.stroke
79
+
80
+ # the seconds indicator
81
+ color = Clutter::Color.get_static(:white)
82
+ color.alpha = 128
83
+ cairo_context.set_source_clutter_color(color)
84
+ cairo_context.move_to(0, 0)
85
+ cairo_context.arc(Math.sin(seconds) * 0.4,
86
+ -Math.cos(seconds) * 0.4,
87
+ 0.05,
88
+ 0,
89
+ Math::PI * 2)
90
+ cairo_context.fill
91
+
92
+ # the minutes hand
93
+ color = Clutter::Color.get_static(:chameleon_dark)
94
+ color.alpha = 196
95
+ cairo_context.set_source_clutter_color(color)
96
+ cairo_context.move_to(0, 0)
97
+ cairo_context.line_to(Math.sin(minutes) * 0.4,
98
+ -Math.cos(minutes) * 0.4)
99
+ cairo_context.stroke
100
+
101
+ # the hours hand
102
+ cairo_context.move_to(0, 0)
103
+ cairo_context.line_to(Math.sin(hours) * 0.2,
104
+ -Math.cos(hours) * 0.2)
105
+ cairo_context.stroke
106
+
107
+ true
108
+ end
109
+
110
+ canvas.invalidate
111
+
112
+ Clutter::Threads.add_timeout(100, 1000) do
113
+ canvas.invalidate
114
+ GLib::Source::CONTINUE
115
+ end
116
+
117
+ Clutter.main
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This sample code is a port of clutter/examples/constraints.c.
4
+ # It is licensed under the terms of the GNU Lesser General Public
5
+ # License, version 2.1 or (at your option) later.
6
+ #
7
+ # Copyright (C) 2013 Ruby-GNOME2 Project Team
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+
23
+ require "clutter"
24
+
25
+ Clutter.init
26
+
27
+ stage = Clutter::Stage.new
28
+ stage.name = "stage"
29
+ stage.title = "Snap Constraint"
30
+ stage.user_resizable = true
31
+ stage.background_color = Clutter::Color.get_static(:aluminium_1)
32
+ stage.signal_connect("destroy") do
33
+ Clutter.main_quit
34
+ end
35
+
36
+ layer_a = Clutter::Actor.new
37
+ layer_a.background_color = Clutter::Color.new(:scarlet_red)
38
+ layer_a.name = "layerA"
39
+ layer_a.set_size(100.0, 25.0)
40
+ stage.add_child(layer_a)
41
+
42
+ layer_a.add_constraint(Clutter::AlignConstraint.new(stage, :both, 0.5))
43
+
44
+ layer_b = Clutter::Actor.new
45
+ layer_b.background_color = Clutter::Color.new(:butter_dark)
46
+ layer_b.name = "layerB"
47
+ stage.add_child(layer_b)
48
+
49
+ layer_b.add_constraint(Clutter::BindConstraint.new(layer_a, :x, 0.0))
50
+ layer_b.add_constraint(Clutter::BindConstraint.new(layer_a, :width, 0.0))
51
+
52
+ layer_b.add_constraint(Clutter::SnapConstraint.new(layer_a,
53
+ :top,
54
+ :bottom,
55
+ 10.0))
56
+
57
+ layer_b.add_constraint(Clutter::SnapConstraint.new(stage,
58
+ :bottom,
59
+ :bottom,
60
+ -10.0))
61
+
62
+ layer_c = Clutter::Actor.new
63
+ layer_c.background_color = Clutter::Color.new(:chameleon_light)
64
+ layer_c.name = "layerC"
65
+ stage.add_child(layer_c)
66
+
67
+ layer_c.add_constraint(Clutter::BindConstraint.new(layer_a, :x, 0.0))
68
+ layer_c.add_constraint(Clutter::BindConstraint.new(layer_a, :width, 0.0))
69
+
70
+ layer_c.add_constraint(Clutter::SnapConstraint.new(layer_a,
71
+ :bottom,
72
+ :top,
73
+ -10.0))
74
+ layer_c.add_constraint(Clutter::SnapConstraint.new(stage,
75
+ :top,
76
+ :top,
77
+ 10.0))
78
+
79
+ stage.show
80
+
81
+ Clutter.main
@@ -0,0 +1,177 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This sample code is a port of clutter/examples/drag-action.c.
4
+ # It is licensed under the terms of the GNU Lesser General Public
5
+ # License, version 2.1 or (at your option) later.
6
+ #
7
+ # Copyright (C) 2013 Ruby-GNOME2 Project Team
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+
23
+ require "optparse"
24
+
25
+ require "clutter"
26
+
27
+ Clutter.init
28
+
29
+ drag_axis = :none
30
+ x_drag_threshold = 0
31
+ y_drag_threshold = 0
32
+
33
+ parser = OptionParser.new
34
+ parser.on("--x-threshold=PIXELS", Integer,
35
+ "Set the horizontal drag threshold",
36
+ "(#{x_drag_threshold})") do |pixels|
37
+ x_drag_threshold = pixels
38
+ end
39
+ parser.on("--y-threshold=PIXELS", Integer,
40
+ "Set the vertical drag threshold",
41
+ "(#{y_drag_threshold})") do |pixels|
42
+ y_drag_threshold = pixels
43
+ end
44
+ axises = {
45
+ :none => :axis_none,
46
+ :x => :x_axis,
47
+ :y => :y_axis,
48
+ }
49
+ parser.on("--axis=AXIS", axises.keys,
50
+ "Set the drag axis",
51
+ "[#{axises.keys.join(', ')}]",
52
+ "(#{drag_axis})") do |axis|
53
+ drag_axis = axis
54
+ end
55
+ parser.parse!
56
+
57
+ drag_axis = axises[drag_axis]
58
+
59
+ stage = Clutter::Stage.new
60
+ stage.title = "Drag Test"
61
+ stage.set_size(800, 600)
62
+ stage.signal_connect("destroy") do
63
+ Clutter.main_quit
64
+ end
65
+
66
+ handle = Clutter::Actor.new
67
+ handle.background_color = Clutter::Color.new(:sky_blue)
68
+ handle.set_size(128, 128)
69
+ handle.set_position((800 - 128) / 2, (600 - 128) / 2)
70
+ handle.reactive = true
71
+ stage.add_child(handle)
72
+
73
+ handle.signal_connect("enter-event") do |actor, event|
74
+ transition = actor.get_transition("curl")
75
+ if transition.nil?
76
+ transition = Clutter::PropertyTransition.new("@effects.curl.period")
77
+ transition.duration = 250
78
+ actor.add_transition("curl", transition);
79
+ end
80
+
81
+ transition.from = 0.0
82
+ transition.to = 0.25
83
+ transition.rewind
84
+ transition.start
85
+
86
+ Clutter::Event::STOP
87
+ end
88
+
89
+ handle.signal_connect("leave-event") do |actor, event|
90
+ transition = actor.get_transition("curl")
91
+ if transition.nil?
92
+ transition = Clutter::PropertyTransition.new("@effects.curl.period")
93
+ transition.duration = 250
94
+ actor.add_transition("curl", transition)
95
+ end
96
+
97
+ transition.from = 0.25
98
+ transition.to = 0.0
99
+ transition.rewind
100
+ transition.start
101
+
102
+ Clutter::Event::STOP
103
+ end
104
+
105
+ action = Clutter::DragAction.new
106
+ action.set_drag_threshold(x_drag_threshold,
107
+ y_drag_threshold)
108
+ action.drag_axis = drag_axis
109
+
110
+ action.signal_connect("drag-begin") do |_action, actor, event_x, event_y, modifiers|
111
+ copy_p = modifiers.shift_mask?
112
+ if copy_p
113
+ drag_handle = Clutter::Actor.new
114
+ drag_handle.set_size(48, 48)
115
+ drag_handle.background_color = Clutter::Color.new(:sky_blue_dark)
116
+ stage.add_child(drag_handle)
117
+ drag_handle.set_position(event_x, event_y)
118
+ else
119
+ drag_handle = actor
120
+ end
121
+
122
+ _action.drag_handle = drag_handle
123
+
124
+ transition = actor.get_transition("disable")
125
+ if transition.nil?
126
+ transition = Clutter::PropertyTransition.new("@effects.disable.factor")
127
+ transition.duration = 250
128
+ actor.add_transition("disable", transition)
129
+ end
130
+
131
+ transition.from = 0.0
132
+ transition.to = 1.0
133
+ transition.rewind
134
+ transition.start
135
+ end
136
+
137
+ action.signal_connect("drag-end") do |_action, actor, event_x, event_y, modifiers|
138
+ drag_handle = _action.drag_handle
139
+ if actor != drag_handle
140
+ drag_handle.save_easing_state do
141
+ drag_handle.easing_mode = :linear
142
+ drag_handle.opacity = 0
143
+ end
144
+ drag_handle.signal_connect("transitions-completed") do
145
+ drag_handle.destroy
146
+ end
147
+
148
+ parent = actor.parent
149
+ succeeded, real_x, real_y = parent.transform_stage_point(event_x, event_y)
150
+
151
+ actor.save_easing_state do
152
+ actor.easing_mode = :ease_out_cubic
153
+ actor.set_position(real_x, real_y)
154
+ end
155
+ end
156
+
157
+ transition = actor.get_transition("disable")
158
+ if transition.nil?
159
+ transition = Clutter::PropertyTransition.new("@effects.disable.factor")
160
+ transition.duration = 250
161
+ actor.add_transition("disable", transition)
162
+ end
163
+
164
+ transition.from = 1.0
165
+ transition.to = 0.0
166
+ transition.rewind
167
+ transition.start
168
+ end
169
+
170
+ handle.add_action(action)
171
+
172
+ handle.add_effect_with_name("disable", Clutter::DesaturateEffect.new(0.0))
173
+ handle.add_effect_with_name("curl", Clutter::PageTurnEffect.new(0.0, 45.0, 12.0))
174
+
175
+ stage.show
176
+
177
+ Clutter.main
@@ -0,0 +1,198 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This sample code is a port of clutter/examples/drop-action.c.
4
+ # It is licensed under the terms of the GNU Lesser General Public
5
+ # License, version 2.1 or (at your option) later.
6
+ #
7
+ # Copyright (C) 2013 Ruby-GNOME2 Project Team
8
+ #
9
+ # This library is free software; you can redistribute it and/or
10
+ # modify it under the terms of the GNU Lesser General Public
11
+ # License as published by the Free Software Foundation; either
12
+ # version 2.1 of the License, or (at your option) any later version.
13
+ #
14
+ # This library is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ # Lesser General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU Lesser General Public
20
+ # License along with this library; if not, write to the Free Software
21
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+
23
+ require "clutter"
24
+
25
+ Clutter.init
26
+
27
+ TARGET_SIZE = 200
28
+ HANDLE_SIZE = 128
29
+
30
+ stage = Clutter::Stage.new
31
+ stage.title = "Drop Action"
32
+ stage.signal_connect("destroy") do
33
+ Clutter.main_quit
34
+ end
35
+
36
+ target1 = Clutter::Actor.new
37
+ target1.background_color = Clutter::Color.new(:scarlet_red_light)
38
+ target1.set_size(TARGET_SIZE, TARGET_SIZE)
39
+ target1.opacity = 64
40
+ target1.add_constraint(Clutter::AlignConstraint.new(stage, :y_axis, 0.5))
41
+ target1.x = 10
42
+ target1.reactive = true
43
+
44
+ on_target_over = lambda do |action, actor, over_p|
45
+ final_opacity = over_p ? 128 : 64
46
+
47
+ target = action.actor
48
+ target.save_easing_state do
49
+ target.easing_mode = :linear
50
+ target.opacity = final_opacity
51
+ end
52
+ end
53
+
54
+ target1.add_action_with_name("drop", Clutter::DropAction.new)
55
+ drop_action = target1.get_action("drop")
56
+ drop_action.signal_connect("over-in") do |_action, actor|
57
+ on_target_over.call(_action, actor, true)
58
+ end
59
+ drop_action.signal_connect("over-out") do |_action, actor|
60
+ on_target_over.call(_action, actor, false)
61
+ end
62
+
63
+ drag = nil
64
+ drop_successful = false
65
+ add_drag_object = lambda do |target|
66
+ if drag.nil?
67
+ drag = Clutter::Actor.new
68
+ drag.background_color = Clutter::Color.new(:sky_blue_light)
69
+ drag.set_size(HANDLE_SIZE, HANDLE_SIZE)
70
+ drag.set_position((TARGET_SIZE - HANDLE_SIZE) / 2.0,
71
+ (TARGET_SIZE - HANDLE_SIZE) / 2.0)
72
+ drag.reactive = true
73
+
74
+ action = Clutter::DragAction.new
75
+ action.signal_connect("drag-begin") do |_action, _actor, event_x, event_y, modifiers|
76
+ position = _actor.position
77
+ handle = Clutter::Actor.new
78
+ handle.background_color = Clutter::Color.new(:sky_blue_dark)
79
+ handle.set_size(128, 128)
80
+ handle.set_position(event_x - position.x, event_y - position.y)
81
+ stage.add_child(handle)
82
+
83
+ _action.drag_handle = handle
84
+
85
+ _actor.save_easing_state do
86
+ _actor.easing_mode = :linear
87
+ _actor.opacity = 128
88
+ end
89
+
90
+ drop_successful = true
91
+ end
92
+ action.signal_connect("drag-end") do |_action, _actor, event_x, event_y, modifiers|
93
+ handle = _action.drag_handle
94
+ printf("Drag ended at: %.0f, %.0f\n", event_x, event_y)
95
+ _actor.save_easing_state do
96
+ _actor.easing_mode = :linear
97
+ _actor.opacity = 255
98
+ end
99
+
100
+ handle.save_easing_state do
101
+ if drop_successful
102
+ handle.easing_mode = :linear
103
+ handle.opacity = 0
104
+ else
105
+ parent = _actor.parent
106
+
107
+ parent.save_easing_state do
108
+ parent.easing_mode = :linear
109
+ parent.opacity = 255
110
+ end
111
+
112
+ x_pos, y_pos = _actor.transformed_position
113
+
114
+ handle.easing_mode = :ease_out_bounce
115
+ handle.set_position(x_pos, y_pos)
116
+ handle.opacity = 0
117
+ end
118
+ end
119
+
120
+ handle.signal_connect("transitions-completed") do |_actor|
121
+ _actor.destroy
122
+ end
123
+ end
124
+
125
+ drag.add_action(action)
126
+ end
127
+
128
+ parent = drag.parent
129
+ if parent == target
130
+ target.save_easing_state do
131
+ target.easing_mode = :linear
132
+ target.opacity = 255
133
+ end
134
+ return
135
+ end
136
+
137
+ if parent and parent != stage
138
+ parent.remove_child(drag)
139
+
140
+ parent.save_easing_state do
141
+ parent.easing_mode :linear
142
+ parent.opacity = 64
143
+ end
144
+ end
145
+ target.add_child(drag)
146
+
147
+ target.save_easing_state do
148
+ target.easing_mode :linear
149
+ target.opacity = 255
150
+ end
151
+ end
152
+
153
+ on_target_drop = lambda do |_action, actor, event_x, event_y|
154
+ succeeded, actor_x, actor_y = actor.transform_stage_point(event_x, event_y)
155
+ printf("Dropped at %.0f, %.0f (screen: %.0f, %.0f)\n",
156
+ actor_x, actor_y,
157
+ event_x, event_y)
158
+
159
+ drop_successful = TRUE;
160
+ add_drag_object.call(actor)
161
+ end
162
+ drop_action.signal_connect("drop", &on_target_drop)
163
+
164
+ dummy = Clutter::Actor.new
165
+ dummy.background_color = Clutter::Color.new(:orange_dark)
166
+ dummy.set_size(640 - (2 * 10) - (2 * (TARGET_SIZE + 10)),
167
+ TARGET_SIZE)
168
+ dummy.add_constraint(Clutter::AlignConstraint.new(stage, :x_axis, 0.5))
169
+ dummy.add_constraint(Clutter::AlignConstraint.new(stage, :y_axis, 0.5))
170
+ dummy.reactive = true
171
+
172
+ target2 = Clutter::Actor.new
173
+ target2.background_color = Clutter::Color.new(:chameleon_light)
174
+ target2.set_size(TARGET_SIZE, TARGET_SIZE)
175
+ target2.opacity = 64
176
+ target2.add_constraint(Clutter::AlignConstraint.new(stage, :y_axis, 0.5))
177
+ target2.x = 640 - TARGET_SIZE - 10
178
+ target2.reactive = true
179
+
180
+ target2.add_action_with_name("drop", Clutter::DropAction.new)
181
+ drop_action = target2.get_action("drop")
182
+ drop_action.signal_connect("over-in") do |_action, actor|
183
+ on_target_over.call(_action, actor, true)
184
+ end
185
+ drop_action.signal_connect("over-out") do |_action, actor|
186
+ on_target_over.call(_action, actor, false)
187
+ end
188
+ drop_action.signal_connect("drop", &on_target_drop)
189
+
190
+ stage.add_child(target1)
191
+ stage.add_child(dummy)
192
+ stage.add_child(target2)
193
+
194
+ add_drag_object.call(target1)
195
+
196
+ stage.show
197
+
198
+ Clutter.main