Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file graphics_js.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129(* Js_of_ocaml library
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2014 Hugo Heuzard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)openJs_of_ocamlopenJs_of_ocaml_lwtincludeGraphicstypecontextlet_=Callback.register_exception"Graphics.Graphic_failure"(Graphic_failure"")let(>>=)=Lwt.bindletget_context()=Js.Unsafe.(fun_call(variable"caml_gr_state_get")[||])letset_contextctx=Js.Unsafe.(fun_call(variable"caml_gr_state_set")[|injectctx|])letcreate_contextcanvaswh=Js.Unsafe.(fun_call(variable"caml_gr_state_create")[|injectcanvas;injectw;injecth|])letdocument_of_contextctx=Js.Unsafe.(fun_call(variable"caml_gr_doc_of_state")[|injectctx|])letopen_canvasx=letctx=create_contextxx##.widthx##.heightinset_contextctxletcompute_real_poselt=letrecloopeltlefttop=lettop=elt##.offsetTop-elt##.scrollTop+topandleft=elt##.offsetLeft-elt##.scrollLeft+leftinmatchJs.Opt.to_optionelt##.offsetParentwith|None->top,left|Somep->loopplefttopinloopelt00letmouse_pos()=letctx=get_context()inletelt=ctx##.canvasinLwt_js_events.mousemoveelt>>=funev->lettop,left=compute_real_poseltinLwt.return(Js.Optdef.getev##.pageX(fun_->0)-left,elt##.height-(Js.Optdef.getev##.pageY(fun_->0)-top))letbutton_down()=letctx=get_context()inletelt=ctx##.canvasinLwt_js_events.mousedownelt>>=fun_ev->Lwt.returntrueletread_key()=(* let ctx = get_context() in *)(* let elt = ctx##canvas in *)letdoc=document_of_context(get_context())inLwt_js_events.keypressdoc>>=funev->Lwt.return(Char.chrev##.keyCode)letloopelistf:unit=letctx=get_context()inletelt=ctx##.canvasinletdoc=document_of_context(get_context())inletbutton=reffalseinletnull=char_of_int0inletmouse_x,mouse_y=ref0,ref0inletget_pos_mouse()=!mouse_x,!mouse_yinifList.memButton_downelistthenelt##.onmousedown:=Dom_html.handler(fun_ev->letmouse_x,mouse_y=get_pos_mouse()inbutton:=true;lets={mouse_x;mouse_y;button=true;keypressed=false;key=null}infs;Js._true);ifList.memButton_upelistthenelt##.onmouseup:=Dom_html.handler(fun_ev->letmouse_x,mouse_y=get_pos_mouse()inbutton:=false;lets={mouse_x;mouse_y;button=false;keypressed=false;key=null}infs;Js._true);elt##.onmousemove:=Dom_html.handler(funev->letcy,cx=compute_real_poseltinmouse_x:=Js.Optdef.getev##.pageX(fun_->0)-cx;mouse_y:=elt##.height-(Js.Optdef.getev##.pageY(fun_->0)-cy);(ifList.memMouse_motionelistthenletmouse_x,mouse_y=get_pos_mouse()inlets={mouse_x;mouse_y;button=!button;keypressed=false;key=null}infs);Js._true);(* EventListener sur le doc car pas de moyen simple de le faire
sur un canvasElement *)ifList.memKey_pressedelistthendoc##.onkeypress:=Dom_html.handler(funev->(* Uncaught Invalid_argument char_of_int with key € for example *)letkey=trychar_of_int(Js.Optdef.getev##.charCode(fun_->0))withInvalid_argument_->nullinletmouse_x,mouse_y=get_pos_mouse()inlets={mouse_x;mouse_y;button=!button;keypressed=true;key}infs;Js._true)letloop_at_exiteventshandler:unit=at_exit(fun_->loopeventshandler)