File: test.rb

package info (click to toggle)
ruby-gnome2 3.1.0-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,072 kB
  • ctags: 17,433
  • sloc: ansic: 93,621; ruby: 62,273; xml: 335; sh: 246; makefile: 25
file content (169 lines) | stat: -rwxr-xr-x 4,555 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env ruby
#
# This sample code is a port of
# clutter-gtk/examples/gtk-clutter-test.c. The image file used in
# this sample code is copied from
# clutter-gtk/examples/redhand.png. They are licensed under the terms
# of the GNU Lesser General Public License, version 2.1 or (at your
# option) later.
#
# Copyright (C) 2013  Ruby-GNOME2 Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

require "clutter-gtk"
require "gdk_pixbuf2"

N_HANDS       = 4
WINDOW_WIDTH  = 400
WINDOW_HEIGHT = 400
RADIUS        = 150

fade_p        = false
fullscreen_p  = false

pixbuf = GdkPixbuf::Pixbuf.new(:file => File.expand_path("redhand.png", File.dirname(__FILE__)))

window = Gtk::Window.new
window.set_default_size(WINDOW_WIDTH, WINDOW_HEIGHT)
window.title = "Clutter Embedding"
window.signal_connect("destroy") do
  Gtk.main_quit
end

vbox = Gtk::Grid.new
vbox.orientation = :vertical
vbox.hexpand = true
vbox.vexpand = true
window.add(vbox)

clutter = ClutterGtk::Embed.new
vbox.add(clutter)

stage = clutter.stage
stage.background_color = Clutter::Color.new(:sky_blue_light)

label = Gtk::Label.new("This is a label")
vbox.add(label)
label.hexpand = true

button = Gtk::Button.new(:label => "This is a button...clicky")
button.signal_connect("clicked") do |_button|
  fade_p = !fade_p
end
vbox.add(button)
button.hexpand = true

button = Gtk::Button.new(:label => "Fullscreen")
button.image = Gtk::Image.new(:stock => Gtk::Stock::FULLSCREEN, :size => :button)
button.signal_connect("clicked") do |_button|
  if fullscreen_p
    window.unfullscreen
    fullscreen_p = false
  else
    window.fullscreen
    fullscreen_p = true
  end
end
vbox.add(button)
button.hexpand = true

button = Gtk::Button.new(:stock_id => Gtk::Stock::QUIT)
button.signal_connect("clicked") do |_button|
  _button.destroy
end
vbox.add(button)
button.hexpand = true

group = Clutter::Actor.new
hands = []
N_HANDS.times do |i|
  if i.zero?
    hands[i] = ClutterGtk::Texture.new
    hands[i].set_from_pixbuf(pixbuf)
  else
    hands[i] = Clutter::Clone.new(hands[0])
  end

  # Place around a circle
  w = hands[0].width
  h = hands[0].height

  x = WINDOW_WIDTH / 2  + RADIUS * Math.cos(i * Math::PI / (N_HANDS / 2)) - w / 2
  y = WINDOW_HEIGHT / 2 + RADIUS * Math.sin(i * Math::PI / (N_HANDS / 2)) - h / 2

  hands[i].set_position(x, y)

  # Add to our group group
  group.add_child(hands[i])
end

# Add the group to the stage
stage.add_child(group)

constraint = Clutter::AlignConstraint.new(stage, :x_axis, 0.5)
group.add_constraint(constraint)
constraint = Clutter::AlignConstraint.new(stage, :y_axis, 0.5)
group.add_constraint(constraint)

stage.signal_connect("button-press-event") do |_stage, event|
  x, y = event.coords
  actor = _stage.get_actor_at_pos(:all, x, y)
  if actor.is_a?(ClutterGtk::Texture) or actor.is_a?(Clutter::Clone)
    actor.hide
  end

  Clutter::Event::STOP
end
stage.signal_connect("key-release-event") do |_stage, event|
  puts("*** key press event (key:#{event.key_unicode}) ***")
  case event.key_symbol
  when Clutter::Keys::KEY_q
      Gtk.main_quit
  when Clutter::Keys::KEY_r
    N_HANDS.times do |i|
      hands[i].show
    end
  end

  Clutter::Event::STOP
end

window.show_all

# Create a timeline to manage animation
timeline = Clutter::Timeline.new(6000)
timeline.repeat_count = -1

# fire a callback for frame change
timeline.signal_connect("new-frame") do |_timeline, msecs|
  rotation = _timeline.progress * 360.0
  group.set_rotation(:z_axis, rotation, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, 0)
  N_HANDS.times do |i|
    hands[i].set_rotation(:z_axis,
                          -(6.0 * rotation),
                          hands[i].width / 2,
                          hands[i].height / 2,
                          0)
    if fade_p
      hands[i].opacity = (255 - (rotation % 255))
    end
  end
end

# and start it
timeline.start

Gtk.main