clutter-ocaml / tests / test-unproject.ml

open Clutter
module Stage = ClutterStage
module Rectangle = ClutterRectangle
module Label = ClutterLabel

let _ =
  let rect_l = 200 and rect_t = 150 and rect_w = 320 and rect_h = 240 in
  let stage_clr = Color.rgb (0x0, 0x0, 0x0)
  and white = Color.rgb (0xff, 0xff, 0xff)
  and blue = Color.rgb (0, 0xff, 0xff) in

  let rotate_x = ref 0. and rotate_y = ref 60. and rotate_z = ref 0. in

  Array.iteri (fun i arg -> match arg with
		   "--rotate-x" -> rotate_x := float_of_string Sys.argv.(i+1)
		 | "--rotate-y" -> rotate_y := float_of_string Sys.argv.(i+1)
		 | "--rotate-z" -> rotate_z := float_of_string Sys.argv.(i+1)
		 | "--help" ->
		     Printf.printf "%s\n%!"
		       (Sys.argv.(0) ^ " [--rotate-x degrees] " ^
		       "[--rotate-y degrees]" ^
		       "[--rotate-z degrees]");
		     exit 0
		 | _ -> ()
	      ) Sys.argv;
  
  let stage = Stage.get_default () in
  stage#set_color stage_clr;
  stage#set_size 640 480;

  let rect = Rectangle.new_with_color white () in
  rect#set_size rect_w rect_h;
  rect#set_position rect_l rect_t;
  rect#set_rotation `X_AXIS !rotate_x 0 0 0;
  rect#set_rotation `Y_AXIS !rotate_y 0 0 0;
  rect#set_rotation `Z_AXIS !rotate_z 0 0 0;

  stage#add rect;

  let txt = Printf.sprintf "Rectangle: L %d, R %d, T %d, B %d\n
    Rotation: x %g, y %g, z %g" rect_l (rect_l + rect_w)
    rect_t (rect_t + rect_h) !rotate_x !rotate_y !rotate_z in
  let label0 = Label.new_with_text "Mono 8pt" txt () in
  label0#set_color white;
  label0#set_position 10 10;
  stage#add label0;

  let label = Label.new_with_text "Mono 8pt" "Click around!" () in
  label#set_color blue;
  label#set_position 10 50;
  stage#add label;

  stage#show_all;

  let _ = stage#connect#event
    (fun evt ->
       match Event.get_type evt with
	   `BUTTON_PRESS ->
	     let x,y = Event.get_coords evt in
	     let actor = stage#get_actor_at_pos x y in

	     let xu2, yu2, success = actor#transform_stage_point
	       (units_from_device x) (units_from_device y) in
	     if success then begin
	       (* FIXME:
		  let what = if actor#obj == stage#obj
		  then "rectangle" else "stage" in
	       *)
	       let what = "rectangle" in

	       let txt =
		 Printf.sprintf "Click on %s\n
		   Screen coords: [%d, %d]\n
		   Local coords: [%d, %d]" what x y
		   (units_to_device xu2)
		   (units_to_device yu2)
	       in
	       label#set_text txt;
	     end else
	       label#set_text "Unprojection failed";
	     true
	 | _ -> false
    ) in
  
  clutter_main ()
    
    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.