#!/usr/bin/perl

use strict;
use warnings;

use Clutter qw( :init );
use Gtk2::Gdk::Keysyms;;

main: {
    #-- read script from __DATA__ section
    my $data = do { local $/=undef; <DATA> };

    #-- define script params
    my $params = {
        WIDTH   => 400,
        HEIGHT  => 300,
    };

    #-- expand them
    $data =~ s/\$([A-Z_]+)/$params->{$1}/eg;

    #-- load script
    my $script = Clutter::Script->new;
    $script->load_from_data($data);

    #-- make params convenient
    my $width  = $params->{WIDTH};
    my $height = $params->{HEIGHT};

    #-- dunno how to set rotation in script (syntax in examples/script.pl
    #-- doesn't work), so I do it manually here...
    $script->get_object("front")->set_rotation("y-axis",   0, 0, 0, 0);
    $script->get_object("right")->set_rotation("y-axis",  90, 0, 0, 0);
    $script->get_object("rear") ->set_rotation("y-axis", 180, 0, 0, 0);
    $script->get_object("left") ->set_rotation("y-axis", 270, 0, 0, 0);

    #-- initially rotate cube
    my $degree = 360 - 45;
    $script->get_object("cube")->set_rotation("y-axis", $degree, $width/2, 0, -$width/2);

    #-- setup keyboard handling to rotate cube with left/right keys
    $script->get_object("main-stage")->signal_connect("key-press-event", sub {
        my (undef, $event) = @_;

        #-- exit on "q"
        if ( $event->keyval == $Gtk2::Gdk::Keysyms{q} ) {
            Clutter->main_quit;
        }
        #-- rotate right
        elsif ( $event->keyval == $Gtk2::Gdk::Keysyms{Right} )
        {
            $degree = ($degree + 5)%360;
            $script->get_object("cube")->set_rotation("y-axis", $degree, $width/2, 0, -$width/2);
        }
        #-- rotate left
        elsif ( $event->keyval == $Gtk2::Gdk::Keysyms{Left} )
        {
            $degree = ($degree - 5)%360;
            $script->get_object("cube")->set_rotation("y-axis", $degree, $width/2, 0, -$width/2);
        }
        1;
    });

    #-- start Clutter mainloop
    $script->get_object("main-stage")->show_all;
    Clutter->main;
}

__DATA__

[
  {
    "id":       "main-stage",
    "type":     "ClutterStage",
    "color":    "#000000",
    "width":    $WIDTH,
    "height":   $HEIGHT,
    "title":    "Cube example",
    "children": [
       {
        "id":       "cube",
        "type":     "ClutterGroup",
        "depth":    -$WIDTH,
        "children": [
          {
            "id":           "left",
            "type":         "ClutterRectangle",
            "x":            0,
            "y":            0,
            "depth":        -$WIDTH,
            "width":        $WIDTH,
            "height":       $HEIGHT,
            "color":        "blue"
          },
          {
            "id":           "rear",
            "type":         "ClutterRectangle",
            "x":            $WIDTH,
            "y":            0,
            "depth":        -$WIDTH,
            "width":        $WIDTH,
            "height":       $HEIGHT,
            "color":        "yellow"
          },
          {
            "id":           "right",
            "type":         "ClutterRectangle",
            "x":            $WIDTH,
            "y":            0,
            "depth":        0,
            "width":        $WIDTH,
            "height":       $HEIGHT,
            "color":        "green"
          },
          {
            "id":           "front",
            "type":         "ClutterRectangle",
            "x":            0,
            "y":            0,
            "depth":        0,
            "width":        $WIDTH,
            "height":       $HEIGHT,
            "color":        "red"
          }
        ]
      }
    ]
  }
]
