Gjs で ClutterActor の回転など

Clutterのバージョンが 1.10 から1.12 になると大きく変わるようで、今回のサンプルは Fedora 17 では動作するけど、Momongaの開発版や Fedora 18 では動作しなくなると思います。
が、せっかく書いたので、公開します。今回は ClutterTexture をぐるぐる回転させています。ClutterTexture をメンバーにもつクラス MovingTexture を作り、イベント処理を行なっています。そういった意味でも、備忘録としてのサンプルとして役立つと思います。
処理するイベントは、button-press-event と scroll-event です。これらのイベント処理は ClutterTexture の親クラス ClutterActor の機能を使っています。
また、ClutterMouseEvent やClutterScrollEvent の第二引数(この例ではevent)から様々な情報が得られるので、処理も楽です。

バージョン違いで動作しないのは、ClutterActor の get_rotation() メソッドと set_rotation() メソッドです。引数やピポットポイントの設定方法が大きく変更されます。ま、現在の get_rotation() メソッドのマニュアルにも問題があるので、早く消え去ったほうが良いかもしれません。

#!/usr/bin/env gjs
const Clutter = imports.gi.Clutter;
const Lang = imports.lang;

const MovingTexture = new Lang.Class({
    Name : "MovingTexture",
    
    _init : function () {
        this.texture = new Clutter.Texture({reactive : true});
        this.texture.set_from_file("/usr/share/pixmaps/gdm-foot-logo.png");
        this.texture.connect('button-press-event', Lang.bind(this, this._button_press));
        this.texture.connect('scroll-event', Lang.bind(this, this._scroll));
    },
    
    _button_press : function(self, event) {
        switch(event.get_button()) {
            case 1:
                [angle, y, z] = this.texture.get_rotation(Clutter.RotateAxis.X_AXIS);
                angle += 30;
                this.texture.set_rotation(Clutter.RotateAxis.X_AXIS, angle, 100, 100, 0);
                break;
            case 2:
                [angle, x, z] = this.texture.get_rotation(Clutter.RotateAxis.Y_AXIS);
                angle += 30;
                this.texture.set_rotation(Clutter.RotateAxis.Y_AXIS, angle, 100, 100, 0);
                break;
            case 3:
                [angle, x, y] = this.texture.get_rotation(Clutter.RotateAxis.Z_AXIS);
                angle += 30;
                this.texture.set_rotation(Clutter.RotateAxis.Z_AXIS, angle, 100, 100, 0);
                break;
            default:
                print('unknown button');
        }
    },
    
    _scroll : function(self, event) {
        let y = this.texture.get_y();
        switch(event.get_scroll_direction()) {
            case Clutter.ScrollDirection.UP:
                this.texture.set_y(y - 10);
                break;
            case Clutter.ScrollDirection.DOWN:
                this.texture.set_y(y + 10);
                break;
            default:
                print('unknown direction');
        }
    }
});

Clutter.init(null);
let stage = new Clutter.Stage();
stage.set_background_color(Clutter.Color.get_static(Clutter.StaticColor.BLACK));
stage.connect('destroy', Clutter.main_quit);

let mt = new MovingTexture();
stage.add_actor(mt.texture);
stage.show();
Clutter.main();

多分、説明の必要はないと思います。マウスのボタン 3 つとホイールでグリグリ動くのをお楽しみください。