summaryrefslogtreecommitdiff
path: root/examples/camera-list.js
blob: a876e06bdeedf3b1e1c3098900f8e678e8f96f2f (plain)
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
/*
 * Copyright (C) 2013 Intel Corporation
 *
 * 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 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., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

const Lang = imports.lang;
const Mainloop = imports.mainloop;
const Gettext = imports.gettext;
const _ = imports.gettext.gettext;

const Clutter = imports.gi.Clutter;
const ClutterGst = imports.gi.ClutterGst;

const ANIMATION_MS = 300;

Clutter.init(null, null);
ClutterGst.init(null, null);

let stage = new Clutter.Stage({
    width: 200,
    height: 600,
    layout_manager: new Clutter.BoxLayout({
        orientation: Clutter.Orientation.VERTICAL,
        pack_start: false,
        use_animations: true,
        spacing: 10,
        homogeneous: true,
        easing_duration: ANIMATION_MS,
        easing_mode: Clutter.AnimationMode.LINEAR,
    }),
    user_resizable: true,
    background_color: new Clutter.Color({
        red: 0, green: 0, blue: 0, alpha: 0xff,
    }),
});
stage.connect('destroy', Clutter.main_quit);


let players = [];

let addCamera = function(device) {
    log('adding camera : ' + device.get_name());
    let player = new ClutterGst.Camera({
        device: device,
    });
    player.connect('ready', Lang.bind(this, function() {
        let actor = new Clutter.Actor({
            content: new ClutterGst.Aspectratio({
                player: player,
            }),
            width: 200,
            height: 200,
            scale_x: 0,
            scale_y: 0,
            pivot_point: new Clutter.Point({
                x: 0.5,
                y: 0.5,
            }),
        });
        stage.add_child(actor);

        actor.save_easing_state();
        actor.set_easing_duration(ANIMATION_MS);
        actor.set_easing_mode(Clutter.AnimationMode.LINEAR);
        actor.scale_x = actor.scale_y = 1;
        actor.restore_easing_state();
    }));
    player.set_playing(true);
};

let removeCamera = function(device) {
    log('removing camera : ' + device.get_name());
    let children = stage.get_children();
    for (let i in children) {
        let actor = children[i];
        if (actor.content.player.device == device) {
            actor.save_easing_state();
            actor.set_easing_duration(ANIMATION_MS)
            actor.set_easing_mode(Clutter.AnimationMode.LINEAR);
            actor.scale_x = actor.scale_y = 0;
            actor.restore_easing_state();

            let timeline = actor.get_transition('scale-x');
            timeline.connect('completed', Lang.bind(this, function() {
                stage.remove_child(actor);
            }));
            break;
        }
    }
};


let cameraManager = ClutterGst.CameraManager.get_default();
cameraManager.connect('camera-added', Lang.bind(this, function(manager, cameraDevice) {
    addCamera(cameraDevice);
}));
cameraManager.connect('camera-removed', Lang.bind(this, function(manager, cameraDevice) {
    removeCamera(cameraDevice);
}));

let devices = cameraManager.get_camera_devices();
for (let i in devices)
    addCamera(devices[i]);

stage.show();
Clutter.main();