1. Reid van Melle
  2. clutter-ocaml

Source

clutter-ocaml / tests / test-clip.ml


open Clutter
module Rectangle = ClutterRectangle
module Texture = ClutterTexture
module Timeline = ClutterTimeline
module Stage = ClutterStage
module Group = ClutterGroup
module CloneTexture = ClutterActor.CloneTexture

let _ =
  let tl_scale = 5 in
  let blue = Color.rgb (0x40,0x40,0xff) in
  let stage = Stage.get_default () in
  let group = Group.create () in

  let geom = stage#get_geometry in
  let geom = {left=geom.width / 4; top=geom.height / 4;
	      width=geom.width / 2; height=geom.height / 2;} in
  group#set_geometry geom;

  let rect = Rectangle.new_with_color blue () in
  rect#set_geometry geom;

  stage#add rect;
  stage#add group;
  group#set_clip {left=0; top=0; width=geom.width; height=geom.height};

  let hand = Texture.new_from_file "./examples/redhand.png" () in
  group#add hand;

  for y=0 to 1 do
    for x=0 to 1 do
      let other_hand = CloneTexture.create hand () in
      other_hand#set_anchor_point_from_gravity `GRAVITY_CENTER;
      other_hand#set_position (x * geom.width) (y * geom.height);
      group#add other_hand
    done
  done;

  hand#raise_top;
  let tl = Timeline.create ~num_frames:(360 * tl_scale) ~fps:60 () in
  tl#start;
  tl#set_loop true;

  let _ = tl#connect#new_frame
    (fun _ frame_num ->
       (*Printf.printf "frame=%d\n%!" frame_num;*)
       let stage_width = float stage#width
       and stage_height = float stage#height in
       let progress = tl#progress in
       let angle = progress *. 2. *. 3.14159 *. (float tl_scale) in
       let xpos = stage_width *. 0.45 *. (sin angle) +. (stage_width /. 8.)
       and ypos = stage_height *. 0.45 *. (sin angle) +. (stage_height /. 8.)
       and zpos = stage_width *. (cos angle) -. (stage_width /. 2.) in

       hand#set_position (int_of_float xpos) (int_of_float ypos);
       hand#set_depth (int_of_float zpos);
       hand#set_rotation `Y_AXIS (angle /. 3.14159 *. 180. *. 3.)
	 (hand#width / 2) (hand#height / 2) 0;
       let rotation = if progress < (1. /. 3.) then
	 [|0.; 0.; 360. *. progress *. 3.|]
       else if progress < (2. /. 3.) then
	 [|0.; 360. *. progress *. 3.; 0.|]
       else
	 [|360. *. progress *. 3.; 0.; 0.|] in
       let axis = [|`X_AXIS; `Y_AXIS; `Z_AXIS|] in
       for i = 0 to 2 do
	 group#set_rotation axis.(i) rotation.(i) (rect#width / 2)
	   (rect#height / 2) 0;
	 rect#set_rotation axis.(i) rotation.(i) (rect#width / 2)
	   (rect#height / 2) 0;
       done;

	 
    ) in
  stage#show_all;
  clutter_main ()