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
|
#!/usr/bin/env ruby
#
# This sample code is a port of clutter/examples/image-content.c. The
# image file used in this sample code is copied from
# clutter/tests/data/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"
require "gdk_pixbuf2"
gravities = Clutter::ContentGravity.values
current_gravity_index = gravities.size - 1
stage = Clutter::Stage.new
stage.name = "Stage"
stage.title = "Content Box"
stage.user_resizable = true
stage.signal_connect("destroy") do
Clutter.main_quit
end
stage.show
box = Clutter::Actor.new
box.name = "Image"
box.margin_top = 12
box.margin_right = 12
box.margin_bottom = 12
box.margin_left = 12
box.add_constraint(Clutter::BindConstraint.new(stage, :size, 0.0))
stage.add_child(box)
pixbuf = GdkPixbuf::Pixbuf.new(:file => File.expand_path("redhand.png", File.dirname(__FILE__)))
image = Clutter::Image.new
image.set_data(pixbuf.pixels,
pixbuf.has_alpha? ? :rgba_8888 : :rgb_888,
pixbuf.width,
pixbuf.height,
pixbuf.rowstride)
box.set_content_scaling_filters(:trilinear, :linear)
box.content_gravity = gravities[current_gravity_index]
box.content = image
text = Clutter::Text.new
text.add_constraint(Clutter::AlignConstraint.new(stage, :both, 0.5))
stage.add_child(text)
update_text = lambda do
current_gravity = gravities[current_gravity_index]
text.text = "Content gravity: #{current_gravity.nick}"
end
update_text.call
action = Clutter::TapAction.new
action.signal_connect("tap") do |_action, actor|
current_gravity_index = (current_gravity_index + 1) % gravities.size
actor.save_easing_state do
actor.content_gravity = gravities[current_gravity_index]
end
update_text.call
end
box.reactive = true
box.add_action(action)
Clutter.main
|