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
(** 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 ~inherits:false "image-keep-ratio")

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

    (** {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

    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

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

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

    method! min_width_ =
      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_ =
      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 = Some self#min_width
    method! max_height = 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

    (**/**)

    (** 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 -> 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 -> self#set_surface s

    (**/**)
    method destroy_surface =
      (match surface with
      | None -> ()
      | Some s -> Sdl.free_surface 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 -> Log.warn (fun m -> m "%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
                       let w = Option.value ~default:w0 self#width in
                       let h = Option.value ~default:h0 self#height in
                       Texture.from_scaled_texture rend ~destroy_orig:true ~w ~h t
                 in
                 image_texture <- Some t ;
                 image_texture
            )
          else
            None
      | x -> x

    initializer
      Gc.finalise (fun o -> o#destroy_surface) self
  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.}
  }
  See {!Widget.widget_arguments} for other arguments. *)
let image ?class_ ?name ?props ?width ?height ?keep_ratio ?file ?pack () =
  let w = new image ?class_ ?name ?props () in
  Option.iter w#set_keep_ratio keep_ratio ;
  Option.iter w#set_width width ;
  Option.iter w#set_height height ;
  Option.iter w#load_file file ;
  Widget.may_pack ?pack w ;
  w