~ubuntu-branches/ubuntu/wily/clutter-perl/wily

« back to all changes in this revision

Viewing changes to examples/reflection.pl

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2009-09-25 14:21:14 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20090925142114-r1uvxq7zytn04s0p
Tags: 1.0.1-0ubuntu1
* New upstream release.
* Clutter 0.8 -> 1.0 transition. (LP: #364630)
* debian/control:
 - Bump libclutter build dep,
 - Drop deprecated clutter-gst, clutter-gtk, and 
   clutter-cairo build deps.
* debian/patches/10_notify-osd_by_default.patch:
 - Disable POD generation to fix FTBFS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Clutter::Ex::TextureReflection;
 
2
 
 
3
use warnings;
 
4
use strict;
 
5
 
 
6
use Glib qw( :constants );
 
7
use Clutter;
 
8
 
 
9
=pod
 
10
 
 
11
=head1 NAME
 
12
 
 
13
Clutter::Ex::TextureReflection - Reflection of a texture
 
14
 
 
15
=head1 SYNOPSIS
 
16
 
 
17
  use Clutter qw( :init );
 
18
  use Clutter::Ex::TextureReflection;
 
19
 
 
20
  my $texture = Clutter::Texture->new($filename);
 
21
 
 
22
  my $reflection = Clutter::Ex::TextureReflection->new();
 
23
  $reflection->set_parent_texture($texture);
 
24
 
 
25
=head1 DESCRIPTION
 
26
 
 
27
This page describes the API of C<Clutter::Ex::TextureReflection>, a
 
28
subclass of L<Clutter::Texture> that efficiently paints a reflection
 
29
of the parent texture.
 
30
 
 
31
=head1 HIERARCHY
 
32
 
 
33
  Glib::Object
 
34
  +----Glib::InitiallyUnowned
 
35
       +----Clutter::Actor
 
36
            +----Clutter::Texture
 
37
                 +----Clutter::Ex::TextureReflection
 
38
 
 
39
=cut
 
40
 
 
41
use Data::Dumper;
 
42
 
 
43
use Glib::Object::Subclass
 
44
    'Clutter::Texture',
 
45
    signals => { },
 
46
    properties => [
 
47
        Glib::ParamSpec->int(
 
48
            'reflection-height',
 
49
            'Reflection Height',
 
50
            'The height of the reflection, or -1',
 
51
            -1, 65536, -1,
 
52
            [ qw( readable writable ) ],
 
53
        ),
 
54
        Glib::ParamSpec->object(
 
55
            'parent-texture',
 
56
            'Parent Texture',
 
57
            'The texture to do the reflection for',
 
58
            'Clutter::Texture',
 
59
            [ qw( readable writable ) ],
 
60
        ),
 
61
    ];
 
62
 
 
63
sub GET_PREFERRED_WIDTH {
 
64
    my ($self, $for_height) = @_;
 
65
 
 
66
    return (0, 0) unless defined $self->{parent_texture};
 
67
 
 
68
    return $self->{parent_texture}->get_preferred_width($for_height);
 
69
}
 
70
 
 
71
sub GET_PREFERRED_HEIGHT {
 
72
    my ($self, $for_width) = @_;
 
73
 
 
74
    return (0, 0) unless defined $self->{parent_texture};
 
75
 
 
76
    return $self->{parent_texture}->get_preferred_height($for_width);
 
77
}
 
78
 
 
79
sub PAINT {
 
80
    my ($self) = @_;
 
81
 
 
82
    my $parent = $self->get_parent_texture();
 
83
    return unless $parent;
 
84
 
 
85
    # get the handle from the parent texture
 
86
    my $cogl_tex = $parent->get_cogl_texture();
 
87
    return unless $cogl_tex;
 
88
 
 
89
    my $box = $self->get_allocation_box();
 
90
 
 
91
    my ($width, $height) = $box->get_size();
 
92
    return unless $width > 0 and $height > 0;
 
93
 
 
94
    my $r_height = $self->{reflection_height};
 
95
 
 
96
    # clamp the reflection height if needed
 
97
    $r_height = $height if $r_height < 0 or $r_height > $height;
 
98
 
 
99
    my $rty = $r_height / $height;
 
100
 
 
101
    my $opacity = $self->get_paint_opacity() / 255;
 
102
 
 
103
    # the colors of the vertices
 
104
    my $start = [ 1.0, 1.0, 1.0, $opacity ];
 
105
    my $stop  = [ 1.0, 1.0, 1.0,      0.0 ];
 
106
 
 
107
    # we need to premultiply the alpha
 
108
    $start = Clutter::Cogl::Color->premultiply($start);
 
109
    $stop  = Clutter::Cogl::Color->premultiply($stop);
 
110
 
 
111
    # these are the reflection vertices. each vertex
 
112
    # is an arrayref in the form:
 
113
    #
 
114
    #   [
 
115
    #     x, y, z: coordinates in the modelview
 
116
    #     tx, ty: texture coordinates
 
117
    #     color: color of the vertex
 
118
    #   ]
 
119
    #
 
120
    # to paint the reflection of the parent texture we paint
 
121
    # the texture using four vertices in clockwise order, with
 
122
    # the upper left and the upper right at full opacity and
 
123
    # the lower right and lower left and 0 opacity; OpenGL will
 
124
    # do the gradient for us
 
125
    my $vertices = [
 
126
        [      0,         0, 0, 0.0, $rty,   $start ],
 
127
        [ $width,         0, 0, 1.0, $rty,   $start ],
 
128
        [ $width, $r_height, 0, 1.0,    0.0, $stop  ],
 
129
        [      0, $r_height, 0, 0.0,    0.0, $stop  ],
 
130
    ];
 
131
 
 
132
    # paint the original texture again, at the new vertices
 
133
    Clutter::Cogl->polygon($vertices, TRUE);
 
134
}
 
135
 
 
136
sub SET_PROPERTY {
 
137
    my ($self, $pspec, $value) = @_;
 
138
 
 
139
    $self->set_reflection_height($value)
 
140
        if ($pspec->get_name() eq 'reflection_height');
 
141
 
 
142
    $self->set_parent_texture($value)
 
143
        if ($pspec->get_name() eq 'parent_texture');
 
144
}
 
145
 
 
146
sub GET_PROPERTY {
 
147
    my ($self, $pspec) = @_;
 
148
 
 
149
    return $self->get_reflection_height()
 
150
        if ($pspec->get_name() eq 'reflection_height');
 
151
 
 
152
    return $self->get_parent_texture()
 
153
        if ($pspec->get_name() eq 'parent_texture');
 
154
}
 
155
 
 
156
sub INIT_INSTANCE {
 
157
    my ($self) = @_;
 
158
 
 
159
    $self->{reflection_height} = -1;
 
160
    $self->{parent_texture}    = undef;
 
161
}
 
162
 
 
163
=pod
 
164
 
 
165
=head1 METHODS
 
166
 
 
167
=over
 
168
 
 
169
=item B<< actor = Clutter::Ex::TextureReflection->new ($parent_texture) >>
 
170
 
 
171
  * $parent_texture (Clutter::Texture) the parent texture
 
172
 
 
173
Creates a new Clutter::Ex::TextureReflection actor for the given
 
174
I<parent_texture>
 
175
 
 
176
=cut
 
177
 
 
178
sub new {
 
179
    my ($class, $parent_texture) = @_;
 
180
 
 
181
    return Glib::Object::new(
 
182
        'Clutter::Ex::TextureReflection',
 
183
        parent_texture => $parent_texture,
 
184
    );
 
185
}
 
186
 
 
187
=pod
 
188
 
 
189
=item B<< $reflection->set_reflection_height ($height) >>
 
190
 
 
191
  * $height (integer)
 
192
 
 
193
Sets the height of the reflection, in pixels. If I<height> is a negative
 
194
value, the height of the parent texture will be used instead.
 
195
 
 
196
=cut
 
197
 
 
198
sub set_reflection_height {
 
199
    my ($self, $height) = @_;
 
200
 
 
201
    $self->{reflection_height} = $height;
 
202
 
 
203
    $self->queue_relayout();
 
204
 
 
205
    $self->notify('reflection-height');
 
206
}
 
207
 
 
208
=pod
 
209
 
 
210
=item B<< height = $reflection->get_reflection_height >>
 
211
 
 
212
Retrieves the height of the reflection.
 
213
 
 
214
=cut
 
215
 
 
216
sub get_reflection_height {
 
217
    my ($self) = @_;
 
218
 
 
219
    return $self->{reflection_height};
 
220
}
 
221
 
 
222
=pod
 
223
 
 
224
=item B<< $reflection->set_parent_texture ($texturer) >>
 
225
 
 
226
  * $texture (Clutter::Texture)
 
227
 
 
228
Sets the texture to be reflected.
 
229
 
 
230
=cut
 
231
 
 
232
sub set_parent_texture {
 
233
    my ($self, $texture) = @_;
 
234
 
 
235
    $self->{parent_texture} = $texture;
 
236
 
 
237
    $self->queue_relayout();
 
238
 
 
239
    $self->notify('parent-texture');
 
240
}
 
241
 
 
242
=pod
 
243
 
 
244
=item B<< texture = $reflection->get_parent_texture >>
 
245
 
 
246
Retrieves the parent texture.
 
247
 
 
248
=cut
 
249
 
 
250
sub get_parent_texture {
 
251
    my ($self) = @_;
 
252
 
 
253
    return $self->{parent_texture};
 
254
}
 
255
 
 
256
=pod
 
257
 
 
258
=back
 
259
 
 
260
=head1 PROPERTIES
 
261
 
 
262
=over
 
263
 
 
264
=item 'reflection-height' (integer : readable / writable)
 
265
 
 
266
Height of the reflection, in pixels, or -1 to use the same height
 
267
of the parent texture.
 
268
 
 
269
=item 'parent-texture' (Clutter::Texture : readable / writable)
 
270
 
 
271
The L<Clutter::Texture> to generate a reflection for.
 
272
 
 
273
=back
 
274
 
 
275
=head1 SEE ALSO
 
276
 
 
277
L<Clutter>, L<Glib::Object>, L<Glib::InitiallyUnowned>, L<Clutter::Actor>,
 
278
L<Clutter::Texture>, L<Clutter::Cogl::Texture>
 
279
 
 
280
=head1 COPYRIGHT
 
281
 
 
282
Copyright (C) 2008, 2009  Intel Corporation.
 
283
 
 
284
This module is free software; you can redistribute it and/or modify it
 
285
under the terms of the GNU Lesser General Public License version 2.1, or
 
286
under the terms of the Artistic License. See Clutter for the full copyright
 
287
notice.
 
288
 
 
289
=cut
 
290
 
 
291
package main;
 
292
 
 
293
use Glib qw( :constants );
 
294
use Clutter qw( :init );
 
295
 
 
296
my $filename = defined $ARGV[0] ? $ARGV[0] : 'redhand.png';
 
297
 
 
298
my $stage = Clutter::Stage->new();
 
299
$stage->set_size(640, 480);
 
300
$stage->set_color(Clutter::Color->from_string('Black'));
 
301
$stage->signal_connect(destroy => sub { Clutter->main_quit() });
 
302
$stage->set_title('TextureReflection');
 
303
 
 
304
my $group = Clutter::Group->new();
 
305
$stage->add($group);
 
306
 
 
307
my $tex;
 
308
eval { $tex = Clutter::Texture->new($filename) };
 
309
if ($@) {
 
310
    warn ("Unable to load '$filename': $@");
 
311
    exit 1;
 
312
}
 
313
 
 
314
my $reflect = Clutter::Ex::TextureReflection->new($tex);
 
315
$reflect->set_opacity(100);
 
316
 
 
317
$group->add($tex, $reflect);
 
318
 
 
319
$tex->set_position(0, 0);
 
320
$reflect->set_position(0, $tex->get_height() + 10);
 
321
 
 
322
my $x_pos = ($stage->get_width() - $tex->get_width()) / 2;
 
323
$group->set_position($x_pos, 20);
 
324
 
 
325
my $timeline = Clutter::Timeline->new(3000);
 
326
$timeline->set_loop(TRUE);
 
327
my $alpha = Clutter::Alpha->new($timeline, 'linear');
 
328
my $behaviour = Clutter::Behaviour::Rotate->new($alpha, 'y-axis', 'cw', 0, 360);
 
329
$behaviour->set_center($group->get_width() / 2, 0, 0);
 
330
$behaviour->apply($group);
 
331
 
 
332
$stage->show();
 
333
 
 
334
$timeline->start();
 
335
 
 
336
Clutter->main();
 
337
 
 
338
0;