Vala プログラミング

Rust プログラミング

おながのブログ

Vala言語とOpenGL (3)

f:id:onagat12:20150301021658p:plain

三角形の回転にScaleウィジェを利用しています。

gtkclutter3.vala
using Gtk;
using GtkClutter;
using Clutter;

public class MainWindow : Gtk.Window
{
   private Clutter.Actor coglbox;
   private static float alpha;

   public MainWindow ()
   {
        title = "GtkClutterGL";
        set_default_size (400, 460);
        destroy.connect (Gtk.main_quit);

        var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
        var box2 = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
        var scale = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL, 0, 360, 2);
        var button = new Gtk.Button.with_label ("Quit");
        button.clicked.connect (Gtk.main_quit);

        vbox.pack_start (box2, true, true, 0);
        vbox.pack_start (scale, false, false, 0);
        vbox.pack_start (button, false, false, 0);
        add (vbox);

        var widget = new Embed ();
        box2.pack_start (widget, true, true, 0);

        var stage = widget.get_stage ();
        stage.width = stage.height = 400;
        stage.background_color = Clutter.Color () { alpha = 255 };

        coglbox = new Clutter.Actor ();
        stage.add_actor (coglbox);
        coglbox.paint.connect (paint_cb);

        scale.value_changed.connect *1;
             alpha = (float)scale.get_value ();
             coglbox.queue_redraw ();
        });
   }

   private void paint_cb ()
   {
       Cogl.TextureVertex vertices1[3];

       Cogl.push_matrix ();
       Cogl.translate (200, 200, 0);
       Cogl.rotate (alpha, 0, 0, 1);

       vertices1[0].x = 0;
       vertices1[0].y = -150;
       vertices1[0].z = 0;
       vertices1[0].color.set_from_4f (1.0f, 0.0f, 0.0f, 1.0f);

       vertices1[1].x = -150;
       vertices1[1].y = 150;
       vertices1[1].z = 0;
       vertices1[1].color.set_from_4f (0.0f, 1.0f, 0.0f, 1.0f);

       vertices1[2].x = 150;
       vertices1[2].y = 150;
       vertices1[2].z = 0;
       vertices1[2].color.set_from_4f (0.0f, 0.0f, 1.0f, 1.0f);

       Cogl.polygon (vertices1, true);

       Cogl.pop_matrix();
    }
}

void main (string[] args)
{
     GtkClutter.init (ref args);
     var window = new MainWindow ();
     window.show_all ();
     Gtk.main ();
}
ビルド
valac --pkg gtk+-3.0 --pkg clutter-gtk-1.0 gtkcluttergl3.vala

Scaleの使い方
1 Scaleウィジェの生成には、Scale.with_rangeメソッドを使います。
        var scale = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL, 0, 360, 2);
    Scaleの方向、初めの値、終りの値、ステップの値を設定します。
2 Scaleの値によって三角形を回転させるには、value_changedシグナルを使います。
    これは、Scaleの親クラスであるRangeのシグナルです。
        scale.value_changed.connect *2;
            alpha = (float)scale.get_value ();
            coglbox.queue_redraw ();
        });

*1:) => {;
             stdout.printf ("%f\n", scale.get_value (

*2:) => {
            stdout.printf ("%f\n", scale.get_value (