Source

clutter-ocaml / tests / test-depth.ml

Full commit

open Clutter
module Stage = ClutterStage
module Group = ClutterGroup
module Label = ClutterActor.Label
module CloneTexture = ClutterActor.CloneTexture
module Depth = ClutterBehaviour.Depth
module Rotate = ClutterBehaviour.Rotate
module Alpha = ClutterAlpha
module Texture = ClutterTexture
module Rectangle = ClutterRectangle
module Timeline = ClutterTimeline
let to_actor = ClutterActor.to_actor

let janus_group front_text back_text =
  let slide_color = Color.rgb (0,0,0)
  and red = Color.rgb (0xff,0,0)
  and green = Color.rgb (0,0xff,0) in
  let group = Group.create () in
  let rectangle = Rectangle.create ~color:slide_color () in
  let front = Label.create ~font_name:"Sans 50px" ~text:front_text
    ~color:red ()
  and back = Label.create ~font_name:"Sans 50px" ~text:back_text
    ~color:green () in
  let width,height = front#get_size
  and width2,height2 = back#get_size in
  let width = max width2 width
  and height = max height2 height in

  rectangle#set_size ~width ~height;
  back#set_rotation `Y_AXIS ~angle:180. ~x:(width/2) ~y:0 ~z:0;
  (* FIXME:*)
  group#add_many [|to_actor back; to_actor rectangle; to_actor front|];
  group#show_all;
  group

let clone_box orig =
  let width,height = orig#get_size in
  let group = Group.create () in
  let clone = CloneTexture.create orig () in
  group#add clone;
  clone#set_depth (width/2);

  let clone = CloneTexture.create orig () in
  group#add clone;
  clone#set_rotation `Y_AXIS ~angle:180. ~x:(width/2) ~y:0 ~z:0;
  clone#set_depth (-width/2);

  let clone = CloneTexture.create orig () in
  group#add clone;
  clone#set_rotation `Y_AXIS ~angle:90. ~x:0 ~y:0 ~z:0;
  clone#set_depth (width/2);
  clone#set_position ~x:0 ~y:0;

  let clone = CloneTexture.create orig () in
  group#add clone;
  clone#set_rotation `Y_AXIS ~angle:90. ~x:0 ~y:0 ~z:0;
  clone#set_depth (width/2);
  clone#set_position ~x:width ~y:0;

  let clone = CloneTexture.create orig () in
  group#add clone;
  clone#set_rotation `X_AXIS ~angle:90. ~x:0 ~y:0 ~z:0;
  clone#set_depth (-width/2);
  clone#set_position ~x:0 ~y:height;

  let clone = CloneTexture.create orig () in
  group#add clone;
  clone#set_rotation `X_AXIS ~angle:90. ~x:0 ~y:0 ~z:0;
  clone#set_depth (-width/2);
  clone#set_position ~x:0 ~y:0;
  
  group#show_all;
  group
  
let _ =

  let stage_color = Color.rgb (0xcc,0xcc,0xcc)
  and rect_color = Color.rgba (0,0,0,0x88) in

  let stage = Stage.get_default () in
  stage#set_color stage_color;
  stage#set_use_fog true;
  stage#set_fog ~density:1. ~z_near:10. ~z_far:(-50.);
  let _ = stage#connect#button_press_event (fun evt -> main_quit (); true) in
  let group = Group.create () in
  stage#add group;
  group#show;

  let label = Label.create ~font_name:"Mono 26" ~text:"Clutter"
    ~x:120 ~y:200 () in
  label#show;

  let hand = try
    Texture.new_from_file "./examples/redhand.png" ~x:240 ~y:100 ()
  with GError _ -> assert false in
  hand#show;

  let rect = Rectangle.create ~color:rect_color ~x:340 ~y:100
    ~width:200 ~height:200 () in
  rect#show;

  group#add rect;
  stage#add label;

  (* 3 seconds, @ 60 fps *)
  let timeline = Timeline.create ~num_frames:160 ~fps:60 () in
  let _ = timeline#connect#completed
    (fun _ ->
       timeline#reverse;
       timeline#start
    ) in
  let d_behave = Depth.create
    ~alpha:(Alpha.create ~timeline ~alpha_func:`RAMP_INC)
    ~depth_start:(-100) ~depth_end:100 () in
  d_behave#apply label;

  (* add two faced actor *)
  let janus = janus_group "GREEN" "RED" in
  stage#add_actor janus;
  janus#set_position ~x:300 ~y:350;

  let r_behave = Rotate.create
    ~alpha:(Alpha.create ~timeline ~alpha_func:`RAMP_INC)
    ~axis:`Y_AXIS ~direction:`ROTATE_CW ~angle_start:0.
    ~angle_end:360. () in
  r_behave#apply janus;

  (* add a hand box *)
  let box = clone_box hand in
  stage#add_actor box;
  box#set_position ~x:200 ~y:250;
  box#set_scale ~xscale:0.5 ~yscale:0.5;
  box#set_rotation `X_AXIS ~angle:45. ~x:0 ~y:0 ~z:0;
  box#set_opacity (char_of_int 0x44);

  let r_behave = Rotate.create
    ~alpha:(Alpha.create ~timeline ~alpha_func:`RAMP_INC)
    ~axis:`Y_AXIS ~direction:`ROTATE_CW ~angle_start:0.
    ~angle_end:360. () in
  r_behave#apply box;

  stage#show;
  timeline#start;

  let raise_arr = [| to_actor rect; to_actor hand; |] in
  let pos = ref 0 in
  let _ = GMain.Timeout.add 2000
    (fun () ->
       raise_arr.(!pos mod 2)#raise_top;
       true
    ) in
  clutter_main ()