123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199(** Image widget. *)openTsdlopenTsdl_imageopenMisc(** Property ["image-keep-ratio"] to indicate whether image ratio must be
perserved. Default is [true].
Keeping ratio means that when width is set with
{{!image.set_width}image#set_width}, height of rendered image will
be set accordingly.
If height is set with {{!image.set_height}image#set_height}, width or rendered image
will be set accordingly.
*)letkeep_ratio=Props.(bool_prop~after:[Resize]~default:true~inherits:false"image-keep-ratio")(** A widget to display an image. *)classimage?(class_="image")?name?props()=object(self)inheritWidget.widget~class_?name?props()assuper(**/**)valmutablesurface=Nonevalmutableimage_texture=Nonevalmutableratio=Nonevalmutablelast_set=(None:[`W|`H]option)(**/**)(** {2 Properties} *)methodwidth=self#opt_pProps.widthmethodset_widthw=self#destroy_image_texture;(matchself#keep_ratio,ratiowith|true,Somer->leth=truncate(floatw/.r)inProps.setpropsProps.heighth|_,_->());last_set<-Some`W;self#set_pProps.widthwmethodheight=self#opt_pProps.heightmethodset_heighth=self#destroy_image_texture;(matchself#keep_ratio,ratiowith|true,Somer->letw=truncate(floath*.r)inProps.setpropsProps.widthw|_,_->());last_set<-Some`H;self#set_pProps.heighthmethodkeep_ratio=self#get_pkeep_ratiomethodset_keep_ratio=self#set_pkeep_ratio(**/**)methoddestroy_image_texture=matchimage_texturewith|None->()|Somet->Texture.destroyt;image_texture<-Nonemethod!min_width_=matchself#widthwith|Somex->x|None->matchsurfacewith|None->0|Somes->fst(Tsdl.Sdl.get_surface_sizes)method!min_height_=matchself#heightwith|Somex->x|None->matchsurfacewith|None->0|Somes->snd(Tsdl.Sdl.get_surface_sizes)method!max_width=Someself#min_widthmethod!max_height=Someself#min_height(**/**)(** Returns orignal (width, height) of image, if an image is loaded. *)methodimage_size=Option.mapTsdl.Sdl.get_surface_sizesurface(**/**)methodprivateupdate_from_ratio=matchlast_set,ratiowith|None,_|_,None->()|Some`H,Somer->(matchself#heightwith|None->()|Someh->letw=truncate(floath*.r)inProps.setpropsProps.widthw)|Some`W,Somer->(matchself#widthwith|None->()|Somew->leth=truncate(floatw/.r)inProps.setpropsProps.heighth)methodprivateset_surfaces=self#destroy_surface;surface<-Somes;let(w,h)=Tsdl.Sdl.get_surface_sizesinratio<-Some(floatw/.floath);ifself#keep_ratiothenself#update_from_ratio;self#need_resize(**/**)(** Load image from rw operations.
Beware that this may be a io-blocking operation. *)methodload_rwrw=matchImage.load_rwrwtruewith|Error(`Msgmsg)->Log.err(funm->m"%s: Could not load image from data: %s"self#memsg)|Oks->self#set_surfaces(** Load image from file.
Beware that this is a io-blocking operation. *)methodload_filefile=matchImage.loadfilewith|Error(`Msgmsg)->Log.err(funm->m"%s: Could not load image from %S: %s"self#mefilemsg)|Oks->self#set_surfaces(**/**)methoddestroy_surface=(matchsurfacewith|None->()|Somes->Sdl.free_surfaces;surface<-None);self#destroy_image_texturemethod!render_me~layerrend~offsetgeom=super#render_with_prepare~layerrend~offsetgeommethod!prepare~(layer:Layer.t)(rend:Sdl.renderer)(geom:G.t)=iflayer=self#get_pProps.layerthen(matchself#image_texturerendwith|None->Log.warn(funm->m"%s#render_me: no texture"self#me);None|Somet->Somet)elseNonemethodprivateimage_texturerend=matchimage_texturewith|None->ifg_inner.w>0&&g_inner.h>0then(matchsurfacewith|None->None|Somes->lett=matchself#width,self#heightwith|None,None->(* no scaling *)Texture.from_surfacerends|_->let(w0,h0)=Tsdl.Sdl.get_surface_sizesinlet>t=Sdl.create_texture_from_surfacerendsinletw=Option.value~default:w0self#widthinleth=Option.value~default:h0self#heightinTexture.from_scaled_texturerend~destroy_orig:true~w~htinimage_texture<-Somet;image_texture)elseNone|x->xinitializerGc.finalise(funo->o#destroy_surface)selfend(** Convenient function to create a {!class-image}.
Optional arguments:
{ul
{- [width] specifies width of rendered image.}
{- [height] specifies height of renderer image.}
{- [file] specifies a file to load an image from.}
{- [keep_ratio] specifies the {!val-keep_ratio} property.}
}
See {!Widget.widget_arguments} for other arguments. *)letimage?class_?name?props?width?height?keep_ratio?file?pack()=letw=newimage?class_?name?props()inOption.iterw#set_keep_ratiokeep_ratio;Option.iterw#set_widthwidth;Option.iterw#set_heightheight;Option.iterw#load_filefile;Widget.may_pack?packw;w