package stk

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file image.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
(*********************************************************************************)
(*                OCaml-Stk                                                      *)
(*                                                                               *)
(*    Copyright (C) 2023-2024 INRIA All rights reserved.                         *)
(*    Author: Maxence Guesdon, INRIA Saclay                                      *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU General Public License as                    *)
(*    published by the Free Software Foundation, version 3 of the License.       *)
(*                                                                               *)
(*    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 General Public License for more details.                               *)
(*                                                                               *)
(*    You should have received a copy of the GNU 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                                                            *)
(*                                                                               *)
(*    As a special exception, you have permission to link this program           *)
(*    with the OCaml compiler and distribute executables, as long as you         *)
(*    follow the requirements of the GNU GPL in regard to all of the             *)
(*    software in the executable aside from the OCaml compiler.                  *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*********************************************************************************)

(** Image widget. *)

open Tsdl
open Tsdl_image
open Misc

(** 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.
*)
let keep_ratio = Props.(bool_prop
   ~after:[Resize] ~default:true ~inherited:false "image_keep_ratio")
let css_keep_ratio = Theme.bool_prop keep_ratio

(** Property ["image-autosize"] to indicate whether image must fit in
  allocated geometry. Default is [false].*)
let autosize = Props.(bool_prop
   ~after:[Resize] ~default:false ~inherited:false "image_autosize")
let css_autosize = Theme.bool_prop autosize

(** A widget to display an image. *)
class image ?classes ?name ?props ?wdata () =
  object(self)
    inherit Widget.widget ?classes ?name ?props ?wdata () as super
    (**/**)
    val mutable surface = None
    val mutable image_texture = None
    val mutable ratio = None
    val mutable last_set = (None : [`W | `H] option)
    method kind = "image"
    (**/**)

    (** {2 Properties} *)

    method width = self#opt_p Props.width
    method set_width w =
      self#destroy_image_texture ;
      (match self#keep_ratio, ratio with
       | true, Some r ->
           let h = truncate (float w /. r) in
           Props.set props Props.height h
       | _, _ -> ()
      );
      last_set <- Some `W;
      self#set_p Props.width w;
      self#need_render ~layer:self#layer g

    method height = self#opt_p Props.height
    method set_height h =
      self#destroy_image_texture ;
      (match self#keep_ratio, ratio with
       | true, Some r ->
           let w = truncate (float h *. r) in
           Props.set props Props.width w
       | _, _ -> ()
      );
      last_set <- Some `H;
      self#set_p Props.height h;
      self#need_render ~layer:self#layer g

    method keep_ratio = self#get_p keep_ratio
    method set_keep_ratio = self#set_p keep_ratio

    method autosize = self#get_p autosize
    method set_autosize = self#set_p autosize

    (**/**)
    method destroy_image_texture =
      match image_texture with
      | None -> ()
      | Some t -> Texture.destroy t; image_texture <- None

    method! min_width_ =
      if self#autosize then
        0
      else
        match self#width with
        | Some x -> x
        | None ->
            match surface with
            | None -> 0
            | Some s -> fst (Tsdl.Sdl.get_surface_size s)

    method! min_height_ =
      if self#autosize then
        0
      else
        match self#height with
        | Some x -> x
        | None ->
            match surface with
            | None -> 0
            | Some s -> snd (Tsdl.Sdl.get_surface_size s)

    method! max_width = if self#autosize then None else Some self#min_width
    method! max_height = if self#autosize then None else Some self#min_height

    (**/**)

    (** Returns orignal (width, height) of image, if an image is loaded. *)
    method image_size = Option.map Tsdl.Sdl.get_surface_size surface

    (**/**)
    method private update_from_ratio =
      match last_set, ratio with
      | None, _
      | _, None -> ()
      | Some `H, Some r ->
          (match self#height with
           | None -> ()
           | Some h ->
               let w = truncate (float h *. r) in
               Props.set props Props.width w
          )
      | Some `W, Some r ->
          (match self#width with
           | None -> ()
           | Some w ->
               let h = truncate (float w /. r) in
               Props.set props Props.height h
          )

    method private set_surface s =
      self#destroy_surface ;
      surface <- Some s;
      let (w,h) = Tsdl.Sdl.get_surface_size s in
      ratio <- Some (float w /. float h);
      if self#keep_ratio then self#update_from_ratio ;
      self#need_resize

    method! set_geometry geom =
      let old_g = g in
      super#set_geometry geom;
      match self#autosize with
      | false -> ()
      | true when old_g = g -> ()
      | true ->
          match self#keep_ratio, ratio with
          | false, _ ->
              self#set_width g_inner.w ;
              self#set_height g_inner.h
          | _, None -> ()
          | true, Some r when r >= 1. ->
              self#set_width g_inner.w ;
              Option.iter
                (fun h -> if h > g_inner.h then
                     self#set_height g_inner.h)
                self#height
          | true, Some r ->
              self#set_height g_inner.h ;
              Option.iter
                (fun w -> if w > g_inner.w then
                     self#set_width g_inner.w)
                self#width

    (**/**)

    (** Load image from rw operations.
        Beware that this may be a io-blocking operation. *)
    method load_rw rw =
      match Image.load_rw rw true with
      | Error (`Msg msg) ->
          Log.err (fun m -> m "%s: Could not load image from data: %s" self#me msg)
      | Ok s ->
          Gc.finalise (fun s -> Tsdl.Sdl.free_surface s) s;
          self#set_surface s

    (** Load image from file.
        Beware that this is a io-blocking operation. *)
    method load_file file =
      match Image.load file with
      | Error (`Msg msg) ->
          Log.err (fun m -> m "%s: Could not load image from %S: %s" self#me file msg)
      | Ok s ->
          Gc.finalise (fun s -> Tsdl.Sdl.free_surface s) s;
          self#set_surface s

    (** Return the (unscaled) image surface, if any.*)
    method surface = surface

    (**/**)
    method destroy_surface =
      (match surface with
      | None -> ()
      | Some s -> surface <- None);
      self#destroy_image_texture

    method! render_me ~layer rend ~offset geom =
      super#render_with_prepare ~layer rend ~offset geom

    method! prepare ~(layer:Layer.t) (rend:Sdl.renderer) (geom:G.t) =
      if layer = self#get_p Props.layer then
        (match self#image_texture rend with
         | None -> [%debug "%s#render_me: no texture" self#me]; None
         | Some t -> Some t
        )
      else
        None

    method private image_texture rend =
      match image_texture with
      | None ->
          if g_inner.w > 0 && g_inner.h > 0 then
            (
             match surface with
             | None -> None
             | Some s ->
                 let t =
                   match self#width, self#height with
                   | None, None -> (* no scaling *)
                       Texture.from_surface rend s
                   | _ ->
                       let (w0,h0) = Tsdl.Sdl.get_surface_size s in
                       let> t = Sdl.create_texture_from_surface rend s in
                       Texture.finalise_sdl_texture t ;
                       let w = Option.value ~default:w0 self#width in
                       let h = Option.value ~default:h0 self#height in
                       Texture.from_scaled_texture rend ~w ~h t
                 in
                 image_texture <- Some t ;
                 image_texture
            )
          else
            None
      | x -> x

  end


(** 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.}
   {- [autosize] specifies the {!val-autosize} property.}
  }
  See {!Widget.widget_arguments} for other arguments. *)
let image ?classes ?name ?props ?wdata ?width ?height ?keep_ratio ?autosize ?file ?pack () =
  let w = new image ?classes ?name ?props ?wdata () in
  Option.iter w#set_keep_ratio keep_ratio ;
  Option.iter w#set_autosize autosize ;
  Option.iter w#set_width width ;
  Option.iter w#set_height height ;
  Option.iter w#load_file file ;
  Widget.may_pack ?pack w ;
  w
OCaml

Innovation. Community. Security.